From 843374ce82078d73aaefea694f047038f8b6c1de Mon Sep 17 00:00:00 2001 From: yarkin Date: Mon, 18 Dec 2023 12:45:44 +0800 Subject: [PATCH] Let more API use blockNumberOrHash. --- silkworm/silkrpc/commands/eth_api.cpp | 46 +++++++++++++----------- silkworm/silkrpc/commands/parity_api.cpp | 7 ++-- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/silkworm/silkrpc/commands/eth_api.cpp b/silkworm/silkrpc/commands/eth_api.cpp index 1aba2e15..df15cda3 100644 --- a/silkworm/silkrpc/commands/eth_api.cpp +++ b/silkworm/silkrpc/commands/eth_api.cpp @@ -948,19 +948,20 @@ awaitable EthereumRpcApi::handle_eth_get_balance(const nlohmann::json& req co_return; } const auto address = params[0].get(); - const auto block_id = params[1].get(); - SILK_DEBUG << "address: " << silkworm::to_hex(address) << " block_id: " << block_id; + const auto block_number_or_hash = params[1].get(); + SILK_DEBUG << "address: " << silkworm::to_hex(address) << " block_number_or_hash: " << block_number_or_hash; auto tx = co_await database_->begin(); try { ethdb::TransactionDatabase tx_database{*tx}; - ethdb::kv::CachedDatabase cached_database{BlockNumberOrHash{block_id}, *tx, *state_cache_}; - const auto [block_number, is_latest_block] = co_await core::get_block_number(block_id, tx_database, /*latest_required=*/true); + ethdb::kv::CachedDatabase cached_database{block_number_or_hash, *tx, *state_cache_}; + const auto block_with_hash = co_await core::read_block_by_number_or_hash(*block_cache_, tx_database, block_number_or_hash); + const bool is_latest_block = co_await core::get_latest_executed_block_number(tx_database) == block_with_hash->block.header.number; StateReader state_reader{ is_latest_block ? static_cast(cached_database) : static_cast(tx_database)}; - std::optional account{co_await state_reader.read_account(address, block_number + 1)}; + std::optional account{co_await state_reader.read_account(address, block_with_hash->block.header.number + 1)}; reply = make_json_content(request["id"], "0x" + (account ? intx::hex(account->balance) : "0")); } catch (const std::exception& e) { @@ -985,19 +986,20 @@ awaitable EthereumRpcApi::handle_eth_get_code(const nlohmann::json& reques co_return; } const auto address = params[0].get(); - const auto block_id = params[1].get(); - SILK_DEBUG << "address: " << silkworm::to_hex(address) << " block_id: " << block_id; + const auto block_number_or_hash = params[1].get(); + SILK_DEBUG << "address: " << silkworm::to_hex(address) << " block_number_or_hash: " << block_number_or_hash; auto tx = co_await database_->begin(); try { ethdb::TransactionDatabase tx_database{*tx}; - ethdb::kv::CachedDatabase cached_database{BlockNumberOrHash{block_id}, *tx, *state_cache_}; - const auto [block_number, is_latest_block] = co_await core::get_block_number(block_id, tx_database, /*latest_required=*/true); + ethdb::kv::CachedDatabase cached_database{block_number_or_hash, *tx, *state_cache_}; + const auto block_with_hash = co_await core::read_block_by_number_or_hash(*block_cache_, tx_database, block_number_or_hash); + const bool is_latest_block = co_await core::get_latest_executed_block_number(tx_database) == block_with_hash->block.header.number; StateReader state_reader{ is_latest_block ? static_cast(cached_database) : static_cast(tx_database)}; - std::optional account{co_await state_reader.read_account(address, block_number + 1)}; + std::optional account{co_await state_reader.read_account(address, block_with_hash->block.header.number + 1)}; if (account) { auto code{co_await state_reader.read_code(account->code_hash)}; @@ -1027,19 +1029,20 @@ awaitable EthereumRpcApi::handle_eth_get_transaction_count(const nlohmann: co_return; } const auto address = params[0].get(); - const auto block_id = params[1].get(); - SILK_DEBUG << "address: " << silkworm::to_hex(address) << " block_id: " << block_id; + const auto block_number_or_hash = params[1].get(); + SILK_DEBUG << "address: " << silkworm::to_hex(address) << " block_number_or_hash: " << block_number_or_hash; auto tx = co_await database_->begin(); try { ethdb::TransactionDatabase tx_database{*tx}; - ethdb::kv::CachedDatabase cached_database{BlockNumberOrHash{block_id}, *tx, *state_cache_}; - const auto [block_number, is_latest_block] = co_await core::get_block_number(block_id, tx_database, /*latest_required=*/true); + ethdb::kv::CachedDatabase cached_database{block_number_or_hash, *tx, *state_cache_}; + const auto block_with_hash = co_await core::read_block_by_number_or_hash(*block_cache_, tx_database, block_number_or_hash); + const bool is_latest_block = co_await core::get_latest_executed_block_number(tx_database) == block_with_hash->block.header.number; StateReader state_reader{ is_latest_block ? static_cast(cached_database) : static_cast(tx_database)}; - std::optional account{co_await state_reader.read_account(address, block_number + 1)}; + std::optional account{co_await state_reader.read_account(address, block_with_hash->block.header.number + 1)}; if (account) { reply = make_json_content(request["id"], to_quantity(account->nonce)); @@ -1069,21 +1072,22 @@ awaitable EthereumRpcApi::handle_eth_get_storage_at(const nlohmann::json& } const auto address = params[0].get(); const auto location = params[1].get(); - const auto block_id = params[2].get(); - SILK_DEBUG << "address: " << silkworm::to_hex(address) << " block_id: " << block_id; + const auto block_number_or_hash = params[2].get(); + SILK_DEBUG << "address: " << silkworm::to_hex(address) << " block_number_or_hash: " << block_number_or_hash; auto tx = co_await database_->begin(); try { ethdb::TransactionDatabase tx_database{*tx}; - ethdb::kv::CachedDatabase cached_database{BlockNumberOrHash{block_id}, *tx, *state_cache_}; - const auto [block_number, is_latest_block] = co_await core::get_block_number(block_id, tx_database, /*latest_required=*/true); + ethdb::kv::CachedDatabase cached_database{block_number_or_hash, *tx, *state_cache_}; + const auto block_with_hash = co_await core::read_block_by_number_or_hash(*block_cache_, tx_database, block_number_or_hash); + const bool is_latest_block = co_await core::get_latest_executed_block_number(tx_database) == block_with_hash->block.header.number; StateReader state_reader{ is_latest_block ? static_cast(cached_database) : static_cast(tx_database)}; - std::optional account{co_await state_reader.read_account(address, block_number + 1)}; + std::optional account{co_await state_reader.read_account(address, block_with_hash->block.header.number + 1)}; if (account) { - auto storage{co_await state_reader.read_storage(address, account->incarnation, location, block_number + 1)}; + auto storage{co_await state_reader.read_storage(address, account->incarnation, location, block_with_hash->block.header.number + 1)}; reply = make_json_content(request["id"], "0x" + silkworm::to_hex(storage)); } else { reply = make_json_content(request["id"], "0x0000000000000000000000000000000000000000000000000000000000000000"); diff --git a/silkworm/silkrpc/commands/parity_api.cpp b/silkworm/silkrpc/commands/parity_api.cpp index eea81972..80b999b6 100644 --- a/silkworm/silkrpc/commands/parity_api.cpp +++ b/silkworm/silkrpc/commands/parity_api.cpp @@ -42,16 +42,15 @@ awaitable ParityRpcApi::handle_parity_get_block_receipts(const nlohmann::j reply = make_json_error(request["id"], 100, error_msg); co_return; } - const auto block_id = params[0].get(); - SILK_DEBUG << "block_id: " << block_id; + const auto block_number_or_hash = params[0].get(); + SILK_DEBUG << "block_number_or_hash: " << block_number_or_hash; auto tx = co_await database_->begin(); try { ethdb::TransactionDatabase tx_database{*tx}; + const auto block_with_hash = co_await core::read_block_by_number_or_hash(*block_cache_, tx_database, block_number_or_hash); - const auto block_number = co_await core::get_block_number(block_id, tx_database); - const auto block_with_hash = co_await core::read_block_by_number(*block_cache_, tx_database, block_number); auto receipts{co_await core::get_receipts(tx_database, *block_with_hash)}; SILK_INFO << "#receipts: " << receipts.size();