Skip to content

Commit

Permalink
Update getBlockReceipts to accept block hash (sei-protocol#1733)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Kbhat1 authored Jun 17, 2024
1 parent 225a8bb commit 0a83cdb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
16 changes: 9 additions & 7 deletions evmrpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
26 changes: 26 additions & 0 deletions evmrpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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{}) {
Expand Down
10 changes: 9 additions & 1 deletion evmrpc/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const DebugTraceMockHeight = 101

var DebugTraceHashHex = "0x1234567890123456789023456789012345678901234567890123456789000004"
var DebugTraceBlockHash = "BE17E0261E539CB7E9A91E123A6D794E0163D656FCF9B8EAC07823F7ED28512B"
var MultiTxBlockHash = "0000000000000000000000000000000000000000000000000000000000000002"

var TestCosmosTxHash = "690D39ADF56D4C811B766DFCD729A415C36C4BFFE80D63E305373B9518EBFB14"
var TestEvmTxHash = "0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872e"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 0a83cdb

Please sign in to comment.