From 0a83cdbde9f44b10ca39b86944614da064676744 Mon Sep 17 00:00:00 2001 From: Kartik Bhat Date: Mon, 17 Jun 2024 15:18:41 -0400 Subject: [PATCH] Update getBlockReceipts to accept block hash (#1733) * Add logging * Add support for get block receipts by hash * Update tests * Update to use GetBlockNumberByNrOrHash * Update tests * Update go.mod * Bump go sum * Bump go mod --- evmrpc/block.go | 16 +++++++++------- evmrpc/block_test.go | 26 ++++++++++++++++++++++++++ evmrpc/setup_test.go | 10 +++++++++- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/evmrpc/block.go b/evmrpc/block.go index d3e0de0e74..f5fdc75e5d 100644 --- a/evmrpc/block.go +++ b/evmrpc/block.go @@ -90,24 +90,26 @@ func (a *BlockAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, return EncodeTmBlock(a.ctxProvider(LatestCtxHeight), block, blockRes, a.keeper, a.txConfig.TxDecoder(), fullTx) } -func (a *BlockAPI) GetBlockReceipts(ctx context.Context, number rpc.BlockNumber) (result []map[string]interface{}, returnErr error) { +func (a *BlockAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (result []map[string]interface{}, returnErr error) { startTime := time.Now() defer recordMetrics("eth_getBlockReceipts", a.connectionType, startTime, returnErr == nil) // Get height from params - heightPtr, err := getBlockNumber(ctx, a.tmClient, number) + heightPtr, err := GetBlockNumberByNrOrHash(ctx, a.tmClient, blockNrOrHash) if err != nil { return nil, err } - // Get the block by height + block, err := blockByNumberWithRetry(ctx, a.tmClient, heightPtr, 1) if err != nil { return nil, err } - // Get all tx hashes for the block - height := LatestCtxHeight - if heightPtr != nil { - height = *heightPtr + + if block == nil { + return nil, errors.New("could not retrieve block requested") } + + // Get all tx hashes for the block + height := block.Block.Header.Height txHashes := a.keeper.GetTxHashesOnHeight(a.ctxProvider(height), height) // Get tx receipts for all hashes in parallel wg := sync.WaitGroup{} diff --git a/evmrpc/block_test.go b/evmrpc/block_test.go index 0bc1845a7a..7a50edda81 100644 --- a/evmrpc/block_test.go +++ b/evmrpc/block_test.go @@ -49,6 +49,7 @@ func TestGetBlockTransactionCount(t *testing.T) { } func TestGetBlockReceipts(t *testing.T) { + // Query by block height resObj := sendRequestGood(t, "getBlockReceipts", "0x2") result := resObj["result"].([]interface{}) require.Equal(t, 3, len(result)) @@ -65,6 +66,31 @@ func TestGetBlockReceipts(t *testing.T) { require.Equal(t, "0x2", receipt3["transactionIndex"]) require.Equal(t, multiTxBlockTx3.Hash().Hex(), receipt3["transactionHash"]) + // Query by block hash + resObj2 := sendRequestGood(t, "getBlockReceipts", "0x0000000000000000000000000000000000000000000000000000000000000002") + result = resObj2["result"].([]interface{}) + require.Equal(t, 3, len(result)) + receipt1 = result[0].(map[string]interface{}) + require.Equal(t, "0x2", receipt1["blockNumber"]) + require.Equal(t, "0x0", receipt1["transactionIndex"]) + require.Equal(t, multiTxBlockTx1.Hash().Hex(), receipt1["transactionHash"]) + receipt2 = result[1].(map[string]interface{}) + require.Equal(t, "0x2", receipt2["blockNumber"]) + require.Equal(t, "0x1", receipt2["transactionIndex"]) + require.Equal(t, multiTxBlockTx2.Hash().Hex(), receipt2["transactionHash"]) + receipt3 = result[2].(map[string]interface{}) + require.Equal(t, "0x2", receipt3["blockNumber"]) + require.Equal(t, "0x2", receipt3["transactionIndex"]) + require.Equal(t, multiTxBlockTx3.Hash().Hex(), receipt3["transactionHash"]) + + // Query by tag latest => retrieves block 8 + resObj3 := sendRequestGood(t, "getBlockReceipts", "latest") + result = resObj3["result"].([]interface{}) + require.Equal(t, 1, len(result)) + receipt1 = result[0].(map[string]interface{}) + require.Equal(t, "0x8", receipt1["blockNumber"]) + require.Equal(t, "0x0", receipt1["transactionIndex"]) + require.Equal(t, multiTxBlockTx4.Hash().Hex(), receipt1["transactionHash"]) } func verifyBlockResult(t *testing.T, resObj map[string]interface{}) { diff --git a/evmrpc/setup_test.go b/evmrpc/setup_test.go index 2921d75f6f..03e132d488 100644 --- a/evmrpc/setup_test.go +++ b/evmrpc/setup_test.go @@ -52,6 +52,7 @@ const DebugTraceMockHeight = 101 var DebugTraceHashHex = "0x1234567890123456789023456789012345678901234567890123456789000004" var DebugTraceBlockHash = "BE17E0261E539CB7E9A91E123A6D794E0163D656FCF9B8EAC07823F7ED28512B" +var MultiTxBlockHash = "0000000000000000000000000000000000000000000000000000000000000002" var TestCosmosTxHash = "690D39ADF56D4C811B766DFCD729A415C36C4BFFE80D63E305373B9518EBFB14" var TestEvmTxHash = "0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872e" @@ -84,6 +85,10 @@ var MockBlockID = tmtypes.BlockID{ Hash: bytes.HexBytes(mustHexToBytes("0000000000000000000000000000000000000000000000000000000000000001")), } +var MockBlockIDMultiTx = tmtypes.BlockID{ + Hash: bytes.HexBytes(mustHexToBytes(MultiTxBlockHash)), +} + var NewHeadsCalled = make(chan struct{}) type MockClient struct { @@ -121,7 +126,7 @@ func mockBlockHeader(height int64) tmtypes.Header { func (c *MockClient) mockBlock(height int64) *coretypes.ResultBlock { if height == MultiTxBlockHeight { return &coretypes.ResultBlock{ - BlockID: MockBlockID, + BlockID: MockBlockIDMultiTx, Block: &tmtypes.Block{ Header: mockBlockHeader(height), Data: tmtypes.Data{ @@ -255,6 +260,9 @@ func (c *MockClient) BlockByHash(_ context.Context, hash bytes.HexBytes) (*coret if hash.String() == DebugTraceBlockHash { return c.mockBlock(DebugTraceMockHeight), nil } + if hash.String() == MultiTxBlockHash { + return c.mockBlock(MultiTxBlockHeight), nil + } return c.mockBlock(MockHeight), nil }