mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-13 03:26:38 +00:00
eth/filters: avoid rebuild the hash map multi times (#32965)
This commit is contained in:
parent
69df6bb8d5
commit
cfb311148c
2 changed files with 10 additions and 24 deletions
|
|
@ -563,7 +563,7 @@ type ReceiptWithTx struct {
|
||||||
// In addition to returning receipts, it also returns the corresponding transactions.
|
// In addition to returning receipts, it also returns the corresponding transactions.
|
||||||
// This is because receipts only contain low-level data, while user-facing data
|
// This is because receipts only contain low-level data, while user-facing data
|
||||||
// may require additional information from the Transaction.
|
// may require additional information from the Transaction.
|
||||||
func filterReceipts(txHashes []common.Hash, ev core.ChainEvent) []*ReceiptWithTx {
|
func filterReceipts(txHashes map[common.Hash]bool, ev core.ChainEvent) []*ReceiptWithTx {
|
||||||
var ret []*ReceiptWithTx
|
var ret []*ReceiptWithTx
|
||||||
|
|
||||||
receipts := ev.Receipts
|
receipts := ev.Receipts
|
||||||
|
|
@ -583,27 +583,9 @@ func filterReceipts(txHashes []common.Hash, ev core.ChainEvent) []*ReceiptWithTx
|
||||||
Transaction: txs[i],
|
Transaction: txs[i],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if len(txHashes) == 1 {
|
|
||||||
// Filter by single transaction hash.
|
|
||||||
// This is a common case, so we distinguish it from filtering by multiple tx hashes and made a small optimization.
|
|
||||||
for i, receipt := range receipts {
|
|
||||||
if receipt.TxHash == txHashes[0] {
|
|
||||||
ret = append(ret, &ReceiptWithTx{
|
|
||||||
Receipt: receipt,
|
|
||||||
Transaction: txs[i],
|
|
||||||
})
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Filter by multiple transaction hashes.
|
|
||||||
txHashMap := make(map[common.Hash]bool, len(txHashes))
|
|
||||||
for _, hash := range txHashes {
|
|
||||||
txHashMap[hash] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, receipt := range receipts {
|
for i, receipt := range receipts {
|
||||||
if txHashMap[receipt.TxHash] {
|
if txHashes[receipt.TxHash] {
|
||||||
ret = append(ret, &ReceiptWithTx{
|
ret = append(ret, &ReceiptWithTx{
|
||||||
Receipt: receipt,
|
Receipt: receipt,
|
||||||
Transaction: txs[i],
|
Transaction: txs[i],
|
||||||
|
|
|
||||||
|
|
@ -185,9 +185,9 @@ type subscription struct {
|
||||||
txs chan []*types.Transaction
|
txs chan []*types.Transaction
|
||||||
headers chan *types.Header
|
headers chan *types.Header
|
||||||
receipts chan []*ReceiptWithTx
|
receipts chan []*ReceiptWithTx
|
||||||
txHashes []common.Hash // contains transaction hashes for transactionReceipts subscription filtering
|
txHashes map[common.Hash]bool // contains transaction hashes for transactionReceipts subscription filtering
|
||||||
installed chan struct{} // closed when the filter is installed
|
installed chan struct{} // closed when the filter is installed
|
||||||
err chan error // closed when the filter is uninstalled
|
err chan error // closed when the filter is uninstalled
|
||||||
}
|
}
|
||||||
|
|
||||||
// EventSystem creates subscriptions, processes events and broadcasts them to the
|
// EventSystem creates subscriptions, processes events and broadcasts them to the
|
||||||
|
|
@ -403,6 +403,10 @@ func (es *EventSystem) SubscribePendingTxs(txs chan []*types.Transaction) *Subsc
|
||||||
// transactions when they are included in blocks. If txHashes is provided, only receipts
|
// transactions when they are included in blocks. If txHashes is provided, only receipts
|
||||||
// for those specific transaction hashes will be delivered.
|
// for those specific transaction hashes will be delivered.
|
||||||
func (es *EventSystem) SubscribeTransactionReceipts(txHashes []common.Hash, receipts chan []*ReceiptWithTx) *Subscription {
|
func (es *EventSystem) SubscribeTransactionReceipts(txHashes []common.Hash, receipts chan []*ReceiptWithTx) *Subscription {
|
||||||
|
hashSet := make(map[common.Hash]bool)
|
||||||
|
for _, h := range txHashes {
|
||||||
|
hashSet[h] = true
|
||||||
|
}
|
||||||
sub := &subscription{
|
sub := &subscription{
|
||||||
id: rpc.NewID(),
|
id: rpc.NewID(),
|
||||||
typ: TransactionReceiptsSubscription,
|
typ: TransactionReceiptsSubscription,
|
||||||
|
|
@ -411,7 +415,7 @@ func (es *EventSystem) SubscribeTransactionReceipts(txHashes []common.Hash, rece
|
||||||
txs: make(chan []*types.Transaction),
|
txs: make(chan []*types.Transaction),
|
||||||
headers: make(chan *types.Header),
|
headers: make(chan *types.Header),
|
||||||
receipts: receipts,
|
receipts: receipts,
|
||||||
txHashes: txHashes,
|
txHashes: hashSet,
|
||||||
installed: make(chan struct{}),
|
installed: make(chan struct{}),
|
||||||
err: make(chan error),
|
err: make(chan error),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue