@@ -272,11 +272,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
272272 defer pm .removePeer (p .id )
273273
274274 // Register the peer in the downloader. If the downloader considers it banned, we disconnect
275- err := pm .downloader .RegisterPeer (p .id , p .version , p .Head (),
276- p .RequestHeadersByHash , p .RequestHeadersByNumber ,
277- p .RequestBodies , p .RequestReceipts , p .RequestNodeData ,
278- )
279- if err != nil {
275+ if err := pm .downloader .RegisterPeer (p .id , p .version , p .Head , p .RequestHeadersByHash , p .RequestHeadersByNumber , p .RequestBodies , p .RequestReceipts , p .RequestNodeData ); err != nil {
280276 return err
281277 }
282278 // Propagate existing transactions. new transactions appearing
@@ -413,7 +409,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
413409 // If we already have a DAO header, we can check the peer's TD against it. If
414410 // the peer's ahead of this, it too must have a reply to the DAO check
415411 if daoHeader := pm .blockchain .GetHeaderByNumber (pm .chainconfig .DAOForkBlock .Uint64 ()); daoHeader != nil {
416- if p . Td () .Cmp (pm .blockchain .GetTd (daoHeader .Hash (), daoHeader .Number .Uint64 ())) >= 0 {
412+ if _ , td := p . Head (); td .Cmp (pm .blockchain .GetTd (daoHeader .Hash (), daoHeader .Number .Uint64 ())) >= 0 {
417413 verifyDAO = false
418414 }
419415 }
@@ -619,7 +615,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
619615 // Mark the hashes as present at the remote node
620616 for _ , block := range announces {
621617 p .MarkBlock (block .Hash )
622- p .SetHead (block .Hash )
623618 }
624619 // Schedule all the unknown hashes for retrieval
625620 unknown := make ([]announce , 0 , len (announces ))
@@ -646,16 +641,23 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
646641
647642 // Mark the peer as owning the block and schedule it for import
648643 p .MarkBlock (request .Block .Hash ())
649- p .SetHead (request .Block .Hash ())
650-
651644 pm .fetcher .Enqueue (p .id , request .Block )
652645
653- // Update the peers total difficulty if needed, schedule a download if gapped
654- if request .TD .Cmp (p .Td ()) > 0 {
655- p .SetTd (request .TD )
646+ // Assuming the block is importable by the peer, but possibly not yet done so,
647+ // calculate the head hash and TD that the peer truly must have.
648+ var (
649+ trueHead = request .Block .ParentHash ()
650+ trueTD = new (big.Int ).Sub (request .TD , request .Block .Difficulty ())
651+ )
652+ // Update the peers total difficulty if better than the previous
653+ if _ , td := p .Head (); trueTD .Cmp (td ) > 0 {
654+ p .SetHead (trueHead , trueTD )
655+
656+ // Schedule a sync if above ours. Note, this will not fire a sync for a gap of
657+ // a singe block (as the true TD is below the propagated block), however this
658+ // scenario should easily be covered by the fetcher.
656659 currentBlock := pm .blockchain .CurrentBlock ()
657- td := pm .blockchain .GetTd (currentBlock .Hash (), currentBlock .NumberU64 ())
658- if request .TD .Cmp (new (big.Int ).Add (td , request .Block .Difficulty ())) > 0 {
660+ if trueTD .Cmp (pm .blockchain .GetTd (currentBlock .Hash (), currentBlock .NumberU64 ())) > 0 {
659661 go pm .synchronise (p )
660662 }
661663 }
0 commit comments