Skip to content

Commit

Permalink
rpcdaemon: providers into Providers type (#2403)
Browse files Browse the repository at this point in the history
Co-authored-by: canepat <[email protected]>
  • Loading branch information
lupin012 and canepat authored Oct 7, 2024
1 parent 0dcff8b commit 522f009
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 142 deletions.
5 changes: 1 addition & 4 deletions cmd/dev/grpc_toolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,10 +990,7 @@ int execute_temporal_kv_query(const std::string& target, KVQueryFunc<Q> query_fu
db::kv::grpc::client::RemoteClient client{channel_factory,
*grpc_context,
&state_cache,
rpc::ethdb::kv::block_provider(&eth_backend),
rpc::ethdb::kv::block_number_from_txn_hash_provider(&eth_backend),
rpc::ethdb::kv::block_number_from_block_hash_provider(&eth_backend),
rpc::ethdb::kv::block_hash_from_block_number_provider(&eth_backend)};
ethdb::kv::make_backend_providers(&eth_backend)};
auto kv_service = client.service();

// NOLINTNEXTLINE(performance-unnecessary-value-param)
Expand Down
32 changes: 12 additions & 20 deletions silkworm/db/chain/remote_chain_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,8 @@

namespace silkworm::db::chain {

RemoteChainStorage::RemoteChainStorage(kv::api::Transaction& tx,
BlockProvider block_provider,
BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider,
BlockNumberFromBlockHashProvider block_number_from_block_hash_provider,
BlockHashFromBlockNumberProvider block_hash_from_number_provider)
: tx_{tx},
block_provider_{std::move(block_provider)},
block_number_from_txn_hash_provider_{std::move(block_number_from_txn_hash_provider)},
block_number_from_block_hash_provider_{std::move(block_number_from_block_hash_provider)},
block_hash_from_number_provider_{std::move(block_hash_from_number_provider)} {}
RemoteChainStorage::RemoteChainStorage(kv::api::Transaction& tx, Providers providers)
: tx_{tx}, providers_{std::move(providers)} {}

Task<ChainConfig> RemoteChainStorage::read_chain_config() const {
const auto genesis_block_hash{co_await read_canonical_block_hash(tx_, kEarliestBlockNumber)};
Expand Down Expand Up @@ -67,26 +59,26 @@ Task<std::optional<BlockNum>> RemoteChainStorage::read_block_number(const Hash&
}

Task<bool> RemoteChainStorage::read_block(HashAsSpan hash, BlockNum number, bool read_senders, Block& block) const {
co_return co_await block_provider_(number, hash, read_senders, block);
co_return co_await providers_.block(number, hash, read_senders, block);
}

Task<bool> RemoteChainStorage::read_block(const Hash& hash, BlockNum number, Block& block) const {
co_return co_await block_provider_(number, hash.bytes, /*.read_senders=*/false, block);
co_return co_await providers_.block(number, hash.bytes, /*.read_senders=*/false, block);
}

Task<bool> RemoteChainStorage::read_block(const Hash& hash, Block& block) const {
const BlockNum block_number = co_await read_header_number(tx_, hash);
co_return co_await block_provider_(block_number, hash.bytes, /*.read_senders=*/false, block);
co_return co_await providers_.block(block_number, hash.bytes, /*.read_senders=*/false, block);
}

Task<bool> RemoteChainStorage::read_block(BlockNum number, bool read_senders, Block& block) const {
const auto hash = co_await read_canonical_block_hash(tx_, number);
co_return co_await block_provider_(number, hash.bytes, read_senders, block);
co_return co_await providers_.block(number, hash.bytes, read_senders, block);
}

Task<std::optional<BlockHeader>> RemoteChainStorage::read_header(BlockNum number, HashAsArray hash) const {
Block block;
const bool success = co_await block_provider_(number, hash, /*.read_senders=*/false, block);
const bool success = co_await providers_.block(number, hash, /*.read_senders=*/false, block);
std::optional<BlockHeader> header;
if (success) {
header = std::move(block.header);
Expand All @@ -109,7 +101,7 @@ Task<std::vector<BlockHeader>> RemoteChainStorage::read_sibling_headers(BlockNum

Task<bool> RemoteChainStorage::read_body(BlockNum number, HashAsArray hash, bool read_senders, BlockBody& body) const {
Block block;
const bool success = co_await block_provider_(number, hash, read_senders, block);
const bool success = co_await providers_.block(number, hash, read_senders, block);
if (!success) {
co_return false;
}
Expand Down Expand Up @@ -140,7 +132,7 @@ Task<std::optional<BlockHeader>> RemoteChainStorage::read_canonical_header(Block
Task<bool> RemoteChainStorage::read_canonical_body(BlockNum number, BlockBody& body) const {
Block block;
const auto hash = co_await read_canonical_block_hash(tx_, number);
const bool success = co_await block_provider_(number, hash.bytes, /*.read_senders=*/false, block);
const bool success = co_await providers_.block(number, hash.bytes, /*.read_senders=*/false, block);
if (!success) {
co_return false;
}
Expand All @@ -152,7 +144,7 @@ Task<bool> RemoteChainStorage::read_canonical_body(BlockNum number, BlockBody& b

Task<bool> RemoteChainStorage::read_canonical_block(BlockNum number, Block& block) const {
const auto hash = co_await read_canonical_block_hash(tx_, number);
const bool success = co_await block_provider_(number, hash.bytes, /*.read_senders=*/false, block);
const bool success = co_await providers_.block(number, hash.bytes, /*.read_senders=*/false, block);
if (!success) {
co_return false;
}
Expand All @@ -171,7 +163,7 @@ Task<bool> RemoteChainStorage::has_body(BlockNum number, const Hash& hash) const

Task<bool> RemoteChainStorage::read_rlp_transactions(BlockNum number, const evmc::bytes32& hash, std::vector<Bytes>& rlp_txs) const {
Block block;
const bool success = co_await block_provider_(number, hash.bytes, /*.read_senders=*/false, block);
const bool success = co_await providers_.block(number, hash.bytes, /*.read_senders=*/false, block);
if (!success) {
co_return false;
}
Expand All @@ -191,7 +183,7 @@ Task<std::optional<intx::uint256>> RemoteChainStorage::read_total_difficulty(con
}

Task<std::optional<BlockNum>> RemoteChainStorage::read_block_number_by_transaction_hash(const evmc::bytes32& transaction_hash) const {
co_return co_await block_number_from_txn_hash_provider_(transaction_hash.bytes);
co_return co_await providers_.block_number_from_txn_hash(transaction_hash.bytes);
}

} // namespace silkworm::db::chain
20 changes: 10 additions & 10 deletions silkworm/db/chain/remote_chain_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ namespace silkworm::db::chain {
using BlockProvider = std::function<Task<bool>(BlockNum, HashAsSpan, bool, Block&)>;
using BlockNumberFromTxnHashProvider = std::function<Task<BlockNum>(HashAsSpan)>;
using BlockNumberFromBlockHashProvider = std::function<Task<BlockNum>(HashAsSpan)>;
using BlockHashFromBlockNumberProvider = std::function<Task<evmc::bytes32>(BlockNum)>;
using CanonicalBlockHashFromNumberProvider = std::function<Task<evmc::bytes32>(BlockNum)>;

struct Providers {
BlockProvider block;
BlockNumberFromTxnHashProvider block_number_from_txn_hash;
BlockNumberFromBlockHashProvider block_number_from_hash;
CanonicalBlockHashFromNumberProvider canonical_block_hash_from_number;
};

//! RemoteChainStorage must be used when blockchain data is remote with respect to the running component, i.e. it is
//! in remote database (accessed via gRPC KV I/F) or remote snapshot files (accessed via gRPC ETHBACKEND I/F)
class RemoteChainStorage : public ChainStorage {
public:
RemoteChainStorage(kv::api::Transaction& tx,
BlockProvider block_provider,
BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider,
BlockNumberFromBlockHashProvider block_number_from_block_hash_provider,
BlockHashFromBlockNumberProvider block_hash_from_number_provider);
RemoteChainStorage(kv::api::Transaction& tx, Providers providers);
~RemoteChainStorage() override = default;

[[nodiscard]] Task<ChainConfig> read_chain_config() const override;
Expand Down Expand Up @@ -82,10 +85,7 @@ class RemoteChainStorage : public ChainStorage {

private:
kv::api::Transaction& tx_;
BlockProvider block_provider_;
BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider_;
BlockNumberFromBlockHashProvider block_number_from_block_hash_provider_;
BlockHashFromBlockNumberProvider block_hash_from_number_provider_;
Providers providers_;
};

} // namespace silkworm::db::chain
2 changes: 1 addition & 1 deletion silkworm/db/chain/remote_chain_storage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static Bytes kChainConfig{*from_hex(

struct RemoteChainStorageTest : public silkworm::test_util::ContextTestBase {
test_util::MockTransaction transaction;
RemoteChainStorage storage{transaction, {}, {}, {}, {}};
RemoteChainStorage storage{transaction, Providers{}};
};

TEST_CASE_METHOD(RemoteChainStorageTest, "read_chain_config") {
Expand Down
50 changes: 10 additions & 40 deletions silkworm/db/kv/grpc/client/remote_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,23 @@ class RemoteClientImpl final : public api::Service {
RemoteClientImpl(const rpc::ChannelFactory& create_channel,
agrpc::GrpcContext& grpc_context,
api::StateCache* state_cache,
chain::BlockProvider block_provider,
chain::BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider,
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider,
chain::BlockHashFromBlockNumberProvider block_hash_from_number_provider,
chain::Providers providers,
rpc::DisconnectHook on_disconnect)
: channel_{create_channel()},
stub_{proto::KV::NewStub(channel_)},
grpc_context_{grpc_context},
state_cache_{state_cache},
block_provider_{std::move(block_provider)},
block_number_from_txn_hash_provider_{std::move(block_number_from_txn_hash_provider)},
block_number_from_block_hash_provider_{std::move(block_number_from_block_hash_provider)},
block_hash_from_number_provider_{std::move(block_hash_from_number_provider)},
providers_{std::move(providers)},
on_disconnect_{std::move(on_disconnect)} {}
RemoteClientImpl(std::unique_ptr<Stub> stub,
agrpc::GrpcContext& grpc_context,
api::StateCache* state_cache,
chain::BlockProvider block_provider,
chain::BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider,
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider,
chain::BlockHashFromBlockNumberProvider block_hash_from_number_provider,
chain::Providers providers,
rpc::DisconnectHook on_disconnect)
: stub_{std::move(stub)},
grpc_context_{grpc_context},
state_cache_{state_cache},
block_provider_{std::move(block_provider)},
block_number_from_txn_hash_provider_{std::move(block_number_from_txn_hash_provider)},
block_number_from_block_hash_provider_{std::move(block_number_from_block_hash_provider)},
block_hash_from_number_provider_{std::move(block_hash_from_number_provider)},
providers_{std::move(providers)},
on_disconnect_{std::move(on_disconnect)} {}

~RemoteClientImpl() override = default;
Expand All @@ -87,10 +75,7 @@ class RemoteClientImpl final : public api::Service {
auto tx = std::make_unique<RemoteTransaction>(*stub_,
grpc_context_,
state_cache_,
block_provider_,
block_number_from_txn_hash_provider_,
block_number_from_block_hash_provider_,
block_hash_from_number_provider_);
providers_);
co_await tx->open();
co_return tx;
}
Expand Down Expand Up @@ -159,10 +144,7 @@ class RemoteClientImpl final : public api::Service {
std::unique_ptr<Stub> stub_;
agrpc::GrpcContext& grpc_context_;
api::StateCache* state_cache_;
chain::BlockProvider block_provider_;
chain::BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider_;
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider_;
chain::BlockHashFromBlockNumberProvider block_hash_from_number_provider_;
chain::Providers providers_;
rpc::DisconnectHook on_disconnect_;
std::chrono::milliseconds min_backoff_timeout_{rpc::kDefaultMinBackoffReconnectTimeout};
std::chrono::milliseconds max_backoff_timeout_{rpc::kDefaultMaxBackoffReconnectTimeout};
Expand All @@ -171,35 +153,23 @@ class RemoteClientImpl final : public api::Service {
RemoteClient::RemoteClient(const rpc::ChannelFactory& create_channel,
agrpc::GrpcContext& grpc_context,
api::StateCache* state_cache,
chain::BlockProvider block_provider,
chain::BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider,
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider,
chain::BlockHashFromBlockNumberProvider block_hash_from_number_provider,
chain::Providers providers,
rpc::DisconnectHook on_disconnect)
: p_impl_{std::make_shared<RemoteClientImpl>(create_channel,
grpc_context,
state_cache,
std::move(block_provider),
std::move(block_number_from_txn_hash_provider),
std::move(block_number_from_block_hash_provider),
std::move(block_hash_from_number_provider),
std::move(providers),
std::move(on_disconnect))} {}

RemoteClient::RemoteClient(std::unique_ptr<Stub> stub,
agrpc::GrpcContext& grpc_context,
api::StateCache* state_cache,
chain::BlockProvider block_provider,
chain::BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider,
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider,
chain::BlockHashFromBlockNumberProvider block_hash_from_number_provider,
chain::Providers providers,
rpc::DisconnectHook on_disconnect)
: p_impl_{std::make_shared<RemoteClientImpl>(std::move(stub),
grpc_context,
state_cache,
std::move(block_provider),
std::move(block_number_from_txn_hash_provider),
std::move(block_number_from_block_hash_provider),
std::move(block_hash_from_number_provider),
std::move(providers),
std::move(on_disconnect))} {}

// Must be here (not in header) because RemoteClientImpl size is necessary for std::unique_ptr in PIMPL idiom
Expand Down
10 changes: 2 additions & 8 deletions silkworm/db/kv/grpc/client/remote_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,13 @@ struct RemoteClient : public api::Client {
const rpc::ChannelFactory& create_channel,
agrpc::GrpcContext& grpc_context,
api::StateCache* state_cache,
chain::BlockProvider block_provider,
chain::BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider,
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider,
chain::BlockHashFromBlockNumberProvider block_hash_from_number_provider,
chain::Providers providers,
std::function<Task<void>()> on_disconnect = []() -> Task<void> { co_return; });
RemoteClient(
std::unique_ptr<::remote::KV::StubInterface> stub,
agrpc::GrpcContext& grpc_context,
api::StateCache* state_cache,
chain::BlockProvider block_provider,
chain::BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider,
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider,
chain::BlockHashFromBlockNumberProvider block_hash_from_number_provider,
chain::Providers providers,
std::function<Task<void>()> on_disconnect = []() -> Task<void> { co_return; });
~RemoteClient() override;

Expand Down
10 changes: 5 additions & 5 deletions silkworm/db/kv/grpc/client/remote_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ struct RemoteClientTestRunner : public TestRunner<RemoteClient, StrictMockKVStub
[](HashAsSpan) -> Task<BlockNum> { co_return 0; }};
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider{
[](HashAsSpan) -> Task<BlockNum> { co_return 0; }};
chain::BlockHashFromBlockNumberProvider block_hash_from_block_number_provider{
chain::CanonicalBlockHashFromNumberProvider canonical_block_hash_from_number_provider{
[](BlockNum) -> Task<evmc::bytes32> { co_return 0; }};

protected:
RemoteClient make_api_client() override {
return RemoteClient{std::move(stub_),
grpc_context_,
state_cache.get(),
block_provider,
block_number_from_txn_hash_provider,
block_number_from_block_hash_provider,
block_hash_from_block_number_provider};
{block_provider,
block_number_from_txn_hash_provider,
block_number_from_block_hash_provider,
canonical_block_hash_from_number_provider}};
}
};

Expand Down
12 changes: 3 additions & 9 deletions silkworm/db/kv/grpc/client/remote_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@ RemoteTransaction::RemoteTransaction(
Stub& stub,
agrpc::GrpcContext& grpc_context,
api::StateCache* state_cache,
chain::BlockProvider block_provider,
chain::BlockNumberFromTxnHashProvider block_number_from_txn_hash_provider,
chain::BlockNumberFromBlockHashProvider block_number_from_block_hash_provider,
chain::BlockHashFromBlockNumberProvider block_hash_from_number_provider)
chain::Providers providers)
: BaseTransaction(state_cache),
block_provider_{std::move(block_provider)},
block_number_from_txn_hash_provider_{std::move(block_number_from_txn_hash_provider)},
block_number_from_block_hash_provider_{std::move(block_number_from_block_hash_provider)},
block_hash_from_number_provider_{std::move(block_hash_from_number_provider)},
providers_{std::move(providers)},
stub_{stub},
grpc_context_{grpc_context},
tx_rpc_{grpc_context_} {}
Expand Down Expand Up @@ -118,7 +112,7 @@ std::shared_ptr<silkworm::State> RemoteTransaction::create_state(boost::asio::an
}

std::shared_ptr<chain::ChainStorage> RemoteTransaction::create_storage() {
return std::make_shared<chain::RemoteChainStorage>(*this, block_provider_, block_number_from_txn_hash_provider_, block_number_from_block_hash_provider_, block_hash_from_number_provider_);
return std::make_shared<chain::RemoteChainStorage>(*this, providers_);
}

Task<api::DomainPointResult> RemoteTransaction::domain_get(api::DomainPointQuery&& query) { // NOLINT(*-rvalue-reference-param-not-moved)
Expand Down
Loading

0 comments on commit 522f009

Please sign in to comment.