github.com/theQRL/go-zond@v0.1.1/zonddb/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/theQRL/go-zond/common/hexutil"
    26  	"github.com/theQRL/go-zond/zonddb"
    27  	"github.com/theQRL/go-zond/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, nil
    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) HasAncient(kind string, number uint64) (bool, error) {
    52  	if _, err := db.Ancient(kind, number); err != nil {
    53  		return false, nil
    54  	}
    55  	return true, nil
    56  }
    57  
    58  func (db *Database) Ancient(kind string, number uint64) ([]byte, error) {
    59  	var resp hexutil.Bytes
    60  	err := db.remote.Call(&resp, "debug_dbAncient", kind, number)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return resp, nil
    65  }
    66  
    67  func (db *Database) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
    68  	panic("not supported")
    69  }
    70  
    71  func (db *Database) Ancients() (uint64, error) {
    72  	var resp uint64
    73  	err := db.remote.Call(&resp, "debug_dbAncients")
    74  	return resp, err
    75  }
    76  
    77  func (db *Database) Tail() (uint64, error) {
    78  	panic("not supported")
    79  }
    80  
    81  func (db *Database) AncientSize(kind string) (uint64, error) {
    82  	panic("not supported")
    83  }
    84  
    85  func (db *Database) ReadAncients(fn func(op zonddb.AncientReaderOp) error) (err error) {
    86  	return fn(db)
    87  }
    88  
    89  func (db *Database) Put(key []byte, value []byte) error {
    90  	panic("not supported")
    91  }
    92  
    93  func (db *Database) Delete(key []byte) error {
    94  	panic("not supported")
    95  }
    96  
    97  func (db *Database) ModifyAncients(f func(zonddb.AncientWriteOp) error) (int64, error) {
    98  	panic("not supported")
    99  }
   100  
   101  func (db *Database) TruncateHead(n uint64) (uint64, error) {
   102  	panic("not supported")
   103  }
   104  
   105  func (db *Database) TruncateTail(n uint64) (uint64, error) {
   106  	panic("not supported")
   107  }
   108  
   109  func (db *Database) Sync() error {
   110  	return nil
   111  }
   112  
   113  func (db *Database) MigrateTable(s string, f func([]byte) ([]byte, error)) error {
   114  	panic("not supported")
   115  }
   116  
   117  func (db *Database) NewBatch() zonddb.Batch {
   118  	panic("not supported")
   119  }
   120  
   121  func (db *Database) NewBatchWithSize(size int) zonddb.Batch {
   122  	panic("not supported")
   123  }
   124  
   125  func (db *Database) NewIterator(prefix []byte, start []byte) zonddb.Iterator {
   126  	panic("not supported")
   127  }
   128  
   129  func (db *Database) Stat(property string) (string, error) {
   130  	panic("not supported")
   131  }
   132  
   133  func (db *Database) AncientDatadir() (string, error) {
   134  	panic("not supported")
   135  }
   136  
   137  func (db *Database) Compact(start []byte, limit []byte) error {
   138  	return nil
   139  }
   140  
   141  func (db *Database) NewSnapshot() (zonddb.Snapshot, error) {
   142  	panic("not supported")
   143  }
   144  
   145  func (db *Database) Close() error {
   146  	db.remote.Close()
   147  	return nil
   148  }
   149  
   150  func New(client *rpc.Client) zonddb.Database {
   151  	return &Database{
   152  		remote: client,
   153  	}
   154  }