Skip to content

Commit 60f8042

Browse files
authored
rpcdaemon: move transaction/cursor hierarchy to db (#2136)
1 parent 286187b commit 60f8042

File tree

190 files changed

+1673
-1534
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

190 files changed

+1673
-1534
lines changed

cmd/dev/grpc_toolbox.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@
3131

3232
#include <silkworm/core/common/util.hpp>
3333
#include <silkworm/core/types/address.hpp>
34+
#include <silkworm/db/kv/api/util.hpp>
3435
#include <silkworm/infra/common/log.hpp>
3536
#include <silkworm/infra/grpc/client/client_context_pool.hpp>
37+
#include <silkworm/infra/grpc/client/util.hpp>
3638
#include <silkworm/interfaces/remote/ethbackend.grpc.pb.h>
3739
#include <silkworm/interfaces/remote/kv.grpc.pb.h>
3840
#include <silkworm/interfaces/types/types.pb.h>
3941
#include <silkworm/rpc/common/constants.hpp>
4042
#include <silkworm/rpc/common/util.hpp>
4143
#include <silkworm/rpc/ethbackend/remote_backend.hpp>
42-
#include <silkworm/rpc/grpc/util.hpp>
4344

4445
using namespace silkworm;
4546
using namespace silkworm::rpc;

examples/get_latest_block.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <boost/asio/use_future.hpp>
2929
#include <grpcpp/grpcpp.h>
3030

31+
#include <silkworm/db/kv/api/state_cache.hpp>
3132
#include <silkworm/infra/common/log.hpp>
3233
#include <silkworm/infra/grpc/client/client_context_pool.hpp>
3334
#include <silkworm/rpc/common/constants.hpp>
@@ -36,6 +37,7 @@
3637
#include <silkworm/rpc/ethdb/kv/remote_database.hpp>
3738

3839
using namespace silkworm;
40+
using namespace silkworm::db;
3941
using namespace silkworm::rpc;
4042

4143
ABSL_FLAG(std::string, target, kDefaultPrivateApiAddr, "server location as string <address>:<port>");
@@ -93,7 +95,7 @@ int main(int argc, char* argv[]) {
9395
auto* io_context = context.io_context();
9496
auto* grpc_context = context.grpc_context();
9597

96-
ethdb::kv::CoherentStateCache state_cache;
98+
kv::api::CoherentStateCache state_cache;
9799
auto channel{::grpc::CreateChannel(target, ::grpc::InsecureChannelCredentials())};
98100
auto backend{std::make_unique<rpc::ethbackend::RemoteBackEnd>(*io_context, channel, *grpc_context)};
99101
auto database = std::make_unique<ethdb::kv::RemoteDatabase>(backend.get(), &state_cache, *grpc_context, channel);

silkworm/db/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ add_subdirectory(snapshots)
2222
add_subdirectory(test_util)
2323

2424
find_package(absl REQUIRED)
25+
find_package(asio-grpc REQUIRED)
26+
find_package(Boost REQUIRED headers)
27+
find_package(gRPC REQUIRED)
2528
find_package(magic_enum REQUIRED)
2629
find_package(Microsoft.GSL REQUIRED)
2730
find_package(nlohmann_json REQUIRED)
31+
find_package(Protobuf REQUIRED)
2832

2933
set(LIBS_PUBLIC
3034
absl::btree
@@ -41,6 +45,10 @@ set(LIBS_PRIVATE
4145
magic_enum::magic_enum
4246
Microsoft.GSL::GSL
4347
nlohmann_json::nlohmann_json
48+
asio-grpc::asio-grpc
49+
Boost::headers
50+
gRPC::grpc++
51+
protobuf::libprotobuf
4452
silkworm_db_etl
4553
silkworm_infra
4654
silkworm_snapshots
@@ -53,4 +61,4 @@ silkworm_library(
5361
PRIVATE ${LIBS_PRIVATE}
5462
)
5563

56-
target_link_libraries(silkworm_db_test PRIVATE silkworm_db_test_util)
64+
target_link_libraries(silkworm_db_test PRIVATE silkworm_db_test_util silkworm_infra_test_util)

silkworm/db/access_layer_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <silkworm/db/test_util/temp_chain_data.hpp>
3131
#include <silkworm/infra/test_util/log.hpp>
3232

33-
#include "test_util/mock_cursor.hpp"
33+
#include "test_util/mock_ro_cursor.hpp"
3434
#include "test_util/mock_txn.hpp"
3535

3636
namespace silkworm {

silkworm/db/chain/chain.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2023 The Silkworm Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#include "chain.hpp"
18+
19+
#include <string>
20+
#include <utility>
21+
22+
#include <silkworm/core/common/endian.hpp>
23+
#include <silkworm/core/common/util.hpp>
24+
#include <silkworm/core/rlp/decode.hpp>
25+
#include <silkworm/core/types/address.hpp>
26+
#include <silkworm/core/types/evmc_bytes32.hpp>
27+
#include <silkworm/db/kv/api/util.hpp>
28+
#include <silkworm/db/tables.hpp>
29+
#include <silkworm/db/util.hpp>
30+
#include <silkworm/infra/common/log.hpp>
31+
32+
namespace silkworm::db::chain {
33+
34+
Task<uint64_t> read_header_number(kv::api::Transaction& tx, const evmc::bytes32& block_hash) {
35+
const ByteView block_hash_bytes{block_hash.bytes, kHashLength};
36+
const auto value{co_await tx.get_one(table::kHeaderNumbersName, block_hash_bytes)};
37+
if (value.empty()) {
38+
throw std::invalid_argument{"empty block number value in read_header_number"};
39+
}
40+
co_return endian::load_big_u64(value.data());
41+
}
42+
43+
Task<evmc::bytes32> read_canonical_block_hash(kv::api::Transaction& tx, uint64_t block_number) {
44+
const auto block_key = db::block_key(block_number);
45+
SILK_TRACE << "read_canonical_block_hash block_key: " << to_hex(block_key);
46+
const auto value{co_await tx.get_one(table::kCanonicalHashesName, block_key)};
47+
if (value.empty()) {
48+
throw std::invalid_argument{"empty block hash value in read_canonical_block_hash"};
49+
}
50+
const auto canonical_block_hash{to_bytes32(value)};
51+
SILK_DEBUG << "read_canonical_block_hash canonical block hash: " << to_hex(canonical_block_hash);
52+
co_return canonical_block_hash;
53+
}
54+
55+
Task<intx::uint256> read_total_difficulty(kv::api::Transaction& tx, const evmc::bytes32& block_hash, uint64_t block_number) {
56+
const auto block_key = db::block_key(block_number, block_hash.bytes);
57+
SILK_TRACE << "read_total_difficulty block_key: " << to_hex(block_key);
58+
const auto result{co_await tx.get_one(table::kDifficultyName, block_key)};
59+
if (result.empty()) {
60+
throw std::invalid_argument{"empty total difficulty value in read_total_difficulty"};
61+
}
62+
ByteView value{result};
63+
intx::uint256 total_difficulty{0};
64+
auto decoding_result{rlp::decode(value, total_difficulty)};
65+
if (!decoding_result) {
66+
throw std::runtime_error{"cannot RLP-decode total difficulty value in read_total_difficulty"};
67+
}
68+
SILK_DEBUG << "read_total_difficulty canonical total difficulty: " << total_difficulty;
69+
co_return total_difficulty;
70+
}
71+
72+
Task<evmc::bytes32> read_head_header_hash(kv::api::Transaction& tx) {
73+
const Bytes kHeadHeaderKey = bytes_of_string(table::kHeadHeaderName);
74+
const auto value = co_await tx.get_one(table::kHeadHeaderName, kHeadHeaderKey);
75+
if (value.empty()) {
76+
throw std::runtime_error{"empty head header hash value in read_head_header_hash"};
77+
}
78+
const auto head_header_hash{to_bytes32(value)};
79+
SILK_DEBUG << "head header hash: " << to_hex(head_header_hash);
80+
co_return head_header_hash;
81+
}
82+
83+
} // namespace silkworm::db::chain

silkworm/rpc/core/rawdb/chain.hpp renamed to silkworm/db/chain/chain.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@
2424
#include <intx/intx.hpp>
2525
#include <nlohmann/json.hpp>
2626

27-
#include <silkworm/rpc/ethdb/transaction.hpp>
27+
#include <silkworm/db/kv/api/transaction.hpp>
2828

29-
namespace silkworm::rpc::core::rawdb {
29+
namespace silkworm::db::chain {
3030

3131
// TODO(canepat) BlockReader or migrate to ChainStorage?
3232

3333
using Transactions = std::vector<silkworm::Transaction>;
3434

35-
Task<uint64_t> read_header_number(ethdb::Transaction& tx, const evmc::bytes32& block_hash);
35+
Task<uint64_t> read_header_number(kv::api::Transaction& tx, const evmc::bytes32& block_hash);
3636

37-
Task<evmc::bytes32> read_canonical_block_hash(ethdb::Transaction& tx, BlockNum block_number);
37+
Task<evmc::bytes32> read_canonical_block_hash(kv::api::Transaction& tx, BlockNum block_number);
3838

39-
Task<intx::uint256> read_total_difficulty(ethdb::Transaction& tx, const evmc::bytes32& block_hash, BlockNum block_number);
39+
Task<intx::uint256> read_total_difficulty(kv::api::Transaction& tx, const evmc::bytes32& block_hash, BlockNum block_number);
4040

41-
Task<evmc::bytes32> read_head_header_hash(ethdb::Transaction& tx);
41+
Task<evmc::bytes32> read_head_header_hash(kv::api::Transaction& tx);
4242

43-
} // namespace silkworm::rpc::core::rawdb
43+
} // namespace silkworm::db::chain

silkworm/rpc/storage/chain_storage.hpp renamed to silkworm/db/chain/chain_storage.hpp

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <silkworm/core/common/bytes.hpp>
2323
#include <silkworm/core/types/block.hpp>
2424

25-
namespace silkworm::rpc {
25+
namespace silkworm::db::chain {
2626

2727
//! ChainStorage represents the storage for blockchain primary data, namely: chain configuration, block headers,
2828
//! bodies and transactions.
@@ -40,12 +40,12 @@ class ChainStorage {
4040
[[nodiscard]] virtual Task<std::optional<BlockNum>> read_block_number(const Hash& hash) const = 0;
4141

4242
//! Read block returning true on success and false on missing block
43-
virtual Task<bool> read_block(HashAsSpan hash, BlockNum number, bool read_senders, silkworm::Block& block) const = 0;
44-
virtual Task<bool> read_block(const Hash& hash, BlockNum number, silkworm::Block& block) const = 0;
45-
virtual Task<bool> read_block(const Hash& hash, silkworm::Block& block) const = 0;
43+
virtual Task<bool> read_block(HashAsSpan hash, BlockNum number, bool read_senders, Block& block) const = 0;
44+
virtual Task<bool> read_block(const Hash& hash, BlockNum number, Block& block) const = 0;
45+
virtual Task<bool> read_block(const Hash& hash, Block& block) const = 0;
4646

4747
//! Read canonical block by number returning true on success and false on missing block
48-
virtual Task<bool> read_block(BlockNum number, bool read_senders, silkworm::Block& block) const = 0;
48+
virtual Task<bool> read_block(BlockNum number, bool read_senders, Block& block) const = 0;
4949

5050
//! Read block header with the specified key (block number, hash)
5151
[[nodiscard]] virtual Task<std::optional<BlockHeader>> read_header(BlockNum number, HashAsArray hash) const = 0;
@@ -60,9 +60,9 @@ class ChainStorage {
6060
[[nodiscard]] virtual Task<std::vector<BlockHeader>> read_sibling_headers(BlockNum number) const = 0;
6161

6262
//! Read block body in output parameter returning true on success and false on missing block
63-
virtual Task<bool> read_body(BlockNum number, HashAsArray hash, bool read_senders, silkworm::BlockBody& body) const = 0;
64-
virtual Task<bool> read_body(const Hash& hash, BlockNum number, silkworm::BlockBody& body) const = 0;
65-
virtual Task<bool> read_body(const Hash& hash, silkworm::BlockBody& body) const = 0;
63+
virtual Task<bool> read_body(BlockNum number, HashAsArray hash, bool read_senders, BlockBody& body) const = 0;
64+
virtual Task<bool> read_body(const Hash& hash, BlockNum number, BlockBody& body) const = 0;
65+
virtual Task<bool> read_body(const Hash& hash, BlockBody& body) const = 0;
6666

6767
//! Read the canonical block hash at specified height
6868
[[nodiscard]] virtual Task<std::optional<Hash>> read_canonical_hash(BlockNum number) const = 0;
@@ -74,7 +74,7 @@ class ChainStorage {
7474
virtual Task<bool> read_canonical_body(BlockNum height, BlockBody& body) const = 0;
7575

7676
//! Read the canonical block at specified height
77-
virtual Task<bool> read_canonical_block(BlockNum height, silkworm::Block& block) const = 0;
77+
virtual Task<bool> read_canonical_block(BlockNum height, Block& block) const = 0;
7878

7979
//! Check the presence of a block body using block number and hash
8080
[[nodiscard]] virtual Task<bool> has_body(BlockNum number, HashAsArray hash) const = 0;
@@ -89,26 +89,6 @@ class ChainStorage {
8989
[[nodiscard]] virtual Task<std::optional<intx::uint256>> read_total_difficulty(const Hash& block_hash, BlockNum block_number) const = 0;
9090

9191
virtual Task<std::optional<BlockNum>> read_block_number_by_transaction_hash(const evmc::bytes32& transaction_hash) const = 0;
92-
93-
// Task<silkworm::BlockHeader> read_current_header();
94-
95-
// Task<evmc::bytes32> read_head_header_hash();
96-
97-
// Task<uint64_t> read_cumulative_transaction_count(BlockNum block_number);
98-
99-
// Task<silkworm::Bytes> read_header_rlp(const evmc::bytes32& block_hash, BlockNum block_number);
100-
101-
// Task<silkworm::Bytes> read_body_rlp(const evmc::bytes32& block_hash, BlockNum block_number);
102-
103-
// Task<Addresses> read_senders(const evmc::bytes32& block_hash, BlockNum block_number);
104-
105-
// Task<Receipts> read_raw_receipts(BlockNum block_number);
106-
107-
// Task<Receipts> read_receipts(const silkworm::BlockWithHash& block_with_hash);
108-
109-
// Task<Transactions> read_canonical_transactions(uint64_t base_txn_id, uint64_t txn_count);
110-
111-
// Task<Transactions> read_noncanonical_transactions(uint64_t base_txn_id, uint64_t txn_count);
11292
};
11393

114-
} // namespace silkworm::rpc
94+
} // namespace silkworm::db::chain

0 commit comments

Comments
 (0)