Skip to content

Commit

Permalink
rpcdaemon: refactoring to separate SplitCursor from Cursor (#2125)
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat authored Jun 19, 2024
1 parent 2f01f8d commit 1edca39
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 78 deletions.
11 changes: 9 additions & 2 deletions silkworm/db/util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ constexpr auto kZeroHash = 0x000000000000000000000000000000000000000000000000000

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

const auto location_composite_key{storage_prefix(kZeroHash.bytes, 0)};
CHECK(location_composite_key == silkworm::Bytes(40, '\0'));
CHECK(location_composite_key == Bytes(40, '\0'));
}

TEST_CASE("non-zero storage prefix for address and incarnation", "[core][util]") {
const evmc::address address{0x79a4d418f7887dd4d5123a41b6c8c186686ae8cb_address};
const uint64_t incarnation{1};
const auto address_composite_key{storage_prefix(address, incarnation)};
CHECK(to_hex(address_composite_key) == "79a4d418f7887dd4d5123a41b6c8c186686ae8cb0000000000000001");
}

} // namespace silkworm::db
2 changes: 1 addition & 1 deletion silkworm/rpc/core/account_walker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include <silkworm/core/common/util.hpp>
#include <silkworm/core/types/account.hpp>
#include <silkworm/rpc/common/util.hpp>
#include <silkworm/rpc/ethdb/cursor.hpp>
#include <silkworm/rpc/ethdb/database.hpp>
#include <silkworm/rpc/ethdb/split_cursor.hpp>
#include <silkworm/rpc/types/block.hpp>

namespace silkworm::rpc {
Expand Down
11 changes: 2 additions & 9 deletions silkworm/rpc/core/storage_walker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <silkworm/infra/common/decoding_exception.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/rpc/common/util.hpp>
#include <silkworm/rpc/ethdb/cursor.hpp>
#include <silkworm/rpc/ethdb/split_cursor.hpp>

namespace silkworm::rpc {

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

silkworm::Bytes make_key(const evmc::address& address, uint64_t incarnation) {
silkworm::Bytes res(silkworm::kAddressLength + 8, '\0');
std::memcpy(&res[0], address.bytes, silkworm::kAddressLength);
endian::store_big_u64(&res[silkworm::kAddressLength], incarnation);
return res;
}

struct StorageItem {
silkworm::Bytes key;
silkworm::Bytes sec_key;
Expand Down Expand Up @@ -95,7 +88,7 @@ Task<void> StorageWalker::walk_of_storages(
SILK_TRACE << "block_number=" << block_number << " address=" << address << " START";

auto ps_cursor = co_await transaction_.cursor_dup_sort(db::table::kPlainStateName);
auto ps_key{make_key(address, incarnation)};
auto ps_key{db::storage_prefix(address, incarnation)};
ethdb::SplitCursorDupSort ps_split_cursor{*ps_cursor,
ps_key,
start_location.bytes, /* subkey */
Expand Down
1 change: 0 additions & 1 deletion silkworm/rpc/core/storage_walker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
namespace silkworm::rpc {

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

class StorageWalker {
public:
Expand Down
8 changes: 0 additions & 8 deletions silkworm/rpc/core/storage_walker_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,4 @@ TEST_CASE("make key for address and location") {
CHECK(silkworm::to_hex(key) == "79a4d418f7887dd4d5123a41b6c8c186686ae8cb56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421");
}

TEST_CASE("make key for address, incarnation ") {
evmc::address address = 0x79a4d418f7887dd4d5123a41b6c8c186686ae8cb_address;
uint64_t incarnation = 1;

auto key = make_key(address, incarnation);
CHECK(silkworm::to_hex(key) == "79a4d418f7887dd4d5123a41b6c8c186686ae8cb0000000000000001");
}

} // namespace silkworm::rpc
55 changes: 0 additions & 55 deletions silkworm/rpc/ethdb/cursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,59 +58,4 @@ class CursorDupSort : public Cursor {
virtual Task<KeyValue> next_dup() = 0;
};

struct SplittedKeyValue {
silkworm::Bytes key1;
silkworm::Bytes key2;
silkworm::Bytes key3;
silkworm::Bytes value;
};

class SplitCursor {
public:
SplitCursor(Cursor& inner_cursor, silkworm::ByteView key, uint64_t match_bits, uint64_t part1_end, uint64_t part2_start, uint64_t part3_start);
SplitCursor& operator=(const SplitCursor&) = delete;

Task<SplittedKeyValue> seek();

Task<SplittedKeyValue> next();

private:
Cursor& inner_cursor_;
silkworm::Bytes key_;
silkworm::Bytes first_bytes_;
uint8_t last_bits_;
uint64_t part1_end_;
uint64_t part2_start_;
uint64_t part3_start_;
uint64_t match_bytes_;
uint8_t mask_;

bool match_key(const silkworm::ByteView& key);
SplittedKeyValue split_key_value(const KeyValue& kv);
};

class SplitCursorDupSort {
public:
SplitCursorDupSort(CursorDupSort& inner_cursor, silkworm::ByteView key, silkworm::ByteView subkey, uint64_t match_bits, uint64_t part1_end, uint64_t value_offset);
SplitCursorDupSort& operator=(const SplitCursorDupSort&) = delete;

Task<SplittedKeyValue> seek_both();

Task<SplittedKeyValue> next_dup();

private:
CursorDupSort& inner_cursor_;
silkworm::Bytes key_;
silkworm::Bytes subkey_;
silkworm::Bytes first_bytes_;
uint8_t last_bits_;
uint64_t part1_end_;
uint64_t match_bytes_;
uint8_t mask_;
uint64_t value_offset_;

bool match_key(const silkworm::ByteView& key);
SplittedKeyValue split_key_value(const KeyValue& kv);
};

} // namespace silkworm::rpc::ethdb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

#include "cursor.hpp"
#include "split_cursor.hpp"

namespace silkworm::rpc::ethdb {

Expand Down
86 changes: 86 additions & 0 deletions silkworm/rpc/ethdb/split_cursor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2024 The Silkworm Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <memory>
#include <string>

#include <silkworm/infra/concurrency/task.hpp>

#include <silkworm/core/common/util.hpp>
#include <silkworm/rpc/common/util.hpp>

#include "cursor.hpp"

namespace silkworm::rpc::ethdb {

struct SplittedKeyValue {
Bytes key1;
Bytes key2;
Bytes key3;
Bytes value;
};

class SplitCursor {
public:
SplitCursor(Cursor& inner_cursor, ByteView key, uint64_t match_bits, uint64_t part1_end, uint64_t part2_start, uint64_t part3_start);
SplitCursor& operator=(const SplitCursor&) = delete;

Task<SplittedKeyValue> seek();

Task<SplittedKeyValue> next();

private:
Cursor& inner_cursor_;
Bytes key_;
Bytes first_bytes_;
uint8_t last_bits_;
uint64_t part1_end_;
uint64_t part2_start_;
uint64_t part3_start_;
uint64_t match_bytes_;
uint8_t mask_;

bool match_key(const ByteView& key);
SplittedKeyValue split_key_value(const KeyValue& kv);
};

class SplitCursorDupSort {
public:
SplitCursorDupSort(CursorDupSort& inner_cursor, ByteView key, ByteView subkey, uint64_t match_bits, uint64_t part1_end, uint64_t value_offset);
SplitCursorDupSort& operator=(const SplitCursorDupSort&) = delete;

Task<SplittedKeyValue> seek_both();

Task<SplittedKeyValue> next_dup();

private:
CursorDupSort& inner_cursor_;
silkworm::Bytes key_;
silkworm::Bytes subkey_;
silkworm::Bytes first_bytes_;
uint8_t last_bits_;
uint64_t part1_end_;
uint64_t match_bytes_;
uint8_t mask_;
uint64_t value_offset_;

bool match_key(const silkworm::ByteView& key);
SplittedKeyValue split_key_value(const KeyValue& kv);
};

} // namespace silkworm::rpc::ethdb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

#include "cursor.hpp"
#include "split_cursor.hpp"

#include <string>

Expand Down

0 comments on commit 1edca39

Please sign in to comment.