github.com/ethereum/go-ethereum@v1.16.1/ethdb/remotedb/remotedb.go (about)

     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  
    17  // Package remotedb implements the key-value database layer based on a remote geth
    18  // node. Under the hood, it utilises the `debug_dbGet` method to implement a
    19  // read-only database.
    20  // There really are no guarantees in this database, since the local geth does not
    21  // exclusive access, but it can be used for basic diagnostics of a remote node.
    22  package remotedb
    23  
    24  import (
    25  	"github.com/ethereum/go-ethereum/common/hexutil"
    26  	"github.com/ethereum/go-ethereum/ethdb"
    27  	"github.com/ethereum/go-ethereum/rpc"
    28  )
    29  
    30  // Database is a key-value lookup for a remote database via debug_dbGet.
    31  type Database struct {
    32  	remote *rpc.Client
    33  }
    34  
    35  func (db *Database) Has(key []byte) (bool, error) {
    36  	if _, err := db.Get(key); err != nil {
    37  		return false, err
    38  	}
    39  	return true, nil
    40  }
    41  
    42  func (db *Database) Get(key []byte) ([]byte, error) {
    43  	var resp hexutil.Bytes
    44  	err := db.remote.Call(&resp, "debug_dbGet", hexutil.Bytes(key))
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return resp, nil
    49  }
    50  
    51  func (db *Database) Ancient(kind string, number uint64) ([]byte, error) {
    52  	var resp hexutil.Bytes
    53  	err := db.remote.Call(&resp, "debug_dbAncient", kind, number)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	return resp, nil
    58  }
    59  
    60  func (db *Database) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
    61  	panic("not supported")
    62  }
    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  
    70  func (db *Database) Tail() (uint64, error) {
    71  	panic("not supported")
    72  }
    73  
    74  func (db *Database) AncientSize(kind string) (uint64, error) {
    75  	panic("not supported")
    76  }
    77  
    78  func (db *Database) ReadAncients(fn func(op ethdb.AncientReaderOp) error) (err error) {
    79  	return fn(db)
    80  }
    81  
    82  func (db *Database) Put(key []byte, value []byte) error {
    83  	panic("not supported")
    84  }
    85  
    86  func (db *Database) Delete(key []byte) error {
    87  	panic("not supported")
    88  }
    89  
    90  func (db *Database) DeleteRange(start, end []byte) error {
    91  	panic("not supported")
    92  }
    93  
    94  func (db *Database) ModifyAncients(f func(ethdb.AncientWriteOp) error) (int64, error) {
    95  	panic("not supported")
    96  }
    97  
    98  func (db *Database) TruncateHead(n uint64) (uint64, error) {
    99  	panic("not supported")
   100  }
   101  
   102  func (db *Database) TruncateTail(n uint64) (uint64, error) {
   103  	panic("not supported")
   104  }
   105  
   106  func (db *Database) SyncAncient() error {
   107  	return nil
   108  }
   109  
   110  func (db *Database) NewBatch() ethdb.Batch {
   111  	panic("not supported")
   112  }
   113  
   114  func (db *Database) NewBatchWithSize(size int) ethdb.Batch {
   115  	panic("not supported")
   116  }
   117  
   118  func (db *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
   119  	panic("not supported")
   120  }
   121  
   122  func (db *Database) Stat() (string, error) {
   123  	return "", nil
   124  }
   125  
   126  func (db *Database) AncientDatadir() (string, error) {
   127  	panic("not supported")
   128  }
   129  
   130  func (db *Database) Compact(start []byte, limit []byte) error {
   131  	return nil
   132  }
   133  
   134  func (db *Database) SyncKeyValue() error {
   135  	return nil
   136  }
   137  
   138  func (db *Database) Close() error {
   139  	db.remote.Close()
   140  	return nil
   141  }
   142  
   143  func New(client *rpc.Client) ethdb.Database {
   144  	if client == nil {
   145  		return nil
   146  	}
   147  	return &Database{remote: client}
   148  }