diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2016-02-29 22:05:37 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2016-02-29 22:05:37 +0800 |
commit | 4044a8cea44cd4cee3a8ddaf51a76b71c9d22042 (patch) | |
tree | 1aa3776381e8e117b66e4a8ed1bf83e29d966ff1 /core/blockchain.go | |
parent | c541b38fb36587d23c60f5e2f2b9b3c8700ec489 (diff) | |
parent | 61be63bb9b8527bb3e2357ad35a0f4ef29304da1 (diff) | |
download | go-tangerine-1.3.4.tar go-tangerine-1.3.4.tar.gz go-tangerine-1.3.4.tar.bz2 go-tangerine-1.3.4.tar.lz go-tangerine-1.3.4.tar.xz go-tangerine-1.3.4.tar.zst go-tangerine-1.3.4.zip |
Merge pull request #2258 from obscuren/release/1.3.4v1.3.4
Homestead Release Candidate
Diffstat (limited to 'core/blockchain.go')
-rw-r--r-- | core/blockchain.go | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index 16ef67e09..832bdda06 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -729,14 +729,18 @@ func (self *BlockChain) writeHeader(header *types.Header) error { if ptd == nil { return ParentError(header.ParentHash) } - td := new(big.Int).Add(header.Difficulty, ptd) + + localTd := self.GetTd(self.currentHeader.Hash()) + externTd := new(big.Int).Add(header.Difficulty, ptd) // Make sure no inconsistent state is leaked during insertion self.mu.Lock() defer self.mu.Unlock() // If the total difficulty is higher than our known, add it to the canonical chain - if td.Cmp(self.GetTd(self.currentHeader.Hash())) > 0 { + // Second clause in the if statement reduces the vulnerability to selfish mining. + // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf + if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) { // Delete any canonical number assignments above the new head for i := header.Number.Uint64() + 1; GetCanonicalHash(self.chainDb, i) != (common.Hash{}); i++ { DeleteCanonicalHash(self.chainDb, i) @@ -757,7 +761,7 @@ func (self *BlockChain) writeHeader(header *types.Header) error { self.currentHeader = types.CopyHeader(header) } // Irrelevant of the canonical status, write the header itself to the database - if err := WriteTd(self.chainDb, header.Hash(), td); err != nil { + if err := WriteTd(self.chainDb, header.Hash(), externTd); err != nil { glog.Fatalf("failed to write header total difficulty: %v", err) } if err := WriteHeader(self.chainDb, header); err != nil { @@ -1052,14 +1056,18 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status writeStatus, err if ptd == nil { return NonStatTy, ParentError(block.ParentHash()) } - td := new(big.Int).Add(block.Difficulty(), ptd) + + localTd := self.GetTd(self.currentBlock.Hash()) + externTd := new(big.Int).Add(block.Difficulty(), ptd) // Make sure no inconsistent state is leaked during insertion self.mu.Lock() defer self.mu.Unlock() // If the total difficulty is higher than our known, add it to the canonical chain - if td.Cmp(self.GetTd(self.currentBlock.Hash())) > 0 { + // Second clause in the if statement reduces the vulnerability to selfish mining. + // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf + if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) { // Reorganize the chain if the parent is not the head block if block.ParentHash() != self.currentBlock.Hash() { if err := self.reorg(self.currentBlock, block); err != nil { @@ -1073,7 +1081,7 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status writeStatus, err status = SideStatTy } // Irrelevant of the canonical status, write the block itself to the database - if err := WriteTd(self.chainDb, block.Hash(), td); err != nil { + if err := WriteTd(self.chainDb, block.Hash(), externTd); err != nil { glog.Fatalf("failed to write block total difficulty: %v", err) } if err := WriteBlock(self.chainDb, block); err != nil { |