Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit 234a099

Browse files
authored
Add implementation of eth_getTransactionByBlockNumberAndIndex API (#59)
1 parent 994b226 commit 234a099

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

docs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The following table shows the current [JSON RPC API](https://eth.wiki/json-rpc/A
2828
| | | |
2929
| eth_getTransactionByHash | - | not yet implemented |
3030
| eth_getTransactionByBlockHashAndIndex | - | not yet implemented |
31-
| eth_getTransactionByBlockNumberAndIndex | - | not yet implemented |
31+
| eth_getTransactionByBlockNumberAndIndex | Yes | |
3232
| eth_getTransactionReceipt | - | not yet implemented |
3333
| | | |
3434
| eth_estimateGas | - | not yet implemented |

silkrpc/commands/eth_api.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,34 @@ asio::awaitable<void> EthereumRpcApi::handle_eth_get_transaction_by_block_hash_a
476476

477477
// https://eth.wiki/json-rpc/API#eth_gettransactionbyblocknumberandindex
478478
asio::awaitable<void> EthereumRpcApi::handle_eth_get_transaction_by_block_number_and_index(const nlohmann::json& request, nlohmann::json& reply) {
479+
auto params = request["params"];
480+
if (params.size() != 2) {
481+
auto error_msg = "invalid eth_getTransactionByBlockNumberAndIndex params: " + params.dump();
482+
SILKRPC_ERROR << error_msg << "\n";
483+
reply = make_json_error(request["id"], 100, error_msg);
484+
co_return;
485+
}
486+
const auto block_id = params[0].get<std::string>();
487+
const auto index = params[1].get<std::string>();
488+
SILKRPC_DEBUG << "block_id: " << block_id << " index: " << index << "\n";
489+
479490
auto tx = co_await database_->begin();
480491

481492
try {
482493
ethdb::TransactionDatabase tx_database{*tx};
483494

484-
reply = make_json_content(request["id"], "0x" + to_hex_no_leading_zeros(0));
495+
const auto block_number = co_await core::get_block_number(block_id, tx_database);
496+
const auto block_with_number = co_await core::rawdb::read_block_by_number(tx_database, block_number);
497+
const auto transactions = block_with_number.block.transactions;
498+
499+
auto idx = std::stoul(index, 0, 16);
500+
if (idx >= transactions.size()) {
501+
auto error_msg = "invalid eth_getTransactionByBlockNumberAndIndex index: " + index;
502+
SILKRPC_ERROR << error_msg << "\n";
503+
reply = make_json_error(request["id"], 100, error_msg);
504+
co_return;
505+
}
506+
reply = make_json_content(request["id"], transactions[idx]);
485507
} catch (const std::exception& e) {
486508
SILKRPC_ERROR << "exception: " << e.what() << "\n";
487509
reply = make_json_error(request["id"], 100, e.what());

tests/integration/run_jsonrpc_commands.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ def run_shell_command(command: str) -> int:
4040

4141
run_shell_command('''curl --silent -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getUncleByBlockNumberAndIndex","params":["0x41b57c", "0x0"],"id":1}' localhost:51515''')
4242
run_shell_command('''curl --silent -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getUncleCountByBlockNumber","params":["0x41b57c"],"id":1}' localhost:51515''')
43+
run_shell_command('''curl --silent -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getTransactionByBlockNumberAndIndex", "params":["0x41b57c", "0x0"],"id":1}' localhost:51515''')
44+
4345

4446
run_shell_command('''curl --silent -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock": "0x3d0900", "toBlock": "0x3d0964", "address": "0x2a89f54a9f8e727a7be754fd055bb8ea93d0557d"}],"id":3}' localhost:51515''')

0 commit comments

Comments
 (0)