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

Implement the eth_getBlockReceipts JSON-RPC endpoint #44

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
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
30 changes: 28 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -564,8 +564,34 @@ func (s *BlockChainAPI) GetBlockReceipts(
ctx context.Context,
blockNumberOrHash rpc.BlockNumberOrHash,
) ([]map[string]interface{}, error) {
result := make([]map[string]interface{}, 0)
return result, nil
receipts := make([]map[string]interface{}, 0)

var block *storage.BlockExecutedPayload
var err error
if blockNumberOrHash.BlockHash != nil {
block, err = s.Store.GetBlockByHash(ctx, *blockNumberOrHash.BlockHash)
if err != nil {
return receipts, err
}
} else if blockNumberOrHash.BlockNumber != nil {
block, err = s.Store.GetBlockByNumber(ctx, uint64(blockNumberOrHash.BlockNumber.Int64()))
if err != nil {
return receipts, err
}
} else {
return receipts, fmt.Errorf("block number or hash not provided")
}

for _, tx := range block.TransactionHashes {
txHash := common.HexToHash(tx)
txReceipt, err := s.GetTransactionReceipt(ctx, txHash)
if err != nil {
return receipts, err
}
receipts = append(receipts, txReceipt)
}

return receipts, nil
}

// eth_getBlockTransactionCountByHash
66 changes: 64 additions & 2 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -663,13 +663,75 @@ func TestBlockChainAPI(t *testing.T) {
})

t.Run("GetBlockReceipts", func(t *testing.T) {
event := transactionExecutedEvent(
3,
"0xb47d74ea64221eb941490bdc0c9a404dacd0a8573379a45c992ac60ee3e83c3c",
"b88c02f88982029a01808083124f809499466ed2e37b892a2ee3e9cd55a98b68f5735db280a4c6888fa10000000000000000000000000000000000000000000000000000000000000006c001a0f84168f821b427dc158c4d8083bdc4b43e178cf0977a2c5eefbcbedcc4e351b0a066a747a38c6c266b9dc2136523cef04395918de37773db63d574aabde59c12eb",
false,
2,
22514,
"0000000000000000000000000000000000000000",
"000000000000000000000000000000000000000000000000000000000000002a",
"f85af8589499466ed2e37b892a2ee3e9cd55a98b68f5735db2e1a024abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503daa0000000000000000000000000000000000000000000000000000000000000002a",
)

store := blockchainAPI.Store
store.StoreTransaction(context.Background(), event)

event = blockExecutedEvent(
3,
"0xaae4530246e61ae58479824ab0863f99ca50414d27aec0c269ae6a7cfc4c7f5b",
7766279631452241920,
"0xf31ee13dad8f38431fd31278b12be62e6b77e6923f0b7a446eb1affb61f21fc9",
"0x0000000000000000000000000000000000000000000000000000000000000000",
[]string{"0xb47d74ea64221eb941490bdc0c9a404dacd0a8573379a45c992ac60ee3e83c3c"},
)

err = store.StoreBlock(context.Background(), event)
require.NoError(t, err)

blockNumber := rpc.BlockNumber(3)
receipts, err := blockchainAPI.GetBlockReceipts(
context.Background(),
rpc.BlockNumberOrHashWithNumber(rpc.FinalizedBlockNumber),
rpc.BlockNumberOrHash{BlockNumber: &blockNumber},
)
require.NoError(t, err)

assert.Equal(t, make([]map[string]interface{}, 0), receipts)
expectedReceipt := map[string]interface{}{}
expectedReceipt["blockNumber"] = (*hexutil.Big)(big.NewInt(3))
expectedReceipt["transactionHash"] = common.HexToHash("0xb47d74ea64221eb941490bdc0c9a404dacd0a8573379a45c992ac60ee3e83c3c")
expectedReceipt["status"] = hexutil.Uint64(1)
expectedReceipt["type"] = hexutil.Uint64(2)
expectedReceipt["gasUsed"] = hexutil.Uint64(22514)
expectedReceipt["contractAddress"] = common.HexToAddress("0x0000000000000000000000000000000000000000")
expectedReceipt["from"] = common.HexToAddress("0x658Bdf435d810C91414eC09147DAA6DB62406379")
to := common.HexToAddress("0x99466ED2E37B892A2Ee3E9CD55a98b68f5735db2")
expectedReceipt["to"] = &to
expectedReceipt["blockHash"] = common.HexToHash("0xaae4530246e61ae58479824ab0863f99ca50414d27aec0c269ae6a7cfc4c7f5b")

txIndex := uint64(0)
expectedReceipt["transactionIndex"] = (*hexutil.Uint64)(&txIndex)
expectedReceipt["cumulativeGasUsed"] = hexutil.Uint64(50000)
expectedReceipt["effectiveGasPrice"] = (*hexutil.Big)(big.NewInt(20000000000))

data, err := hex.DecodeString("000000000000000000000000000000000000000000000000000000000000002a")
require.NoError(t, err)
log := &types.Log{
Index: 0,
BlockNumber: 0,
BlockHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
TxHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
TxIndex: 0,
Address: common.HexToAddress("0x99466ed2e37b892a2ee3e9cd55a98b68f5735db2"),
Data: data,
Topics: []common.Hash{common.HexToHash("0x24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da")},
}
logs := []*types.Log{log}
expectedReceipt["logs"] = logs
expectedReceipt["logsBloom"] = hexutil.Bytes(types.LogsBloom(logs))
expectedReceipts := []map[string]interface{}{expectedReceipt}

assert.Equal(t, receipts, expectedReceipts)
})

t.Run("GetBlockTransactionCountByHash", func(t *testing.T) {
42 changes: 42 additions & 0 deletions api/server_test.go
Original file line number Diff line number Diff line change
@@ -445,6 +445,48 @@ func TestServerJSONRPCOveHTTPHandler(t *testing.T) {

assert.Equal(t, expectedResponse, strings.TrimSuffix(string(content), "\n"))
})

t.Run("eth_getBlockReceipts", func(t *testing.T) {
request := `{"jsonrpc":"2.0","id":1,"method":"eth_getBlockReceipts","params":["0x3"]}`
expectedResponse := `{"jsonrpc":"2.0","id":1,"result":[{"blockHash":"0xaae4530246e61ae58479824ab0863f99ca50414d27aec0c269ae6a7cfc4c7f5b","blockNumber":"0x3","contractAddress":"0x0000000000000000000000000000000000000000","cumulativeGasUsed":"0xc350","effectiveGasPrice":"0x4a817c800","from":"0x658bdf435d810c91414ec09147daa6db62406379","gasUsed":"0x57f2","logs":[{"address":"0x99466ed2e37b892a2ee3e9cd55a98b68f5735db2","topics":["0x24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da"],"data":"0x000000000000000000000000000000000000000000000000000000000000002a","blockNumber":"0x0","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x0","blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","logIndex":"0x0","removed":false}],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000020000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000","status":"0x1","to":"0x99466ed2e37b892a2ee3e9cd55a98b68f5735db2","transactionHash":"0xb47d74ea64221eb941490bdc0c9a404dacd0a8573379a45c992ac60ee3e83c3c","transactionIndex":"0x0","type":"0x2"}]}`

event := transactionExecutedEvent(
3,
"0xb47d74ea64221eb941490bdc0c9a404dacd0a8573379a45c992ac60ee3e83c3c",
"b88c02f88982029a01808083124f809499466ed2e37b892a2ee3e9cd55a98b68f5735db280a4c6888fa10000000000000000000000000000000000000000000000000000000000000006c001a0f84168f821b427dc158c4d8083bdc4b43e178cf0977a2c5eefbcbedcc4e351b0a066a747a38c6c266b9dc2136523cef04395918de37773db63d574aabde59c12eb",
false,
2,
22514,
"0000000000000000000000000000000000000000",
"000000000000000000000000000000000000000000000000000000000000002a",
"f85af8589499466ed2e37b892a2ee3e9cd55a98b68f5735db2e1a024abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503daa0000000000000000000000000000000000000000000000000000000000000002a",
)

store := blockchainAPI.Store
store.StoreTransaction(context.Background(), event)

event = blockExecutedEvent(
3,
"0xaae4530246e61ae58479824ab0863f99ca50414d27aec0c269ae6a7cfc4c7f5b",
7766279631452241920,
"0xf31ee13dad8f38431fd31278b12be62e6b77e6923f0b7a446eb1affb61f21fc9",
"0x0000000000000000000000000000000000000000000000000000000000000000",
[]string{"0xb47d74ea64221eb941490bdc0c9a404dacd0a8573379a45c992ac60ee3e83c3c"},
)

err = store.StoreBlock(context.Background(), event)
require.NoError(t, err)

resp := rpcRequest(url, request, "origin", "test.com")
defer resp.Body.Close()

content, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}

assert.Equal(t, expectedResponse, strings.TrimSuffix(string(content), "\n"))
})
}

func TestServerJSONRPCOveWebSocketHandler(t *testing.T) {