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,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 {
|
if db.preimages != nil {
|
||||||
return errors.New("not supported")
|
db.preimages.commit(false)
|
||||||
|
}
|
||||||
|
return hdb.Cap(limit)
|
||||||
}
|
}
|
||||||
if db.preimages != nil {
|
return errNotSupported
|
||||||
db.preimages.commit(false)
|
|
||||||
}
|
|
||||||
return hdb.Cap(limit)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
hdb.Reference(root, parent)
|
||||||
return errors.New("not supported")
|
return nil
|
||||||
}
|
}
|
||||||
hdb.Reference(root, parent)
|
return errNotSupported
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
hdb.Dereference(root)
|
||||||
return errors.New("not supported")
|
return nil
|
||||||
}
|
}
|
||||||
hdb.Dereference(root)
|
return errNotSupported
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,29 +261,27 @@ 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 {
|
var loader triestate.TrieLoader
|
||||||
return errors.New("not supported")
|
if db.config.IsVerkle {
|
||||||
|
// TODO define verkle loader
|
||||||
|
log.Crit("Verkle loader is not defined")
|
||||||
|
} else {
|
||||||
|
loader = trie.NewMerkleLoader(db)
|
||||||
|
}
|
||||||
|
return pdb.Recover(target, loader)
|
||||||
}
|
}
|
||||||
var loader triestate.TrieLoader
|
return errNotSupported
|
||||||
if db.config.IsVerkle {
|
|
||||||
// TODO define verkle loader
|
|
||||||
log.Crit("Verkle loader is not defined")
|
|
||||||
} else {
|
|
||||||
loader = trie.NewMerkleLoader(db)
|
|
||||||
}
|
|
||||||
return pdb.Recover(target, loader)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 pdb.Recoverable(root), nil
|
||||||
return false, errors.New("not supported")
|
|
||||||
}
|
}
|
||||||
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 pdb.Disable()
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
}
|
||||||
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 pdb.Enable(root)
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
}
|
||||||
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 pdb.Journal(root)
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
}
|
||||||
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 pdb.SetBufferSize(size)
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
}
|
||||||
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.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue