Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response returned from eth_getBlockReceipts endpoint is not properly marshalled #533

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

This comment was marked as outdated.

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
24 changes: 24 additions & 0 deletions tests/web3js/eth_non_interactive_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const web3Utils = require('web3-utils')
const { assert } = require('chai')
const conf = require('./config')
const helpers = require('./helpers')
const web3 = conf.web3

it('get chain ID', async () => {
Expand Down Expand Up @@ -84,6 +85,29 @@ 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 response = await helpers.callRPCMethod(
'eth_getTransactionReceipt',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't we use web3.js function for this https://docs.web3js.org/guides/web3_eth/methods#gettransactionreceipt

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 54534f3

[blockReceipt.transactionHash]
)
assert.equal(response.status, 200)

let txReceipt = response.body.result
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
Loading