core: global chain insert lock

This commit is contained in:
obscuren 2015-05-17 00:55:02 +02:00
parent ad99089567
commit 27782bbade
1 changed files with 47 additions and 48 deletions

View File

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