Skip to content

Commit 1edca39

Browse files
authored
rpcdaemon: refactoring to separate SplitCursor from Cursor (#2125)
1 parent 2f01f8d commit 1edca39

File tree

9 files changed

+100
-78
lines changed

9 files changed

+100
-78
lines changed

silkworm/db/util_test.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ constexpr auto kZeroHash = 0x000000000000000000000000000000000000000000000000000
2929

3030
TEST_CASE("all-zero storage prefix", "[core][util]") {
3131
const auto address_composite_key{storage_prefix(kZeroAddress, 0)};
32-
CHECK(address_composite_key == silkworm::Bytes(28, '\0'));
32+
CHECK(address_composite_key == Bytes(28, '\0'));
3333

3434
const auto location_composite_key{storage_prefix(kZeroHash.bytes, 0)};
35-
CHECK(location_composite_key == silkworm::Bytes(40, '\0'));
35+
CHECK(location_composite_key == Bytes(40, '\0'));
36+
}
37+
38+
TEST_CASE("non-zero storage prefix for address and incarnation", "[core][util]") {
39+
const evmc::address address{0x79a4d418f7887dd4d5123a41b6c8c186686ae8cb_address};
40+
const uint64_t incarnation{1};
41+
const auto address_composite_key{storage_prefix(address, incarnation)};
42+
CHECK(to_hex(address_composite_key) == "79a4d418f7887dd4d5123a41b6c8c186686ae8cb0000000000000001");
3643
}
3744

3845
} // namespace silkworm::db

silkworm/rpc/core/account_walker.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
#include <silkworm/core/common/util.hpp>
2828
#include <silkworm/core/types/account.hpp>
2929
#include <silkworm/rpc/common/util.hpp>
30-
#include <silkworm/rpc/ethdb/cursor.hpp>
3130
#include <silkworm/rpc/ethdb/database.hpp>
31+
#include <silkworm/rpc/ethdb/split_cursor.hpp>
3232
#include <silkworm/rpc/types/block.hpp>
3333

3434
namespace silkworm::rpc {

silkworm/rpc/core/storage_walker.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <silkworm/infra/common/decoding_exception.hpp>
2828
#include <silkworm/infra/common/log.hpp>
2929
#include <silkworm/rpc/common/util.hpp>
30-
#include <silkworm/rpc/ethdb/cursor.hpp>
30+
#include <silkworm/rpc/ethdb/split_cursor.hpp>
3131

3232
namespace silkworm::rpc {
3333

@@ -38,13 +38,6 @@ silkworm::Bytes make_key(const evmc::address& address, const evmc::bytes32& loca
3838
return res;
3939
}
4040

41-
silkworm::Bytes make_key(const evmc::address& address, uint64_t incarnation) {
42-
silkworm::Bytes res(silkworm::kAddressLength + 8, '\0');
43-
std::memcpy(&res[0], address.bytes, silkworm::kAddressLength);
44-
endian::store_big_u64(&res[silkworm::kAddressLength], incarnation);
45-
return res;
46-
}
47-
4841
struct StorageItem {
4942
silkworm::Bytes key;
5043
silkworm::Bytes sec_key;
@@ -95,7 +88,7 @@ Task<void> StorageWalker::walk_of_storages(
9588
SILK_TRACE << "block_number=" << block_number << " address=" << address << " START";
9689

9790
auto ps_cursor = co_await transaction_.cursor_dup_sort(db::table::kPlainStateName);
98-
auto ps_key{make_key(address, incarnation)};
91+
auto ps_key{db::storage_prefix(address, incarnation)};
9992
ethdb::SplitCursorDupSort ps_split_cursor{*ps_cursor,
10093
ps_key,
10194
start_location.bytes, /* subkey */

silkworm/rpc/core/storage_walker.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
namespace silkworm::rpc {
3535

3636
silkworm::Bytes make_key(const evmc::address& address, const evmc::bytes32& location);
37-
silkworm::Bytes make_key(const evmc::address& address, uint64_t incarnation);
3837

3938
class StorageWalker {
4039
public:

silkworm/rpc/core/storage_walker_test.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,4 @@ TEST_CASE("make key for address and location") {
434434
CHECK(silkworm::to_hex(key) == "79a4d418f7887dd4d5123a41b6c8c186686ae8cb56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421");
435435
}
436436

437-
TEST_CASE("make key for address, incarnation ") {
438-
evmc::address address = 0x79a4d418f7887dd4d5123a41b6c8c186686ae8cb_address;
439-
uint64_t incarnation = 1;
440-
441-
auto key = make_key(address, incarnation);
442-
CHECK(silkworm::to_hex(key) == "79a4d418f7887dd4d5123a41b6c8c186686ae8cb0000000000000001");
443-
}
444-
445437
} // namespace silkworm::rpc

silkworm/rpc/ethdb/cursor.hpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -58,59 +58,4 @@ class CursorDupSort : public Cursor {
5858
virtual Task<KeyValue> next_dup() = 0;
5959
};
6060

61-
struct SplittedKeyValue {
62-
silkworm::Bytes key1;
63-
silkworm::Bytes key2;
64-
silkworm::Bytes key3;
65-
silkworm::Bytes value;
66-
};
67-
68-
class SplitCursor {
69-
public:
70-
SplitCursor(Cursor& inner_cursor, silkworm::ByteView key, uint64_t match_bits, uint64_t part1_end, uint64_t part2_start, uint64_t part3_start);
71-
SplitCursor& operator=(const SplitCursor&) = delete;
72-
73-
Task<SplittedKeyValue> seek();
74-
75-
Task<SplittedKeyValue> next();
76-
77-
private:
78-
Cursor& inner_cursor_;
79-
silkworm::Bytes key_;
80-
silkworm::Bytes first_bytes_;
81-
uint8_t last_bits_;
82-
uint64_t part1_end_;
83-
uint64_t part2_start_;
84-
uint64_t part3_start_;
85-
uint64_t match_bytes_;
86-
uint8_t mask_;
87-
88-
bool match_key(const silkworm::ByteView& key);
89-
SplittedKeyValue split_key_value(const KeyValue& kv);
90-
};
91-
92-
class SplitCursorDupSort {
93-
public:
94-
SplitCursorDupSort(CursorDupSort& inner_cursor, silkworm::ByteView key, silkworm::ByteView subkey, uint64_t match_bits, uint64_t part1_end, uint64_t value_offset);
95-
SplitCursorDupSort& operator=(const SplitCursorDupSort&) = delete;
96-
97-
Task<SplittedKeyValue> seek_both();
98-
99-
Task<SplittedKeyValue> next_dup();
100-
101-
private:
102-
CursorDupSort& inner_cursor_;
103-
silkworm::Bytes key_;
104-
silkworm::Bytes subkey_;
105-
silkworm::Bytes first_bytes_;
106-
uint8_t last_bits_;
107-
uint64_t part1_end_;
108-
uint64_t match_bytes_;
109-
uint8_t mask_;
110-
uint64_t value_offset_;
111-
112-
bool match_key(const silkworm::ByteView& key);
113-
SplittedKeyValue split_key_value(const KeyValue& kv);
114-
};
115-
11661
} // namespace silkworm::rpc::ethdb

silkworm/rpc/ethdb/cursor.cpp renamed to silkworm/rpc/ethdb/split_cursor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
#include "cursor.hpp"
17+
#include "split_cursor.hpp"
1818

1919
namespace silkworm::rpc::ethdb {
2020

silkworm/rpc/ethdb/split_cursor.hpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
#pragma once
18+
19+
#include <memory>
20+
#include <string>
21+
22+
#include <silkworm/infra/concurrency/task.hpp>
23+
24+
#include <silkworm/core/common/util.hpp>
25+
#include <silkworm/rpc/common/util.hpp>
26+
27+
#include "cursor.hpp"
28+
29+
namespace silkworm::rpc::ethdb {
30+
31+
struct SplittedKeyValue {
32+
Bytes key1;
33+
Bytes key2;
34+
Bytes key3;
35+
Bytes value;
36+
};
37+
38+
class SplitCursor {
39+
public:
40+
SplitCursor(Cursor& inner_cursor, ByteView key, uint64_t match_bits, uint64_t part1_end, uint64_t part2_start, uint64_t part3_start);
41+
SplitCursor& operator=(const SplitCursor&) = delete;
42+
43+
Task<SplittedKeyValue> seek();
44+
45+
Task<SplittedKeyValue> next();
46+
47+
private:
48+
Cursor& inner_cursor_;
49+
Bytes key_;
50+
Bytes first_bytes_;
51+
uint8_t last_bits_;
52+
uint64_t part1_end_;
53+
uint64_t part2_start_;
54+
uint64_t part3_start_;
55+
uint64_t match_bytes_;
56+
uint8_t mask_;
57+
58+
bool match_key(const ByteView& key);
59+
SplittedKeyValue split_key_value(const KeyValue& kv);
60+
};
61+
62+
class SplitCursorDupSort {
63+
public:
64+
SplitCursorDupSort(CursorDupSort& inner_cursor, ByteView key, ByteView subkey, uint64_t match_bits, uint64_t part1_end, uint64_t value_offset);
65+
SplitCursorDupSort& operator=(const SplitCursorDupSort&) = delete;
66+
67+
Task<SplittedKeyValue> seek_both();
68+
69+
Task<SplittedKeyValue> next_dup();
70+
71+
private:
72+
CursorDupSort& inner_cursor_;
73+
silkworm::Bytes key_;
74+
silkworm::Bytes subkey_;
75+
silkworm::Bytes first_bytes_;
76+
uint8_t last_bits_;
77+
uint64_t part1_end_;
78+
uint64_t match_bytes_;
79+
uint8_t mask_;
80+
uint64_t value_offset_;
81+
82+
bool match_key(const silkworm::ByteView& key);
83+
SplittedKeyValue split_key_value(const KeyValue& kv);
84+
};
85+
86+
} // namespace silkworm::rpc::ethdb

silkworm/rpc/ethdb/cursor_test.cpp renamed to silkworm/rpc/ethdb/split_cursor_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
#include "cursor.hpp"
17+
#include "split_cursor.hpp"
1818

1919
#include <string>
2020

0 commit comments

Comments
 (0)