diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-06-09 21:26:44 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-06-09 21:26:44 +0800 |
commit | ebf2aabd254a4e765b68cdb46b18806fa7e4cb4b (patch) | |
tree | d7d6d479d566fb17a31fb5c1cd11bf0e82cba408 /core/chain_manager.go | |
parent | ff84352fb73bdbb07a7b2cf24b417927bf5a5c5e (diff) | |
download | dexon-ebf2aabd254a4e765b68cdb46b18806fa7e4cb4b.tar.gz dexon-ebf2aabd254a4e765b68cdb46b18806fa7e4cb4b.tar.zst dexon-ebf2aabd254a4e765b68cdb46b18806fa7e4cb4b.zip |
core: fix up a deadlock caused by double locking
Diffstat (limited to 'core/chain_manager.go')
-rw-r--r-- | core/chain_manager.go | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/core/chain_manager.go b/core/chain_manager.go index 2ba81550e..c69d3a10e 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -381,9 +381,6 @@ func (bc *ChainManager) insert(block *types.Block) { bc.blockDb.Put(key, block.Hash().Bytes()) bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes()) - bc.mu.Lock() - defer bc.mu.Unlock() - bc.currentBlock = block bc.lastBlockHash = block.Hash() } @@ -488,10 +485,7 @@ func (self *ChainManager) GetAncestors(block *types.Block, length int) (blocks [ } func (bc *ChainManager) setTotalDifficulty(td *big.Int) { - bc.mu.Lock() - defer bc.mu.Unlock() - - bc.td.Set(td) + bc.td = new(big.Int).Set(td) } func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) { @@ -546,6 +540,9 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { self.wg.Add(1) defer self.wg.Done() + self.mu.Lock() + defer self.mu.Unlock() + self.chainmu.Lock() defer self.chainmu.Unlock() @@ -628,7 +625,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { cblock := self.currentBlock // Compare the TD of the last known block in the canonical chain to make sure it's greater. // At this point it's possible that a different chain (fork) becomes the new canonical chain. - if block.Td.Cmp(self.Td()) > 0 { + if block.Td.Cmp(self.td) > 0 { // chain fork if block.ParentHash() != cblock.Hash() { // during split we merge two different chains and create the new canonical chain |