Skip to content

Commit a7842c9

Browse files
authored
core, trie: cleanup trie database (#28062)
1 parent a8d7201 commit a7842c9

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

core/state/statedb.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ type StateDB struct {
135135
StorageUpdated int
136136
AccountDeleted int
137137
StorageDeleted int
138+
139+
// Testing hooks
140+
onCommit func(states *triestate.Set) // Hook invoked when commit is performed
138141
}
139142

140143
// New creates a new state from a given trie.
@@ -1276,13 +1279,17 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er
12761279
}
12771280
if root != origin {
12781281
start := time.Now()
1279-
if err := s.db.TrieDB().Update(root, origin, block, nodes, triestate.New(s.accountsOrigin, s.storagesOrigin, incomplete)); err != nil {
1282+
set := triestate.New(s.accountsOrigin, s.storagesOrigin, incomplete)
1283+
if err := s.db.TrieDB().Update(root, origin, block, nodes, set); err != nil {
12801284
return common.Hash{}, err
12811285
}
12821286
s.originalRoot = root
12831287
if metrics.EnabledExpensive {
12841288
s.TrieDBCommits += time.Since(start)
12851289
}
1290+
if s.onCommit != nil {
1291+
s.onCommit(set)
1292+
}
12861293
}
12871294
// Clear all internal flags at the end of commit operation.
12881295
s.accounts = make(map[common.Hash][]byte)

core/state/statedb_fuzz_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (test *stateTest) run() bool {
181181
storageList = append(storageList, copy2DSet(states.Storages))
182182
}
183183
disk = rawdb.NewMemoryDatabase()
184-
tdb = trie.NewDatabase(disk, &trie.Config{OnCommit: onCommit, PathDB: pathdb.Defaults})
184+
tdb = trie.NewDatabase(disk, &trie.Config{PathDB: pathdb.Defaults})
185185
sdb = NewDatabaseWithNodeDB(disk, tdb)
186186
byzantium = rand.Intn(2) == 0
187187
)
@@ -206,6 +206,8 @@ func (test *stateTest) run() bool {
206206
if err != nil {
207207
panic(err)
208208
}
209+
state.onCommit = onCommit
210+
209211
for i, action := range actions {
210212
if i%test.chunk == 0 && i != 0 {
211213
if byzantium {

trie/database.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ type Config struct {
3333
Preimages bool // Flag whether the preimage of node key is recorded
3434
HashDB *hashdb.Config // Configs for hash-based scheme
3535
PathDB *pathdb.Config // Configs for experimental path-based scheme
36-
37-
// Testing hooks
38-
OnCommit func(states *triestate.Set) // Hook invoked when commit is performed
3936
}
4037

4138
// HashDefaults represents a config for using hash-based scheme with
@@ -88,20 +85,6 @@ type Database struct {
8885
backend backend // The backend for managing trie nodes
8986
}
9087

91-
// prepare initializes the database with provided configs, but the
92-
// database backend is still left as nil.
93-
func prepare(diskdb ethdb.Database, config *Config) *Database {
94-
var preimages *preimageStore
95-
if config != nil && config.Preimages {
96-
preimages = newPreimageStore(diskdb)
97-
}
98-
return &Database{
99-
config: config,
100-
diskdb: diskdb,
101-
preimages: preimages,
102-
}
103-
}
104-
10588
// NewDatabase initializes the trie database with default settings, note
10689
// the legacy hash-based scheme is used by default.
10790
func NewDatabase(diskdb ethdb.Database, config *Config) *Database {
@@ -149,9 +132,6 @@ func (db *Database) Reader(blockRoot common.Hash) (Reader, error) {
149132
// The passed in maps(nodes, states) will be retained to avoid copying everything.
150133
// Therefore, these maps must not be changed afterwards.
151134
func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error {
152-
if db.config != nil && db.config.OnCommit != nil {
153-
db.config.OnCommit(states)
154-
}
155135
if db.preimages != nil {
156136
db.preimages.commit(false)
157137
}

trie/database_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ import (
2525

2626
// newTestDatabase initializes the trie database with specified scheme.
2727
func newTestDatabase(diskdb ethdb.Database, scheme string) *Database {
28-
db := prepare(diskdb, nil)
28+
config := &Config{Preimages: false}
2929
if scheme == rawdb.HashScheme {
30-
db.backend = hashdb.New(diskdb, &hashdb.Config{}, mptResolver{})
30+
config.HashDB = &hashdb.Config{
31+
CleanCacheSize: 0,
32+
} // disable clean cache
3133
} else {
32-
db.backend = pathdb.New(diskdb, &pathdb.Config{}) // disable clean/dirty cache
34+
config.PathDB = &pathdb.Config{
35+
CleanCacheSize: 0,
36+
DirtyCacheSize: 0,
37+
} // disable clean/dirty cache
3338
}
34-
return db
39+
return NewDatabase(diskdb, config)
3540
}

0 commit comments

Comments
 (0)