diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-07-09 18:55:06 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-07-09 18:55:06 +0800 |
commit | 4f95e2f9ec3e416773e6a87b3c76667d97894f36 (patch) | |
tree | c98b17aa87889cc04bc4c7c27d56ec7b33eefe0f /eth/handler.go | |
parent | a2333bcbb46245e07357be1f9af4108e9bf1fe40 (diff) | |
download | go-tangerine-4f95e2f9ec3e416773e6a87b3c76667d97894f36.tar go-tangerine-4f95e2f9ec3e416773e6a87b3c76667d97894f36.tar.gz go-tangerine-4f95e2f9ec3e416773e6a87b3c76667d97894f36.tar.bz2 go-tangerine-4f95e2f9ec3e416773e6a87b3c76667d97894f36.tar.lz go-tangerine-4f95e2f9ec3e416773e6a87b3c76667d97894f36.tar.xz go-tangerine-4f95e2f9ec3e416773e6a87b3c76667d97894f36.tar.zst go-tangerine-4f95e2f9ec3e416773e6a87b3c76667d97894f36.zip |
eth: calculate the correct TD, only update if better
Diffstat (limited to 'eth/handler.go')
-rw-r--r-- | eth/handler.go | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/eth/handler.go b/eth/handler.go index bbb251812..50e2ac99b 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -19,6 +19,7 @@ package eth import ( "fmt" "math" + "math/big" "sync" "time" @@ -412,8 +413,10 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { pm.fetcher.Enqueue(p.id, request.Block) // TODO: Schedule a sync to cover potential gaps (this needs proto update) - p.SetTd(request.TD) - go pm.synchronise(p) + if request.TD.Cmp(p.Td()) > 0 { + p.SetTd(request.TD) + go pm.synchronise(p) + } case TxMsg: // Transactions arrived, parse all of them and deliver to the pool @@ -452,9 +455,18 @@ func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) { // If propagation is requested, send to a subset of the peer if propagate { + // Calculate the TD of the block (it's not imported yet, so block.Td is not valid) + var td *big.Int + if parent := pm.chainman.GetBlock(block.ParentHash()); parent != nil { + td = new(big.Int).Add(parent.Td, block.Difficulty()) + } else { + glog.V(logger.Error).Infof("propagating dangling block #%d [%x]", block.NumberU64(), hash[:4]) + return + } + // Send the block to a subset of our peers transfer := peers[:int(math.Sqrt(float64(len(peers))))] for _, peer := range transfer { - peer.SendNewBlock(block) + peer.SendNewBlock(block, td) } glog.V(logger.Detail).Infof("propagated block %x to %d peers in %v", hash[:4], len(transfer), time.Since(block.ReceivedAt)) } |