Skip to content

Commit

Permalink
Merge pull request #533 from onflow/fix-eth-get-block-receipts-response
Browse files Browse the repository at this point in the history
Response returned from `eth_getBlockReceipts` endpoint is not properly marshalled
  • Loading branch information
sideninja authored Sep 12, 2024
2 parents dea4e39 + 54534f3 commit 6ccaf58
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
35 changes: 22 additions & 13 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,11 @@ func (b *BlockChainAPI) GetBlockByNumber(
// GetBlockReceipts returns the block receipts for the given block hash or number or tag.
func (b *BlockChainAPI) GetBlockReceipts(
ctx context.Context,
numHash rpc.BlockNumberOrHash,
) ([]*models.Receipt, error) {
blockNumberOrHash rpc.BlockNumberOrHash,
) ([]map[string]interface{}, error) {
l := b.logger.With().
Str("endpoint", "getBlockReceipts").
Str("hash", numHash.String()).
Str("hash", blockNumberOrHash.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
Expand All @@ -455,28 +455,37 @@ func (b *BlockChainAPI) GetBlockReceipts(
block *models.Block
err error
)
if numHash.BlockHash != nil {
block, err = b.blocks.GetByID(*numHash.BlockHash)
} else if numHash.BlockNumber != nil {
block, err = b.blocks.GetByHeight(uint64(numHash.BlockNumber.Int64()))
if blockNumberOrHash.BlockHash != nil {
block, err = b.blocks.GetByID(*blockNumberOrHash.BlockHash)
} else if blockNumberOrHash.BlockNumber != nil {
block, err = b.blocks.GetByHeight(uint64(blockNumberOrHash.BlockNumber.Int64()))
} else {
return handleError[[]*models.Receipt](
return handleError[[]map[string]interface{}](
fmt.Errorf("%w: block number or hash not provided", errs.ErrInvalid),
l,
b.collector,
)
}
if err != nil {
return handleError[[]*models.Receipt](err, l, b.collector)
return handleError[[]map[string]interface{}](err, l, b.collector)
}

receipts := make([]*models.Receipt, len(block.TransactionHashes))
receipts := make([]map[string]interface{}, len(block.TransactionHashes))
for i, hash := range block.TransactionHashes {
rcp, err := b.receipts.GetByTransactionID(hash)
tx, err := b.transactions.Get(hash)
if err != nil {
return handleError[[]map[string]interface{}](err, l, b.collector)
}

receipt, err := b.receipts.GetByTransactionID(hash)
if err != nil {
return handleError[[]map[string]interface{}](err, l, b.collector)
}

receipts[i], err = MarshalReceipt(receipt, tx)
if err != nil {
return handleError[[]*models.Receipt](err, l, b.collector)
return handleError[[]map[string]interface{}](err, l, b.collector)
}
receipts[i] = rcp
}

return receipts, nil
Expand Down
30 changes: 30 additions & 0 deletions tests/web3js/eth_non_interactive_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const web3Utils = require('web3-utils')
const { assert } = require('chai')
const conf = require('./config')
const helpers = require('./helpers')
const web3types = require('web3-types')
const web3 = conf.web3

it('get chain ID', async () => {
Expand Down Expand Up @@ -84,6 +86,34 @@ it('get block', async () => {
assert.isNull(no)
})

it('should get block receipts', async () => {
let height = await web3.eth.getBlockNumber()
assert.equal(height, conf.startBlockHeight)

let block = await web3.eth.getBlock(height)
let response = await helpers.callRPCMethod('eth_getBlockReceipts', [block.hash])
assert.equal(response.status, 200)

let blockReceipts = response.body.result
assert.lengthOf(blockReceipts, 3)

for (let blockReceipt of blockReceipts) {
let txReceipt = await web3.eth.getTransactionReceipt(
blockReceipt.transactionHash,
web3types.ETH_DATA_FORMAT
)
// normalize missing fields from transaction receipt
if (txReceipt.to === undefined) {
txReceipt.to = null
}
if (txReceipt.contractAddress === undefined) {
txReceipt.contractAddress = null
}

assert.deepEqual(blockReceipt, txReceipt)
}
})

it('should get block transaction count', async () => {
// call endpoint with block number
let txCount = await web3.eth.getBlockTransactionCount(conf.startBlockHeight)
Expand Down

0 comments on commit 6ccaf58

Please sign in to comment.