Skip to content

Commit 2f01f8d

Browse files
authored
rpcdaemon: refactoring to use db::storage_prefix utility (#2116)
1 parent ee93a91 commit 2f01f8d

File tree

15 files changed

+173
-168
lines changed

15 files changed

+173
-168
lines changed

silkworm/db/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Bytes storage_prefix(ByteView address, uint64_t incarnation) {
3636
}
3737

3838
Bytes storage_prefix(const evmc::address& address, uint64_t incarnation) {
39-
return storage_prefix(ByteView{address.bytes}, incarnation);
39+
return storage_prefix(address.bytes, incarnation);
4040
}
4141

4242
Bytes block_key(BlockNum block_number) {

silkworm/db/util_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2024 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 "util.hpp"
18+
19+
#include <limits>
20+
21+
#include <catch2/catch_test_macros.hpp>
22+
23+
namespace silkworm::db {
24+
25+
using evmc::literals::operator""_address, evmc::literals::operator""_bytes32;
26+
27+
constexpr auto kZeroAddress = 0x0000000000000000000000000000000000000000_address;
28+
constexpr auto kZeroHash = 0x0000000000000000000000000000000000000000000000000000000000000000_bytes32;
29+
30+
TEST_CASE("all-zero storage prefix", "[core][util]") {
31+
const auto address_composite_key{storage_prefix(kZeroAddress, 0)};
32+
CHECK(address_composite_key == silkworm::Bytes(28, '\0'));
33+
34+
const auto location_composite_key{storage_prefix(kZeroHash.bytes, 0)};
35+
CHECK(location_composite_key == silkworm::Bytes(40, '\0'));
36+
}
37+
38+
} // namespace silkworm::db

silkworm/rpc/commands/parity_api.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ Task<void> ParityRpcApi::handle_parity_list_storage_keys(const nlohmann::json& r
9090
}
9191
const auto address = params[0].get<evmc::address>();
9292
const auto quantity = params[1].get<uint64_t>();
93-
std::optional<silkworm::Bytes> offset = std::nullopt;
93+
std::optional<Bytes> offset = std::nullopt;
9494
if (params.size() >= 3 && !params[2].is_null()) {
95-
offset = std::make_optional(params[2].get<silkworm::Bytes>());
95+
offset = std::make_optional(params[2].get<Bytes>());
9696
}
9797
std::string block_id = core::kLatestBlockId;
9898
if (params.size() >= 4) {
@@ -110,19 +110,19 @@ Task<void> ParityRpcApi::handle_parity_list_storage_keys(const nlohmann::json& r
110110

111111
const auto block_number = co_await core::get_block_number(block_id, *tx);
112112
SILK_DEBUG << "read account with address: " << address << " block number: " << block_number;
113-
std::optional<silkworm::Account> account = co_await state_reader.read_account(address, block_number);
113+
std::optional<Account> account = co_await state_reader.read_account(address, block_number);
114114
if (!account) throw std::domain_error{"account not found"};
115115

116-
silkworm::Bytes seek_bytes = silkworm::db::storage_prefix(full_view(address), account->incarnation);
116+
Bytes seek_bytes = db::storage_prefix(full_view(address), account->incarnation);
117117
const auto cursor = co_await tx->cursor_dup_sort(db::table::kPlainStateName);
118118
SILK_TRACE << "ParityRpcApi::handle_parity_list_storage_keys cursor id: " << cursor->cursor_id();
119-
silkworm::Bytes seek_val = offset ? offset.value() : silkworm::Bytes{};
119+
Bytes seek_val = offset ? offset.value() : Bytes{};
120120

121121
std::vector<evmc::bytes32> keys;
122122
auto v = co_await cursor->seek_both(seek_bytes, seek_val);
123123
// We look for keys until we have the quantity we want or the key is invalid/empty
124-
while (v.size() >= silkworm::kHashLength && keys.size() != quantity) {
125-
auto value = silkworm::bytes32_from_hex(silkworm::to_hex(v));
124+
while (v.size() >= kHashLength && keys.size() != quantity) {
125+
auto value = bytes32_from_hex(silkworm::to_hex(v));
126126
keys.push_back(value);
127127
const auto kv_pair = co_await cursor->next_dup();
128128

silkworm/rpc/core/account_dumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Task<void> AccountDumper::load_accounts(const std::vector<silkworm::KeyValue>& c
100100
dump_account.incarnation = account->incarnation;
101101

102102
if (account->incarnation > 0 && account->code_hash == silkworm::kEmptyHash) {
103-
const auto storage_key{silkworm::db::storage_prefix(full_view(address), account->incarnation)};
103+
const auto storage_key{db::storage_prefix(full_view(address), account->incarnation)};
104104
auto code_hash{co_await transaction_.get_one(db::table::kPlainCodeHashName, storage_key)};
105105
if (code_hash.length() == silkworm::kHashLength) {
106106
std::memcpy(dump_account.code_hash.bytes, code_hash.data(), silkworm::kHashLength);

silkworm/rpc/core/rawdb/chain.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <silkworm/db/util.hpp>
3030
#include <silkworm/infra/common/decoding_exception.hpp>
3131
#include <silkworm/infra/common/log.hpp>
32-
#include <silkworm/rpc/core/blocks.hpp>
3332

3433
namespace silkworm::rpc::core::rawdb {
3534

silkworm/rpc/core/rawdb/chain.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <nlohmann/json.hpp>
2626

2727
#include <silkworm/rpc/ethdb/transaction.hpp>
28-
#include <silkworm/rpc/types/chain_config.hpp>
2928

3029
namespace silkworm::rpc::core::rawdb {
3130

silkworm/rpc/core/rawdb/util.cpp

Lines changed: 0 additions & 44 deletions
This file was deleted.

silkworm/rpc/core/rawdb/util_test.cpp

Lines changed: 0 additions & 97 deletions
This file was deleted.

silkworm/rpc/core/state_reader.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <silkworm/infra/common/decoding_exception.hpp>
2626
#include <silkworm/infra/common/log.hpp>
2727
#include <silkworm/rpc/common/util.hpp>
28-
#include <silkworm/rpc/core/rawdb/util.hpp>
2928

3029
namespace silkworm::rpc {
3130

@@ -61,7 +60,7 @@ Task<evmc::bytes32> StateReader::read_storage(
6160
BlockNum block_number) const {
6261
std::optional<silkworm::Bytes> value{co_await read_historical_storage(address, incarnation, location_hash, block_number)};
6362
if (!value) {
64-
auto composite_key{silkworm::composite_storage_key_without_hash_lookup(address, incarnation)};
63+
const auto composite_key{db::storage_prefix(address, incarnation)};
6564
SILK_DEBUG << "StateReader::read_storage composite_key: " << composite_key;
6665
value = co_await tx_.get_both_range(db::table::kPlainStateName, composite_key, location_hash.bytes);
6766
SILK_DEBUG << "StateReader::read_storage value: " << (value ? *value : silkworm::Bytes{});

silkworm/rpc/ethdb/kv/state_cache.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#include <silkworm/infra/common/log.hpp>
2828
#include <silkworm/infra/grpc/common/conversion.hpp>
2929
#include <silkworm/rpc/common/util.hpp>
30-
#include <silkworm/rpc/core/rawdb/util.hpp>
30+
31+
#include "util.hpp"
3132

3233
namespace silkworm::rpc::ethdb::kv {
3334

silkworm/rpc/ethdb/kv/state_cache.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class StateView {
3939
public:
4040
virtual ~StateView() = default;
4141

42-
virtual Task<std::optional<silkworm::Bytes>> get(const silkworm::Bytes& key) = 0;
42+
virtual Task<std::optional<Bytes>> get(const Bytes& key) = 0;
4343

44-
virtual Task<std::optional<silkworm::Bytes>> get_code(const silkworm::Bytes& key) = 0;
44+
virtual Task<std::optional<Bytes>> get_code(const Bytes& key) = 0;
4545
};
4646

4747
class StateCache {
@@ -94,9 +94,9 @@ class CoherentStateView : public StateView {
9494
CoherentStateView(const CoherentStateView&) = delete;
9595
CoherentStateView& operator=(const CoherentStateView&) = delete;
9696

97-
Task<std::optional<silkworm::Bytes>> get(const silkworm::Bytes& key) override;
97+
Task<std::optional<Bytes>> get(const Bytes& key) override;
9898

99-
Task<std::optional<silkworm::Bytes>> get_code(const silkworm::Bytes& key) override;
99+
Task<std::optional<Bytes>> get_code(const Bytes& key) override;
100100

101101
private:
102102
Transaction& txn_;
@@ -135,8 +135,8 @@ class CoherentStateCache : public StateCache {
135135
void process_storage_change(CoherentStateRoot* root, StateViewId view_id, const remote::AccountChange& change);
136136
bool add(const KeyValue& kv, CoherentStateRoot* root, StateViewId view_id);
137137
bool add_code(const KeyValue& kv, CoherentStateRoot* root, StateViewId view_id);
138-
Task<std::optional<silkworm::Bytes>> get(const silkworm::Bytes& key, Transaction& txn);
139-
Task<std::optional<silkworm::Bytes>> get_code(const silkworm::Bytes& key, Transaction& txn);
138+
Task<std::optional<Bytes>> get(const Bytes& key, Transaction& txn);
139+
Task<std::optional<Bytes>> get_code(const Bytes& key, Transaction& txn);
140140
CoherentStateRoot* get_root(StateViewId view_id);
141141
CoherentStateRoot* advance_root(StateViewId view_id);
142142
void evict_roots(StateViewId next_view_id);

silkworm/rpc/ethdb/kv/state_cache_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
#include <silkworm/infra/grpc/common/conversion.hpp>
3636
#include <silkworm/infra/test_util/log.hpp>
3737
#include <silkworm/rpc/common/worker_pool.hpp>
38-
#include <silkworm/rpc/core/rawdb/util.hpp>
3938
#include <silkworm/rpc/test_util/dummy_transaction.hpp>
4039
#include <silkworm/rpc/test_util/mock_cursor.hpp>
4140
#include <silkworm/rpc/test_util/mock_transaction.hpp>
4241

42+
#include "util.hpp"
43+
4344
namespace silkworm::rpc::ethdb::kv {
4445

4546
using namespace evmc::literals; // NOLINT(build/namespaces_literals)

silkworm/rpc/ethdb/kv/util.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 "util.hpp"
18+
19+
#include <cstdlib>
20+
#include <cstring>
21+
22+
#include <silkworm/core/common/base.hpp>
23+
#include <silkworm/core/common/endian.hpp>
24+
#include <silkworm/db/util.hpp>
25+
26+
namespace silkworm::rpc::ethdb::kv {
27+
28+
silkworm::Bytes composite_storage_key(const evmc::address& address, uint64_t incarnation, HashAsArray hash) {
29+
silkworm::Bytes res(kAddressLength + db::kIncarnationLength + kHashLength, '\0');
30+
std::memcpy(&res[0], address.bytes, kAddressLength);
31+
endian::store_big_u64(&res[kAddressLength], incarnation);
32+
std::memcpy(&res[kAddressLength + db::kIncarnationLength], hash, kHashLength);
33+
return res;
34+
}
35+
36+
} // namespace silkworm::rpc::ethdb::kv

silkworm/rpc/core/rawdb/util.hpp renamed to silkworm/rpc/ethdb/kv/util.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020

2121
#include <evmc/evmc.hpp>
2222

23-
#include <silkworm/core/common/util.hpp>
23+
#include <silkworm/core/common/bytes.hpp>
24+
#include <silkworm/core/types/hash.hpp>
2425

25-
namespace silkworm {
26+
namespace silkworm::rpc::ethdb::kv {
2627

27-
silkworm::Bytes composite_storage_key(const evmc::address& address, uint64_t incarnation, const uint8_t (&hash)[silkworm::kHashLength]);
28+
Bytes composite_storage_key(const evmc::address& address, uint64_t incarnation, HashAsArray hash);
2829

29-
silkworm::Bytes composite_storage_key_without_hash_lookup(const evmc::address& address, uint64_t incarnation);
30-
31-
} // namespace silkworm
30+
} // namespace silkworm::rpc::ethdb::kv

0 commit comments

Comments
 (0)