Skip to content

Commit e1eead9

Browse files
authored
rpcdaemon: replace Database hierarchy with KV Service (#2640)
1 parent 567168d commit e1eead9

36 files changed

+267
-834
lines changed

cmake/conan.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ endmacro()
7777
# unset(CONAN_COMMAND CACHE)
7878
find_program(
7979
CONAN_COMMAND "conan"
80-
PATHS /opt/conan2/bin
80+
PATHS /opt/conan2/bin /opt/homebrew/opt/conan@2/bin
8181
NO_DEFAULT_PATH
8282
)
8383
if(NOT CONAN_COMMAND)

examples/get_latest_block.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
#include <grpcpp/grpcpp.h>
3030

3131
#include <silkworm/db/kv/api/state_cache.hpp>
32+
#include <silkworm/db/kv/grpc/client/remote_client.hpp>
3233
#include <silkworm/infra/common/log.hpp>
3334
#include <silkworm/infra/grpc/client/client_context_pool.hpp>
3435
#include <silkworm/rpc/common/constants.hpp>
3536
#include <silkworm/rpc/core/block_reader.hpp>
3637
#include <silkworm/rpc/ethbackend/remote_backend.hpp>
37-
#include <silkworm/rpc/ethdb/kv/remote_database.hpp>
38+
#include <silkworm/rpc/ethdb/kv/backend_providers.hpp>
3839

3940
using namespace silkworm;
4041
using namespace silkworm::db;
@@ -43,10 +44,10 @@ using namespace silkworm::rpc;
4344
ABSL_FLAG(std::string, target, kDefaultPrivateApiAddr, "server location as string <address>:<port>");
4445
// ABSL_FLAG(LogLevel, log_verbosity, LogLevel::Critical, "logging level");
4546

46-
Task<std::optional<uint64_t>> latest_block(ethdb::Database& db) {
47+
Task<std::optional<uint64_t>> latest_block(db::kv::api::Service& service) {
4748
std::optional<uint64_t> block_num;
4849

49-
const auto db_transaction = co_await db.begin();
50+
const auto db_transaction = co_await service.begin_transaction();
5051
try {
5152
const auto chain_storage{db_transaction->create_storage()};
5253
rpc::BlockReader block_reader{*chain_storage, *db_transaction};
@@ -61,11 +62,11 @@ Task<std::optional<uint64_t>> latest_block(ethdb::Database& db) {
6162
co_return block_num;
6263
}
6364

64-
std::optional<uint64_t> get_latest_block(boost::asio::io_context& ioc, ethdb::Database& db) {
65+
std::optional<uint64_t> get_latest_block(boost::asio::io_context& ioc, db::kv::api::Service& service) {
6566
auto result = boost::asio::co_spawn(
6667
ioc,
6768
[&]() -> Task<std::optional<uint64_t>> {
68-
const auto block_num = co_await latest_block(db);
69+
const auto block_num = co_await latest_block(service);
6970
ioc.stop();
7071
co_return block_num;
7172
},
@@ -95,16 +96,17 @@ int main(int argc, char* argv[]) {
9596
ClientContextPool context_pool{1};
9697
auto& context = context_pool.next_context();
9798
auto* ioc = context.ioc();
98-
auto* grpc_context = context.grpc_context();
99+
auto& grpc_context = *context.grpc_context();
99100

100101
kv::api::CoherentStateCache state_cache;
101-
auto channel{::grpc::CreateChannel(target, ::grpc::InsecureChannelCredentials())};
102-
auto backend{std::make_unique<rpc::ethbackend::RemoteBackEnd>(channel, *grpc_context)};
103-
auto database = std::make_unique<ethdb::kv::RemoteDatabase>(backend.get(), &state_cache, *grpc_context, channel);
102+
auto channel = ::grpc::CreateChannel(target, ::grpc::InsecureChannelCredentials());
103+
auto backend = std::make_unique<rpc::ethbackend::RemoteBackEnd>(channel, grpc_context);
104+
auto database = std::make_unique<db::kv::grpc::client::RemoteClient>(
105+
create_channel, grpc_context, &state_cache, ethdb::kv::make_backend_providers(backend.get()));
104106

105107
auto context_pool_thread = std::thread([&]() { context_pool.run(); });
106108

107-
const auto latest_block_num = get_latest_block(*ioc, *database);
109+
const auto latest_block_num = get_latest_block(*ioc, *database->service());
108110
if (latest_block_num) {
109111
std::cout << "latest_block_num: " << latest_block_num.value() << "\n";
110112
}

silkworm/rpc/commands/debug_api.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Task<void> DebugRpcApi::handle_debug_account_range(const nlohmann::json& request
8585
<< " exclude_code: " << exclude_code
8686
<< " exclude_storage: " << exclude_storage;
8787

88-
auto tx = co_await database_->begin();
88+
auto tx = co_await database_->begin_transaction();
8989

9090
try {
9191
auto start = std::chrono::system_clock::now();
@@ -123,7 +123,7 @@ Task<void> DebugRpcApi::handle_debug_get_modified_accounts_by_number(const nlohm
123123
}
124124
SILK_DEBUG << "start_block_id: " << start_block_id << " end_block_id: " << end_block_id;
125125

126-
auto tx = co_await database_->begin();
126+
auto tx = co_await database_->begin_transaction();
127127
const auto chain_storage = tx->create_storage();
128128

129129
try {
@@ -171,7 +171,7 @@ Task<void> DebugRpcApi::handle_debug_get_modified_accounts_by_hash(const nlohman
171171
}
172172
SILK_DEBUG << "start_hash: " << silkworm::to_hex(start_hash) << " end_hash: " << silkworm::to_hex(end_hash);
173173

174-
auto tx = co_await database_->begin();
174+
auto tx = co_await database_->begin_transaction();
175175

176176
try {
177177
const auto chain_storage = tx->create_storage();
@@ -225,7 +225,7 @@ Task<void> DebugRpcApi::handle_debug_storage_range_at(const nlohmann::json& requ
225225
<< " start_key: 0x" << silkworm::to_hex(start_key)
226226
<< " max_result: " << max_result;
227227

228-
auto tx = co_await database_->begin();
228+
auto tx = co_await database_->begin_transaction();
229229

230230
try {
231231
const auto chain_storage = tx->create_storage();
@@ -304,7 +304,7 @@ Task<void> DebugRpcApi::handle_debug_account_at(const nlohmann::json& request, n
304304
<< " tx_index: " << tx_index
305305
<< " address: " << address;
306306

307-
auto tx = co_await database_->begin();
307+
auto tx = co_await database_->begin_transaction();
308308

309309
try {
310310
const auto chain_storage = tx->create_storage();
@@ -399,7 +399,7 @@ Task<void> DebugRpcApi::handle_debug_trace_transaction(const nlohmann::json& req
399399
stream.write_json_field("id", request["id"]);
400400
stream.write_field("jsonrpc", "2.0");
401401

402-
auto tx = co_await database_->begin();
402+
auto tx = co_await database_->begin_transaction();
403403

404404
try {
405405
debug::DebugExecutor executor{*block_cache_, workers_, *tx, config};
@@ -442,7 +442,7 @@ Task<void> DebugRpcApi::handle_debug_trace_call(const nlohmann::json& request, j
442442
stream.write_json_field("id", request["id"]);
443443
stream.write_field("jsonrpc", "2.0");
444444

445-
auto tx = co_await database_->begin();
445+
auto tx = co_await database_->begin_transaction();
446446

447447
try {
448448
const auto chain_storage = tx->create_storage();
@@ -510,7 +510,7 @@ Task<void> DebugRpcApi::handle_debug_trace_call_many(const nlohmann::json& reque
510510
stream.write_json_field("id", request["id"]);
511511
stream.write_field("jsonrpc", "2.0");
512512

513-
auto tx = co_await database_->begin();
513+
auto tx = co_await database_->begin_transaction();
514514

515515
try {
516516
debug::DebugExecutor executor{*block_cache_, workers_, *tx, config};
@@ -538,7 +538,7 @@ Task<void> DebugRpcApi::handle_debug_trace_block_by_number(const nlohmann::json&
538538
co_return;
539539
}
540540

541-
auto tx = co_await database_->begin();
541+
auto tx = co_await database_->begin_transaction();
542542
BlockNum block_num{0};
543543
if (params[0].is_string()) {
544544
auto chain_storage = tx->create_storage();
@@ -607,7 +607,7 @@ Task<void> DebugRpcApi::handle_debug_trace_block_by_hash(const nlohmann::json& r
607607
stream.write_json_field("id", request["id"]);
608608
stream.write_field("jsonrpc", "2.0");
609609

610-
auto tx = co_await database_->begin();
610+
auto tx = co_await database_->begin_transaction();
611611

612612
try {
613613
const auto chain_storage = tx->create_storage();
@@ -681,7 +681,7 @@ Task<void> DebugRpcApi::handle_debug_get_raw_block(const nlohmann::json& request
681681
const auto block_id = params[0].get<std::string>();
682682
SILK_DEBUG << "block_id: " << block_id;
683683

684-
auto tx = co_await database_->begin();
684+
auto tx = co_await database_->begin_transaction();
685685

686686
try {
687687
const auto chain_storage = tx->create_storage();
@@ -718,7 +718,7 @@ Task<void> DebugRpcApi::handle_debug_get_raw_receipts(const nlohmann::json& requ
718718
}
719719
const auto block_num_or_hash = params[0].get<BlockNumOrHash>();
720720

721-
auto tx = co_await database_->begin();
721+
auto tx = co_await database_->begin_transaction();
722722

723723
try {
724724
const auto chain_storage = tx->create_storage();
@@ -779,7 +779,7 @@ Task<void> DebugRpcApi::handle_debug_get_raw_header(const nlohmann::json& reques
779779
const auto block_id = params[0].get<std::string>();
780780
SILK_DEBUG << "block_id: " << block_id;
781781

782-
auto tx = co_await database_->begin();
782+
auto tx = co_await database_->begin_transaction();
783783

784784
try {
785785
const auto chain_storage = tx->create_storage();
@@ -818,7 +818,7 @@ Task<void> DebugRpcApi::handle_debug_get_raw_transaction(const nlohmann::json& r
818818
const auto transaction_hash = params[0].get<evmc::bytes32>();
819819
SILK_DEBUG << "transaction_hash: " << silkworm::to_hex(transaction_hash);
820820

821-
auto tx = co_await database_->begin();
821+
auto tx = co_await database_->begin_transaction();
822822

823823
try {
824824
const auto chain_storage{tx->create_storage()};

silkworm/rpc/commands/debug_api.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
#include <nlohmann/json.hpp>
2323

2424
#include <silkworm/core/common/block_cache.hpp>
25+
#include <silkworm/db/kv/api/client.hpp>
2526
#include <silkworm/db/kv/api/state_cache.hpp>
2627
#include <silkworm/db/kv/api/transaction.hpp>
2728
#include <silkworm/infra/concurrency/private_service.hpp>
2829
#include <silkworm/infra/concurrency/shared_service.hpp>
2930
#include <silkworm/rpc/common/worker_pool.hpp>
3031
#include <silkworm/rpc/core/block_reader.hpp>
3132
#include <silkworm/rpc/ethbackend/backend.hpp>
32-
#include <silkworm/rpc/ethdb/database.hpp>
3333
#include <silkworm/rpc/json/stream.hpp>
3434
#include <silkworm/rpc/json/types.hpp>
3535

@@ -45,7 +45,7 @@ class DebugRpcApi {
4545
: ioc_{ioc},
4646
block_cache_{must_use_shared_service<BlockCache>(ioc_)},
4747
state_cache_{must_use_shared_service<db::kv::api::StateCache>(ioc_)},
48-
database_{must_use_private_service<ethdb::Database>(ioc_)},
48+
database_{must_use_private_service<db::kv::api::Client>(ioc_)->service()},
4949
workers_{workers},
5050
backend_{must_use_private_service<ethbackend::BackEnd>(ioc_)} {}
5151
virtual ~DebugRpcApi() = default;
@@ -76,7 +76,7 @@ class DebugRpcApi {
7676
boost::asio::io_context& ioc_;
7777
BlockCache* block_cache_;
7878
db::kv::api::StateCache* state_cache_;
79-
ethdb::Database* database_;
79+
std::shared_ptr<db::kv::api::Service> database_;
8080
WorkerPool& workers_;
8181
ethbackend::BackEnd* backend_;
8282

0 commit comments

Comments
 (0)