2021-12-30 21:55:21 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
2021-12-31 18:17:57 +00:00
|
|
|
package leveldb_ethdb_rpc
|
2021-12-30 21:55:21 +00:00
|
|
|
|
|
|
|
import (
|
2022-05-27 18:48:36 +00:00
|
|
|
"errors"
|
|
|
|
|
2021-12-30 21:55:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb/leveldb"
|
|
|
|
)
|
|
|
|
|
2022-05-27 18:48:36 +00:00
|
|
|
var errNotSupported = errors.New("this operation is not supported")
|
2021-12-30 21:55:21 +00:00
|
|
|
var _ ethdb.Database = &LevelDBBackend{}
|
|
|
|
|
|
|
|
// NewLevelDBBackend creates a new levelDB RPC server backend
|
2021-12-31 18:17:57 +00:00
|
|
|
func NewLevelDBBackend(conf *Config) (*LevelDBBackend, error) {
|
|
|
|
db, err := leveldb.New(conf.FilePath, conf.Cache, conf.Handles, conf.Namespace, true)
|
2021-12-30 21:55:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-31 18:17:57 +00:00
|
|
|
frdb, err := rawdb.NewDatabaseWithFreezer(db, conf.FreezerPath, conf.Namespace, true)
|
2021-12-30 21:55:21 +00:00
|
|
|
if err != nil {
|
|
|
|
db.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &LevelDBBackend{
|
|
|
|
ethDB: frdb,
|
|
|
|
levelDB: db,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type LevelDBBackend struct {
|
|
|
|
ethDB ethdb.Database
|
|
|
|
levelDB *leveldb.Database
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) Has(key []byte) (bool, error) {
|
|
|
|
return s.ethDB.Has(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) Get(key []byte) ([]byte, error) {
|
|
|
|
return s.ethDB.Get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) HasAncient(kind string, number uint64) (bool, error) {
|
|
|
|
return s.ethDB.HasAncient(kind, number)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) Ancient(kind string, number uint64) ([]byte, error) {
|
|
|
|
return s.ethDB.Ancient(kind, number)
|
|
|
|
}
|
|
|
|
|
2021-12-31 18:17:57 +00:00
|
|
|
func (s *LevelDBBackend) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
|
|
|
|
return s.ethDB.AncientRange(kind, start, count, maxBytes)
|
|
|
|
}
|
|
|
|
|
2022-05-27 18:48:36 +00:00
|
|
|
func (s *LevelDBBackend) ReadAncients(fn func(ethdb.AncientReaderOp) error) error {
|
2021-12-31 18:17:57 +00:00
|
|
|
return s.ethDB.ReadAncients(fn)
|
2021-12-30 21:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) Ancients() (uint64, error) {
|
|
|
|
return s.ethDB.Ancients()
|
|
|
|
}
|
|
|
|
|
2022-05-09 10:18:56 +00:00
|
|
|
func (s *LevelDBBackend) Tail() (uint64, error) {
|
|
|
|
return s.ethDB.Tail()
|
|
|
|
}
|
|
|
|
|
2021-12-30 21:55:21 +00:00
|
|
|
func (s *LevelDBBackend) AncientSize(kind string) (uint64, error) {
|
|
|
|
return s.ethDB.AncientSize(kind)
|
|
|
|
}
|
|
|
|
|
2021-12-31 18:17:57 +00:00
|
|
|
func (s *LevelDBBackend) Put(_ []byte, _ []byte) error {
|
2021-12-30 21:55:21 +00:00
|
|
|
return errWriteNotAllowed
|
|
|
|
}
|
|
|
|
|
2021-12-31 18:17:57 +00:00
|
|
|
func (s *LevelDBBackend) Delete(_ []byte) error {
|
2021-12-30 21:55:21 +00:00
|
|
|
return errWriteNotAllowed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) ModifyAncients(f func(ethdb.AncientWriteOp) error) (int64, error) {
|
|
|
|
return 0, errWriteNotAllowed
|
|
|
|
}
|
|
|
|
|
2024-06-24 16:43:26 +00:00
|
|
|
func (s *LevelDBBackend) TruncateHead(n uint64) (uint64, error) {
|
|
|
|
return 0, errWriteNotAllowed
|
2022-05-09 10:18:56 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 16:43:26 +00:00
|
|
|
func (s *LevelDBBackend) TruncateTail(n uint64) (uint64, error) {
|
|
|
|
return 0, errWriteNotAllowed
|
2022-05-09 10:18:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 21:55:21 +00:00
|
|
|
func (s *LevelDBBackend) Sync() error {
|
|
|
|
return errWriteNotAllowed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) NewBatch() ethdb.Batch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-09 10:18:56 +00:00
|
|
|
func (d *LevelDBBackend) NewBatchWithSize(size int) ethdb.Batch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-31 18:17:57 +00:00
|
|
|
func (s *LevelDBBackend) NewIterator(_ []byte, _ []byte) ethdb.Iterator {
|
2021-12-30 21:55:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) Stat(property string) (string, error) {
|
|
|
|
return s.ethDB.Stat(property)
|
|
|
|
}
|
|
|
|
|
2021-12-31 18:17:57 +00:00
|
|
|
func (s *LevelDBBackend) Compact(_ []byte, _ []byte) error {
|
2021-12-30 21:55:21 +00:00
|
|
|
return errWriteNotAllowed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) Close() error {
|
|
|
|
return errWriteNotAllowed
|
|
|
|
}
|
2022-05-09 10:18:56 +00:00
|
|
|
|
|
|
|
func (s *LevelDBBackend) MigrateTable(string, func([]byte) ([]byte, error)) error {
|
|
|
|
return errWriteNotAllowed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LevelDBBackend) NewSnapshot() (ethdb.Snapshot, error) {
|
|
|
|
return s.ethDB.NewSnapshot()
|
|
|
|
}
|
2022-05-27 18:48:36 +00:00
|
|
|
|
|
|
|
// AncientDatadir returns an error as we don't have a backing chain freezer.
|
|
|
|
func (d *LevelDBBackend) AncientDatadir() (string, error) {
|
|
|
|
return "", errNotSupported
|
|
|
|
}
|