diff options
author | obscuren <geffobscura@gmail.com> | 2015-05-15 21:30:34 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-05-16 02:26:15 +0800 |
commit | 5cec1aad152115502f8ba0d7fcc1c3e40b915d7a (patch) | |
tree | 453ade99aaef0c65bf9c62c3699c2cde0a2344ea /core/chain_manager.go | |
parent | 82c0780f81bef8c88db9dff4bb34917f2934c71d (diff) | |
download | go-tangerine-5cec1aad152115502f8ba0d7fcc1c3e40b915d7a.tar go-tangerine-5cec1aad152115502f8ba0d7fcc1c3e40b915d7a.tar.gz go-tangerine-5cec1aad152115502f8ba0d7fcc1c3e40b915d7a.tar.bz2 go-tangerine-5cec1aad152115502f8ba0d7fcc1c3e40b915d7a.tar.lz go-tangerine-5cec1aad152115502f8ba0d7fcc1c3e40b915d7a.tar.xz go-tangerine-5cec1aad152115502f8ba0d7fcc1c3e40b915d7a.tar.zst go-tangerine-5cec1aad152115502f8ba0d7fcc1c3e40b915d7a.zip |
core, miner: fork resolving and restart miner after sync op
Fork resolving fixes #940
Diffstat (limited to 'core/chain_manager.go')
-rw-r--r-- | core/chain_manager.go | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/core/chain_manager.go b/core/chain_manager.go index a0839ad41..2e8eb927d 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -577,10 +577,10 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { // 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 { - // Check for chain forks. If H(block.num - 1) != block.parent, we're on a fork and need to do some merging - if previous := self.getBlockByNumber(block.NumberU64() - 1); previous.Hash() != block.ParentHash() { + // chain fork + if block.ParentHash() != cblock.Hash() { // during split we merge two different chains and create the new canonical chain - self.merge(previous, block) + self.merge(cblock, block) queue[i] = ChainSplitEvent{block, logs} queueEvent.splitCount++ @@ -641,9 +641,17 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks { oldStart = oldBlock newStart = newBlock ) - // first find common number - for newBlock = newBlock; newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) { - newChain = append(newChain, newBlock) + + // first reduce whoever is higher bound + if oldBlock.NumberU64() > newBlock.NumberU64() { + // reduce old chain + for oldBlock = oldBlock; oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = self.GetBlock(oldBlock.ParentHash()) { + } + } else { + // reduce new chain and append new chain blocks for inserting later on + for newBlock = newBlock; newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) { + newChain = append(newChain, newBlock) + } } numSplit := newBlock.Number() @@ -669,7 +677,7 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks { func (self *ChainManager) merge(oldBlock, newBlock *types.Block) { newChain := self.diff(oldBlock, newBlock) - // insert blocks + // insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly for _, block := range newChain { self.insert(block) } |