Skip to content

Commit

Permalink
core: remove bad use of waitgroup in blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed May 20, 2021
1 parent 289c278 commit aa54a53
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions core/blockchain.go
Expand Up @@ -996,6 +996,8 @@ func (bc *BlockChain) Stop() {
bc.scope.Close()
close(bc.quit)
bc.StopInsert()
bc.chainmu.Lock()
defer bc.chainmu.Unlock()
bc.wg.Wait()

// Ensure that the entirety of the state snapshot is journalled to disk.
Expand Down Expand Up @@ -1410,8 +1412,6 @@ func (bc *BlockChain) writeBlockWithoutState(block *types.Block, td *big.Int) (e
if bc.insertStopped() {
return errInsertionInterrupted
}
bc.wg.Add(1)
defer bc.wg.Done()

batch := bc.db.NewBatch()
rawdb.WriteTd(batch, block.Hash(), block.NumberU64(), td)
Expand All @@ -1425,9 +1425,6 @@ func (bc *BlockChain) writeBlockWithoutState(block *types.Block, td *big.Int) (e
// writeKnownBlock updates the head block flag with a known block
// and introduces chain reorg if necessary.
func (bc *BlockChain) writeKnownBlock(block *types.Block) error {
bc.wg.Add(1)
defer bc.wg.Done()

current := bc.CurrentBlock()
if block.ParentHash() != current.Hash() {
if err := bc.reorg(current, block); err != nil {
Expand All @@ -1452,8 +1449,6 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
if bc.insertStopped() {
return NonStatTy, errInsertionInterrupted
}
bc.wg.Add(1)
defer bc.wg.Done()

// Calculate the total difficulty of the block
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
Expand Down Expand Up @@ -1635,11 +1630,9 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
}
}
// Pre-checks passed, start the full block imports
bc.wg.Add(1)
bc.chainmu.Lock()
n, err := bc.insertChain(chain, true)
bc.chainmu.Unlock()
bc.wg.Done()

return n, err
}
Expand All @@ -1651,11 +1644,9 @@ func (bc *BlockChain) InsertChainWithoutSealVerification(block *types.Block) (in
defer bc.blockProcFeed.Send(false)

// Pre-checks passed, start the full block imports
bc.wg.Add(1)
bc.chainmu.Lock()
n, err := bc.insertChain(types.Blocks([]*types.Block{block}), false)
bc.chainmu.Unlock()
bc.wg.Done()

return n, err
}
Expand Down Expand Up @@ -2281,7 +2272,6 @@ func (bc *BlockChain) update() {
// the extra indices.
func (bc *BlockChain) maintainTxIndex(ancients uint64) {
defer bc.wg.Done()

// Before starting the actual maintenance, we need to handle a special case,
// where user might init Geth with an external ancient database. If so, we
// need to reindex all necessary transactions before starting to process any
Expand Down Expand Up @@ -2396,8 +2386,6 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
bc.chainmu.Lock()
defer bc.chainmu.Unlock()

bc.wg.Add(1)
defer bc.wg.Done()
_, err := bc.hc.InsertHeaderChain(chain, start)
return 0, err
}
Expand Down

0 comments on commit aa54a53

Please sign in to comment.