Skip to content

Commit

Permalink
rpcdaemon: debug_accountAt debug_traceCall debug_traceCallMany debug_…
Browse files Browse the repository at this point in the history
…traceBlockByNumber debug_traceBlockByHash w/ snapshots (#1363)
  • Loading branch information
lupin012 authored Jul 24, 2023
1 parent abda81f commit a176f2e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
18 changes: 12 additions & 6 deletions silkworm/silkrpc/commands/debug_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ awaitable<void> DebugRpcApi::handle_debug_storage_range_at(const nlohmann::json&

try {
ethdb::TransactionDatabase tx_database{*tx};
const auto chain_storage = tx->create_storage(tx_database, backend_);

const auto block_with_hash = co_await core::read_block_by_hash(*block_cache_, tx_database, block_hash);
const auto block_with_hash = co_await core::read_block_by_hash(*block_cache_, *chain_storage, block_hash);
auto block_number = block_with_hash->block.header.number - 1;

nlohmann::json storage({});
Expand Down Expand Up @@ -284,8 +285,9 @@ awaitable<void> DebugRpcApi::handle_debug_account_at(const nlohmann::json& reque

try {
ethdb::TransactionDatabase tx_database{*tx};
const auto chain_storage = tx->create_storage(tx_database, backend_);

const auto block_with_hash = co_await core::read_block_by_hash(*block_cache_, tx_database, block_hash);
const auto block_with_hash = co_await core::read_block_by_hash(*block_cache_, *chain_storage, block_hash);

const auto& block = block_with_hash->block;
auto block_number = block.header.number - 1;
Expand Down Expand Up @@ -430,13 +432,14 @@ awaitable<void> DebugRpcApi::handle_debug_trace_call(const nlohmann::json& reque
try {
ethdb::TransactionDatabase tx_database{*tx};
ethdb::kv::CachedDatabase cached_database{block_number_or_hash, *tx, *state_cache_};
const auto chain_storage = tx->create_storage(tx_database, backend_);

const bool is_latest_block = co_await core::is_latest_block_number(block_number_or_hash, tx_database);
const core::rawdb::DatabaseReader& db_reader =
is_latest_block ? static_cast<core::rawdb::DatabaseReader&>(cached_database) : static_cast<core::rawdb::DatabaseReader&>(tx_database);

debug::DebugExecutor executor{db_reader, *block_cache_, workers_, *tx, config};
co_await executor.trace_call(stream, block_number_or_hash, call);
co_await executor.trace_call(stream, block_number_or_hash, *chain_storage, call);
} catch (const std::exception& e) {
SILK_ERROR << "exception: " << e.what() << " processing request: " << request.dump();
std::ostringstream oss;
Expand Down Expand Up @@ -504,7 +507,8 @@ awaitable<void> DebugRpcApi::handle_debug_trace_call_many(const nlohmann::json&
try {
ethdb::TransactionDatabase tx_database{*tx};
debug::DebugExecutor executor{tx_database, *block_cache_, workers_, *tx, config};
co_await executor.trace_call_many(stream, bundles, simulation_context);
const auto chain_storage = tx->create_storage(tx_database, backend_);
co_await executor.trace_call_many(stream, *chain_storage, bundles, simulation_context);
} catch (...) {
SILK_ERROR << "unexpected exception processing request: " << request.dump();
const Error error{100, "unexpected exception"};
Expand Down Expand Up @@ -544,9 +548,10 @@ awaitable<void> DebugRpcApi::handle_debug_trace_block_by_number(const nlohmann::

try {
ethdb::TransactionDatabase tx_database{*tx};
const auto chain_storage = tx->create_storage(tx_database, backend_);

debug::DebugExecutor executor{tx_database, *block_cache_, workers_, *tx, config};
co_await executor.trace_block(stream, block_number);
co_await executor.trace_block(stream, *chain_storage, block_number);
} catch (const std::invalid_argument& e) {
SILK_ERROR << "exception: " << e.what() << " processing request: " << request.dump();
std::ostringstream oss;
Expand Down Expand Up @@ -596,9 +601,10 @@ awaitable<void> DebugRpcApi::handle_debug_trace_block_by_hash(const nlohmann::js

try {
ethdb::TransactionDatabase tx_database{*tx};
const auto chain_storage = tx->create_storage(tx_database, backend_);

debug::DebugExecutor executor{tx_database, *block_cache_, workers_, *tx, config};
co_await executor.trace_block(stream, block_hash);
co_await executor.trace_block(stream, *chain_storage, block_hash);
} catch (const std::invalid_argument& e) {
SILK_ERROR << "exception: " << e.what() << " processing request: " << request.dump();
std::ostringstream oss;
Expand Down
4 changes: 3 additions & 1 deletion silkworm/silkrpc/commands/debug_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class DebugRpcApi {
block_cache_{must_use_shared_service<BlockCache>(io_context_)},
state_cache_{must_use_shared_service<ethdb::kv::StateCache>(io_context_)},
database_{must_use_private_service<ethdb::Database>(io_context_)},
workers_{workers} {}
workers_{workers},
backend_{must_use_private_service<ethbackend::BackEnd>(io_context_)} {}
virtual ~DebugRpcApi() = default;

DebugRpcApi(const DebugRpcApi&) = delete;
Expand All @@ -73,6 +74,7 @@ class DebugRpcApi {
ethdb::kv::StateCache* state_cache_;
ethdb::Database* database_;
boost::asio::thread_pool& workers_;
ethbackend::BackEnd* backend_;

friend class silkworm::http::RequestHandler;
};
Expand Down
16 changes: 8 additions & 8 deletions silkworm/silkrpc/core/evm_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ void DebugTracer::write_log(const DebugLog& log) {
stream_.write_json(json);
}

boost::asio::awaitable<void> DebugExecutor::trace_block(json::Stream& stream, std::uint64_t block_number) {
const auto block_with_hash = co_await rpc::core::read_block_by_number(block_cache_, database_reader_, block_number);
boost::asio::awaitable<void> DebugExecutor::trace_block(json::Stream& stream, const ChainStorage& storage, std::uint64_t block_number) {
const auto block_with_hash = co_await rpc::core::read_block_by_number(block_cache_, storage, database_reader_, block_number);
stream.write_field("result");
stream.open_array();
co_await execute(stream, block_with_hash->block);
Expand All @@ -283,8 +283,8 @@ boost::asio::awaitable<void> DebugExecutor::trace_block(json::Stream& stream, st
co_return;
}

boost::asio::awaitable<void> DebugExecutor::trace_block(json::Stream& stream, const evmc::bytes32& block_hash) {
const auto block_with_hash = co_await rpc::core::read_block_by_hash(block_cache_, database_reader_, block_hash);
boost::asio::awaitable<void> DebugExecutor::trace_block(json::Stream& stream, const ChainStorage& storage, const evmc::bytes32& block_hash) {
const auto block_with_hash = co_await rpc::core::read_block_by_hash(block_cache_, storage, block_hash);

stream.write_field("result");
stream.open_array();
Expand All @@ -294,8 +294,8 @@ boost::asio::awaitable<void> DebugExecutor::trace_block(json::Stream& stream, co
co_return;
}

boost::asio::awaitable<void> DebugExecutor::trace_call(json::Stream& stream, const BlockNumberOrHash& bnoh, const Call& call) {
const auto block_with_hash = co_await rpc::core::read_block_by_number_or_hash(block_cache_, database_reader_, bnoh);
boost::asio::awaitable<void> DebugExecutor::trace_call(json::Stream& stream, const BlockNumberOrHash& bnoh, const ChainStorage& storage, const Call& call) {
const auto block_with_hash = co_await rpc::core::read_block_by_number_or_hash(block_cache_, storage, database_reader_, bnoh);
rpc::Transaction transaction{call.to_transaction()};

const auto& block = block_with_hash->block;
Expand Down Expand Up @@ -331,8 +331,8 @@ boost::asio::awaitable<void> DebugExecutor::trace_transaction(json::Stream& stre
co_return;
}

boost::asio::awaitable<void> DebugExecutor::trace_call_many(json::Stream& stream, const Bundles& bundles, const SimulationContext& context) {
const auto block_with_hash = co_await rpc::core::read_block_by_number_or_hash(block_cache_, database_reader_, context.block_number);
boost::asio::awaitable<void> DebugExecutor::trace_call_many(json::Stream& stream, const ChainStorage& storage, const Bundles& bundles, const SimulationContext& context) {
const auto block_with_hash = co_await rpc::core::read_block_by_number_or_hash(block_cache_, storage, database_reader_, context.block_number);
auto transaction_index = context.transaction_index;
if (transaction_index == -1) {
transaction_index = static_cast<std::int32_t>(block_with_hash->block.transactions.size());
Expand Down
8 changes: 4 additions & 4 deletions silkworm/silkrpc/core/evm_debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ class DebugExecutor {
DebugExecutor(const DebugExecutor&) = delete;
DebugExecutor& operator=(const DebugExecutor&) = delete;

boost::asio::awaitable<void> trace_block(json::Stream& stream, std::uint64_t block_number);
boost::asio::awaitable<void> trace_block(json::Stream& stream, const evmc::bytes32& block_hash);
boost::asio::awaitable<void> trace_call(json::Stream& stream, const BlockNumberOrHash& bnoh, const Call& call);
boost::asio::awaitable<void> trace_block(json::Stream& stream, const ChainStorage& storage, std::uint64_t block_number);
boost::asio::awaitable<void> trace_block(json::Stream& stream, const ChainStorage& storage, const evmc::bytes32& block_hash);
boost::asio::awaitable<void> trace_call(json::Stream& stream, const BlockNumberOrHash& bnoh, const ChainStorage& storage, const Call& call);
boost::asio::awaitable<void> trace_transaction(json::Stream& stream, const evmc::bytes32& tx_hash);
boost::asio::awaitable<void> trace_call_many(json::Stream& stream, const Bundles& bundles, const SimulationContext& context);
boost::asio::awaitable<void> trace_call_many(json::Stream& stream, const ChainStorage& storage, const Bundles& bundles, const SimulationContext& context);

protected:
boost::asio::awaitable<void> execute(json::Stream& stream, const silkworm::Block& block, const Call& call);
Expand Down

0 comments on commit a176f2e

Please sign in to comment.