Skip to content

Commit fc4fee8

Browse files
committed
eth: enforce stricter known limits on idle peers
1 parent 50e3795 commit fc4fee8

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎eth/peer.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,13 @@ func (p *peer) MarkTransaction(hash common.Hash) {
196196
// SendTransactions sends transactions to the peer and includes the hashes
197197
// in its transaction hash set for future reference.
198198
func (p *peer) SendTransactions(txs types.Transactions) error {
199+
// Mark all the transactions as known, but ensure we don't overflow our limits
199200
for _, tx := range txs {
200201
p.knownTxs.Add(tx.Hash())
201202
}
203+
for p.knownTxs.Cardinality() >= maxKnownTxs {
204+
p.knownTxs.Pop()
205+
}
202206
return p2p.Send(p.rw, TxMsg, txs)
203207
}
204208

@@ -207,9 +211,13 @@ func (p *peer) SendTransactions(txs types.Transactions) error {
207211
func (p *peer) AsyncSendTransactions(txs []*types.Transaction) {
208212
select {
209213
case p.queuedTxs <- txs:
214+
// Mark all the transactions as known, but ensure we don't overflow our limits
210215
for _, tx := range txs {
211216
p.knownTxs.Add(tx.Hash())
212217
}
218+
for p.knownTxs.Cardinality() >= maxKnownTxs {
219+
p.knownTxs.Pop()
220+
}
213221
default:
214222
p.Log().Debug("Dropping transaction propagation", "count", len(txs))
215223
}
@@ -218,9 +226,13 @@ func (p *peer) AsyncSendTransactions(txs []*types.Transaction) {
218226
// SendNewBlockHashes announces the availability of a number of blocks through
219227
// a hash notification.
220228
func (p *peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error {
229+
// Mark all the block hashes as known, but ensure we don't overflow our limits
221230
for _, hash := range hashes {
222231
p.knownBlocks.Add(hash)
223232
}
233+
for p.knownBlocks.Cardinality() >= maxKnownBlocks {
234+
p.knownBlocks.Pop()
235+
}
224236
request := make(newBlockHashesData, len(hashes))
225237
for i := 0; i < len(hashes); i++ {
226238
request[i].Hash = hashes[i]
@@ -235,15 +247,23 @@ func (p *peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error
235247
func (p *peer) AsyncSendNewBlockHash(block *types.Block) {
236248
select {
237249
case p.queuedAnns <- block:
250+
// Mark all the block hash as known, but ensure we don't overflow our limits
238251
p.knownBlocks.Add(block.Hash())
252+
for p.knownBlocks.Cardinality() >= maxKnownBlocks {
253+
p.knownBlocks.Pop()
254+
}
239255
default:
240256
p.Log().Debug("Dropping block announcement", "number", block.NumberU64(), "hash", block.Hash())
241257
}
242258
}
243259

244260
// SendNewBlock propagates an entire block to a remote peer.
245261
func (p *peer) SendNewBlock(block *types.Block, td *big.Int) error {
262+
// Mark all the block hash as known, but ensure we don't overflow our limits
246263
p.knownBlocks.Add(block.Hash())
264+
for p.knownBlocks.Cardinality() >= maxKnownBlocks {
265+
p.knownBlocks.Pop()
266+
}
247267
return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, td})
248268
}
249269

@@ -252,7 +272,11 @@ func (p *peer) SendNewBlock(block *types.Block, td *big.Int) error {
252272
func (p *peer) AsyncSendNewBlock(block *types.Block, td *big.Int) {
253273
select {
254274
case p.queuedProps <- &propEvent{block: block, td: td}:
275+
// Mark all the block hash as known, but ensure we don't overflow our limits
255276
p.knownBlocks.Add(block.Hash())
277+
for p.knownBlocks.Cardinality() >= maxKnownBlocks {
278+
p.knownBlocks.Pop()
279+
}
256280
default:
257281
p.Log().Debug("Dropping block propagation", "number", block.NumberU64(), "hash", block.Hash())
258282
}

0 commit comments

Comments
 (0)