Skip to content

Commit b041aed

Browse files
committed
Merge pull request #1450 from karalabe/fix-propagation-td
eth: calculate the correct TD, only update if better
2 parents cd6d703 + 4f95e2f commit b041aed

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

eth/handler.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package eth
1919
import (
2020
"fmt"
2121
"math"
22+
"math/big"
2223
"sync"
2324
"time"
2425

@@ -412,8 +413,10 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
412413
pm.fetcher.Enqueue(p.id, request.Block)
413414

414415
// 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+
}
417420

418421
case TxMsg:
419422
// Transactions arrived, parse all of them and deliver to the pool
@@ -452,9 +455,18 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
452455

453456
// If propagation is requested, send to a subset of the peer
454457
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
455467
transfer := peers[:int(math.Sqrt(float64(len(peers))))]
456468
for _, peer := range transfer {
457-
peer.SendNewBlock(block)
469+
peer.SendNewBlock(block, td)
458470
}
459471
glog.V(logger.Detail).Infof("propagated block %x to %d peers in %v", hash[:4], len(transfer), time.Since(block.ReceivedAt))
460472
}

eth/peer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ func (p *peer) SendNewBlockHashes(hashes []common.Hash) error {
167167
}
168168

169169
// SendNewBlock propagates an entire block to a remote peer.
170-
func (p *peer) SendNewBlock(block *types.Block) error {
170+
func (p *peer) SendNewBlock(block *types.Block, td *big.Int) error {
171171
propBlockOutPacketsMeter.Mark(1)
172172
propBlockOutTrafficMeter.Mark(block.Size().Int64())
173173

174174
p.knownBlocks.Add(block.Hash())
175-
return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, block.Td})
175+
return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, td})
176176
}
177177

178178
// RequestHashes fetches a batch of hashes from a peer, starting at from, going

0 commit comments

Comments
 (0)