Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpcdaemon: refactoring to use db::storage_prefix utility #2116

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion silkworm/db/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Bytes storage_prefix(ByteView address, uint64_t incarnation) {
}

Bytes storage_prefix(const evmc::address& address, uint64_t incarnation) {
return storage_prefix(ByteView{address.bytes}, incarnation);
return storage_prefix(address.bytes, incarnation);
}

Bytes block_key(BlockNum block_number) {
Expand Down
38 changes: 38 additions & 0 deletions silkworm/db/util_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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.
*/

#include "util.hpp"

#include <limits>

#include <catch2/catch_test_macros.hpp>

namespace silkworm::db {

using evmc::literals::operator""_address, evmc::literals::operator""_bytes32;

constexpr auto kZeroAddress = 0x0000000000000000000000000000000000000000_address;
constexpr auto kZeroHash = 0x0000000000000000000000000000000000000000000000000000000000000000_bytes32;

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'));

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

} // namespace silkworm::db
14 changes: 7 additions & 7 deletions silkworm/rpc/commands/parity_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ Task<void> ParityRpcApi::handle_parity_list_storage_keys(const nlohmann::json& r
}
const auto address = params[0].get<evmc::address>();
const auto quantity = params[1].get<uint64_t>();
std::optional<silkworm::Bytes> offset = std::nullopt;
std::optional<Bytes> offset = std::nullopt;
if (params.size() >= 3 && !params[2].is_null()) {
offset = std::make_optional(params[2].get<silkworm::Bytes>());
offset = std::make_optional(params[2].get<Bytes>());
}
std::string block_id = core::kLatestBlockId;
if (params.size() >= 4) {
Expand All @@ -110,19 +110,19 @@ Task<void> ParityRpcApi::handle_parity_list_storage_keys(const nlohmann::json& r

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion silkworm/rpc/core/account_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Task<void> AccountDumper::load_accounts(const std::vector<silkworm::KeyValue>& c
dump_account.incarnation = account->incarnation;

if (account->incarnation > 0 && account->code_hash == silkworm::kEmptyHash) {
const auto storage_key{silkworm::db::storage_prefix(full_view(address), account->incarnation)};
const auto storage_key{db::storage_prefix(full_view(address), account->incarnation)};
auto code_hash{co_await transaction_.get_one(db::table::kPlainCodeHashName, storage_key)};
if (code_hash.length() == silkworm::kHashLength) {
std::memcpy(dump_account.code_hash.bytes, code_hash.data(), silkworm::kHashLength);
Expand Down
1 change: 0 additions & 1 deletion silkworm/rpc/core/rawdb/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <silkworm/db/util.hpp>
#include <silkworm/infra/common/decoding_exception.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/rpc/core/blocks.hpp>

namespace silkworm::rpc::core::rawdb {

Expand Down
1 change: 0 additions & 1 deletion silkworm/rpc/core/rawdb/chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <nlohmann/json.hpp>

#include <silkworm/rpc/ethdb/transaction.hpp>
#include <silkworm/rpc/types/chain_config.hpp>

namespace silkworm::rpc::core::rawdb {

Expand Down
44 changes: 0 additions & 44 deletions silkworm/rpc/core/rawdb/util.cpp

This file was deleted.

97 changes: 0 additions & 97 deletions silkworm/rpc/core/rawdb/util_test.cpp

This file was deleted.

3 changes: 1 addition & 2 deletions silkworm/rpc/core/state_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <silkworm/infra/common/decoding_exception.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/rpc/common/util.hpp>
#include <silkworm/rpc/core/rawdb/util.hpp>

namespace silkworm::rpc {

Expand Down Expand Up @@ -61,7 +60,7 @@ Task<evmc::bytes32> StateReader::read_storage(
BlockNum block_number) const {
std::optional<silkworm::Bytes> value{co_await read_historical_storage(address, incarnation, location_hash, block_number)};
if (!value) {
auto composite_key{silkworm::composite_storage_key_without_hash_lookup(address, incarnation)};
const auto composite_key{db::storage_prefix(address, incarnation)};
SILK_DEBUG << "StateReader::read_storage composite_key: " << composite_key;
value = co_await tx_.get_both_range(db::table::kPlainStateName, composite_key, location_hash.bytes);
SILK_DEBUG << "StateReader::read_storage value: " << (value ? *value : silkworm::Bytes{});
Expand Down
3 changes: 2 additions & 1 deletion silkworm/rpc/ethdb/kv/state_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/grpc/common/conversion.hpp>
#include <silkworm/rpc/common/util.hpp>
#include <silkworm/rpc/core/rawdb/util.hpp>

#include "util.hpp"

namespace silkworm::rpc::ethdb::kv {

Expand Down
12 changes: 6 additions & 6 deletions silkworm/rpc/ethdb/kv/state_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class StateView {
public:
virtual ~StateView() = default;

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

virtual Task<std::optional<silkworm::Bytes>> get_code(const silkworm::Bytes& key) = 0;
virtual Task<std::optional<Bytes>> get_code(const Bytes& key) = 0;
};

class StateCache {
Expand Down Expand Up @@ -94,9 +94,9 @@ class CoherentStateView : public StateView {
CoherentStateView(const CoherentStateView&) = delete;
CoherentStateView& operator=(const CoherentStateView&) = delete;

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

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

private:
Transaction& txn_;
Expand Down Expand Up @@ -135,8 +135,8 @@ class CoherentStateCache : public StateCache {
void process_storage_change(CoherentStateRoot* root, StateViewId view_id, const remote::AccountChange& change);
bool add(const KeyValue& kv, CoherentStateRoot* root, StateViewId view_id);
bool add_code(const KeyValue& kv, CoherentStateRoot* root, StateViewId view_id);
Task<std::optional<silkworm::Bytes>> get(const silkworm::Bytes& key, Transaction& txn);
Task<std::optional<silkworm::Bytes>> get_code(const silkworm::Bytes& key, Transaction& txn);
Task<std::optional<Bytes>> get(const Bytes& key, Transaction& txn);
Task<std::optional<Bytes>> get_code(const Bytes& key, Transaction& txn);
CoherentStateRoot* get_root(StateViewId view_id);
CoherentStateRoot* advance_root(StateViewId view_id);
void evict_roots(StateViewId next_view_id);
Expand Down
3 changes: 2 additions & 1 deletion silkworm/rpc/ethdb/kv/state_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
#include <silkworm/infra/grpc/common/conversion.hpp>
#include <silkworm/infra/test_util/log.hpp>
#include <silkworm/rpc/common/worker_pool.hpp>
#include <silkworm/rpc/core/rawdb/util.hpp>
#include <silkworm/rpc/test_util/dummy_transaction.hpp>
#include <silkworm/rpc/test_util/mock_cursor.hpp>
#include <silkworm/rpc/test_util/mock_transaction.hpp>

#include "util.hpp"

namespace silkworm::rpc::ethdb::kv {

using namespace evmc::literals; // NOLINT(build/namespaces_literals)
Expand Down
36 changes: 36 additions & 0 deletions silkworm/rpc/ethdb/kv/util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2023 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.
*/

#include "util.hpp"

#include <cstdlib>
#include <cstring>

#include <silkworm/core/common/base.hpp>
#include <silkworm/core/common/endian.hpp>
#include <silkworm/db/util.hpp>

namespace silkworm::rpc::ethdb::kv {

silkworm::Bytes composite_storage_key(const evmc::address& address, uint64_t incarnation, HashAsArray hash) {
silkworm::Bytes res(kAddressLength + db::kIncarnationLength + kHashLength, '\0');
std::memcpy(&res[0], address.bytes, kAddressLength);
endian::store_big_u64(&res[kAddressLength], incarnation);
std::memcpy(&res[kAddressLength + db::kIncarnationLength], hash, kHashLength);
return res;
}

} // namespace silkworm::rpc::ethdb::kv
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@

#include <evmc/evmc.hpp>

#include <silkworm/core/common/util.hpp>
#include <silkworm/core/common/bytes.hpp>
#include <silkworm/core/types/hash.hpp>

namespace silkworm {
namespace silkworm::rpc::ethdb::kv {

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

silkworm::Bytes composite_storage_key_without_hash_lookup(const evmc::address& address, uint64_t incarnation);

} // namespace silkworm
} // namespace silkworm::rpc::ethdb::kv
Loading
Loading