mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
triedb: refactor database for a bit cleaner
This commit is contained in:
parent
6bb13e8e2b
commit
d1241d0d92
1 changed files with 41 additions and 48 deletions
|
|
@ -31,6 +31,8 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/triedb/pathdb"
|
"github.com/ethereum/go-ethereum/triedb/pathdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var errNotSupported = errors.New("not supported")
|
||||||
|
|
||||||
// Config defines all necessary options for database.
|
// Config defines all necessary options for database.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Preimages bool // Flag whether the preimage of node key is recorded
|
Preimages bool // Flag whether the preimage of node key is recorded
|
||||||
|
|
@ -222,15 +224,14 @@ func (db *Database) InsertPreimage(preimages map[common.Hash][]byte) {
|
||||||
//
|
//
|
||||||
// It's only supported by hash-based database and will return an error for others.
|
// It's only supported by hash-based database and will return an error for others.
|
||||||
func (db *Database) Cap(limit common.StorageSize) error {
|
func (db *Database) Cap(limit common.StorageSize) error {
|
||||||
hdb, ok := db.backend.(*hashdb.Database)
|
if hdb, ok := db.backend.(*hashdb.Database); ok {
|
||||||
if !ok {
|
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
|
||||||
if db.preimages != nil {
|
if db.preimages != nil {
|
||||||
db.preimages.commit(false)
|
db.preimages.commit(false)
|
||||||
}
|
}
|
||||||
return hdb.Cap(limit)
|
return hdb.Cap(limit)
|
||||||
}
|
}
|
||||||
|
return errNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
// Reference adds a new reference from a parent node to a child node. This function
|
// Reference adds a new reference from a parent node to a child node. This function
|
||||||
// is used to add reference between internal trie node and external node(e.g. storage
|
// is used to add reference between internal trie node and external node(e.g. storage
|
||||||
|
|
@ -238,34 +239,29 @@ func (db *Database) Cap(limit common.StorageSize) error {
|
||||||
//
|
//
|
||||||
// It's only supported by hash-based database and will return an error for others.
|
// It's only supported by hash-based database and will return an error for others.
|
||||||
func (db *Database) Reference(root common.Hash, parent common.Hash) error {
|
func (db *Database) Reference(root common.Hash, parent common.Hash) error {
|
||||||
hdb, ok := db.backend.(*hashdb.Database)
|
if hdb, ok := db.backend.(*hashdb.Database); ok {
|
||||||
if !ok {
|
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
|
||||||
hdb.Reference(root, parent)
|
hdb.Reference(root, parent)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
return errNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
// Dereference removes an existing reference from a root node. It's only
|
// Dereference removes an existing reference from a root node. It's only
|
||||||
// supported by hash-based database and will return an error for others.
|
// supported by hash-based database and will return an error for others.
|
||||||
func (db *Database) Dereference(root common.Hash) error {
|
func (db *Database) Dereference(root common.Hash) error {
|
||||||
hdb, ok := db.backend.(*hashdb.Database)
|
if hdb, ok := db.backend.(*hashdb.Database); ok {
|
||||||
if !ok {
|
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
|
||||||
hdb.Dereference(root)
|
hdb.Dereference(root)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
return errNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
// Recover rollbacks the database to a specified historical point. The state is
|
// Recover rollbacks the database to a specified historical point. The state is
|
||||||
// supported as the rollback destination only if it's canonical state and the
|
// supported as the rollback destination only if it's canonical state and the
|
||||||
// corresponding trie histories are existent. It's only supported by path-based
|
// corresponding trie histories are existent. It's only supported by path-based
|
||||||
// database and will return an error for others.
|
// database and will return an error for others.
|
||||||
func (db *Database) Recover(target common.Hash) error {
|
func (db *Database) Recover(target common.Hash) error {
|
||||||
pdb, ok := db.backend.(*pathdb.Database)
|
if pdb, ok := db.backend.(*pathdb.Database); ok {
|
||||||
if !ok {
|
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
|
||||||
var loader triestate.TrieLoader
|
var loader triestate.TrieLoader
|
||||||
if db.config.IsVerkle {
|
if db.config.IsVerkle {
|
||||||
// TODO define verkle loader
|
// TODO define verkle loader
|
||||||
|
|
@ -275,17 +271,18 @@ func (db *Database) Recover(target common.Hash) error {
|
||||||
}
|
}
|
||||||
return pdb.Recover(target, loader)
|
return pdb.Recover(target, loader)
|
||||||
}
|
}
|
||||||
|
return errNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
// Recoverable returns the indicator if the specified state is enabled to be
|
// Recoverable returns the indicator if the specified state is enabled to be
|
||||||
// recovered. It's only supported by path-based database and will return an
|
// recovered. It's only supported by path-based database and will return an
|
||||||
// error for others.
|
// error for others.
|
||||||
func (db *Database) Recoverable(root common.Hash) (bool, error) {
|
func (db *Database) Recoverable(root common.Hash) (bool, error) {
|
||||||
pdb, ok := db.backend.(*pathdb.Database)
|
if pdb, ok := db.backend.(*pathdb.Database); ok {
|
||||||
if !ok {
|
|
||||||
return false, errors.New("not supported")
|
|
||||||
}
|
|
||||||
return pdb.Recoverable(root), nil
|
return pdb.Recoverable(root), nil
|
||||||
}
|
}
|
||||||
|
return false, errNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
// Disable deactivates the database and invalidates all available state layers
|
// Disable deactivates the database and invalidates all available state layers
|
||||||
// as stale to prevent access to the persistent state, which is in the syncing
|
// as stale to prevent access to the persistent state, which is in the syncing
|
||||||
|
|
@ -293,45 +290,41 @@ func (db *Database) Recoverable(root common.Hash) (bool, error) {
|
||||||
//
|
//
|
||||||
// It's only supported by path-based database and will return an error for others.
|
// It's only supported by path-based database and will return an error for others.
|
||||||
func (db *Database) Disable() error {
|
func (db *Database) Disable() error {
|
||||||
pdb, ok := db.backend.(*pathdb.Database)
|
if pdb, ok := db.backend.(*pathdb.Database); ok {
|
||||||
if !ok {
|
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
|
||||||
return pdb.Disable()
|
return pdb.Disable()
|
||||||
}
|
}
|
||||||
|
return errNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
// Enable activates database and resets the state tree with the provided persistent
|
// Enable activates database and resets the state tree with the provided persistent
|
||||||
// state root once the state sync is finished.
|
// state root once the state sync is finished.
|
||||||
func (db *Database) Enable(root common.Hash) error {
|
func (db *Database) Enable(root common.Hash) error {
|
||||||
pdb, ok := db.backend.(*pathdb.Database)
|
if pdb, ok := db.backend.(*pathdb.Database); ok {
|
||||||
if !ok {
|
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
|
||||||
return pdb.Enable(root)
|
return pdb.Enable(root)
|
||||||
}
|
}
|
||||||
|
return errNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
// Journal commits an entire diff hierarchy to disk into a single journal entry.
|
// Journal commits an entire diff hierarchy to disk into a single journal entry.
|
||||||
// This is meant to be used during shutdown to persist the snapshot without
|
// This is meant to be used during shutdown to persist the snapshot without
|
||||||
// flattening everything down (bad for reorgs). It's only supported by path-based
|
// flattening everything down (bad for reorgs). It's only supported by path-based
|
||||||
// database and will return an error for others.
|
// database and will return an error for others.
|
||||||
func (db *Database) Journal(root common.Hash) error {
|
func (db *Database) Journal(root common.Hash) error {
|
||||||
pdb, ok := db.backend.(*pathdb.Database)
|
if pdb, ok := db.backend.(*pathdb.Database); ok {
|
||||||
if !ok {
|
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
|
||||||
return pdb.Journal(root)
|
return pdb.Journal(root)
|
||||||
}
|
}
|
||||||
|
return errNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
// SetBufferSize sets the node buffer size to the provided value(in bytes).
|
// SetBufferSize sets the node buffer size to the provided value(in bytes).
|
||||||
// It's only supported by path-based database and will return an error for
|
// It's only supported by path-based database and will return an error for
|
||||||
// others.
|
// others.
|
||||||
func (db *Database) SetBufferSize(size int) error {
|
func (db *Database) SetBufferSize(size int) error {
|
||||||
pdb, ok := db.backend.(*pathdb.Database)
|
if pdb, ok := db.backend.(*pathdb.Database); ok {
|
||||||
if !ok {
|
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
|
||||||
return pdb.SetBufferSize(size)
|
return pdb.SetBufferSize(size)
|
||||||
}
|
}
|
||||||
|
return errNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
// IsVerkle returns the indicator if the database is holding a verkle tree.
|
// IsVerkle returns the indicator if the database is holding a verkle tree.
|
||||||
func (db *Database) IsVerkle() bool {
|
func (db *Database) IsVerkle() bool {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue