core: added future proc mutex lock

Added a future lock which prevents the anything being added or removed
from or to the set when looping over the set of blocks. This fixes a nil
pointer in the range loop when trying to retrieve a block from the set
which was previously available but removed due to regular chain
processing.

Fixes #2305
This commit is contained in:
Jeffrey Wilcke 2016-03-08 15:55:27 +01:00
parent 8b58cd0190
commit 558d18d2b0

View File

@ -85,10 +85,9 @@ type BlockChain struct {
eventMux *event.TypeMux eventMux *event.TypeMux
genesisBlock *types.Block genesisBlock *types.Block
mu sync.RWMutex mu sync.RWMutex // global mutex for locking chain operations
chainmu sync.RWMutex chainmu sync.RWMutex // blockchain insertion lock
tsmu sync.RWMutex procmu sync.RWMutex // block processor lock
procmu sync.RWMutex
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
@ -99,15 +98,15 @@ type BlockChain struct {
blockCache *lru.Cache // Cache for the most recent entire blocks blockCache *lru.Cache // Cache for the most recent entire blocks
futureBlocks *lru.Cache // future blocks are blocks added for later processing futureBlocks *lru.Cache // future blocks are blocks added for later processing
quit chan struct{} quit chan struct{} // blockchain quit channel
running int32 // running must be called automically running int32 // running must be called automically
// procInterrupt must be atomically called // procInterrupt must be atomically called
procInterrupt int32 // interrupt signaler for block processing procInterrupt int32 // interrupt signaler for block processing
wg sync.WaitGroup wg sync.WaitGroup // chain processing wait group for shutting down
pow pow.PoW pow pow.PoW
processor Processor processor Processor // block processor interface
validator Validator validator Validator // block and state validator interface
} }
// NewBlockChain returns a fully initialised block chain using information // NewBlockChain returns a fully initialised block chain using information
@ -567,10 +566,11 @@ func (bc *BlockChain) Stop() {
} }
func (self *BlockChain) procFutureBlocks() { func (self *BlockChain) procFutureBlocks() {
blocks := make([]*types.Block, self.futureBlocks.Len()) blocks := make([]*types.Block, 0, self.futureBlocks.Len())
for i, hash := range self.futureBlocks.Keys() { for _, hash := range self.futureBlocks.Keys() {
block, _ := self.futureBlocks.Get(hash) if block, exist := self.futureBlocks.Get(hash); exist {
blocks[i] = block.(*types.Block) blocks = append(blocks, block.(*types.Block))
}
} }
if len(blocks) > 0 { if len(blocks) > 0 {
types.BlockBy(types.Number).Sort(blocks) types.BlockBy(types.Number).Sort(blocks)
@ -794,6 +794,7 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
if err := WriteBlock(self.chainDb, block); err != nil { if err := WriteBlock(self.chainDb, block); err != nil {
glog.Fatalf("filed to write block contents: %v", err) glog.Fatalf("filed to write block contents: %v", err)
} }
self.futureBlocks.Remove(block.Hash()) self.futureBlocks.Remove(block.Hash())
return return