Skip to content

Commit

Permalink
statedb: get rid of activestate, stopPrefetcher manually (bnb-chain#1006
Browse files Browse the repository at this point in the history
)
  • Loading branch information
qinglin89 authored Aug 31, 2022
1 parent b485651 commit 64549a7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
15 changes: 3 additions & 12 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
// Calculate the total difficulty of the block
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
if ptd == nil {
state.StopPrefetcher()
return consensus.ErrUnknownAncestor
}
// Make sure no inconsistent state is leaked during insertion
Expand Down Expand Up @@ -1809,17 +1810,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool)
bc.reportBlock(block, nil, err)
return it.index, err
}
// No validation errors for the first block (or chain prefix skipped)
var activeState *state.StateDB
defer func() {
// The chain importer is starting and stopping trie prefetchers. If a bad
// block or other error is hit however, an early return may not properly
// terminate the background threads. This defer ensures that we clean up
// and dangling prefetcher, without defering each and holding on live refs.
if activeState != nil {
activeState.StopPrefetcher()
}
}()

for ; block != nil && err == nil || errors.Is(err, ErrKnownBlock); block, err = it.next() {
// If the chain is terminating, stop processing blocks
Expand Down Expand Up @@ -1909,9 +1899,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool)
statedb.SetExpectedStateRoot(block.Root())
statedb, receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
close(interruptCh) // state prefetch can be stopped
activeState = statedb
if err != nil {
bc.reportBlock(block, receipts, err)
statedb.StopPrefetcher()
return it.index, err
}
// Update the metrics touched during block processing
Expand All @@ -1930,6 +1920,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool)
if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil {
log.Error("validate state failed", "error", err)
bc.reportBlock(block, receipts, err)
statedb.StopPrefetcher()
return it.index, err
}
}
Expand Down
16 changes: 5 additions & 11 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,10 @@ func (s *StateDB) StopPrefetcher() {
return
}
s.prefetcherLock.Lock()
defer s.prefetcherLock.Unlock()
if s.prefetcher != nil {
s.prefetcher.close()
s.prefetcher = nil
}
s.prefetcherLock.Unlock()
}

func (s *StateDB) TriePrefetchInAdvance(block *types.Block, signer types.Signer) {
Expand Down Expand Up @@ -1155,15 +1154,7 @@ func (s *StateDB) StateIntermediateRoot() common.Hash {
// the remainder without, but pre-byzantium even the initial prefetcher is
// useless, so no sleep lost.
prefetcher := s.prefetcher
defer func() {
s.prefetcherLock.Lock()
if s.prefetcher != nil {
s.prefetcher.close()
s.prefetcher = nil
}
// try not use defer inside defer
s.prefetcherLock.Unlock()
}()
defer s.StopPrefetcher()

// Now we're about to start to write changes to the trie. The trie is so far
// _untouched_. We can check with the prefetcher, if it can give us a trie
Expand Down Expand Up @@ -1356,10 +1347,12 @@ func (s *StateDB) LightCommit() (common.Hash, *types.DiffLayer, error) {
// Commit writes the state to the underlying in-memory trie database.
func (s *StateDB) Commit(failPostCommitFunc func(), postCommitFuncs ...func() error) (common.Hash, *types.DiffLayer, error) {
if s.dbErr != nil {
s.StopPrefetcher()
return common.Hash{}, nil, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
}
// Finalize any pending changes and merge everything into the tries
if s.lightProcessed {
defer s.StopPrefetcher()
root, diff, err := s.LightCommit()
if err != nil {
return root, diff, err
Expand Down Expand Up @@ -1568,6 +1561,7 @@ func (s *StateDB) Commit(failPostCommitFunc func(), postCommitFuncs ...func() er
if s.pipeCommit {
go commmitTrie()
} else {
defer s.StopPrefetcher()
commitFuncs = append(commitFuncs, commmitTrie)
}
commitRes := make(chan error, len(commitFuncs))
Expand Down

0 comments on commit 64549a7

Please sign in to comment.