Skip to content

Commit af21660

Browse files
authored
db: remove duplicated KV API utilities (#2143)
1 parent 6e2c1e1 commit af21660

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+204
-259
lines changed

cmd/dev/grpc_toolbox.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@
2929
#include <boost/asio/signal_set.hpp>
3030
#include <grpcpp/grpcpp.h>
3131

32+
#include <silkworm/core/common/bytes_to_string.hpp>
3233
#include <silkworm/core/common/util.hpp>
3334
#include <silkworm/core/types/address.hpp>
34-
#include <silkworm/db/kv/api/util.hpp>
3535
#include <silkworm/infra/common/log.hpp>
3636
#include <silkworm/infra/grpc/client/client_context_pool.hpp>
3737
#include <silkworm/infra/grpc/client/util.hpp>
3838
#include <silkworm/interfaces/remote/ethbackend.grpc.pb.h>
3939
#include <silkworm/interfaces/remote/kv.grpc.pb.h>
4040
#include <silkworm/interfaces/types/types.pb.h>
4141
#include <silkworm/rpc/common/constants.hpp>
42-
#include <silkworm/rpc/common/util.hpp>
4342
#include <silkworm/rpc/ethbackend/remote_backend.hpp>
4443

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

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

442441
// 4) Close cursor
@@ -560,8 +559,8 @@ int kv_seek_async_callback(const std::string& target, const std::string& table_n
560559
std::cout << "error reading SEEK gRPC" << std::flush;
561560
return;
562561
}
563-
const auto& key_bytes = silkworm::byte_view_of_string(seek_pair.k());
564-
const auto& value_bytes = silkworm::byte_view_of_string(seek_pair.v());
562+
const auto& key_bytes = silkworm::string_view_to_byte_view(seek_pair.k());
563+
const auto& value_bytes = silkworm::string_view_to_byte_view(seek_pair.v());
565564
std::cout << "KV Tx SEEK <- key: " << key_bytes << " value: " << value_bytes << std::endl;
566565
auto close_message = remote::Cursor{};
567566
close_message.set_op(remote::Op::CLOSE);
@@ -653,8 +652,8 @@ int kv_seek_both(const std::string& target, const std::string& table_name, silkw
653652
std::cout << "KV Tx STATUS: " << reader_writer->Finish() << "\n";
654653
return -1;
655654
}
656-
const auto& rsp_key = silkworm::byte_view_of_string(seek_both_pair.k());
657-
const auto& rsp_value = silkworm::byte_view_of_string(seek_both_pair.v());
655+
const auto& rsp_key = silkworm::string_view_to_byte_view(seek_both_pair.k());
656+
const auto& rsp_value = silkworm::string_view_to_byte_view(seek_both_pair.v());
658657
std::cout << "KV Tx SEEK_BOTH <- key: " << rsp_key << " value: " << rsp_value << std::endl;
659658

660659
// Close cursor

silkworm/core/common/bytes_to_string.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ inline const char* byte_ptr_cast(const uint8_t* ptr) { return reinterpret_cast<c
3131
inline uint8_t* byte_ptr_cast(char* ptr) { return reinterpret_cast<uint8_t*>(ptr); }
3232
inline const uint8_t* byte_ptr_cast(const char* ptr) { return reinterpret_cast<const uint8_t*>(ptr); }
3333

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

3637
template <std::size_t Size>

silkworm/core/common/util.hpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include <cmath>
2020
#include <cstring>
21+
#include <iomanip>
22+
#include <iostream>
2123
#include <optional>
2224
#include <regex>
2325
#include <string_view>
@@ -29,6 +31,17 @@
2931
#include <silkworm/core/common/base.hpp>
3032
#include <silkworm/core/common/bytes.hpp>
3133

34+
// intx does not include operator<< overloading for uint<N>
35+
namespace intx {
36+
37+
template <unsigned N>
38+
inline std::ostream& operator<<(std::ostream& out, const uint<N>& value) {
39+
out << "0x" << intx::hex(value);
40+
return out;
41+
}
42+
43+
} // namespace intx
44+
3245
namespace silkworm {
3346

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

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

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

@@ -154,6 +168,19 @@ inline constexpr Int from_string_sci(const char* str) {
154168
return x;
155169
}
156170

171+
inline std::ostream& operator<<(std::ostream& out, ByteView bytes) {
172+
for (const auto& b : bytes) {
173+
out << std::hex << std::setw(2) << std::setfill('0') << int(b);
174+
}
175+
out << std::dec;
176+
return out;
177+
}
178+
179+
inline std::ostream& operator<<(std::ostream& out, const Bytes& bytes) {
180+
out << to_hex(bytes);
181+
return out;
182+
}
183+
157184
float to_float(const intx::uint256&) noexcept;
158185

159186
} // namespace silkworm

silkworm/core/common/util_test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <catch2/catch_test_macros.hpp>
2020

21+
#include <silkworm/core/test_util/null_stream.hpp>
2122
#include <silkworm/core/types/evmc_bytes32.hpp>
2223

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

231+
TEST_CASE("print intx::uint256") {
232+
const auto i{intx::from_string<intx::uint256>("1000000000000000000000000")};
233+
CHECK(test_util::null_stream() << i);
234+
}
235+
236+
TEST_CASE("print Bytes") {
237+
Bytes b{};
238+
CHECK(test_util::null_stream() << b);
239+
}
240+
241+
TEST_CASE("print ByteView") {
242+
ByteView bv1;
243+
CHECK(test_util::null_stream() << bv1);
244+
Bytes b{*from_hex("0x0608")};
245+
ByteView bv2{b};
246+
CHECK(test_util::null_stream() << bv2);
247+
}
248+
230249
} // namespace silkworm
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 <ostream>
20+
21+
namespace silkworm::test_util {
22+
23+
//! Factory function creating one null output stream (all characters are discarded)
24+
inline std::ostream& null_stream() {
25+
static struct null_buf : public std::streambuf {
26+
int overflow(int c) override { return c; }
27+
} null_buf;
28+
static struct null_strm : public std::ostream {
29+
null_strm() : std::ostream(&null_buf) {}
30+
} null_strm;
31+
return null_strm;
32+
}
33+
34+
} // namespace silkworm::test_util

silkworm/db/chain/chain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
#include <string>
2020
#include <utility>
2121

22+
#include <silkworm/core/common/bytes_to_string.hpp>
2223
#include <silkworm/core/common/endian.hpp>
2324
#include <silkworm/core/common/util.hpp>
2425
#include <silkworm/core/rlp/decode.hpp>
2526
#include <silkworm/core/types/address.hpp>
2627
#include <silkworm/core/types/evmc_bytes32.hpp>
27-
#include <silkworm/db/kv/api/util.hpp>
2828
#include <silkworm/db/tables.hpp>
2929
#include <silkworm/db/util.hpp>
3030
#include <silkworm/infra/common/log.hpp>
@@ -70,7 +70,7 @@ Task<intx::uint256> read_total_difficulty(kv::api::Transaction& tx, const evmc::
7070
}
7171

7272
Task<evmc::bytes32> read_head_header_hash(kv::api::Transaction& tx) {
73-
const Bytes kHeadHeaderKey = bytes_of_string(table::kHeadHeaderName);
73+
const Bytes kHeadHeaderKey = string_to_bytes(table::kHeadHeaderName);
7474
const auto value = co_await tx.get_one(table::kHeadHeaderName, kHeadHeaderKey);
7575
if (value.empty()) {
7676
throw std::runtime_error{"empty head header hash value in read_head_header_hash"};

silkworm/db/kv/api/local_cursor.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "local_cursor.hpp"
1818

19+
#include <silkworm/core/common/bytes_to_string.hpp>
1920
#include <silkworm/infra/common/clock_time.hpp>
2021
#include <silkworm/infra/common/log.hpp>
2122

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

4445
if (result) {
45-
SILK_DEBUG << "LocalCursor::seek found: key: " << key << " value: " << byte_view_of_string(result.value.as_string());
46-
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
46+
SILK_DEBUG << "LocalCursor::seek found: key: " << key << " value: " << string_view_to_byte_view(result.value.as_string());
47+
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
4748
} else {
4849
SILK_DEBUG << "LocalCursor::seek not found key: " << key;
4950
co_return KeyValue{};
@@ -59,8 +60,8 @@ Task<KeyValue> LocalCursor::seek_exact(ByteView key) {
5960
SILK_DEBUG << "LocalCursor::seek_exact result: " << detail::dump_mdbx_result(result);
6061
if (result) {
6162
SILK_DEBUG << "LocalCursor::seek_exact found: "
62-
<< " key: " << key << " value: " << byte_view_of_string(result.value.as_string());
63-
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
63+
<< " key: " << key << " value: " << string_view_to_byte_view(result.value.as_string());
64+
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
6465
}
6566
SILK_ERROR << "LocalCursor::seek_exact !result key: " << key;
6667
}
@@ -75,8 +76,8 @@ Task<KeyValue> LocalCursor::next() {
7576

7677
if (result) {
7778
SILK_DEBUG << "LocalCursor::next: "
78-
<< " key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
79-
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
79+
<< " key: " << string_view_to_byte_view(result.key.as_string()) << " value: " << string_view_to_byte_view(result.value.as_string());
80+
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
8081
} else {
8182
SILK_ERROR << "LocalCursor::next !result";
8283
}
@@ -91,8 +92,8 @@ Task<KeyValue> LocalCursor::previous() {
9192

9293
if (result) {
9394
SILK_DEBUG << "LocalCursor::previous: "
94-
<< " key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
95-
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
95+
<< " key: " << string_view_to_byte_view(result.key.as_string()) << " value: " << string_view_to_byte_view(result.value.as_string());
96+
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
9697
} else {
9798
SILK_ERROR << "LocalCursor::previous !result";
9899
}
@@ -107,8 +108,8 @@ Task<KeyValue> LocalCursor::next_dup() {
107108

108109
if (result) {
109110
SILK_DEBUG << "LocalCursor::next_dup: "
110-
<< " key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
111-
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
111+
<< " key: " << string_view_to_byte_view(result.key.as_string()) << " value: " << string_view_to_byte_view(result.value.as_string());
112+
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
112113
} else {
113114
SILK_ERROR << "LocalCursor::next_dup !result";
114115
}
@@ -122,10 +123,10 @@ Task<Bytes> LocalCursor::seek_both(ByteView key, ByteView value) {
122123
SILK_DEBUG << "LocalCursor::seek_both result: " << detail::dump_mdbx_result(result);
123124

124125
if (result) {
125-
SILK_DEBUG << "LocalCursor::seek_both key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
126-
co_return bytes_of_string(result.value.as_string());
126+
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());
127+
co_return string_to_bytes(result.value.as_string());
127128
}
128-
co_return bytes_of_string("");
129+
co_return string_to_bytes("");
129130
}
130131

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

137138
if (result) {
138139
SILK_DEBUG << "LocalCursor::seek_both_exact: "
139-
<< " key: " << byte_view_of_string(result.key.as_string()) << " value: " << byte_view_of_string(result.value.as_string());
140-
co_return KeyValue{bytes_of_string(result.key.as_string()), bytes_of_string(result.value.as_string())};
140+
<< " key: " << string_view_to_byte_view(result.key.as_string()) << " value: " << string_view_to_byte_view(result.value.as_string());
141+
co_return KeyValue{string_to_bytes(result.key.as_string()), string_to_bytes(result.value.as_string())};
141142
} else {
142143
SILK_ERROR << "LocalCursor::seek_both_exact !found key: " << key << " subkey:" << value;
143144
}

silkworm/db/kv/api/local_cursor.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <silkworm/db/mdbx/mdbx.hpp>
3030

3131
#include "cursor.hpp"
32-
#include "util.hpp"
3332

3433
namespace silkworm::db::kv::api {
3534

silkworm/db/kv/api/state_cache.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
#include <magic_enum.hpp>
2222

2323
#include <silkworm/core/common/assert.hpp>
24+
#include <silkworm/core/common/bytes_to_string.hpp>
2425
#include <silkworm/core/common/util.hpp>
2526
#include <silkworm/core/types/address.hpp>
26-
#include <silkworm/db/kv/api/util.hpp>
2727
#include <silkworm/db/tables.hpp>
2828
#include <silkworm/infra/common/log.hpp>
2929
#include <silkworm/infra/grpc/common/conversion.hpp>
@@ -121,14 +121,14 @@ void CoherentStateCache::on_new_block(const ::remote::StateChangeBatch& state_ch
121121
void CoherentStateCache::process_upsert_change(CoherentStateRoot* root, StateViewId view_id,
122122
const remote::AccountChange& change) {
123123
const auto address = rpc::address_from_H160(change.address());
124-
const auto data_bytes = bytes_of_string(change.data());
124+
const auto data_bytes = string_to_bytes(change.data());
125125
SILK_DEBUG << "CoherentStateCache::process_upsert_change address: " << address << " data: " << data_bytes;
126126
const Bytes address_key{address.bytes, kAddressLength};
127127
add({address_key, data_bytes}, root, view_id);
128128
}
129129

130130
void CoherentStateCache::process_code_change(CoherentStateRoot* root, StateViewId view_id, const remote::AccountChange& change) {
131-
const auto code_bytes = bytes_of_string(change.code());
131+
const auto code_bytes = string_to_bytes(change.code());
132132
const ethash::hash256 code_hash{keccak256(code_bytes)};
133133
const Bytes code_hash_key{code_hash.bytes, kHashLength};
134134
SILK_DEBUG << "CoherentStateCache::process_code_change code_hash_key: " << code_hash_key;
@@ -150,7 +150,7 @@ void CoherentStateCache::process_storage_change(CoherentStateRoot* root, StateVi
150150
for (const auto& storage_change : change.storage_changes()) {
151151
const auto location_hash = rpc::bytes32_from_H256(storage_change.location());
152152
const auto storage_key = composite_storage_key(address, change.incarnation(), location_hash.bytes);
153-
const auto value = bytes_of_string(storage_change.data());
153+
const auto value = string_to_bytes(storage_change.data());
154154
SILK_DEBUG << "CoherentStateCache::process_storage_change key=" << storage_key << " value=" << value;
155155
add({storage_key, value}, root, view_id);
156156
}

silkworm/db/kv/api/state_cache.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <silkworm/interfaces/remote/kv.pb.h> // weird but currently needed
3333

3434
#include "transaction.hpp"
35-
#include "util.hpp"
3635

3736
namespace silkworm::db::kv::api {
3837

0 commit comments

Comments
 (0)