eth/protocols/eth: disconnect if duplicate in same message

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-10-10 13:08:42 +02:00
parent 49a7c24743
commit 1e57bbdf55
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E

View file

@ -494,11 +494,8 @@ func handleTransactions(backend Backend, msg Decoder, peer *Peer) error {
if err := msg.Decode(&txs); err != nil { if err := msg.Decode(&txs); err != nil {
return err return err
} }
// Deduplicate transactions within the message to avoid redundant processing // Duplicate transactions are not allowed
var ( seen := make(map[common.Hash]struct{})
seen = make(map[common.Hash]struct{})
filtered = make(TransactionsPacket, 0, len(txs))
)
for i, tx := range txs { for i, tx := range txs {
// Validate and mark the remote transaction // Validate and mark the remote transaction
if tx == nil { if tx == nil {
@ -506,15 +503,12 @@ func handleTransactions(backend Backend, msg Decoder, peer *Peer) error {
} }
hash := tx.Hash() hash := tx.Hash()
if _, exists := seen[hash]; exists { if _, exists := seen[hash]; exists {
log.Trace("Duplicated transaction", "hash", hash, "peer", peer.id) return fmt.Errorf("Transactions: multiple copies of the same hash %v", hash)
continue
} }
seen[hash] = struct{}{} seen[hash] = struct{}{}
filtered = append(filtered, tx)
peer.markTransaction(hash) peer.markTransaction(hash)
} }
return backend.Handle(peer, &filtered) return backend.Handle(peer, &txs)
} }
func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error { func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error {
@ -528,10 +522,7 @@ func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error {
return err return err
} }
// Deduplicate transactions within the message to avoid redundant processing // Deduplicate transactions within the message to avoid redundant processing
var ( seen := make(map[common.Hash]struct{})
seen = make(map[common.Hash]struct{})
filtered = make(PooledTransactionsResponse, 0, len(txs.PooledTransactionsResponse))
)
for i, tx := range txs.PooledTransactionsResponse { for i, tx := range txs.PooledTransactionsResponse {
// Validate and mark the remote transaction // Validate and mark the remote transaction
if tx == nil { if tx == nil {
@ -539,16 +530,14 @@ func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error {
} }
hash := tx.Hash() hash := tx.Hash()
if _, exists := seen[hash]; exists { if _, exists := seen[hash]; exists {
log.Trace("Duplicated transaction", "hash", hash, "peer", peer.id) return fmt.Errorf("PooledTransactions: multiple copies of the same hash %v", hash)
continue
} }
seen[hash] = struct{}{} seen[hash] = struct{}{}
filtered = append(filtered, tx)
peer.markTransaction(hash) peer.markTransaction(hash)
} }
requestTracker.Fulfil(peer.id, peer.version, PooledTransactionsMsg, txs.RequestId) requestTracker.Fulfil(peer.id, peer.version, PooledTransactionsMsg, txs.RequestId)
return backend.Handle(peer, &filtered)
return backend.Handle(peer, &txs.PooledTransactionsResponse)
} }
func handleBlockRangeUpdate(backend Backend, msg Decoder, peer *Peer) error { func handleBlockRangeUpdate(backend Backend, msg Decoder, peer *Peer) error {