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

Commit 5f30264

Browse files
lupin012canepat
andauthored
Add implementation of eth_getBlockTransactionCountByHash API (#31)
Co-authored-by: canepat <16927169+canepat@users.noreply.github.com>
1 parent 3db81aa commit 5f30264

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

docs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The following table shows the current [JSON RPC API](https://eth.wiki/json-rpc/A
1919
| | | |
2020
| eth_getBlockByHash | Yes | not yet tested for performance |
2121
| eth_getBlockByNumber | Yes | not yet tested for performance |
22-
| eth_getBlockTransactionCountByHash | - | not yet implemented |
22+
| eth_getBlockTransactionCountByHash | Yes | |
2323
| eth_getBlockTransactionCountByNumber | Yes | |
2424
| eth_getUncleByBlockHashAndIndex | - | not yet implemented |
2525
| eth_getUncleByBlockNumberAndIndex | - | not yet implemented |

silkrpc/commands/eth_api.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ asio::awaitable<void> EthereumRpcApi::handle_eth_syncing(const nlohmann::json& r
160160
asio::awaitable<void> EthereumRpcApi::handle_eth_get_block_by_hash(const nlohmann::json& request, nlohmann::json& reply) {
161161
auto params = request["params"];
162162
if (params.size() != 2) {
163-
auto error_msg = "invalid eth_getLogs params: " + params.dump();
163+
auto error_msg = "invalid eth_getBlockByHash params: " + params.dump();
164164
SILKRPC_ERROR << error_msg << "\n";
165165
reply = make_json_error(request["id"], 100, error_msg);
166166
co_return;
@@ -192,11 +192,44 @@ asio::awaitable<void> EthereumRpcApi::handle_eth_get_block_by_hash(const nlohman
192192
co_return;
193193
}
194194

195+
// https://eth.wiki/json-rpc/API#eth_getblocktransactioncountbyhash
196+
asio::awaitable<void> EthereumRpcApi::handle_eth_get_block_transaction_count_by_hash(const nlohmann::json& request, nlohmann::json& reply) {
197+
auto params = request["params"];
198+
if (params.size() != 1) {
199+
auto error_msg = "invalid eth_getBlockTransactionCountByHash params: " + params.dump();
200+
SILKRPC_ERROR << error_msg << "\n";
201+
reply = make_json_error(request["id"], 100, error_msg);
202+
co_return;
203+
}
204+
auto block_hash = params[0].get<evmc::bytes32>();
205+
SILKRPC_DEBUG << "block_hash: " << block_hash << "\n";
206+
207+
auto tx = co_await database_->begin();
208+
209+
try {
210+
ethdb::kv::TransactionDatabase tx_database{*tx};
211+
212+
const auto block_with_hash = co_await core::rawdb::read_block_by_hash(tx_database, block_hash);
213+
const auto tx_count = block_with_hash.block.transactions.size();
214+
215+
reply = make_json_content(request["id"], "0x" + to_hex_no_leading_zeros(tx_count));
216+
} catch (const std::exception& e) {
217+
SILKRPC_ERROR << "exception: " << e.what() << "\n";
218+
reply = make_json_error(request["id"], 100, e.what());
219+
} catch (...) {
220+
SILKRPC_ERROR << "unexpected exception\n";
221+
reply = make_json_error(request["id"], 100, "unexpected exception");
222+
}
223+
224+
co_await tx->close(); // RAII not (yet) available with coroutines
225+
co_return;
226+
}
227+
195228
// https://eth.wiki/json-rpc/API#eth_getblockbynumber
196229
asio::awaitable<void> EthereumRpcApi::handle_eth_get_block_by_number(const nlohmann::json& request, nlohmann::json& reply) {
197230
auto params = request["params"];
198231
if (params.size() != 2) {
199-
auto error_msg = "invalid eth_getLogs params: " + params.dump();
232+
auto error_msg = "invalid getBlockByNumber params: " + params.dump();
200233
SILKRPC_ERROR << error_msg << "\n";
201234
reply = make_json_error(request["id"], 100, error_msg);
202235
co_return;

silkrpc/commands/eth_api.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class EthereumRpcApi {
5959
asio::awaitable<void> handle_eth_get_block_transaction_count_by_number(const nlohmann::json& request, nlohmann::json& reply);
6060
asio::awaitable<void> handle_eth_get_logs(const nlohmann::json& request, nlohmann::json& reply);
6161

62+
asio::awaitable<void> handle_eth_get_block_transaction_count_by_hash(const nlohmann::json& request, nlohmann::json& reply);
63+
6264
asio::awaitable<Roaring> get_topics_bitmap(core::rawdb::DatabaseReader& db_reader, FilterTopics& topics, uint64_t start, uint64_t end);
6365
asio::awaitable<Roaring> get_addresses_bitmap(core::rawdb::DatabaseReader& db_reader, FilterAddresses& addresses, uint64_t start, uint64_t end);
6466
asio::awaitable<Receipts> get_receipts(core::rawdb::DatabaseReader& db_reader, uint64_t number, evmc::bytes32 hash);

silkrpc/http/methods.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ constexpr const char* k_eth_chainId{"eth_chainId"};
3535
constexpr const char* k_eth_protocolVersion{"eth_protocolVersion"};
3636
constexpr const char* k_eth_syncing{"eth_syncing"};
3737
constexpr const char* k_eth_getBlockByHash{"eth_getBlockByHash"};
38+
constexpr const char* k_eth_getBlockTransactionCountByHash{"eth_getBlockTransactionCountByHash"};
3839
constexpr const char* k_eth_getBlockByNumber{"eth_getBlockByNumber"};
3940
constexpr const char* k_eth_getBlockTransactionCountByNumber{"eth_getBlockTransactionCountByNumber"};
4041
constexpr const char* k_eth_getLogs{"eth_getLogs"};

silkrpc/http/request_handler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ std::map<std::string, RequestHandler::HandleMethod> RequestHandler::handlers_ =
4343
{method::k_eth_syncing, &commands::RpcApi::handle_eth_syncing},
4444
{method::k_eth_getBlockByHash, &commands::RpcApi::handle_eth_get_block_by_hash},
4545
{method::k_eth_getBlockByNumber, &commands::RpcApi::handle_eth_get_block_by_number},
46+
{method::k_eth_getBlockTransactionCountByHash, &commands::RpcApi::handle_eth_get_block_transaction_count_by_hash},
4647
{method::k_eth_getBlockTransactionCountByNumber, &commands::RpcApi::handle_eth_get_block_transaction_count_by_number},
4748
{method::k_eth_getLogs, &commands::RpcApi::handle_eth_get_logs},
4849
{method::k_net_listening, &commands::RpcApi::handle_net_listening},

tests/integration/run_jsonrpc_commands.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ def run_shell_command(command: str) -> int:
1313
sys.exit(process.returncode)
1414
response = json.loads(process.stdout)
1515
if "error" in response:
16+
print ("execute KO:", command_and_args)
1617
sys.exit(1)
18+
else:
19+
print ("execute OK:", command_and_args)
1720

1821
run_shell_command('''curl --silent -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}' localhost:51515''')
1922
run_shell_command('''curl --silent -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"web3_sha3","params":["0x00"],"id":1}' localhost:51515''')
@@ -32,3 +35,5 @@ def run_shell_command(command: str) -> int:
3235
run_shell_command('''curl --silent -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["0x41b57c"],"id":1}' localhost:51515''')
3336

3437
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}' http://localhost:51515''')
38+
39+
run_shell_command('''curl --silent -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByHash","params":["0x814672e6913a3879217169c6ba461114fa032d9510a56a409d3aab19f668e299"],"id":1}' http://localhost:51515''')

0 commit comments

Comments
 (0)