From fc4fee8649f11e6ab500af3ee3a458d85fbd0867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 12 Jun 2019 12:30:06 +0300 Subject: [PATCH] eth: enforce stricter known limits on idle peers --- eth/peer.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/eth/peer.go b/eth/peer.go index 54c0ea0d53..208badc5ed 100644 --- a/eth/peer.go +++ b/eth/peer.go @@ -196,9 +196,13 @@ func (p *peer) MarkTransaction(hash common.Hash) { // SendTransactions sends transactions to the peer and includes the hashes // in its transaction hash set for future reference. func (p *peer) SendTransactions(txs types.Transactions) error { + // Mark all the transactions as known, but ensure we don't overflow our limits for _, tx := range txs { p.knownTxs.Add(tx.Hash()) } + for p.knownTxs.Cardinality() >= maxKnownTxs { + p.knownTxs.Pop() + } return p2p.Send(p.rw, TxMsg, txs) } @@ -207,9 +211,13 @@ func (p *peer) SendTransactions(txs types.Transactions) error { func (p *peer) AsyncSendTransactions(txs []*types.Transaction) { select { case p.queuedTxs <- txs: + // Mark all the transactions as known, but ensure we don't overflow our limits for _, tx := range txs { p.knownTxs.Add(tx.Hash()) } + for p.knownTxs.Cardinality() >= maxKnownTxs { + p.knownTxs.Pop() + } default: p.Log().Debug("Dropping transaction propagation", "count", len(txs)) } @@ -218,9 +226,13 @@ func (p *peer) AsyncSendTransactions(txs []*types.Transaction) { // SendNewBlockHashes announces the availability of a number of blocks through // a hash notification. func (p *peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error { + // Mark all the block hashes as known, but ensure we don't overflow our limits for _, hash := range hashes { p.knownBlocks.Add(hash) } + for p.knownBlocks.Cardinality() >= maxKnownBlocks { + p.knownBlocks.Pop() + } request := make(newBlockHashesData, len(hashes)) for i := 0; i < len(hashes); i++ { request[i].Hash = hashes[i] @@ -235,7 +247,11 @@ func (p *peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error func (p *peer) AsyncSendNewBlockHash(block *types.Block) { select { case p.queuedAnns <- block: + // Mark all the block hash as known, but ensure we don't overflow our limits p.knownBlocks.Add(block.Hash()) + for p.knownBlocks.Cardinality() >= maxKnownBlocks { + p.knownBlocks.Pop() + } default: p.Log().Debug("Dropping block announcement", "number", block.NumberU64(), "hash", block.Hash()) } @@ -243,7 +259,11 @@ func (p *peer) AsyncSendNewBlockHash(block *types.Block) { // SendNewBlock propagates an entire block to a remote peer. func (p *peer) SendNewBlock(block *types.Block, td *big.Int) error { + // Mark all the block hash as known, but ensure we don't overflow our limits p.knownBlocks.Add(block.Hash()) + for p.knownBlocks.Cardinality() >= maxKnownBlocks { + p.knownBlocks.Pop() + } return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, td}) } @@ -252,7 +272,11 @@ func (p *peer) SendNewBlock(block *types.Block, td *big.Int) error { func (p *peer) AsyncSendNewBlock(block *types.Block, td *big.Int) { select { case p.queuedProps <- &propEvent{block: block, td: td}: + // Mark all the block hash as known, but ensure we don't overflow our limits p.knownBlocks.Add(block.Hash()) + for p.knownBlocks.Cardinality() >= maxKnownBlocks { + p.knownBlocks.Pop() + } default: p.Log().Debug("Dropping block propagation", "number", block.NumberU64(), "hash", block.Hash()) }