diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-06-10 02:02:26 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-06-10 02:02:26 +0800 |
commit | fecf2141750fe61640870672987023fe7213901a (patch) | |
tree | dc1b4e9e10b1ad2d03fb8ab28f4e9849e6ad708a /core | |
parent | 5f341e5db5c2c3c14c9e076959a84e05c6d917f4 (diff) | |
download | dexon-fecf2141750fe61640870672987023fe7213901a.tar dexon-fecf2141750fe61640870672987023fe7213901a.tar.gz dexon-fecf2141750fe61640870672987023fe7213901a.tar.bz2 dexon-fecf2141750fe61640870672987023fe7213901a.tar.lz dexon-fecf2141750fe61640870672987023fe7213901a.tar.xz dexon-fecf2141750fe61640870672987023fe7213901a.tar.zst dexon-fecf2141750fe61640870672987023fe7213901a.zip |
core: fix a lock annoyance and potential deadlock
Diffstat (limited to 'core')
-rw-r--r-- | core/chain_manager.go | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/core/chain_manager.go b/core/chain_manager.go index c69d3a10e..82fdbb1f1 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -376,6 +376,8 @@ func (self *ChainManager) ExportN(w io.Writer, first uint64, last uint64) error return nil } +// insert appends injects a block into the current chain block chain. Note, this +// function assumes that the `mu` mutex is held! func (bc *ChainManager) insert(block *types.Block) { key := append(blockNumPre, block.Number().Bytes()...) bc.blockDb.Put(key, block.Hash().Bytes()) @@ -484,6 +486,8 @@ func (self *ChainManager) GetAncestors(block *types.Block, length int) (blocks [ return } +// setTotalDifficulty updates the TD of the chain manager. Note, this function +// assumes that the `mu` mutex is held! func (bc *ChainManager) setTotalDifficulty(td *big.Int) { bc.td = new(big.Int).Set(td) } @@ -540,9 +544,6 @@ 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() @@ -625,7 +626,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 @@ -638,8 +639,10 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { queueEvent.splitCount++ } + self.mu.Lock() self.setTotalDifficulty(block.Td) self.insert(block) + self.mu.Unlock() jsonlogger.LogJson(&logger.EthChainNewHead{ BlockHash: block.Hash().Hex(), @@ -747,9 +750,11 @@ func (self *ChainManager) merge(oldBlock, newBlock *types.Block) error { } // insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly + self.mu.Lock() for _, block := range newChain { self.insert(block) } + self.mu.Unlock() return nil } |