diff --git a/core/blockchain.go b/core/blockchain.go index 2139a53a6c0aa..d450dcd549083 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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. @@ -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) @@ -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 { @@ -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) @@ -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 } @@ -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 } @@ -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 @@ -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 }