Skip to content

Commit cfb3111

Browse files
authored
eth/filters: avoid rebuild the hash map multi times (#32965)
1 parent 69df6bb commit cfb3111

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

eth/filters/filter.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ type ReceiptWithTx struct {
563563
// In addition to returning receipts, it also returns the corresponding transactions.
564564
// This is because receipts only contain low-level data, while user-facing data
565565
// may require additional information from the Transaction.
566-
func filterReceipts(txHashes []common.Hash, ev core.ChainEvent) []*ReceiptWithTx {
566+
func filterReceipts(txHashes map[common.Hash]bool, ev core.ChainEvent) []*ReceiptWithTx {
567567
var ret []*ReceiptWithTx
568568

569569
receipts := ev.Receipts
@@ -583,27 +583,9 @@ func filterReceipts(txHashes []common.Hash, ev core.ChainEvent) []*ReceiptWithTx
583583
Transaction: txs[i],
584584
}
585585
}
586-
} else if len(txHashes) == 1 {
587-
// Filter by single transaction hash.
588-
// This is a common case, so we distinguish it from filtering by multiple tx hashes and made a small optimization.
589-
for i, receipt := range receipts {
590-
if receipt.TxHash == txHashes[0] {
591-
ret = append(ret, &ReceiptWithTx{
592-
Receipt: receipt,
593-
Transaction: txs[i],
594-
})
595-
break
596-
}
597-
}
598586
} else {
599-
// Filter by multiple transaction hashes.
600-
txHashMap := make(map[common.Hash]bool, len(txHashes))
601-
for _, hash := range txHashes {
602-
txHashMap[hash] = true
603-
}
604-
605587
for i, receipt := range receipts {
606-
if txHashMap[receipt.TxHash] {
588+
if txHashes[receipt.TxHash] {
607589
ret = append(ret, &ReceiptWithTx{
608590
Receipt: receipt,
609591
Transaction: txs[i],

eth/filters/filter_system.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ type subscription struct {
185185
txs chan []*types.Transaction
186186
headers chan *types.Header
187187
receipts chan []*ReceiptWithTx
188-
txHashes []common.Hash // contains transaction hashes for transactionReceipts subscription filtering
189-
installed chan struct{} // closed when the filter is installed
190-
err chan error // closed when the filter is uninstalled
188+
txHashes map[common.Hash]bool // contains transaction hashes for transactionReceipts subscription filtering
189+
installed chan struct{} // closed when the filter is installed
190+
err chan error // closed when the filter is uninstalled
191191
}
192192

193193
// EventSystem creates subscriptions, processes events and broadcasts them to the
@@ -403,6 +403,10 @@ func (es *EventSystem) SubscribePendingTxs(txs chan []*types.Transaction) *Subsc
403403
// transactions when they are included in blocks. If txHashes is provided, only receipts
404404
// for those specific transaction hashes will be delivered.
405405
func (es *EventSystem) SubscribeTransactionReceipts(txHashes []common.Hash, receipts chan []*ReceiptWithTx) *Subscription {
406+
hashSet := make(map[common.Hash]bool)
407+
for _, h := range txHashes {
408+
hashSet[h] = true
409+
}
406410
sub := &subscription{
407411
id: rpc.NewID(),
408412
typ: TransactionReceiptsSubscription,
@@ -411,7 +415,7 @@ func (es *EventSystem) SubscribeTransactionReceipts(txHashes []common.Hash, rece
411415
txs: make(chan []*types.Transaction),
412416
headers: make(chan *types.Header),
413417
receipts: receipts,
414-
txHashes: txHashes,
418+
txHashes: hashSet,
415419
installed: make(chan struct{}),
416420
err: make(chan error),
417421
}

0 commit comments

Comments
 (0)