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

db: remove duplicated KV API utilities #2143

Merged
merged 2 commits into from
Jun 25, 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
19 changes: 9 additions & 10 deletions cmd/dev/grpc_toolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@
#include <boost/asio/signal_set.hpp>
#include <grpcpp/grpcpp.h>

#include <silkworm/core/common/bytes_to_string.hpp>
#include <silkworm/core/common/util.hpp>
#include <silkworm/core/types/address.hpp>
#include <silkworm/db/kv/api/util.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/grpc/client/client_context_pool.hpp>
#include <silkworm/infra/grpc/client/util.hpp>
#include <silkworm/interfaces/remote/ethbackend.grpc.pb.h>
#include <silkworm/interfaces/remote/kv.grpc.pb.h>
#include <silkworm/interfaces/types/types.pb.h>
#include <silkworm/rpc/common/constants.hpp>
#include <silkworm/rpc/common/util.hpp>
#include <silkworm/rpc/ethbackend/remote_backend.hpp>

using namespace silkworm;
Expand Down Expand Up @@ -325,8 +324,8 @@ int kv_seek(const std::string& target, const std::string& table_name, silkworm::
std::cout << "KV Tx STATUS: " << reader_writer->Finish() << "\n";
return -1;
}
const auto& rsp_key = silkworm::byte_view_of_string(seek_pair.k());
const auto& rsp_value = silkworm::byte_view_of_string(seek_pair.v());
const auto& rsp_key = silkworm::string_view_to_byte_view(seek_pair.k());
const auto& rsp_value = silkworm::string_view_to_byte_view(seek_pair.v());
std::cout << "KV Tx SEEK <- key: " << rsp_key << " value: " << rsp_value << std::endl;

// Close cursor
Expand Down Expand Up @@ -435,8 +434,8 @@ int kv_seek_async(const std::string& target, const std::string& table_name, silk
if (!has_event || got_tag != SEEK_TAG) {
return -1;
}
const auto& key_bytes = silkworm::byte_view_of_string(seek_pair.k());
const auto& value_bytes = silkworm::byte_view_of_string(seek_pair.v());
const auto& key_bytes = silkworm::string_view_to_byte_view(seek_pair.k());
const auto& value_bytes = silkworm::string_view_to_byte_view(seek_pair.v());
std::cout << "KV Tx SEEK <- key: " << key_bytes << " value: " << value_bytes << std::endl;

// 4) Close cursor
Expand Down Expand Up @@ -560,8 +559,8 @@ int kv_seek_async_callback(const std::string& target, const std::string& table_n
std::cout << "error reading SEEK gRPC" << std::flush;
return;
}
const auto& key_bytes = silkworm::byte_view_of_string(seek_pair.k());
const auto& value_bytes = silkworm::byte_view_of_string(seek_pair.v());
const auto& key_bytes = silkworm::string_view_to_byte_view(seek_pair.k());
const auto& value_bytes = silkworm::string_view_to_byte_view(seek_pair.v());
std::cout << "KV Tx SEEK <- key: " << key_bytes << " value: " << value_bytes << std::endl;
auto close_message = remote::Cursor{};
close_message.set_op(remote::Op::CLOSE);
Expand Down Expand Up @@ -653,8 +652,8 @@ int kv_seek_both(const std::string& target, const std::string& table_name, silkw
std::cout << "KV Tx STATUS: " << reader_writer->Finish() << "\n";
return -1;
}
const auto& rsp_key = silkworm::byte_view_of_string(seek_both_pair.k());
const auto& rsp_value = silkworm::byte_view_of_string(seek_both_pair.v());
const auto& rsp_key = silkworm::string_view_to_byte_view(seek_both_pair.k());
const auto& rsp_value = silkworm::string_view_to_byte_view(seek_both_pair.v());
std::cout << "KV Tx SEEK_BOTH <- key: " << rsp_key << " value: " << rsp_value << std::endl;

// Close cursor
Expand Down
1 change: 1 addition & 0 deletions silkworm/core/common/bytes_to_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ inline const char* byte_ptr_cast(const uint8_t* ptr) { return reinterpret_cast<c
inline uint8_t* byte_ptr_cast(char* ptr) { return reinterpret_cast<uint8_t*>(ptr); }
inline const uint8_t* byte_ptr_cast(const char* ptr) { return reinterpret_cast<const uint8_t*>(ptr); }

inline Bytes string_to_bytes(const std::string& s) { return {s.begin(), s.end()}; }
inline ByteView string_view_to_byte_view(std::string_view v) { return {byte_ptr_cast(v.data()), v.length()}; }

template <std::size_t Size>
Expand Down
31 changes: 29 additions & 2 deletions silkworm/core/common/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <optional>
#include <regex>
#include <string_view>
Expand All @@ -29,6 +31,17 @@
#include <silkworm/core/common/base.hpp>
#include <silkworm/core/common/bytes.hpp>

// intx does not include operator<< overloading for uint<N>
namespace intx {

template <unsigned N>
inline std::ostream& operator<<(std::ostream& out, const uint<N>& value) {
out << "0x" << intx::hex(value);
return out;
}

} // namespace intx

namespace silkworm {

//! \brief Strips leftmost zeroed bytes from byte sequence
Expand Down Expand Up @@ -63,7 +76,8 @@ inline bool is_valid_address(std::string_view s) {
std::string to_hex(ByteView bytes, bool with_prefix = false);

//! \brief Returns a string representing the hex form of provided integral
template <typename T, typename = std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>>>
template <typename T>
requires(std::is_integral_v<T> && std::is_unsigned_v<T>)
std::string to_hex(T value, bool with_prefix = false) {
uint8_t bytes[sizeof(T)];
intx::be::store(bytes, value);
Expand Down Expand Up @@ -98,7 +112,7 @@ inline ethash::hash256 keccak256(ByteView view) { return ethash::keccak256(view.

//! \brief Create an intx::uint256 from a string supporting both fixed decimal and scientific notation
template <UnsignedIntegral Int>
inline constexpr Int from_string_sci(const char* str) {
constexpr Int from_string_sci(const char* str) {
auto s = str;
auto m = Int{};

Expand Down Expand Up @@ -154,6 +168,19 @@ inline constexpr Int from_string_sci(const char* str) {
return x;
}

inline std::ostream& operator<<(std::ostream& out, ByteView bytes) {
for (const auto& b : bytes) {
out << std::hex << std::setw(2) << std::setfill('0') << int(b);
}
out << std::dec;
return out;
}

inline std::ostream& operator<<(std::ostream& out, const Bytes& bytes) {
out << to_hex(bytes);
return out;
}

float to_float(const intx::uint256&) noexcept;

} // namespace silkworm
19 changes: 19 additions & 0 deletions silkworm/core/common/util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <catch2/catch_test_macros.hpp>

#include <silkworm/core/test_util/null_stream.hpp>
#include <silkworm/core/types/evmc_bytes32.hpp>

namespace silkworm {
Expand Down Expand Up @@ -227,4 +228,22 @@ TEST_CASE("intx::uint256 to_float") {
CHECK(to_float(intx::from_string<intx::uint256>("1000000000000000000000000")) == 1e24f);
}

TEST_CASE("print intx::uint256") {
const auto i{intx::from_string<intx::uint256>("1000000000000000000000000")};
CHECK(test_util::null_stream() << i);
}

TEST_CASE("print Bytes") {
Bytes b{};
CHECK(test_util::null_stream() << b);
}

TEST_CASE("print ByteView") {
ByteView bv1;
CHECK(test_util::null_stream() << bv1);
Bytes b{*from_hex("0x0608")};
ByteView bv2{b};
CHECK(test_util::null_stream() << bv2);
}

} // namespace silkworm
34 changes: 34 additions & 0 deletions silkworm/core/test_util/null_stream.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
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 <ostream>

namespace silkworm::test_util {

//! Factory function creating one null output stream (all characters are discarded)
inline std::ostream& null_stream() {
static struct null_buf : public std::streambuf {
int overflow(int c) override { return c; }
} null_buf;
static struct null_strm : public std::ostream {
null_strm() : std::ostream(&null_buf) {}
} null_strm;
return null_strm;
}

} // namespace silkworm::test_util
4 changes: 2 additions & 2 deletions silkworm/db/chain/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
#include <string>
#include <utility>

#include <silkworm/core/common/bytes_to_string.hpp>
#include <silkworm/core/common/endian.hpp>
#include <silkworm/core/common/util.hpp>
#include <silkworm/core/rlp/decode.hpp>
#include <silkworm/core/types/address.hpp>
#include <silkworm/core/types/evmc_bytes32.hpp>
#include <silkworm/db/kv/api/util.hpp>
#include <silkworm/db/tables.hpp>
#include <silkworm/db/util.hpp>
#include <silkworm/infra/common/log.hpp>
Expand Down Expand Up @@ -70,7 +70,7 @@ Task<intx::uint256> read_total_difficulty(kv::api::Transaction& tx, const evmc::
}

Task<evmc::bytes32> read_head_header_hash(kv::api::Transaction& tx) {
const Bytes kHeadHeaderKey = bytes_of_string(table::kHeadHeaderName);
const Bytes kHeadHeaderKey = string_to_bytes(table::kHeadHeaderName);
const auto value = co_await tx.get_one(table::kHeadHeaderName, kHeadHeaderKey);
if (value.empty()) {
throw std::runtime_error{"empty head header hash value in read_head_header_hash"};
Expand Down
31 changes: 16 additions & 15 deletions silkworm/db/kv/api/local_cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "local_cursor.hpp"

#include <silkworm/core/common/bytes_to_string.hpp>
#include <silkworm/infra/common/clock_time.hpp>
#include <silkworm/infra/common/log.hpp>

Expand All @@ -42,8 +43,8 @@ Task<KeyValue> LocalCursor::seek(ByteView key) {
SILK_DEBUG << "LocalCursor::seek result: " << detail::dump_mdbx_result(result);

if (result) {
SILK_DEBUG << "LocalCursor::seek found: key: " << key << " value: " << byte_view_of_string(result.value.as_string());
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
SILK_DEBUG << "LocalCursor::seek found: key: " << key << " value: " << string_view_to_byte_view(result.value.as_string());
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
} else {
SILK_DEBUG << "LocalCursor::seek not found key: " << key;
co_return KeyValue{};
Expand All @@ -59,8 +60,8 @@ Task<KeyValue> LocalCursor::seek_exact(ByteView key) {
SILK_DEBUG << "LocalCursor::seek_exact result: " << detail::dump_mdbx_result(result);
if (result) {
SILK_DEBUG << "LocalCursor::seek_exact found: "
<< " key: " << key << " value: " << byte_view_of_string(result.value.as_string());
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
<< " key: " << key << " value: " << string_view_to_byte_view(result.value.as_string());
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
}
SILK_ERROR << "LocalCursor::seek_exact !result key: " << key;
}
Expand All @@ -75,8 +76,8 @@ Task<KeyValue> LocalCursor::next() {

if (result) {
SILK_DEBUG << "LocalCursor::next: "
<< " key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
<< " key: " << string_view_to_byte_view(result.key.as_string()) << " value: " << string_view_to_byte_view(result.value.as_string());
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
} else {
SILK_ERROR << "LocalCursor::next !result";
}
Expand All @@ -91,8 +92,8 @@ Task<KeyValue> LocalCursor::previous() {

if (result) {
SILK_DEBUG << "LocalCursor::previous: "
<< " key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
<< " key: " << string_view_to_byte_view(result.key.as_string()) << " value: " << string_view_to_byte_view(result.value.as_string());
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
} else {
SILK_ERROR << "LocalCursor::previous !result";
}
Expand All @@ -107,8 +108,8 @@ Task<KeyValue> LocalCursor::next_dup() {

if (result) {
SILK_DEBUG << "LocalCursor::next_dup: "
<< " key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
<< " key: " << string_view_to_byte_view(result.key.as_string()) << " value: " << string_view_to_byte_view(result.value.as_string());
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
} else {
SILK_ERROR << "LocalCursor::next_dup !result";
}
Expand All @@ -122,10 +123,10 @@ Task<Bytes> LocalCursor::seek_both(ByteView key, ByteView value) {
SILK_DEBUG << "LocalCursor::seek_both result: " << detail::dump_mdbx_result(result);

if (result) {
SILK_DEBUG << "LocalCursor::seek_both key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
co_return bytes_of_string(result.value.as_string());
SILK_DEBUG << "LocalCursor::seek_both key: " << string_view_to_byte_view(result.key.as_string()) << " value: " << string_view_to_byte_view(result.value.as_string());
co_return string_to_bytes(result.value.as_string());
}
co_return bytes_of_string("");
co_return string_to_bytes("");
}

Task<KeyValue> LocalCursor::seek_both_exact(ByteView key, ByteView value) {
Expand All @@ -136,8 +137,8 @@ Task<KeyValue> LocalCursor::seek_both_exact(ByteView key, ByteView value) {

if (result) {
SILK_DEBUG << "LocalCursor::seek_both_exact: "
<< " key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
<< " key: " << string_view_to_byte_view(result.key.as_string()) << " value: " << string_view_to_byte_view(result.value.as_string());
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
} else {
SILK_ERROR << "LocalCursor::seek_both_exact !found key: " << key << " subkey:" << value;
}
Expand Down
1 change: 0 additions & 1 deletion silkworm/db/kv/api/local_cursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <silkworm/db/mdbx/mdbx.hpp>

#include "cursor.hpp"
#include "util.hpp"

namespace silkworm::db::kv::api {

Expand Down
8 changes: 4 additions & 4 deletions silkworm/db/kv/api/state_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#include <magic_enum.hpp>

#include <silkworm/core/common/assert.hpp>
#include <silkworm/core/common/bytes_to_string.hpp>
#include <silkworm/core/common/util.hpp>
#include <silkworm/core/types/address.hpp>
#include <silkworm/db/kv/api/util.hpp>
#include <silkworm/db/tables.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/grpc/common/conversion.hpp>
Expand Down Expand Up @@ -121,14 +121,14 @@ void CoherentStateCache::on_new_block(const ::remote::StateChangeBatch& state_ch
void CoherentStateCache::process_upsert_change(CoherentStateRoot* root, StateViewId view_id,
const remote::AccountChange& change) {
const auto address = rpc::address_from_H160(change.address());
const auto data_bytes = bytes_of_string(change.data());
const auto data_bytes = string_to_bytes(change.data());
SILK_DEBUG << "CoherentStateCache::process_upsert_change address: " << address << " data: " << data_bytes;
const Bytes address_key{address.bytes, kAddressLength};
add({address_key, data_bytes}, root, view_id);
}

void CoherentStateCache::process_code_change(CoherentStateRoot* root, StateViewId view_id, const remote::AccountChange& change) {
const auto code_bytes = bytes_of_string(change.code());
const auto code_bytes = string_to_bytes(change.code());
const ethash::hash256 code_hash{keccak256(code_bytes)};
const Bytes code_hash_key{code_hash.bytes, kHashLength};
SILK_DEBUG << "CoherentStateCache::process_code_change code_hash_key: " << code_hash_key;
Expand All @@ -150,7 +150,7 @@ void CoherentStateCache::process_storage_change(CoherentStateRoot* root, StateVi
for (const auto& storage_change : change.storage_changes()) {
const auto location_hash = rpc::bytes32_from_H256(storage_change.location());
const auto storage_key = composite_storage_key(address, change.incarnation(), location_hash.bytes);
const auto value = bytes_of_string(storage_change.data());
const auto value = string_to_bytes(storage_change.data());
SILK_DEBUG << "CoherentStateCache::process_storage_change key=" << storage_key << " value=" << value;
add({storage_key, value}, root, view_id);
}
Expand Down
1 change: 0 additions & 1 deletion silkworm/db/kv/api/state_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <silkworm/interfaces/remote/kv.pb.h> // weird but currently needed

#include "transaction.hpp"
#include "util.hpp"

namespace silkworm::db::kv::api {

Expand Down
Loading
Loading