Skip to content

Commit 45d89bd

Browse files
authored
trie: faster snapshot generation ethereum#22504 (#1062)
1 parent 93c2745 commit 45d89bd

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
@@ -126,7 +126,7 @@ func checkStateConsistency(db ethdb.Database, root common.Hash) error {
126126

127127
// Tests that an empty state is not scheduled for syncing.
128128
func TestEmptyStateSync(t *testing.T) {
129-
if req := NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), trie.NewSyncBloom(1, memorydb.New())).Missing(1); len(req) != 0 {
129+
if req := NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), trie.NewSyncBloom(1, memorydb.New()), nil).Missing(1); len(req) != 0 {
130130
t.Errorf("content requested for empty state: %v", req)
131131
}
132132
}
@@ -146,7 +146,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool) {
146146
}
147147
// Create a destination state and sync with the scheduler
148148
dstDb := rawdb.NewMemoryDatabase()
149-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
149+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
150150

151151
queue := append([]common.Hash{}, sched.Missing(count)...)
152152
for len(queue) > 0 {
@@ -185,7 +185,7 @@ func TestIterativeDelayedStateSync(t *testing.T) {
185185

186186
// Create a destination state and sync with the scheduler
187187
dstDb := rawdb.NewMemoryDatabase()
188-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
188+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
189189

190190
queue := append([]common.Hash{}, sched.Missing(0)...)
191191
for len(queue) > 0 {
@@ -229,7 +229,7 @@ func testIterativeRandomStateSync(t *testing.T, count int) {
229229

230230
// Create a destination state and sync with the scheduler
231231
dstDb := rawdb.NewMemoryDatabase()
232-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
232+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
233233

234234
queue := make(map[common.Hash]struct{})
235235
for _, hash := range sched.Missing(count) {
@@ -276,7 +276,7 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
276276

277277
// Create a destination state and sync with the scheduler
278278
dstDb := rawdb.NewMemoryDatabase()
279-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
279+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
280280

281281
queue := make(map[common.Hash]struct{})
282282
for _, hash := range sched.Missing(0) {
@@ -339,7 +339,7 @@ func TestIncompleteStateSync(t *testing.T) {
339339

340340
// Create a destination state and sync with the scheduler
341341
dstDb := rawdb.NewMemoryDatabase()
342-
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
342+
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb), nil)
343343

344344
added := []common.Hash{}
345345
queue := append([]common.Hash{}, sched.Missing(1)...)

eth/downloader/statesync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ type stateTask struct {
282282
func newStateSync(d *Downloader, root common.Hash) *stateSync {
283283
return &stateSync{
284284
d: d,
285-
sched: state.NewStateSync(root, d.stateDB, trie.NewSyncBloom(1, memorydb.New())),
285+
sched: state.NewStateSync(root, d.stateDB, trie.NewSyncBloom(1, memorydb.New()), nil),
286286
keccak: sha3.NewLegacyKeccak256(),
287287
tasks: make(map[common.Hash]*stateTask),
288288
deliver: make(chan *stateReq),

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)