2022-05-16 12:11:01 +00:00
|
|
|
// Copyright © 2022 Vulcanize, Inc
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errNotSupported = errors.New("this operation is not supported")
|
|
|
|
|
2022-05-16 12:58:29 +00:00
|
|
|
var _ ethdb.Database = &DatabaseClient{}
|
2022-05-16 12:11:01 +00:00
|
|
|
|
2022-05-16 12:58:29 +00:00
|
|
|
// Type that satisfies the ethdb.DatabaseClient using a leveldb-ethdb-rpc client
|
|
|
|
type DatabaseClient struct {
|
2022-05-16 12:11:01 +00:00
|
|
|
client *rpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabase returns a ethdb.Database interface
|
|
|
|
func NewDatabaseClient(url string) (ethdb.Database, error) {
|
|
|
|
rpcClient, err := rpc.Dial(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-16 12:58:29 +00:00
|
|
|
database := DatabaseClient{
|
2022-05-16 12:11:01 +00:00
|
|
|
client: rpcClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &database, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has satisfies the ethdb.KeyValueReader interface
|
|
|
|
// Has retrieves if a key is present in the key-value data store
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Has(key []byte) (bool, error) {
|
2022-05-16 12:11:01 +00:00
|
|
|
var resp bool
|
|
|
|
err := d.client.Call(&resp, "leveldb_has", key)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get satisfies the ethdb.KeyValueReader interface
|
|
|
|
// Get retrieves the given key if it's present in the key-value data store
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Get(key []byte) ([]byte, error) {
|
2022-05-16 12:11:01 +00:00
|
|
|
var resp []byte
|
|
|
|
err := d.client.Call(&resp, "leveldb_get", key)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put satisfies the ethdb.KeyValueWriter interface
|
|
|
|
// Put inserts the given value into the key-value data store
|
|
|
|
// Key is expected to be the keccak256 hash of value
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Put(key []byte, value []byte) error {
|
2022-05-16 12:11:01 +00:00
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete satisfies the ethdb.KeyValueWriter interface
|
|
|
|
// Delete removes the key from the key-value data store
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Delete(key []byte) error {
|
2022-05-16 12:11:01 +00:00
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat satisfies the ethdb.Stater interface
|
|
|
|
// Stat returns a particular internal stat of the database
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Stat(property string) (string, error) {
|
|
|
|
var resp string
|
|
|
|
err := d.client.Call(&resp, "leveldb_stat", property)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2022-05-16 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compact satisfies the ethdb.Compacter interface
|
|
|
|
// Compact flattens the underlying data store for the given key range
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Compact(start []byte, limit []byte) error {
|
2022-05-16 12:11:01 +00:00
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBatch satisfies the ethdb.Batcher interface
|
|
|
|
// NewBatch creates a write-only database that buffers changes to its host db
|
|
|
|
// until a final write is called
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) NewBatch() ethdb.Batch {
|
2022-05-16 12:11:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBatchWithSize satisfies the ethdb.Batcher interface.
|
|
|
|
// NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) NewBatchWithSize(size int) ethdb.Batch {
|
2022-05-16 12:11:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIterator satisfies the ethdb.Iteratee interface
|
|
|
|
// it creates a binary-alphabetical iterator over a subset
|
|
|
|
// of database content with a particular key prefix, starting at a particular
|
|
|
|
// initial key (or after, if it does not exist).
|
|
|
|
//
|
|
|
|
// Note: This method assumes that the prefix is NOT part of the start, so there's
|
|
|
|
// no need for the caller to prepend the prefix to the start
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
|
2022-05-16 12:11:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close satisfies the io.Closer interface
|
|
|
|
// Close closes the db connection
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Close() error {
|
2022-05-16 12:11:01 +00:00
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasAncient satisfies the ethdb.AncientReader interface
|
|
|
|
// HasAncient returns an indicator whether the specified data exists in the ancient store
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) HasAncient(kind string, number uint64) (bool, error) {
|
|
|
|
var resp bool
|
|
|
|
err := d.client.Call(&resp, "leveldb_hasAncient", kind, number)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2022-05-16 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ancient satisfies the ethdb.AncientReader interface
|
|
|
|
// Ancient retrieves an ancient binary blob from the append-only immutable files
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Ancient(kind string, number uint64) ([]byte, error) {
|
|
|
|
var resp []byte
|
|
|
|
err := d.client.Call(&resp, "leveldb_ancient", kind, number)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2022-05-16 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ancients satisfies the ethdb.AncientReader interface
|
|
|
|
// Ancients returns the ancient item numbers in the ancient store
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Ancients() (uint64, error) {
|
|
|
|
var resp uint64
|
|
|
|
err := d.client.Call(&resp, "leveldb_ancients")
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2022-05-16 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tail satisfies the ethdb.AncientReader interface.
|
|
|
|
// Tail returns the number of first stored item in the freezer.
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Tail() (uint64, error) {
|
2022-05-16 12:11:01 +00:00
|
|
|
return 0, errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// AncientSize satisfies the ethdb.AncientReader interface
|
|
|
|
// AncientSize returns the ancient size of the specified category
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) AncientSize(kind string) (uint64, error) {
|
|
|
|
var resp uint64
|
|
|
|
err := d.client.Call(&resp, "leveldb_ancientSize")
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2022-05-16 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AncientRange retrieves all the items in a range, starting from the index 'start'.
|
|
|
|
// It will return
|
|
|
|
// - at most 'count' items,
|
|
|
|
// - at least 1 item (even if exceeding the maxBytes), but will otherwise
|
|
|
|
// return as many items as fit into maxBytes.
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
|
|
|
|
var resp [][]byte
|
|
|
|
err := d.client.Call(&resp, "leveldb_ancientRange", kind, start, count, maxBytes)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2022-05-16 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAncients applies the provided AncientReader function
|
2022-05-27 18:48:36 +00:00
|
|
|
func (d *DatabaseClient) ReadAncients(fn func(ethdb.AncientReaderOp) error) (err error) {
|
2022-05-16 12:11:01 +00:00
|
|
|
return fn(d)
|
|
|
|
}
|
|
|
|
|
2022-05-17 04:34:42 +00:00
|
|
|
// ModifyAncients satisfies the ethdb.AncientWriter interface.
|
|
|
|
// ModifyAncients runs a write operation on the ancient store.
|
|
|
|
func (d *DatabaseClient) ModifyAncients(f func(ethdb.AncientWriteOp) error) (int64, error) {
|
|
|
|
return 0, errNotSupported
|
|
|
|
}
|
|
|
|
|
2022-05-16 12:11:01 +00:00
|
|
|
// TruncateHead satisfies the ethdb.AncientWriter interface.
|
|
|
|
// TruncateHead discards all but the first n ancient data from the ancient store.
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) TruncateHead(n uint64) error {
|
2022-05-16 12:11:01 +00:00
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// TruncateTail satisfies the ethdb.AncientWriter interface.
|
|
|
|
// TruncateTail discards the first n ancient data from the ancient store.
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) TruncateTail(n uint64) error {
|
2022-05-16 12:11:01 +00:00
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync satisfies the ethdb.AncientWriter interface
|
|
|
|
// Sync flushes all in-memory ancient store data to disk
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) Sync() error {
|
2022-05-16 12:11:01 +00:00
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// MigrateTable satisfies the ethdb.AncientWriter interface.
|
|
|
|
// MigrateTable processes and migrates entries of a given table to a new format.
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) MigrateTable(string, func([]byte) ([]byte, error)) error {
|
2022-05-16 12:11:01 +00:00
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSnapshot satisfies the ethdb.Snapshotter interface.
|
|
|
|
// NewSnapshot creates a database snapshot based on the current state.
|
2022-05-16 12:58:29 +00:00
|
|
|
func (d *DatabaseClient) NewSnapshot() (ethdb.Snapshot, error) {
|
2022-05-16 12:11:01 +00:00
|
|
|
return nil, errNotSupported
|
|
|
|
}
|
2022-05-27 18:48:36 +00:00
|
|
|
|
|
|
|
// AncientDatadir returns an error as we don't have a backing chain freezer.
|
|
|
|
func (d *DatabaseClient) AncientDatadir() (string, error) {
|
|
|
|
return "", errNotSupported
|
|
|
|
}
|