Skip to content

Commit 1409ebc

Browse files
committed
Improve performance of eth_getBlockBy* endpoints by fetching all the receipts of a given block
1 parent 104d41e commit 1409ebc

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

api/api.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -926,22 +926,30 @@ func (b *BlockChainAPI) GetStorageAt(
926926

927927
func (b *BlockChainAPI) fetchBlockTransactions(
928928
block *models.Block,
929+
receipts []*models.Receipt,
929930
) ([]*ethTypes.Transaction, error) {
930-
transactions := make([]*ethTypes.Transaction, 0)
931-
for _, txHash := range block.TransactionHashes {
932-
transaction, err := b.prepareTransactionResponse(txHash)
931+
transactions := make([]*ethTypes.Transaction, len(receipts))
932+
for i, receipt := range receipts {
933+
tx, err := b.transactions.Get(receipt.TxHash)
933934
if err != nil {
934935
return nil, err
935936
}
936-
if transaction == nil {
937+
938+
if tx == nil {
937939
b.logger.Error().
938-
Str("tx-hash", txHash.String()).
940+
Str("tx-hash", receipt.TxHash.String()).
939941
Uint64("evm-height", block.Height).
940942
Msg("not found a transaction the block references")
941943

942944
continue
943945
}
944-
transactions = append(transactions, transaction)
946+
947+
transaction, err := ethTypes.NewTransactionResult(tx, *receipt, b.config.EVMNetworkID)
948+
if err != nil {
949+
return nil, err
950+
}
951+
952+
transactions[i] = transaction
945953
}
946954

947955
return transactions, nil
@@ -996,26 +1004,28 @@ func (b *BlockChainAPI) prepareBlockResponse(
9961004
}
9971005
blockSize := rlp.ListSize(uint64(len(blockBytes)))
9981006

999-
transactions, err := b.fetchBlockTransactions(block)
1007+
receipts, err := b.receipts.GetByBlockHeight(block.Height)
1008+
if err != nil {
1009+
return nil, err
1010+
}
1011+
1012+
transactions, err := b.fetchBlockTransactions(block, receipts)
10001013
if err != nil {
10011014
return nil, err
10021015
}
10031016

10041017
if len(transactions) > 0 {
10051018
totalGasUsed := hexutil.Uint64(0)
1006-
receipts := types.Receipts{}
1007-
for _, tx := range transactions {
1008-
txReceipt, err := b.receipts.GetByTransactionID(tx.Hash)
1009-
if err != nil {
1010-
return nil, err
1011-
}
1019+
gethReceipts := types.Receipts{}
1020+
for i, tx := range transactions {
1021+
txReceipt := receipts[i]
10121022
totalGasUsed += hexutil.Uint64(txReceipt.GasUsed)
1013-
receipts = append(receipts, txReceipt.ToGethReceipt())
1023+
gethReceipts = append(gethReceipts, txReceipt.ToGethReceipt())
10141024
blockSize += tx.Size()
10151025
}
10161026
blockResponse.GasUsed = totalGasUsed
10171027
// TODO(m-Peter): Consider if its worthwhile to move this in storage.
1018-
blockResponse.LogsBloom = types.MergeBloom(receipts).Bytes()
1028+
blockResponse.LogsBloom = types.MergeBloom(gethReceipts).Bytes()
10191029
}
10201030
blockResponse.Size = hexutil.Uint64(rlp.ListSize(blockSize))
10211031

0 commit comments

Comments
 (0)