@@ -19,6 +19,7 @@ package eth
19
19
import (
20
20
"fmt"
21
21
"math"
22
+ "math/big"
22
23
"sync"
23
24
"time"
24
25
@@ -412,8 +413,10 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
412
413
pm .fetcher .Enqueue (p .id , request .Block )
413
414
414
415
// TODO: Schedule a sync to cover potential gaps (this needs proto update)
415
- p .SetTd (request .TD )
416
- go pm .synchronise (p )
416
+ if request .TD .Cmp (p .Td ()) > 0 {
417
+ p .SetTd (request .TD )
418
+ go pm .synchronise (p )
419
+ }
417
420
418
421
case TxMsg :
419
422
// Transactions arrived, parse all of them and deliver to the pool
@@ -452,9 +455,18 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
452
455
453
456
// If propagation is requested, send to a subset of the peer
454
457
if propagate {
458
+ // Calculate the TD of the block (it's not imported yet, so block.Td is not valid)
459
+ var td * big.Int
460
+ if parent := pm .chainman .GetBlock (block .ParentHash ()); parent != nil {
461
+ td = new (big.Int ).Add (parent .Td , block .Difficulty ())
462
+ } else {
463
+ glog .V (logger .Error ).Infof ("propagating dangling block #%d [%x]" , block .NumberU64 (), hash [:4 ])
464
+ return
465
+ }
466
+ // Send the block to a subset of our peers
455
467
transfer := peers [:int (math .Sqrt (float64 (len (peers ))))]
456
468
for _ , peer := range transfer {
457
- peer .SendNewBlock (block )
469
+ peer .SendNewBlock (block , td )
458
470
}
459
471
glog .V (logger .Detail ).Infof ("propagated block %x to %d peers in %v" , hash [:4 ], len (transfer ), time .Since (block .ReceivedAt ))
460
472
}
0 commit comments