FindCanonicalChain returns true or false when we are on the Canonical chain or not
This commit is contained in:
parent
b52b1fca89
commit
274d5cc91c
|
@ -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
|
// 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
|
// 1. Calculate TD of the current chain
|
||||||
// 2. Calculate TD of the new chain
|
// 2. Calculate TD of the new chain
|
||||||
// Reset state to the correct one
|
// Reset state to the correct one
|
||||||
|
@ -138,8 +139,10 @@ func (bc *BlockChain) FindCanonicalChain(msg *ethwire.Msg, commonBlockHash []byt
|
||||||
if chainDifficulty.Cmp(curChainDifficulty) == 1 {
|
if chainDifficulty.Cmp(curChainDifficulty) == 1 {
|
||||||
log.Println("[BCHAIN] The incoming Chain beat our asses, resetting")
|
log.Println("[BCHAIN] The incoming Chain beat our asses, resetting")
|
||||||
bc.ResetTillBlockHash(commonBlockHash)
|
bc.ResetTillBlockHash(commonBlockHash)
|
||||||
|
return false
|
||||||
} else {
|
} else {
|
||||||
log.Println("[BCHAIN] Our chain showed the incoming chain who is boss. Ignoring.")
|
log.Println("[BCHAIN] Our chain showed the incoming chain who is boss. Ignoring.")
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (bc *BlockChain) ResetTillBlockHash(hash []byte) error {
|
func (bc *BlockChain) ResetTillBlockHash(hash []byte) error {
|
||||||
|
|
14
peer.go
14
peer.go
|
@ -316,11 +316,18 @@ func (p *Peer) HandleInbound() {
|
||||||
// 3. Yes: Let's continue what we are doing
|
// 3. Yes: Let's continue what we are doing
|
||||||
// 4. No: Let's request more blocks back.
|
// 4. No: Let's request more blocks back.
|
||||||
|
|
||||||
|
// Make sure we are actually receiving anything
|
||||||
if msg.Data.Len()-1 > 1 {
|
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))
|
lastBlock = ethchain.NewBlockFromRlpValue(msg.Data.Get(msg.Data.Len() - 1))
|
||||||
if p.ethereum.StateManager().BlockChain().HasBlock(lastBlock.Hash()) {
|
if p.ethereum.StateManager().BlockChain().HasBlock(lastBlock.Hash()) {
|
||||||
fmt.Println("[PEER] We found a common ancestor, let's continue.")
|
fmt.Println("[PEER] We found a common ancestor, let's continue.")
|
||||||
} else {
|
} 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.")
|
fmt.Println("[PEER] No common ancestor found, requesting more blocks.")
|
||||||
p.blocksRequested = p.blocksRequested * 2
|
p.blocksRequested = p.blocksRequested * 2
|
||||||
p.catchingUp = false
|
p.catchingUp = false
|
||||||
|
@ -329,14 +336,16 @@ func (p *Peer) HandleInbound() {
|
||||||
|
|
||||||
for i := msg.Data.Len() - 1; i >= 0; i-- {
|
for i := msg.Data.Len() - 1; i >= 0; i-- {
|
||||||
block = ethchain.NewBlockFromRlpValue(msg.Data.Get(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()) {
|
if p.ethereum.StateManager().BlockChain().HasBlock(block.Hash()) {
|
||||||
fmt.Println("[PEER] Block found, checking next one.")
|
fmt.Println("[PEER] Block found, checking next one.")
|
||||||
} else {
|
} else {
|
||||||
// We don't have this block, but we do have a block with the same prevHash, diversion time!
|
// 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) {
|
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)
|
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 {
|
} else {
|
||||||
fmt.Println("[PEER] Both local and foreign chain have same parent. Continue normally")
|
fmt.Println("[PEER] Both local and foreign chain have same parent. Continue normally")
|
||||||
}
|
}
|
||||||
|
@ -358,7 +367,6 @@ func (p *Peer) HandleInbound() {
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
ethutil.Config.Log.Infof("[PEER] Block %x added\n", block.Hash())
|
|
||||||
lastBlock = block
|
lastBlock = block
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue