remotedb.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2022 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package remotedb implements the key-value database layer based on a remote geth
  17. // node. Under the hood, it utilises the `debug_dbGet` method to implement a
  18. // read-only database.
  19. // There really are no guarantees in this database, since the local geth does not
  20. // exclusive access, but it can be used for basic diagnostics of a remote node.
  21. package remotedb
  22. import (
  23. "errors"
  24. "strings"
  25. "github.com/ethereum/go-ethereum/common/hexutil"
  26. "github.com/ethereum/go-ethereum/ethdb"
  27. "github.com/ethereum/go-ethereum/rpc"
  28. )
  29. // Database is a key-value lookup for a remote database via debug_dbGet.
  30. type Database struct {
  31. remote *rpc.Client
  32. }
  33. func (db *Database) Has(key []byte) (bool, error) {
  34. if _, err := db.Get(key); err != nil {
  35. return false, nil
  36. }
  37. return true, nil
  38. }
  39. func (db *Database) Get(key []byte) ([]byte, error) {
  40. var resp hexutil.Bytes
  41. err := db.remote.Call(&resp, "debug_dbGet", hexutil.Bytes(key))
  42. if err != nil {
  43. return nil, err
  44. }
  45. return resp, nil
  46. }
  47. func (db *Database) HasAncient(kind string, number uint64) (bool, error) {
  48. if _, err := db.Ancient(kind, number); err != nil {
  49. return false, nil
  50. }
  51. return true, nil
  52. }
  53. func (db *Database) Ancient(kind string, number uint64) ([]byte, error) {
  54. var resp hexutil.Bytes
  55. err := db.remote.Call(&resp, "debug_dbAncient", kind, number)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return resp, nil
  60. }
  61. func (db *Database) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
  62. panic("not supported")
  63. }
  64. func (db *Database) Ancients() (uint64, error) {
  65. var resp uint64
  66. err := db.remote.Call(&resp, "debug_dbAncients")
  67. return resp, err
  68. }
  69. func (db *Database) Tail() (uint64, error) {
  70. panic("not supported")
  71. }
  72. func (db *Database) AncientSize(kind string) (uint64, error) {
  73. panic("not supported")
  74. }
  75. func (db *Database) ReadAncients(fn func(op ethdb.AncientReaderOp) error) (err error) {
  76. return fn(db)
  77. }
  78. func (db *Database) Put(key []byte, value []byte) error {
  79. panic("not supported")
  80. }
  81. func (db *Database) Delete(key []byte) error {
  82. panic("not supported")
  83. }
  84. func (db *Database) ModifyAncients(f func(ethdb.AncientWriteOp) error) (int64, error) {
  85. panic("not supported")
  86. }
  87. func (db *Database) TruncateHead(n uint64) error {
  88. panic("not supported")
  89. }
  90. func (db *Database) TruncateTail(n uint64) error {
  91. panic("not supported")
  92. }
  93. func (db *Database) Sync() error {
  94. return nil
  95. }
  96. func (db *Database) MigrateTable(s string, f func([]byte) ([]byte, error)) error {
  97. panic("not supported")
  98. }
  99. func (db *Database) NewBatch() ethdb.Batch {
  100. panic("not supported")
  101. }
  102. func (db *Database) NewBatchWithSize(size int) ethdb.Batch {
  103. panic("not supported")
  104. }
  105. func (db *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
  106. panic("not supported")
  107. }
  108. func (db *Database) Stat(property string) (string, error) {
  109. panic("not supported")
  110. }
  111. func (db *Database) AncientDatadir() (string, error) {
  112. panic("not supported")
  113. }
  114. func (db *Database) Compact(start []byte, limit []byte) error {
  115. return nil
  116. }
  117. func (db *Database) NewSnapshot() (ethdb.Snapshot, error) {
  118. panic("not supported")
  119. }
  120. func (db *Database) Close() error {
  121. db.remote.Close()
  122. return nil
  123. }
  124. func dialRPC(endpoint string) (*rpc.Client, error) {
  125. if endpoint == "" {
  126. return nil, errors.New("endpoint must be specified")
  127. }
  128. if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
  129. // Backwards compatibility with geth < 1.5 which required
  130. // these prefixes.
  131. endpoint = endpoint[4:]
  132. }
  133. return rpc.Dial(endpoint)
  134. }
  135. func New(endpoint string) (ethdb.Database, error) {
  136. client, err := dialRPC(endpoint)
  137. if err != nil {
  138. return nil, err
  139. }
  140. return &Database{
  141. remote: client,
  142. }, nil
  143. }