Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statedb: get rid of activestate, stopPrefetcher manually #1006

Merged
merged 4 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
brilliant-lx marked this conversation as resolved.
Show resolved Hide resolved
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
brilliant-lx marked this conversation as resolved.
Show resolved Hide resolved
}
s.prefetcherLock.Unlock()
brilliant-lx marked this conversation as resolved.
Show resolved Hide resolved
}

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()
brilliant-lx marked this conversation as resolved.
Show resolved Hide resolved
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