Merge pull request #1021 from obscuren/global_chain_lock

core: global chain insert lock
This commit is contained in:
Jeffrey Wilcke 2015-05-17 08:54:16 -07:00
commit 443d024843

View File

@ -84,6 +84,7 @@ type ChainManager struct {
genesisBlock *types.Block
// Last known total difficulty
mu sync.RWMutex
chainmu sync.RWMutex
tsmu sync.RWMutex
td *big.Int
@ -518,6 +519,9 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
self.wg.Add(1)
defer self.wg.Done()
self.chainmu.Lock()
defer self.chainmu.Unlock()
// A queued approach to delivering events. This is generally faster than direct delivery and requires much less mutex acquiring.
var (
queue = make([]interface{}, len(chain))
@ -542,7 +546,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
continue
}
block.Td = new(big.Int)
// Do not penelise on future block. We'll need a block queue eventually that will queue
// future block for future use
if err == BlockFutureErr {
@ -568,8 +571,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
return i, err
}
self.mu.Lock()
{
cblock := self.currentBlock
// Write block to database. Eventually we'll have to improve on this and throw away blocks that are
// not in the canonical chain.
@ -614,8 +615,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
queueEvent.sideCount++
}
self.futureBlocks.Delete(block.Hash())
}
self.mu.Unlock()
stats.processed++