aboutsummaryrefslogtreecommitdiffstats
path: root/core/blockchain.go
diff options
context:
space:
mode:
authorBas van Kervel <bas@ethdev.com>2016-07-26 22:37:04 +0800
committerBas van Kervel <bas@ethdev.com>2016-08-16 21:21:22 +0800
commitbb8059f6aa86d1052d7c2dd75a6985982cb278f4 (patch)
tree44c57f98592518652f9791655be98c75fc198760 /core/blockchain.go
parent4c2cc32f2e279baa3059603b8c8a4329f31606f6 (diff)
downloaddexon-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar.gz
dexon-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar.zst
dexon-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.zip
core: ensure the canonical block is written before the canonical hash is set
Diffstat (limited to 'core/blockchain.go')
-rw-r--r--core/blockchain.go18
1 files changed, 9 insertions, 9 deletions
diff --git a/core/blockchain.go b/core/blockchain.go
index 950804d40..888c98dce 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -778,6 +778,14 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
localTd := self.GetTd(self.currentBlock.Hash(), self.currentBlock.NumberU64())
externTd := new(big.Int).Add(block.Difficulty(), ptd)
+ // Irrelevant of the canonical status, write the block itself to the database
+ if err := self.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil {
+ glog.Fatalf("failed to write block total difficulty: %v", err)
+ }
+ if err := WriteBlock(self.chainDb, block); err != nil {
+ glog.Fatalf("failed to write block contents: %v", err)
+ }
+
// 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
@@ -788,19 +796,11 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
return NonStatTy, err
}
}
- // Insert the block as the new head of the chain
- self.insert(block)
+ self.insert(block) // Insert the block as the new head of the chain
status = CanonStatTy
} else {
status = SideStatTy
}
- // Irrelevant of the canonical status, write the block itself to the database
- if err := self.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil {
- glog.Fatalf("failed to write block total difficulty: %v", err)
- }
- if err := WriteBlock(self.chainDb, block); err != nil {
- glog.Fatalf("failed to write block contents: %v", err)
- }
self.futureBlocks.Remove(block.Hash())