Skip to content

Commit

Permalink
core: fix snapshot update for fast node sync (#2773)
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 authored Nov 28, 2024
1 parent a4fb352 commit 2ae712b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
err = p.slash(spoiledVal, state, header, cx, txs, receipts, systemTxs, usedGas, false)
if err != nil {
// it is possible that slash validator failed because of the slash channel is disabled.
log.Error("slash validator failed", "block hash", header.Hash(), "address", spoiledVal)
log.Error("slash validator failed", "block hash", header.Hash(), "address", spoiledVal, "err", err)
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,15 @@ func (db *CachingDB) Reader(stateRoot common.Hash) (Reader, error) {
readers = append(readers, sr) // snap reader is optional
}
}
// Set up the trie reader, which is expected to always be available
// as the gatekeeper unless the state is corrupted.
tr, err := newTrieReader(stateRoot, db.triedb, db.pointCache)
if err != nil {
return nil, err
if !db.NoTries() {
// Set up the trie reader, which is expected to always be available
// as the gatekeeper unless the state is corrupted.
tr, err := newTrieReader(stateRoot, db.triedb, db.pointCache)
if err != nil {
return nil, err
}
readers = append(readers, tr)
}
readers = append(readers, tr)

return newMultiReader(readers...)
}
Expand Down
46 changes: 21 additions & 25 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,31 +1025,30 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
usedAddrs [][]byte
deletedAddrs []common.Address
)
if !s.noTrie {
for addr, op := range s.mutations {
if op.applied {
continue
}
op.applied = true

if op.isDelete() {
deletedAddrs = append(deletedAddrs, addr)
} else {
s.updateStateObject(s.stateObjects[addr])
s.AccountUpdated += 1
}
usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure
}
for _, deletedAddr := range deletedAddrs {
s.deleteStateObject(deletedAddr)
s.AccountDeleted += 1
for addr, op := range s.mutations {
if op.applied {
continue
}
s.AccountUpdates += time.Since(start)
op.applied = true

if s.prefetcher != nil {
s.prefetcher.used(common.Hash{}, s.originalRoot, usedAddrs)
if op.isDelete() {
deletedAddrs = append(deletedAddrs, addr)
} else {
s.updateStateObject(s.stateObjects[addr])
s.AccountUpdated += 1
}
usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure
}
for _, deletedAddr := range deletedAddrs {
s.deleteStateObject(deletedAddr)
s.AccountDeleted += 1
}
s.AccountUpdates += time.Since(start)

if s.prefetcher != nil && len(usedAddrs) > 0 {
s.prefetcher.used(common.Hash{}, s.originalRoot, usedAddrs)
}

// Track the amount of time wasted on hashing the account trie
defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())

Expand Down Expand Up @@ -1335,7 +1334,7 @@ func (s *StateDB) commit(deleteEmptyObjects bool) (*stateUpdate, error) {
// code didn't anticipate for.
workers.Go(func() error {
if s.noTrie {
root = types.EmptyRootHash
root = s.expectedRoot
return nil
}
// Write the account trie changes, measuring the amount of wasted time
Expand All @@ -1360,9 +1359,6 @@ func (s *StateDB) commit(deleteEmptyObjects bool) (*stateUpdate, error) {
// 2 threads in total. But that kind of depends on the account commit being
// more expensive than it should be, so let's fix that and revisit this todo.
for addr, op := range s.mutations {
if s.noTrie {
continue
}
if op.isDelete() {
continue
}
Expand Down
1 change: 1 addition & 0 deletions core/state/trie_prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ func (sf *subfetcher) loop() {
for {
select {
case <-sf.wake:
//TODO(zzzckck): why OpenTrie twice?
// Subfetcher was woken up, retrieve any tasks to avoid spinning the lock
if sf.trie == nil {
if sf.owner == (common.Hash{}) {
Expand Down

0 comments on commit 2ae712b

Please sign in to comment.