@@ -27,14 +27,17 @@ import (
2727 "github.com/ava-labs/libevm/trie/triestate"
2828 "github.com/ava-labs/libevm/triedb/database"
2929 "github.com/ava-labs/libevm/triedb/hashdb"
30+ "github.com/ava-labs/libevm/triedb/pathdb"
3031)
3132
3233// Config defines all necessary options for database.
3334type Config struct {
34- Preimages bool // Flag whether the preimage of node key is recorded
35- IsVerkle bool // Flag whether the db is holding a verkle tree
36- HashDB hashBackender // Configs for hash-based scheme
37- PathDB pathBackender // Configs for experimental path-based scheme
35+ Preimages bool // Flag whether the preimage of node key is recorded
36+ IsVerkle bool // Flag whether the db is holding a verkle tree
37+ HashDB * hashdb.Config // Configs for hash-based scheme
38+ PathDB * pathdb.Config // Configs for experimental path-based scheme
39+
40+ DBOverride BackendConstructor // Injects an arbitrary backend implementation
3841}
3942
4043// HashDefaults represents a config for using hash-based scheme with
@@ -44,15 +47,6 @@ var HashDefaults = &Config{
4447 HashDB : hashdb .Defaults ,
4548}
4649
47- type Backend backend
48-
49- type hashBackender interface {
50- New (diskdb ethdb.Database , resolver hashdb.ChildResolver ) database.HashBackend
51- }
52- type pathBackender interface {
53- New (diskdb ethdb.Database ) database.PathBackend
54- }
55-
5650// backend defines the methods needed to access/update trie nodes in different
5751// state scheme.
5852type backend interface {
@@ -84,10 +78,6 @@ type backend interface {
8478
8579 // Close closes the trie database backend and releases all held resources.
8680 Close () error
87-
88- // Reader returns a node reader associated with the specific state.
89- // An error will be returned if the specified state is not available.
90- Reader (stateRoot common.Hash ) (database.Reader , error )
9181}
9282
9383// Database is the wrapper of the underlying backend which is shared by different
@@ -120,7 +110,7 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
120110 log .Crit ("Both 'hash' and 'path' mode are configured" )
121111 }
122112 if config .PathDB != nil {
123- db .backend = config . PathDB . New (diskdb )
113+ db .backend = pathdb . New (diskdb , config . PathDB )
124114 } else {
125115 var resolver hashdb.ChildResolver
126116 if config .IsVerkle {
@@ -129,19 +119,24 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
129119 } else {
130120 resolver = trie.MerkleResolver {}
131121 }
132- if config .HashDB == nil {
133- // some tests don't set this yet pass a non-nil config
134- config .HashDB = hashdb .Defaults
135- }
136- db .backend = config .HashDB .New (diskdb , resolver )
122+ db .backend = hashdb .New (diskdb , config .HashDB , resolver )
137123 }
124+ db .overrideBackend (diskdb , config )
138125 return db
139126}
140127
141128// Reader returns a reader for accessing all trie nodes with provided state root.
142129// An error will be returned if the requested state is not available.
143130func (db * Database ) Reader (blockRoot common.Hash ) (database.Reader , error ) {
144- return db .backend .Reader (blockRoot )
131+ switch b := db .backend .(type ) {
132+ case ReaderProvider :
133+ return b .Reader (blockRoot )
134+ case * hashdb.Database :
135+ return b .Reader (blockRoot )
136+ case * pathdb.Database :
137+ return b .Reader (blockRoot )
138+ }
139+ return nil , errors .New ("unknown backend" )
145140}
146141
147142// Update performs a state transition by committing dirty nodes contained in the
@@ -231,7 +226,7 @@ func (db *Database) InsertPreimage(preimages map[common.Hash][]byte) {
231226//
232227// It's only supported by hash-based database and will return an error for others.
233228func (db * Database ) Cap (limit common.StorageSize ) error {
234- hdb , ok := db .backend .(database. HashBackend )
229+ hdb , ok := db .backend .(HashBackend )
235230 if ! ok {
236231 return errors .New ("not supported" )
237232 }
@@ -247,7 +242,7 @@ func (db *Database) Cap(limit common.StorageSize) error {
247242//
248243// It's only supported by hash-based database and will return an error for others.
249244func (db * Database ) Reference (root common.Hash , parent common.Hash ) error {
250- hdb , ok := db .backend .(database. HashBackend )
245+ hdb , ok := db .backend .(HashBackend )
251246 if ! ok {
252247 return errors .New ("not supported" )
253248 }
@@ -258,7 +253,7 @@ func (db *Database) Reference(root common.Hash, parent common.Hash) error {
258253// Dereference removes an existing reference from a root node. It's only
259254// supported by hash-based database and will return an error for others.
260255func (db * Database ) Dereference (root common.Hash ) error {
261- hdb , ok := db .backend .(database. HashBackend )
256+ hdb , ok := db .backend .(HashBackend )
262257 if ! ok {
263258 return errors .New ("not supported" )
264259 }
@@ -271,7 +266,7 @@ func (db *Database) Dereference(root common.Hash) error {
271266// corresponding trie histories are existent. It's only supported by path-based
272267// database and will return an error for others.
273268func (db * Database ) Recover (target common.Hash ) error {
274- pdb , ok := db .backend .(database. PathBackend )
269+ pdb , ok := db .backend .(PathBackend )
275270 if ! ok {
276271 return errors .New ("not supported" )
277272 }
@@ -289,7 +284,7 @@ func (db *Database) Recover(target common.Hash) error {
289284// recovered. It's only supported by path-based database and will return an
290285// error for others.
291286func (db * Database ) Recoverable (root common.Hash ) (bool , error ) {
292- pdb , ok := db .backend .(database. PathBackend )
287+ pdb , ok := db .backend .(PathBackend )
293288 if ! ok {
294289 return false , errors .New ("not supported" )
295290 }
@@ -302,7 +297,7 @@ func (db *Database) Recoverable(root common.Hash) (bool, error) {
302297//
303298// It's only supported by path-based database and will return an error for others.
304299func (db * Database ) Disable () error {
305- pdb , ok := db .backend .(database. PathBackend )
300+ pdb , ok := db .backend .(PathBackend )
306301 if ! ok {
307302 return errors .New ("not supported" )
308303 }
@@ -312,7 +307,7 @@ func (db *Database) Disable() error {
312307// Enable activates database and resets the state tree with the provided persistent
313308// state root once the state sync is finished.
314309func (db * Database ) Enable (root common.Hash ) error {
315- pdb , ok := db .backend .(database. PathBackend )
310+ pdb , ok := db .backend .(PathBackend )
316311 if ! ok {
317312 return errors .New ("not supported" )
318313 }
@@ -324,7 +319,7 @@ func (db *Database) Enable(root common.Hash) error {
324319// flattening everything down (bad for reorgs). It's only supported by path-based
325320// database and will return an error for others.
326321func (db * Database ) Journal (root common.Hash ) error {
327- pdb , ok := db .backend .(database. PathBackend )
322+ pdb , ok := db .backend .(PathBackend )
328323 if ! ok {
329324 return errors .New ("not supported" )
330325 }
@@ -335,7 +330,7 @@ func (db *Database) Journal(root common.Hash) error {
335330// It's only supported by path-based database and will return an error for
336331// others.
337332func (db * Database ) SetBufferSize (size int ) error {
338- pdb , ok := db .backend .(database. PathBackend )
333+ pdb , ok := db .backend .(PathBackend )
339334 if ! ok {
340335 return errors .New ("not supported" )
341336 }
0 commit comments