triedb: refactor database for a bit cleaner

This commit is contained in:
Ha DANG 2024-05-30 15:34:06 +07:00 committed by prpeh
parent 6bb13e8e2b
commit d1241d0d92

View file

@ -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,14 +224,13 @@ 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
@ -238,23 +239,21 @@ 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
@ -262,10 +261,7 @@ func (db *Database) Dereference(root common.Hash) error {
// 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
@ -274,17 +270,18 @@ func (db *Database) Recover(target common.Hash) error {
loader = trie.NewMerkleLoader(db) loader = trie.NewMerkleLoader(db)
} }
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
@ -293,21 +290,19 @@ 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.
@ -315,22 +310,20 @@ func (db *Database) Enable(root common.Hash) error {
// 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.