Skip to content

Commit 26986da

Browse files
committed
trie: faster snapshot generation ethereum#22504
1 parent 55f8714 commit 26986da

File tree

13 files changed

+61
-29
lines changed

13 files changed

+61
-29
lines changed

XDCx/tradingstate/state_liquidationprice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (l *liquidationPriceState) updateRoot(db Database) error {
136136
if l.dbErr != nil {
137137
return l.dbErr
138138
}
139-
root, err := l.trie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
139+
root, err := l.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
140140
var orderList orderList
141141
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
142142
return nil

XDCx/tradingstate/state_orderbook.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func (te *tradingExchanges) CommitAsksTrie(db Database) error {
245245
if te.dbErr != nil {
246246
return te.dbErr
247247
}
248-
root, err := te.asksTrie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
248+
root, err := te.asksTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
249249
var orderList orderList
250250
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
251251
return nil
@@ -307,7 +307,7 @@ func (te *tradingExchanges) CommitBidsTrie(db Database) error {
307307
if te.dbErr != nil {
308308
return te.dbErr
309309
}
310-
root, err := te.bidsTrie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
310+
root, err := te.bidsTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
311311
var orderList orderList
312312
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
313313
return nil
@@ -783,7 +783,7 @@ func (t *tradingExchanges) CommitLiquidationPriceTrie(db Database) error {
783783
if t.dbErr != nil {
784784
return t.dbErr
785785
}
786-
root, err := t.liquidationPriceTrie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
786+
root, err := t.liquidationPriceTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
787787
var orderList orderList
788788
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
789789
return nil

XDCx/tradingstate/statedb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func (t *TradingStateDB) Commit() (root common.Hash, err error) {
589589
}
590590
}
591591
// Write trie changes.
592-
root, err = t.trie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
592+
root, err = t.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
593593
var exchange tradingExchangeObject
594594
if err := rlp.DecodeBytes(leaf, &exchange); err != nil {
595595
return nil

XDCxlending/lendingstate/state_lendingbook.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ func (le *lendingExchangeState) CommitInvestingTrie(db Database) error {
472472
if le.dbErr != nil {
473473
return le.dbErr
474474
}
475-
root, err := le.investingTrie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
475+
root, err := le.investingTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
476476
var orderList itemList
477477
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
478478
return nil
@@ -493,7 +493,7 @@ func (le *lendingExchangeState) CommitBorrowingTrie(db Database) error {
493493
if le.dbErr != nil {
494494
return le.dbErr
495495
}
496-
root, err := le.borrowingTrie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
496+
root, err := le.borrowingTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
497497
var orderList itemList
498498
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
499499
return nil
@@ -514,7 +514,7 @@ func (le *lendingExchangeState) CommitLiquidationTimeTrie(db Database) error {
514514
if le.dbErr != nil {
515515
return le.dbErr
516516
}
517-
root, err := le.liquidationTimeTrie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
517+
root, err := le.liquidationTimeTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
518518
var orderList itemList
519519
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
520520
return nil

XDCxlending/lendingstate/statedb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ func (ls *LendingStateDB) Commit() (root common.Hash, err error) {
578578
}
579579
}
580580
// Write trie changes.
581-
root, err = ls.trie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
581+
root, err = ls.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
582582
var exchange lendingObject
583583
if err := rlp.DecodeBytes(leaf, &exchange); err != nil {
584584
return nil

core/state/statedb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
840840
// Write the account trie changes, measuing the amount of wasted time
841841
defer func(start time.Time) { s.AccountCommits += time.Since(start) }(time.Now())
842842

843-
return s.trie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
843+
return s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
844844
var account Account
845845
if err := rlp.DecodeBytes(leaf, &account); err != nil {
846846
return nil

core/state/sync.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,31 @@ import (
2626
)
2727

2828
// NewStateSync create a new state trie download scheduler.
29-
func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.SyncBloom) *trie.Sync {
29+
func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.SyncBloom, onLeaf func(paths [][]byte, leaf []byte) error) *trie.Sync {
30+
// Register the storage slot callback if the external callback is specified.
31+
var onSlot func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error
32+
if onLeaf != nil {
33+
onSlot = func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error {
34+
return onLeaf(paths, leaf)
35+
}
36+
}
37+
// Register the account callback to connect the state trie and the storage
38+
// trie belongs to the contract.
3039
var syncer *trie.Sync
31-
callback := func(path []byte, leaf []byte, parent common.Hash) error {
40+
onAccount := func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error {
41+
if onLeaf != nil {
42+
if err := onLeaf(paths, leaf); err != nil {
43+
return err
44+
}
45+
}
3246
var obj Account
3347
if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil {
3448
return err
3549
}
36-
syncer.AddSubTrie(obj.Root, path, parent, nil)
37-
syncer.AddCodeEntry(common.BytesToHash(obj.CodeHash), path, parent)
50+
syncer.AddSubTrie(obj.Root, hexpath, parent, onSlot)
51+
syncer.AddCodeEntry(common.BytesToHash(obj.CodeHash), hexpath, parent)
3852
return nil
3953
}
40-
syncer = trie.NewSync(root, database, callback, bloom)
54+
syncer = trie.NewSync(root, database, onAccount, bloom)
4155
return syncer
4256
}

core/state/sync_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func checkStateConsistency(db ethdb.Database, root common.Hash) error {
133133

134134
// Tests that an empty state is not scheduled for syncing.
135135
func TestEmptyStateSync(t *testing.T) {
136-
sync := NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), trie.NewSyncBloom(1, memorydb.New()))
136+
sync := NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), trie.NewSyncBloom(1, memorydb.New()), nil)
137137
if nodes, paths, codes := sync.Missing(1); len(nodes) != 0 || len(paths) != 0 || len(codes) != 0 {
138138
t.Errorf(" content requested for empty state: %v, %v, %v", nodes, paths, codes)
139139
}
@@ -170,7 +170,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
170170

171171
// Create a destination state and sync with the scheduler
172172
dstDb := rawdb.NewMemoryDatabase()
173-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
173+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
174174

175175
nodes, paths, codes := sched.Missing(count)
176176
var (
@@ -249,7 +249,7 @@ func TestIterativeDelayedStateSync(t *testing.T) {
249249

250250
// Create a destination state and sync with the scheduler
251251
dstDb := rawdb.NewMemoryDatabase()
252-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
252+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
253253

254254
nodes, _, codes := sched.Missing(0)
255255
queue := append(append([]common.Hash{}, nodes...), codes...)
@@ -297,7 +297,7 @@ func testIterativeRandomStateSync(t *testing.T, count int) {
297297

298298
// Create a destination state and sync with the scheduler
299299
dstDb := rawdb.NewMemoryDatabase()
300-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
300+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
301301

302302
queue := make(map[common.Hash]struct{})
303303
nodes, _, codes := sched.Missing(count)
@@ -347,7 +347,7 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
347347

348348
// Create a destination state and sync with the scheduler
349349
dstDb := rawdb.NewMemoryDatabase()
350-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
350+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
351351

352352
queue := make(map[common.Hash]struct{})
353353
nodes, _, codes := sched.Missing(0)
@@ -414,7 +414,7 @@ func TestIncompleteStateSync(t *testing.T) {
414414

415415
// Create a destination state and sync with the scheduler
416416
dstDb := rawdb.NewMemoryDatabase()
417-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
417+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
418418

419419
var added []common.Hash
420420

eth/downloader/statesync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ type codeTask struct {
294294
func newStateSync(d *Downloader, root common.Hash) *stateSync {
295295
return &stateSync{
296296
d: d,
297-
sched: state.NewStateSync(root, d.stateDB, trie.NewSyncBloom(1, memorydb.New())),
297+
sched: state.NewStateSync(root, d.stateDB, trie.NewSyncBloom(1, memorydb.New()), nil),
298298
keccak: sha3.NewLegacyKeccak256(),
299299
trieTasks: make(map[common.Hash]*trieTask),
300300
codeTasks: make(map[common.Hash]*codeTask),

trie/committer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ func (c *committer) commitLoop(db *Database) {
218218
switch n := n.(type) {
219219
case *shortNode:
220220
if child, ok := n.Val.(valueNode); ok {
221-
c.onleaf(nil, child, hash)
221+
c.onleaf(nil, nil, child, hash)
222222
}
223223
case *fullNode:
224224
// For children in range [0, 15], it's impossible
225225
// to contain valuenode. Only check the 17th child.
226226
if n.Children[16] != nil {
227-
c.onleaf(nil, n.Children[16].(valueNode), hash)
227+
c.onleaf(nil, nil, n.Children[16].(valueNode), hash)
228228
}
229229
}
230230
}

0 commit comments

Comments
 (0)