eth/protocols: dedup the duplicated txs in one msg

This commit is contained in:
jsvisa 2025-09-24 11:48:40 +08:00 committed by Gary Rong
parent 11208553dd
commit 2ecee9a735

View file

@ -494,14 +494,26 @@ 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
seen := make(map[common.Hash]struct{})
deduped := make([]*types.Transaction, 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 {
return fmt.Errorf("Transactions: transaction %d is nil", i) return fmt.Errorf("Transactions: transaction %d is nil", i)
} }
peer.markTransaction(tx.Hash()) hash := tx.Hash()
if _, exists := seen[hash]; exists {
continue
} }
return backend.Handle(peer, &txs) seen[hash] = struct{}{}
deduped = append(deduped, tx)
peer.markTransaction(hash)
}
dedupedPacket := TransactionsPacket(deduped)
return backend.Handle(peer, &dedupedPacket)
} }
func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error { func handlePooledTransactions(backend Backend, msg Decoder, peer *Peer) error {
@ -514,16 +526,27 @@ func handlePooledTransactions(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
seen := make(map[common.Hash]struct{})
deduped := make([]*types.Transaction, 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 {
return fmt.Errorf("PooledTransactions: transaction %d is nil", i) return fmt.Errorf("PooledTransactions: transaction %d is nil", i)
} }
peer.markTransaction(tx.Hash()) hash := tx.Hash()
if _, exists := seen[hash]; exists {
continue
}
seen[hash] = struct{}{}
deduped = append(deduped, tx)
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, &txs.PooledTransactionsResponse) dedupedPacket := PooledTransactionsResponse(deduped)
return backend.Handle(peer, &dedupedPacket)
} }
func handleBlockRangeUpdate(backend Backend, msg Decoder, peer *Peer) error { func handleBlockRangeUpdate(backend Backend, msg Decoder, peer *Peer) error {