Locking WriteBlockAndState for Istanbul Consensus

This commit is contained in:
Jitendra Bhurat 2018-09-24 15:29:00 -04:00
parent 8c9ed51fa0
commit 2991562244
1 changed files with 11 additions and 0 deletions

View File

@ -92,6 +92,7 @@ type BlockChain struct {
mu sync.RWMutex // global mutex for locking chain operations mu sync.RWMutex // global mutex for locking chain operations
chainmu sync.RWMutex // blockchain insertion lock chainmu sync.RWMutex // blockchain insertion lock
procmu sync.RWMutex // block processor lock procmu sync.RWMutex // block processor lock
insertmu sync.Mutex // block and state insert lock
checkpoint int // checkpoint counts towards the new checkpoint checkpoint int // checkpoint counts towards the new checkpoint
currentBlock *types.Block // Current head of the block chain currentBlock *types.Block // Current head of the block chain
@ -813,6 +814,16 @@ func (bc *BlockChain) WriteBlockAndState(block *types.Block, receipts []*types.R
bc.wg.Add(1) bc.wg.Add(1)
defer bc.wg.Done() defer bc.wg.Done()
bc.insertmu.Lock()
defer bc.insertmu.Unlock()
if bc.config.IsQuorum && bc.config.Istanbul != nil {
if bc.GetBlockByHash(block.Hash()) != nil {
log.Warn("Block already inserted", "number", block.NumberU64(), "hash", block.Hash())
return CanonStatTy, nil
}
}
// Calculate the total difficulty of the block // Calculate the total difficulty of the block
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1) ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
if ptd == nil { if ptd == nil {