diff --git a/triedb/database.go b/triedb/database.go index 73fd23a3aec8..6ec8b157a742 100644 --- a/triedb/database.go +++ b/triedb/database.go @@ -37,7 +37,7 @@ type Config struct { HashDB *hashdb.Config // Configs for hash-based scheme PathDB *pathdb.Config // Configs for experimental path-based scheme - DBOverride BackendConstructor // Injects an arbitrary backend implementation + DBOverride DBConstructor // Injects an arbitrary backend-database implementation } // HashDefaults represents a config for using hash-based scheme with @@ -228,7 +228,7 @@ func (db *Database) InsertPreimage(preimages map[common.Hash][]byte) { // // It's only supported by hash-based database and will return an error for others. func (db *Database) Cap(limit common.StorageSize) error { - hdb, ok := db.backend.(HashBackend) + hdb, ok := db.backend.(HashDB) if !ok { return errors.New("not supported") } @@ -244,7 +244,7 @@ func (db *Database) Cap(limit common.StorageSize) error { // // 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 { - hdb, ok := db.backend.(HashBackend) + hdb, ok := db.backend.(HashDB) if !ok { return errors.New("not supported") } @@ -255,7 +255,7 @@ func (db *Database) Reference(root common.Hash, parent common.Hash) error { // Dereference removes an existing reference from a root node. It's only // supported by hash-based database and will return an error for others. func (db *Database) Dereference(root common.Hash) error { - hdb, ok := db.backend.(HashBackend) + hdb, ok := db.backend.(HashDB) if !ok { return errors.New("not supported") } @@ -268,7 +268,7 @@ func (db *Database) Dereference(root common.Hash) error { // corresponding trie histories are existent. It's only supported by path-based // database and will return an error for others. func (db *Database) Recover(target common.Hash) error { - pdb, ok := db.backend.(PathBackend) + pdb, ok := db.backend.(PathDB) if !ok { return errors.New("not supported") } @@ -286,7 +286,7 @@ func (db *Database) Recover(target common.Hash) error { // recovered. It's only supported by path-based database and will return an // error for others. func (db *Database) Recoverable(root common.Hash) (bool, error) { - pdb, ok := db.backend.(PathBackend) + pdb, ok := db.backend.(PathDB) if !ok { return false, errors.New("not supported") } @@ -299,7 +299,7 @@ func (db *Database) Recoverable(root common.Hash) (bool, error) { // // It's only supported by path-based database and will return an error for others. func (db *Database) Disable() error { - pdb, ok := db.backend.(PathBackend) + pdb, ok := db.backend.(PathDB) if !ok { return errors.New("not supported") } @@ -309,7 +309,7 @@ func (db *Database) Disable() error { // Enable activates database and resets the state tree with the provided persistent // state root once the state sync is finished. func (db *Database) Enable(root common.Hash) error { - pdb, ok := db.backend.(PathBackend) + pdb, ok := db.backend.(PathDB) if !ok { return errors.New("not supported") } @@ -321,7 +321,7 @@ func (db *Database) Enable(root common.Hash) error { // flattening everything down (bad for reorgs). It's only supported by path-based // database and will return an error for others. func (db *Database) Journal(root common.Hash) error { - pdb, ok := db.backend.(PathBackend) + pdb, ok := db.backend.(PathDB) if !ok { return errors.New("not supported") } @@ -332,7 +332,7 @@ func (db *Database) Journal(root common.Hash) error { // It's only supported by path-based database and will return an error for // others. func (db *Database) SetBufferSize(size int) error { - pdb, ok := db.backend.(PathBackend) + pdb, ok := db.backend.(PathDB) if !ok { return errors.New("not supported") } diff --git a/triedb/database.libevm.go b/triedb/database.libevm.go index 5752227b3e64..01998d464554 100644 --- a/triedb/database.libevm.go +++ b/triedb/database.libevm.go @@ -26,27 +26,28 @@ import ( "github.com/ava-labs/libevm/triedb/pathdb" ) -// Backend defines the intersection of methods shared by [hashdb.Database] and -// [pathdb.Database]. -type Backend backend +// BackendDB defines the intersection of methods shared by [hashdb.Database] and +// [pathdb.Database]. It is defined to export an otherwise internal type used by +// the non-libevm geth implementation. +type BackendDB backend // A ReaderProvider exposes its underlying Reader as an interface. Both // [hashdb.Database] and [pathdb.Database] return concrete types so Go's lack of // support for [covariant types] means that this method can't be defined on -// [Backend]. +// [BackendDB]. // // [covariant types]: https://go.dev/doc/faq#covariant_types type ReaderProvider interface { Reader(common.Hash) (database.Reader, error) } -// A BackendConstructor constructs alternative backend implementations. -type BackendConstructor func(ethdb.Database, *Config) BackendOverride +// A DBConstructor constructs alternative backend-database implementations. +type DBConstructor func(ethdb.Database, *Config) DBOverride -// A BackendOverride is an arbitrary implementation of a [Database] backend. It -// MUST be either a [HashBackend] or a [PathBackend]. -type BackendOverride interface { - Backend +// A DBOverride is an arbitrary implementation of a [Database] backend. It MUST +// be either a [HashDB] or a [PathDB]. +type DBOverride interface { + BackendDB ReaderProvider } @@ -60,8 +61,8 @@ func (db *Database) overrideBackend(diskdb ethdb.Database, config *Config) bool db.backend = config.DBOverride(diskdb, config) switch db.backend.(type) { - case HashBackend: - case PathBackend: + case HashDB: + case PathDB: default: log.Crit("Database override is neither hash- nor path-based") } @@ -70,22 +71,22 @@ func (db *Database) overrideBackend(diskdb ethdb.Database, config *Config) bool var ( // If either of these break then the respective interface SHOULD be updated. - _ HashBackend = (*hashdb.Database)(nil) - _ PathBackend = (*pathdb.Database)(nil) + _ HashDB = (*hashdb.Database)(nil) + _ PathDB = (*pathdb.Database)(nil) ) -// A HashBackend mirrors the functionality of a [hashdb.Database]. -type HashBackend interface { - Backend +// A HashDB mirrors the functionality of a [hashdb.Database]. +type HashDB interface { + BackendDB Cap(limit common.StorageSize) error Reference(root common.Hash, parent common.Hash) Dereference(root common.Hash) } -// A PathBackend mirrors the functionality of a [pathdb.Database]. -type PathBackend interface { - Backend +// A PathDB mirrors the functionality of a [pathdb.Database]. +type PathDB interface { + BackendDB Recover(root common.Hash, loader triestate.TrieLoader) error Recoverable(root common.Hash) bool diff --git a/triedb/database.libevm_test.go b/triedb/database.libevm_test.go index ea9345e6926e..6eedcb77a77b 100644 --- a/triedb/database.libevm_test.go +++ b/triedb/database.libevm_test.go @@ -28,7 +28,7 @@ import ( func TestDBOverride(t *testing.T) { config := &Config{ - DBOverride: func(d ethdb.Database, c *Config) BackendOverride { + DBOverride: func(d ethdb.Database, c *Config) DBOverride { return override{} }, } @@ -42,7 +42,7 @@ func TestDBOverride(t *testing.T) { } type override struct { - PathBackend + PathDB } type reader struct {