From 07baf66200c74a97b440a199dce7321b23aea4cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Tue, 9 Jun 2015 15:23:20 +0300 Subject: core: fix data race in accessing ChainManager.td --- core/chain_manager.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index a0ce20006..2333368de 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -56,10 +56,7 @@ func CalcTD(block, parent *types.Block) *big.Int { if parent == nil { return block.Difficulty() } - - td := new(big.Int).Add(parent.Td, block.Header().Difficulty) - - return td + return new(big.Int).Add(parent.Td, block.Header().Difficulty) } func CalcGasLimit(parent *types.Block) *big.Int { @@ -178,7 +175,7 @@ func (self *ChainManager) Td() *big.Int { self.mu.RLock() defer self.mu.RUnlock() - return self.td + return new(big.Int).Set(self.td) } func (self *ChainManager) GasLimit() *big.Int { @@ -204,7 +201,7 @@ func (self *ChainManager) Status() (td *big.Int, currentBlock common.Hash, genes self.mu.RLock() defer self.mu.RUnlock() - return self.td, self.currentBlock.Hash(), self.genesisBlock.Hash() + return new(big.Int).Set(self.td), self.currentBlock.Hash(), self.genesisBlock.Hash() } func (self *ChainManager) SetProcessor(proc types.BlockProcessor) { @@ -488,8 +485,10 @@ func (self *ChainManager) GetAncestors(block *types.Block, length int) (blocks [ } func (bc *ChainManager) setTotalDifficulty(td *big.Int) { - //bc.blockDb.Put([]byte("LTD"), td.Bytes()) - bc.td = td + bc.mu.Lock() + defer bc.mu.Unlock() + + bc.td.Set(td) } func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) { @@ -626,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 -- cgit v1.2.3 From ca8cb65b73b5bdb6a30b6a45304b3c45acc66bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Tue, 9 Jun 2015 15:30:46 +0300 Subject: core: fix data race accessing ChainManager.currentBlock --- core/chain_manager.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index 2333368de..2ba81550e 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -379,8 +379,11 @@ func (self *ChainManager) ExportN(w io.Writer, first uint64, last uint64) error func (bc *ChainManager) insert(block *types.Block) { key := append(blockNumPre, block.Number().Bytes()...) 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() } -- cgit v1.2.3 From ebf2aabd254a4e765b68cdb46b18806fa7e4cb4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Tue, 9 Jun 2015 16:26:44 +0300 Subject: core: fix up a deadlock caused by double locking --- core/chain_manager.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'core') 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 -- cgit v1.2.3