From baf457e6d4a8ff38e0976119cf0cca590264ec3c Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Sun, 4 Mar 2018 13:42:08 +0400 Subject: [PATCH] return error if peer sent us a block we didn't expect with a height too far ahead/behind --- blockchain/pool.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/blockchain/pool.go b/blockchain/pool.go index 5de719792..603b4bf2a 100644 --- a/blockchain/pool.go +++ b/blockchain/pool.go @@ -40,6 +40,9 @@ const ( // Assuming a DSL connection (not a good choice) 128 Kbps (upload) ~ 15 KB/s, // sending data across atlantic ~ 7.5 KB/s. minRecvRate = 7680 + + // Maximum difference between current and new block's height. + maxDiffBetweenCurrentAndReceivedBlockHeight = 100 ) var peerTimeout = 15 * time.Second // not const so we can override with tests @@ -230,8 +233,14 @@ func (pool *BlockPool) AddBlock(peerID p2p.ID, block *types.Block, blockSize int requester := pool.requesters[block.Height] if requester == nil { - // a block we didn't expect. - // TODO:if height is too far ahead, punish peer + pool.Logger.Info("peer sent us a block we didn't expect", "peer", peerID, "curHeight", pool.height, "blockHeight", block.Height) + diff := pool.height - block.Height + if diff < 0 { + diff *= -1 + } + if diff > maxDiffBetweenCurrentAndReceivedBlockHeight { + pool.sendError(errors.New("peer sent us a block we didn't expect with a height too far ahead/behind"), peerID) + } return }