mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 20:56:42 +00:00
eth/protocols: dedup the duplicated txs in one msg
This commit is contained in:
parent
11208553dd
commit
2ecee9a735
1 changed files with 27 additions and 4 deletions
|
|
@ -494,14 +494,26 @@ func handleTransactions(backend Backend, msg Decoder, peer *Peer) error {
|
|||
if err := msg.Decode(&txs); err != nil {
|
||||
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 {
|
||||
// Validate and mark the remote transaction
|
||||
if tx == nil {
|
||||
return fmt.Errorf("Transactions: 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)
|
||||
}
|
||||
return backend.Handle(peer, &txs)
|
||||
|
||||
dedupedPacket := TransactionsPacket(deduped)
|
||||
return backend.Handle(peer, &dedupedPacket)
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
// Validate and mark the remote transaction
|
||||
if tx == nil {
|
||||
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)
|
||||
|
||||
return backend.Handle(peer, &txs.PooledTransactionsResponse)
|
||||
dedupedPacket := PooledTransactionsResponse(deduped)
|
||||
return backend.Handle(peer, &dedupedPacket)
|
||||
}
|
||||
|
||||
func handleBlockRangeUpdate(backend Backend, msg Decoder, peer *Peer) error {
|
||||
|
|
|
|||
Loading…
Reference in a new issue