diff options
author | Maran <maran.hidskes@gmail.com> | 2014-03-24 17:24:06 +0800 |
---|---|---|
committer | Maran <maran.hidskes@gmail.com> | 2014-03-24 17:24:06 +0800 |
commit | 274d5cc91c45349ec8d7a1f5a20ef29896b38b2e (patch) | |
tree | 6524987835bcfebd788fd3e89e3a42603cd9f056 | |
parent | b52b1fca89fd56549ecc0f086d96a39d6009e568 (diff) | |
download | dexon-274d5cc91c45349ec8d7a1f5a20ef29896b38b2e.tar dexon-274d5cc91c45349ec8d7a1f5a20ef29896b38b2e.tar.gz dexon-274d5cc91c45349ec8d7a1f5a20ef29896b38b2e.tar.bz2 dexon-274d5cc91c45349ec8d7a1f5a20ef29896b38b2e.tar.lz dexon-274d5cc91c45349ec8d7a1f5a20ef29896b38b2e.tar.xz dexon-274d5cc91c45349ec8d7a1f5a20ef29896b38b2e.tar.zst dexon-274d5cc91c45349ec8d7a1f5a20ef29896b38b2e.zip |
FindCanonicalChain returns true or false when we are on the Canonical chain or not
-rw-r--r-- | ethchain/block_chain.go | 5 | ||||
-rw-r--r-- | peer.go | 14 |
2 files changed, 15 insertions, 4 deletions
diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go index 6eea14652..f25f0ca5a 100644 --- a/ethchain/block_chain.go +++ b/ethchain/block_chain.go @@ -103,7 +103,8 @@ func (bc *BlockChain) CalculateBlockTD(block *Block) *big.Int { } // Is tasked by finding the CanonicalChain and resetting the chain if we are not the Conical one -func (bc *BlockChain) FindCanonicalChain(msg *ethwire.Msg, commonBlockHash []byte) { +// Return true if we are the using the canonical chain false if not +func (bc *BlockChain) FindCanonicalChain(msg *ethwire.Msg, commonBlockHash []byte) bool { // 1. Calculate TD of the current chain // 2. Calculate TD of the new chain // Reset state to the correct one @@ -138,8 +139,10 @@ func (bc *BlockChain) FindCanonicalChain(msg *ethwire.Msg, commonBlockHash []byt if chainDifficulty.Cmp(curChainDifficulty) == 1 { log.Println("[BCHAIN] The incoming Chain beat our asses, resetting") bc.ResetTillBlockHash(commonBlockHash) + return false } else { log.Println("[BCHAIN] Our chain showed the incoming chain who is boss. Ignoring.") + return true } } func (bc *BlockChain) ResetTillBlockHash(hash []byte) error { @@ -316,11 +316,18 @@ func (p *Peer) HandleInbound() { // 3. Yes: Let's continue what we are doing // 4. No: Let's request more blocks back. + // Make sure we are actually receiving anything if msg.Data.Len()-1 > 1 { + // We requested blocks and now we need to make sure we have a common ancestor somewhere in these blocks so we can find + // common ground to start syncing from lastBlock = ethchain.NewBlockFromRlpValue(msg.Data.Get(msg.Data.Len() - 1)) if p.ethereum.StateManager().BlockChain().HasBlock(lastBlock.Hash()) { fmt.Println("[PEER] We found a common ancestor, let's continue.") } else { + + // If we can't find a common ancenstor we need to request more blocks. + // FIXME: At one point this won't scale anymore since we are not asking for an offset + // we just keep increasing the amount of blocks. fmt.Println("[PEER] No common ancestor found, requesting more blocks.") p.blocksRequested = p.blocksRequested * 2 p.catchingUp = false @@ -329,14 +336,16 @@ func (p *Peer) HandleInbound() { for i := msg.Data.Len() - 1; i >= 0; i-- { block = ethchain.NewBlockFromRlpValue(msg.Data.Get(i)) - // Do we have this block on our chain? + // Do we have this block on our chain? If so we can continue if p.ethereum.StateManager().BlockChain().HasBlock(block.Hash()) { fmt.Println("[PEER] Block found, checking next one.") } else { // We don't have this block, but we do have a block with the same prevHash, diversion time! if p.ethereum.StateManager().BlockChain().HasBlockWithPrevHash(block.PrevHash) { fmt.Printf("[PEER] Local and foreign chain have diverted after %x, we are going to get freaky with it!\n", block.PrevHash) - p.ethereum.StateManager().BlockChain().FindCanonicalChain(msg, block.PrevHash) + if p.ethereum.StateManager().BlockChain().FindCanonicalChain(msg, block.PrevHash) { + return + } } else { fmt.Println("[PEER] Both local and foreign chain have same parent. Continue normally") } @@ -358,7 +367,6 @@ func (p *Peer) HandleInbound() { } break } else { - ethutil.Config.Log.Infof("[PEER] Block %x added\n", block.Hash()) lastBlock = block } } |