@@ -27,15 +27,14 @@ import (
2727 "github.com/ethereum/go-ethereum/trie/triestate"
2828 "github.com/ethereum/go-ethereum/triedb/database"
2929 "github.com/ethereum/go-ethereum/triedb/hashdb"
30- "github.com/ethereum/go-ethereum/triedb/pathdb"
3130)
3231
3332// Config defines all necessary options for database.
3433type Config struct {
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
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
3938}
4039
4140// HashDefaults represents a config for using hash-based scheme with
@@ -45,6 +44,15 @@ var HashDefaults = &Config{
4544 HashDB : hashdb .Defaults ,
4645}
4746
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+
4856// backend defines the methods needed to access/update trie nodes in different
4957// state scheme.
5058type backend interface {
@@ -76,6 +84,10 @@ type backend interface {
7684
7785 // Close closes the trie database backend and releases all held resources.
7886 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 )
7991}
8092
8193// Database is the wrapper of the underlying backend which is shared by different
@@ -108,7 +120,7 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
108120 log .Crit ("Both 'hash' and 'path' mode are configured" )
109121 }
110122 if config .PathDB != nil {
111- db .backend = pathdb . New (diskdb , config . PathDB )
123+ db .backend = config . PathDB . New (diskdb )
112124 } else {
113125 var resolver hashdb.ChildResolver
114126 if config .IsVerkle {
@@ -117,21 +129,19 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
117129 } else {
118130 resolver = trie.MerkleResolver {}
119131 }
120- db .backend = hashdb .New (diskdb , config .HashDB , resolver )
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 )
121137 }
122138 return db
123139}
124140
125141// Reader returns a reader for accessing all trie nodes with provided state root.
126142// An error will be returned if the requested state is not available.
127143func (db * Database ) Reader (blockRoot common.Hash ) (database.Reader , error ) {
128- switch b := db .backend .(type ) {
129- case * hashdb.Database :
130- return b .Reader (blockRoot )
131- case * pathdb.Database :
132- return b .Reader (blockRoot )
133- }
134- return nil , errors .New ("unknown backend" )
144+ return db .backend .Reader (blockRoot )
135145}
136146
137147// Update performs a state transition by committing dirty nodes contained in the
@@ -221,7 +231,7 @@ func (db *Database) InsertPreimage(preimages map[common.Hash][]byte) {
221231//
222232// It's only supported by hash-based database and will return an error for others.
223233func (db * Database ) Cap (limit common.StorageSize ) error {
224- hdb , ok := db .backend .(* hashdb. Database )
234+ hdb , ok := db .backend .(database. HashBackend )
225235 if ! ok {
226236 return errors .New ("not supported" )
227237 }
@@ -237,7 +247,7 @@ func (db *Database) Cap(limit common.StorageSize) error {
237247//
238248// It's only supported by hash-based database and will return an error for others.
239249func (db * Database ) Reference (root common.Hash , parent common.Hash ) error {
240- hdb , ok := db .backend .(* hashdb. Database )
250+ hdb , ok := db .backend .(database. HashBackend )
241251 if ! ok {
242252 return errors .New ("not supported" )
243253 }
@@ -248,7 +258,7 @@ func (db *Database) Reference(root common.Hash, parent common.Hash) error {
248258// Dereference removes an existing reference from a root node. It's only
249259// supported by hash-based database and will return an error for others.
250260func (db * Database ) Dereference (root common.Hash ) error {
251- hdb , ok := db .backend .(* hashdb. Database )
261+ hdb , ok := db .backend .(database. HashBackend )
252262 if ! ok {
253263 return errors .New ("not supported" )
254264 }
@@ -261,7 +271,7 @@ func (db *Database) Dereference(root common.Hash) error {
261271// corresponding trie histories are existent. It's only supported by path-based
262272// database and will return an error for others.
263273func (db * Database ) Recover (target common.Hash ) error {
264- pdb , ok := db .backend .(* pathdb. Database )
274+ pdb , ok := db .backend .(database. PathBackend )
265275 if ! ok {
266276 return errors .New ("not supported" )
267277 }
@@ -279,7 +289,7 @@ func (db *Database) Recover(target common.Hash) error {
279289// recovered. It's only supported by path-based database and will return an
280290// error for others.
281291func (db * Database ) Recoverable (root common.Hash ) (bool , error ) {
282- pdb , ok := db .backend .(* pathdb. Database )
292+ pdb , ok := db .backend .(database. PathBackend )
283293 if ! ok {
284294 return false , errors .New ("not supported" )
285295 }
@@ -292,7 +302,7 @@ func (db *Database) Recoverable(root common.Hash) (bool, error) {
292302//
293303// It's only supported by path-based database and will return an error for others.
294304func (db * Database ) Disable () error {
295- pdb , ok := db .backend .(* pathdb. Database )
305+ pdb , ok := db .backend .(database. PathBackend )
296306 if ! ok {
297307 return errors .New ("not supported" )
298308 }
@@ -302,7 +312,7 @@ func (db *Database) Disable() error {
302312// Enable activates database and resets the state tree with the provided persistent
303313// state root once the state sync is finished.
304314func (db * Database ) Enable (root common.Hash ) error {
305- pdb , ok := db .backend .(* pathdb. Database )
315+ pdb , ok := db .backend .(database. PathBackend )
306316 if ! ok {
307317 return errors .New ("not supported" )
308318 }
@@ -314,7 +324,7 @@ func (db *Database) Enable(root common.Hash) error {
314324// flattening everything down (bad for reorgs). It's only supported by path-based
315325// database and will return an error for others.
316326func (db * Database ) Journal (root common.Hash ) error {
317- pdb , ok := db .backend .(* pathdb. Database )
327+ pdb , ok := db .backend .(database. PathBackend )
318328 if ! ok {
319329 return errors .New ("not supported" )
320330 }
@@ -325,7 +335,7 @@ func (db *Database) Journal(root common.Hash) error {
325335// It's only supported by path-based database and will return an error for
326336// others.
327337func (db * Database ) SetBufferSize (size int ) error {
328- pdb , ok := db .backend .(* pathdb. Database )
338+ pdb , ok := db .backend .(database. PathBackend )
329339 if ! ok {
330340 return errors .New ("not supported" )
331341 }
0 commit comments