Bläddra i källkod

core: solve a remote-import/local-mine data race

Péter Szilágyi 9 år sedan
förälder
incheckning
f5a29eab5c
1 ändrade filer med 3 tillägg och 4 borttagningar
  1. 3 4
      core/blockchain.go

+ 3 - 4
core/blockchain.go

@@ -771,14 +771,13 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
 	if ptd == nil {
 		return NonStatTy, ParentError(block.ParentHash())
 	}
-
-	localTd := self.GetTd(self.currentBlock.Hash(), self.currentBlock.NumberU64())
-	externTd := new(big.Int).Add(block.Difficulty(), ptd)
-
 	// Make sure no inconsistent state is leaked during insertion
 	self.mu.Lock()
 	defer self.mu.Unlock()
 
+	localTd := self.GetTd(self.currentBlock.Hash(), self.currentBlock.NumberU64())
+	externTd := new(big.Int).Add(block.Difficulty(), ptd)
+
 	// If the total difficulty is higher than our known, add it to the canonical chain
 	// Second clause in the if statement reduces the vulnerability to selfish mining.
 	// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf