diff options
author | Péter Szilágyi <peterke@gmail.com> | 2016-08-09 16:38:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-09 16:38:34 +0800 |
commit | 44ea0da2b0bc96505299c083cac44d60814a78eb (patch) | |
tree | 5da823845a7b894d20271fe968abac6a5385763e /eth/handler.go | |
parent | 4f4e1026f50860b05a61fe4b5d53d6933f62f549 (diff) | |
parent | 1dd272080dfb49a07a87c46e18d8aeaa0fd41a08 (diff) | |
download | go-tangerine-44ea0da2b0bc96505299c083cac44d60814a78eb.tar go-tangerine-44ea0da2b0bc96505299c083cac44d60814a78eb.tar.gz go-tangerine-44ea0da2b0bc96505299c083cac44d60814a78eb.tar.bz2 go-tangerine-44ea0da2b0bc96505299c083cac44d60814a78eb.tar.lz go-tangerine-44ea0da2b0bc96505299c083cac44d60814a78eb.tar.xz go-tangerine-44ea0da2b0bc96505299c083cac44d60814a78eb.tar.zst go-tangerine-44ea0da2b0bc96505299c083cac44d60814a78eb.zip |
Merge pull request #2861 from karalabe/track-peer-heads-properly
eth, eth/downloader: better remote head tracking
Diffstat (limited to 'eth/handler.go')
-rw-r--r-- | eth/handler.go | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/eth/handler.go b/eth/handler.go index 9ad430976..8723300f5 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -272,11 +272,7 @@ func (pm *ProtocolManager) handle(p *peer) error { defer pm.removePeer(p.id) // Register the peer in the downloader. If the downloader considers it banned, we disconnect - err := pm.downloader.RegisterPeer(p.id, p.version, p.Head(), - p.RequestHeadersByHash, p.RequestHeadersByNumber, - p.RequestBodies, p.RequestReceipts, p.RequestNodeData, - ) - if err != nil { + if err := pm.downloader.RegisterPeer(p.id, p.version, p.Head, p.RequestHeadersByHash, p.RequestHeadersByNumber, p.RequestBodies, p.RequestReceipts, p.RequestNodeData); err != nil { return err } // Propagate existing transactions. new transactions appearing @@ -413,7 +409,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { // If we already have a DAO header, we can check the peer's TD against it. If // the peer's ahead of this, it too must have a reply to the DAO check if daoHeader := pm.blockchain.GetHeaderByNumber(pm.chainconfig.DAOForkBlock.Uint64()); daoHeader != nil { - if p.Td().Cmp(pm.blockchain.GetTd(daoHeader.Hash(), daoHeader.Number.Uint64())) >= 0 { + if _, td := p.Head(); td.Cmp(pm.blockchain.GetTd(daoHeader.Hash(), daoHeader.Number.Uint64())) >= 0 { verifyDAO = false } } @@ -619,7 +615,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { // Mark the hashes as present at the remote node for _, block := range announces { p.MarkBlock(block.Hash) - p.SetHead(block.Hash) } // Schedule all the unknown hashes for retrieval unknown := make([]announce, 0, len(announces)) @@ -646,16 +641,23 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { // Mark the peer as owning the block and schedule it for import p.MarkBlock(request.Block.Hash()) - p.SetHead(request.Block.Hash()) - pm.fetcher.Enqueue(p.id, request.Block) - // Update the peers total difficulty if needed, schedule a download if gapped - if request.TD.Cmp(p.Td()) > 0 { - p.SetTd(request.TD) + // Assuming the block is importable by the peer, but possibly not yet done so, + // calculate the head hash and TD that the peer truly must have. + var ( + trueHead = request.Block.ParentHash() + trueTD = new(big.Int).Sub(request.TD, request.Block.Difficulty()) + ) + // Update the peers total difficulty if better than the previous + if _, td := p.Head(); trueTD.Cmp(td) > 0 { + p.SetHead(trueHead, trueTD) + + // Schedule a sync if above ours. Note, this will not fire a sync for a gap of + // a singe block (as the true TD is below the propagated block), however this + // scenario should easily be covered by the fetcher. currentBlock := pm.blockchain.CurrentBlock() - td := pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) - if request.TD.Cmp(new(big.Int).Add(td, request.Block.Difficulty())) > 0 { + if trueTD.Cmp(pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())) > 0 { go pm.synchronise(p) } } |