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

Commit 312954e

Browse files
authored
Fix worker pool life cycle for eth_call (#97)
1 parent 0edf22a commit 312954e

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

docs/API.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ The following table shows the current [JSON RPC API](https://eth.wiki/json-rpc/A
1717
| eth_syncing | Yes | |
1818
| eth_gasPrice | - | not yet implemented |
1919
| | | |
20-
| eth_getBlockByHash | Yes | not yet tested for performance |
21-
| eth_getBlockByNumber | Yes | not yet tested for performance |
20+
| eth_getBlockByHash | Yes | |
21+
| eth_getBlockByNumber | Yes | |
2222
| eth_getBlockTransactionCountByHash | Yes | |
2323
| eth_getBlockTransactionCountByNumber | Yes | |
2424
| eth_getUncleByBlockHashAndIndex | Yes | |
@@ -37,7 +37,7 @@ The following table shows the current [JSON RPC API](https://eth.wiki/json-rpc/A
3737
| eth_getCode | Yes | |
3838
| eth_getTransactionCount | Yes | |
3939
| eth_getStorageAt | Yes | |
40-
| eth_call | - | work in progress |
40+
| eth_call | Yes | not yet tested for performance |
4141
| | | |
4242
| eth_newFilter | - | not yet implemented |
4343
| eth_newBlockFilter | - | not yet implemented |

silkrpc/commands/eth_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ asio::awaitable<void> EthereumRpcApi::handle_eth_call(const nlohmann::json& requ
794794
const auto chain_config_ptr = silkworm::lookup_chain_config(chain_id);
795795
const auto block_number = co_await core::get_block_number(block_id, tx_database);
796796

797-
Executor executor{context_, tx_database, *chain_config_ptr, block_number};
797+
Executor executor{context_, tx_database, *chain_config_ptr, workers_, block_number};
798798
const auto block_with_hash = co_await core::rawdb::read_block_by_number(tx_database, block_number);
799799
silkworm::Transaction txn{call.to_transaction()};
800800
const auto execution_result = co_await executor.call(block_with_hash.block, txn, txn.gas_limit);

silkrpc/commands/eth_api.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
#ifndef SILKRPC_COMMANDS_ETH_API_HPP_
1818
#define SILKRPC_COMMANDS_ETH_API_HPP_
1919

20+
#include <cstddef>
2021
#include <memory>
22+
#include <thread>
2123
#include <vector>
2224

2325
#include <silkrpc/config.hpp> // NOLINT(build/include_order)
2426

2527
#include <asio/awaitable.hpp>
28+
#include <asio/thread_pool.hpp>
2629
#include <evmc/evmc.hpp>
2730
#include <nlohmann/json.hpp>
2831

@@ -43,7 +46,8 @@ namespace silkrpc::commands {
4346

4447
class EthereumRpcApi {
4548
public:
46-
explicit EthereumRpcApi(Context& context) : context_(context), database_(context.database), backend_(context.backend) {}
49+
explicit EthereumRpcApi(Context& context, std::size_t workers_count = std::thread::hardware_concurrency())
50+
: context_(context), database_(context.database), backend_(context.backend), workers_{workers_count} {}
4751
virtual ~EthereumRpcApi() {}
4852

4953
EthereumRpcApi(const EthereumRpcApi&) = delete;
@@ -99,6 +103,7 @@ class EthereumRpcApi {
99103
Context& context_;
100104
std::unique_ptr<ethdb::Database>& database_;
101105
std::unique_ptr<ethbackend::BackEnd>& backend_;
106+
asio::thread_pool workers_;
102107

103108
friend class silkrpc::http::RequestHandler;
104109
};

silkrpc/core/executor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ asio::awaitable<ExecutionResult> Executor::call(const silkworm::Block& block, co
8282
const auto exec_result = co_await asio::async_compose<decltype(asio::use_awaitable), void(ExecutionResult)>(
8383
[this, &block, &txn, gas](auto&& self) {
8484
SILKRPC_TRACE << "Executor::call post block: " << block.header.number << " txn: " << &txn << " gas: " << gas << "\n";
85-
asio::post(thread_pool_, [this, &block, &txn, gas, self = std::move(self)]() mutable {
85+
asio::post(workers_, [this, &block, &txn, gas, self = std::move(self)]() mutable {
8686
silkworm::IntraBlockState state{buffer_};
8787
silkworm::EVM evm{block, state, config_};
8888

silkrpc/core/executor.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class Executor {
4545
public:
4646
static std::string get_error_message(int64_t error_code);
4747

48-
explicit Executor(const Context& context, const core::rawdb::DatabaseReader& db_reader, const silkworm::ChainConfig& config, uint64_t block_number)
49-
: context_(context), db_reader_(db_reader), config_(config), buffer_{*context.io_context, db_reader, block_number}, thread_pool_{10} {}
48+
explicit Executor(const Context& context, const core::rawdb::DatabaseReader& db_reader, const silkworm::ChainConfig& config, asio::thread_pool& workers, uint64_t block_number)
49+
: context_(context), db_reader_(db_reader), config_(config), workers_{workers}, buffer_{*context.io_context, db_reader, block_number} {}
5050
virtual ~Executor() {}
5151

5252
Executor(const Executor&) = delete;
@@ -58,8 +58,8 @@ class Executor {
5858
const Context& context_;
5959
const core::rawdb::DatabaseReader& db_reader_;
6060
const silkworm::ChainConfig& config_;
61+
asio::thread_pool& workers_;
6162
RemoteBuffer buffer_;
62-
asio::thread_pool thread_pool_;
6363
};
6464

6565
} // namespace silkrpc

0 commit comments

Comments
 (0)