Skip to content

Commit 58b5366

Browse files
Optimise fetching all receipts (Snowfork#1547) (#14)
* optimise fetching all receipts * release relayer * rollback ci * revert space * updates comment Co-authored-by: Clara van Staden <[email protected]>
1 parent 85ec68c commit 58b5366

File tree

1 file changed

+9
-40
lines changed

1 file changed

+9
-40
lines changed

relayer/chain/ethereum/receipt.go

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,18 @@ package ethereum
55

66
import (
77
"context"
8-
"sync"
98

109
etypes "github.com/ethereum/go-ethereum/core/types"
11-
"golang.org/x/sync/errgroup"
10+
"github.com/ethereum/go-ethereum/rpc"
11+
log "github.com/sirupsen/logrus"
1212
)
1313

14-
const receiptFetchBatchSize int = 10
15-
16-
// Fetch all receipts for the given block in batches of `receiptFetchBatchSize`
14+
// GetAllReceipts fetches all the transaction receipts in the given block.
1715
func GetAllReceipts(ctx context.Context, conn *Connection, block *etypes.Block) (etypes.Receipts, error) {
18-
transactions := block.Body().Transactions
19-
numTransactions := len(transactions)
20-
receiptsByIndex := sync.Map{}
21-
22-
for i := 0; i < numTransactions; i += receiptFetchBatchSize {
23-
eg, ctx := errgroup.WithContext(ctx)
24-
upper := i + receiptFetchBatchSize
25-
if upper >= numTransactions {
26-
upper = numTransactions
27-
}
28-
for j, tx := range transactions[i:upper] {
29-
index := i + j
30-
txHash := tx.Hash()
31-
eg.Go(func() error {
32-
receipt, err := conn.client.TransactionReceipt(ctx, txHash)
33-
if err != nil {
34-
return err
35-
}
36-
receiptsByIndex.Store(index, receipt)
37-
return nil
38-
})
39-
}
40-
err := eg.Wait()
41-
if err != nil {
42-
return nil, err
43-
}
44-
}
45-
46-
// Place receipts in same order as corresponding transactions
47-
receipts := make([]*etypes.Receipt, numTransactions)
48-
receiptsByIndex.Range(func(index interface{}, receipt interface{}) bool {
49-
receipts[index.(int)] = receipt.(*etypes.Receipt)
50-
return true
51-
})
52-
return receipts, nil
16+
blockHash := block.Hash()
17+
log.WithFields(log.Fields{
18+
"numTransactions": len(block.Body().Transactions),
19+
"blockHash": blockHash,
20+
}).Debug("Querying transaction receipts")
21+
return conn.client.BlockReceipts(ctx, rpc.BlockNumberOrHashWithHash(blockHash, false))
5322
}

0 commit comments

Comments
 (0)