diff --git a/cmd/dev/grpc_toolbox.cpp b/cmd/dev/grpc_toolbox.cpp index 690658307f..42dbfd3ed0 100644 --- a/cmd/dev/grpc_toolbox.cpp +++ b/cmd/dev/grpc_toolbox.cpp @@ -29,9 +29,9 @@ #include #include +#include #include #include -#include #include #include #include @@ -39,7 +39,6 @@ #include #include #include -#include #include using namespace silkworm; @@ -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 @@ -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 @@ -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); @@ -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 diff --git a/silkworm/core/common/bytes_to_string.hpp b/silkworm/core/common/bytes_to_string.hpp index db73631c83..f3b98c1599 100644 --- a/silkworm/core/common/bytes_to_string.hpp +++ b/silkworm/core/common/bytes_to_string.hpp @@ -31,6 +31,7 @@ inline const char* byte_ptr_cast(const uint8_t* ptr) { return reinterpret_cast(ptr); } inline const uint8_t* byte_ptr_cast(const char* ptr) { return reinterpret_cast(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 diff --git a/silkworm/core/common/util.hpp b/silkworm/core/common/util.hpp index 2bbfc9f5bf..b47b59d5dc 100644 --- a/silkworm/core/common/util.hpp +++ b/silkworm/core/common/util.hpp @@ -18,6 +18,8 @@ #include #include +#include +#include #include #include #include @@ -29,6 +31,17 @@ #include #include +// intx does not include operator<< overloading for uint +namespace intx { + +template +inline std::ostream& operator<<(std::ostream& out, const uint& value) { + out << "0x" << intx::hex(value); + return out; +} + +} // namespace intx + namespace silkworm { //! \brief Strips leftmost zeroed bytes from byte sequence @@ -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 && std::is_unsigned_v>> +template + requires(std::is_integral_v && std::is_unsigned_v) std::string to_hex(T value, bool with_prefix = false) { uint8_t bytes[sizeof(T)]; intx::be::store(bytes, value); @@ -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 -inline constexpr Int from_string_sci(const char* str) { +constexpr Int from_string_sci(const char* str) { auto s = str; auto m = Int{}; @@ -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 diff --git a/silkworm/core/common/util_test.cpp b/silkworm/core/common/util_test.cpp index 4b39a17c0e..de7b69a1ea 100644 --- a/silkworm/core/common/util_test.cpp +++ b/silkworm/core/common/util_test.cpp @@ -18,6 +18,7 @@ #include +#include #include namespace silkworm { @@ -227,4 +228,22 @@ TEST_CASE("intx::uint256 to_float") { CHECK(to_float(intx::from_string("1000000000000000000000000")) == 1e24f); } +TEST_CASE("print intx::uint256") { + const auto i{intx::from_string("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 diff --git a/silkworm/core/test_util/null_stream.hpp b/silkworm/core/test_util/null_stream.hpp new file mode 100644 index 0000000000..f6c653f4f7 --- /dev/null +++ b/silkworm/core/test_util/null_stream.hpp @@ -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 + +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 diff --git a/silkworm/db/chain/chain.cpp b/silkworm/db/chain/chain.cpp index 8d93d7bbc9..6b727acaf9 100644 --- a/silkworm/db/chain/chain.cpp +++ b/silkworm/db/chain/chain.cpp @@ -19,12 +19,12 @@ #include #include +#include #include #include #include #include #include -#include #include #include #include @@ -70,7 +70,7 @@ Task read_total_difficulty(kv::api::Transaction& tx, const evmc:: } Task 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"}; diff --git a/silkworm/db/kv/api/local_cursor.cpp b/silkworm/db/kv/api/local_cursor.cpp index 5d62c9fcd1..63960d4954 100644 --- a/silkworm/db/kv/api/local_cursor.cpp +++ b/silkworm/db/kv/api/local_cursor.cpp @@ -16,6 +16,7 @@ #include "local_cursor.hpp" +#include #include #include @@ -42,8 +43,8 @@ Task 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{}; @@ -59,8 +60,8 @@ Task 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; } @@ -75,8 +76,8 @@ Task 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"; } @@ -91,8 +92,8 @@ Task 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"; } @@ -107,8 +108,8 @@ Task 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"; } @@ -122,10 +123,10 @@ Task 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 LocalCursor::seek_both_exact(ByteView key, ByteView value) { @@ -136,8 +137,8 @@ Task 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; } diff --git a/silkworm/db/kv/api/local_cursor.hpp b/silkworm/db/kv/api/local_cursor.hpp index 65b5a5460a..37be1c910e 100644 --- a/silkworm/db/kv/api/local_cursor.hpp +++ b/silkworm/db/kv/api/local_cursor.hpp @@ -29,7 +29,6 @@ #include #include "cursor.hpp" -#include "util.hpp" namespace silkworm::db::kv::api { diff --git a/silkworm/db/kv/api/state_cache.cpp b/silkworm/db/kv/api/state_cache.cpp index 277936ad98..017f5ec9a1 100644 --- a/silkworm/db/kv/api/state_cache.cpp +++ b/silkworm/db/kv/api/state_cache.cpp @@ -21,9 +21,9 @@ #include #include +#include #include #include -#include #include #include #include @@ -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; @@ -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); } diff --git a/silkworm/db/kv/api/state_cache.hpp b/silkworm/db/kv/api/state_cache.hpp index b976c33625..8490e11310 100644 --- a/silkworm/db/kv/api/state_cache.hpp +++ b/silkworm/db/kv/api/state_cache.hpp @@ -32,7 +32,6 @@ #include // weird but currently needed #include "transaction.hpp" -#include "util.hpp" namespace silkworm::db::kv::api { diff --git a/silkworm/db/kv/api/util.hpp b/silkworm/db/kv/api/util.hpp deleted file mode 100644 index e9f65748ee..0000000000 --- a/silkworm/db/kv/api/util.hpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - 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. -*/ - -#pragma once - -#include -#include -#include - -#include - -#include -#include - -namespace intx { -template -inline std::ostream& operator<<(std::ostream& out, const uint& value) { - out << "0x" << intx::hex(value); - return out; -} -} // namespace intx - -namespace silkworm { - -template -ByteView full_view(const uint8_t (&bytes)[N]) { - return {bytes, N}; -} - -inline ByteView full_view(const evmc::address& address) { return {address.bytes, kAddressLength}; } - -inline ByteView full_view(const evmc::bytes32& hash) { return {hash.bytes, kHashLength}; } - -inline ByteView full_view(const ethash::hash256& hash) { return {hash.bytes, kHashLength}; } - -inline ByteView byte_view_of_string(const std::string& s) { - return {reinterpret_cast(s.data()), s.length()}; -} - -inline Bytes bytes_of_string(const std::string& s) { - return {s.begin(), s.end()}; -} - -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; -} - -} // namespace silkworm diff --git a/silkworm/db/kv/api/util_test.cpp b/silkworm/db/kv/api/util_test.cpp deleted file mode 100644 index 9fcb3afcdf..0000000000 --- a/silkworm/db/kv/api/util_test.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - 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 - -#include - -namespace silkworm::db::kv::api { - -TEST_CASE("print Bytes", "[rpc][common][util]") { - Bytes b{}; - CHECK_NOTHROW(test_util::null_stream() << b); -} - -TEST_CASE("byte view from string", "[rpc][common][util]") { - CHECK(byte_view_of_string("").empty()); -} - -TEST_CASE("bytes from string", "[rpc][common][util]") { - CHECK(bytes_of_string("").empty()); -} - -TEST_CASE("print ByteView", "[rpc][common][util]") { - Bytes b1; - CHECK_NOTHROW(test_util::null_stream() << b1); - Bytes b2{*from_hex("0x0608")}; - CHECK_NOTHROW(test_util::null_stream() << b2); -} - -} // namespace silkworm::db::kv::api diff --git a/silkworm/db/kv/grpc/client/remote_cursor.cpp b/silkworm/db/kv/grpc/client/remote_cursor.cpp index 4fc90d81fd..6931d435c0 100644 --- a/silkworm/db/kv/grpc/client/remote_cursor.cpp +++ b/silkworm/db/kv/grpc/client/remote_cursor.cpp @@ -16,6 +16,7 @@ #include "remote_cursor.hpp" +#include #include #include #include @@ -48,8 +49,8 @@ Task RemoteCursor::seek(ByteView key) { seek_message.set_cursor(cursor_id_); seek_message.set_k(key.data(), key.length()); auto seek_pair = co_await tx_rpc_.write_and_read(seek_message); - const auto k = silkworm::bytes_of_string(seek_pair.k()); - const auto v = silkworm::bytes_of_string(seek_pair.v()); + const auto k = string_to_bytes(seek_pair.k()); + const auto v = string_to_bytes(seek_pair.v()); SILK_DEBUG << "RemoteCursor::seek k: " << k << " v: " << v << " c=" << cursor_id_ << " t=" << clock_time::since(start_time); co_return api::KeyValue{k, v}; } @@ -62,8 +63,8 @@ Task RemoteCursor::seek_exact(ByteView key) { seek_message.set_cursor(cursor_id_); seek_message.set_k(key.data(), key.length()); auto seek_pair = co_await tx_rpc_.write_and_read(seek_message); - const auto k = silkworm::bytes_of_string(seek_pair.k()); - const auto v = silkworm::bytes_of_string(seek_pair.v()); + const auto k = string_to_bytes(seek_pair.k()); + const auto v = string_to_bytes(seek_pair.v()); SILK_DEBUG << "RemoteCursor::seek_exact k: " << k << " v: " << v << " c=" << cursor_id_ << " t=" << clock_time::since(start_time); co_return api::KeyValue{k, v}; } @@ -74,8 +75,8 @@ Task RemoteCursor::next() { next_message.set_op(remote::Op::NEXT); next_message.set_cursor(cursor_id_); auto next_pair = co_await tx_rpc_.write_and_read(next_message); - const auto k = silkworm::bytes_of_string(next_pair.k()); - const auto v = silkworm::bytes_of_string(next_pair.v()); + const auto k = string_to_bytes(next_pair.k()); + const auto v = string_to_bytes(next_pair.v()); SILK_DEBUG << "RemoteCursor::next k: " << k << " v: " << v << " c=" << cursor_id_ << " t=" << clock_time::since(start_time); co_return api::KeyValue{k, v}; } @@ -86,8 +87,8 @@ Task RemoteCursor::previous() { next_message.set_op(remote::Op::PREV); next_message.set_cursor(cursor_id_); auto next_pair = co_await tx_rpc_.write_and_read(next_message); - const auto k = silkworm::bytes_of_string(next_pair.k()); - const auto v = silkworm::bytes_of_string(next_pair.v()); + const auto k = string_to_bytes(next_pair.k()); + const auto v = string_to_bytes(next_pair.v()); SILK_DEBUG << "RemoteCursor::previous k: " << k << " v: " << v << " c=" << cursor_id_ << " t=" << clock_time::since(start_time); co_return api::KeyValue{k, v}; } @@ -98,8 +99,8 @@ Task RemoteCursor::next_dup() { next_message.set_op(remote::Op::NEXT_DUP); next_message.set_cursor(cursor_id_); auto next_pair = co_await tx_rpc_.write_and_read(next_message); - const auto k = silkworm::bytes_of_string(next_pair.k()); - const auto v = silkworm::bytes_of_string(next_pair.v()); + const auto k = string_to_bytes(next_pair.k()); + const auto v = string_to_bytes(next_pair.v()); SILK_DEBUG << "RemoteCursor::next k: " << k << " v: " << v << " c=" << cursor_id_ << " t=" << clock_time::since(start_time); co_return api::KeyValue{k, v}; } @@ -113,8 +114,8 @@ Task RemoteCursor::seek_both(ByteView key, ByteView value) { seek_message.set_k(key.data(), key.length()); seek_message.set_v(value.data(), value.length()); auto seek_pair = co_await tx_rpc_.write_and_read(seek_message); - const auto k = silkworm::bytes_of_string(seek_pair.k()); - const auto v = silkworm::bytes_of_string(seek_pair.v()); + const auto k = string_to_bytes(seek_pair.k()); + const auto v = string_to_bytes(seek_pair.v()); SILK_DEBUG << "RemoteCursor::seek_both k: " << k << " v: " << v << " c=" << cursor_id_ << " t=" << clock_time::since(start_time); co_return v; } @@ -128,8 +129,8 @@ Task RemoteCursor::seek_both_exact(ByteView key, ByteView value) seek_message.set_k(key.data(), key.length()); seek_message.set_v(value.data(), value.length()); auto seek_pair = co_await tx_rpc_.write_and_read(seek_message); - const auto k = silkworm::bytes_of_string(seek_pair.k()); - const auto v = silkworm::bytes_of_string(seek_pair.v()); + const auto k = string_to_bytes(seek_pair.k()); + const auto v = string_to_bytes(seek_pair.v()); SILK_DEBUG << "RemoteCursor::seek_both_exact k: " << k << " v: " << v << " c=" << cursor_id_ << " t=" << clock_time::since(start_time); co_return api::KeyValue{k, v}; } diff --git a/silkworm/db/kv/grpc/client/remote_cursor.hpp b/silkworm/db/kv/grpc/client/remote_cursor.hpp index 5bd3afe90a..1b1a86eb4a 100644 --- a/silkworm/db/kv/grpc/client/remote_cursor.hpp +++ b/silkworm/db/kv/grpc/client/remote_cursor.hpp @@ -26,7 +26,6 @@ #include #include -#include #include "../../api/cursor.hpp" #include "rpc.hpp" diff --git a/silkworm/db/kv/grpc/client/remote_cursor_test.cpp b/silkworm/db/kv/grpc/client/remote_cursor_test.cpp index 112d241253..9cbca917d2 100644 --- a/silkworm/db/kv/grpc/client/remote_cursor_test.cpp +++ b/silkworm/db/kv/grpc/client/remote_cursor_test.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -39,16 +40,16 @@ namespace test = rpc::test; static const char* kPlainStateKey{"e0a2bd4258d2768837baa26a28fe71dc079f84c7"}; static const char* kPlainStateValue{""}; -static const silkworm::Bytes kPlainStateKeyBytes{silkworm::bytes_of_string(kPlainStateKey)}; -static const silkworm::Bytes kPlainStateValueBytes{silkworm::bytes_of_string(kPlainStateValue)}; +static const silkworm::Bytes kPlainStateKeyBytes{string_to_bytes(kPlainStateKey)}; +static const silkworm::Bytes kPlainStateValueBytes{string_to_bytes(kPlainStateValue)}; static const char* kAccountChangeSetKey{"0000000000532b9f"}; static const char* kAccountChangeSetSubkey{"0000000000000000000000000000000000000000"}; static const char* kAccountChangeSetValue{"020944ed67f28fd50bb8e9"}; -static const silkworm::Bytes kAccountChangeSetKeyBytes{silkworm::bytes_of_string(kAccountChangeSetKey)}; -static const silkworm::Bytes kAccountChangeSetSubkeyBytes{silkworm::bytes_of_string(kAccountChangeSetSubkey)}; -static const silkworm::Bytes kAccountChangeSetValueBytes{silkworm::bytes_of_string(kAccountChangeSetValue)}; +static const silkworm::Bytes kAccountChangeSetKeyBytes{string_to_bytes(kAccountChangeSetKey)}; +static const silkworm::Bytes kAccountChangeSetSubkeyBytes{string_to_bytes(kAccountChangeSetSubkey)}; +static const silkworm::Bytes kAccountChangeSetValueBytes{string_to_bytes(kAccountChangeSetValue)}; struct RemoteCursorTest : test_util::KVTestBase { RemoteCursorTest() { diff --git a/silkworm/db/state/state_reader.cpp b/silkworm/db/state/state_reader.cpp index 63f07b6d0a..59f12714a8 100644 --- a/silkworm/db/state/state_reader.cpp +++ b/silkworm/db/state/state_reader.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -31,7 +30,7 @@ namespace silkworm::db::state { Task> StateReader::read_account(const evmc::address& address, BlockNum block_number) const { std::optional encoded{co_await read_historical_account(address, block_number)}; if (!encoded) { - encoded = co_await tx_.get_one(table::kPlainStateName, full_view(address)); + encoded = co_await tx_.get_one(table::kPlainStateName, address.bytes); } SILK_DEBUG << "StateReader::read_account encoded: " << (encoded ? *encoded : Bytes{}); if (!encoded || encoded->empty()) { @@ -43,7 +42,7 @@ Task> StateReader::read_account(const evmc::address& addr if (account->incarnation > 0 && account->code_hash == kEmptyHash) { // Restore code hash - const auto storage_key{storage_prefix(full_view(address), account->incarnation)}; + const auto storage_key{storage_prefix(address.bytes, account->incarnation)}; auto code_hash{co_await tx_.get_one(table::kPlainCodeHashName, storage_key)}; if (code_hash.length() == kHashLength) { std::memcpy(account->code_hash.bytes, code_hash.data(), kHashLength); @@ -78,7 +77,7 @@ Task> StateReader::read_code(const evmc::bytes32& code_hash if (code_hash == kEmptyHash) { co_return std::nullopt; } - co_return co_await tx_.get_one(table::kCodeName, full_view(code_hash)); + co_return co_await tx_.get_one(table::kCodeName, code_hash.bytes); } Task> StateReader::read_historical_account(const evmc::address& address, BlockNum block_number) const { @@ -87,7 +86,7 @@ Task> StateReader::read_historical_account(const evmc::addr const auto kv_pair{co_await tx_.get(table::kAccountHistoryName, account_history_key)}; SILK_DEBUG << "StateReader::read_historical_account kv_pair.key: " << to_hex(kv_pair.key); - const auto address_view{full_view(address)}; + const ByteView address_view{address.bytes}; if (kv_pair.key.substr(0, kAddressLength) != address_view) { co_return std::nullopt; } @@ -120,8 +119,9 @@ Task> StateReader::read_historical_storage(const evmc::addr SILK_DEBUG << "StateReader::read_historical_storage storage_history_key: " << storage_history_key; const auto kv_pair{co_await tx_.get(table::kStorageHistoryName, storage_history_key)}; - const auto location_hash_view{full_view(location_hash)}; - if (kv_pair.key.substr(0, kAddressLength) != full_view(address) || + const ByteView address_view{address.bytes}; + const ByteView location_hash_view{location_hash.bytes}; + if (kv_pair.key.substr(0, kAddressLength) != address_view || kv_pair.key.substr(kAddressLength, kHashLength) != location_hash_view) { co_return std::nullopt; } diff --git a/silkworm/db/state/state_reader_test.cpp b/silkworm/db/state/state_reader_test.cpp index 724acc3d3a..c1111174b0 100644 --- a/silkworm/db/state/state_reader_test.cpp +++ b/silkworm/db/state/state_reader_test.cpp @@ -23,7 +23,6 @@ #include #include -#include #include #include #include @@ -70,7 +69,7 @@ TEST_CASE_METHOD(StateReaderTest, "StateReader::read_account") { co_return KeyValue{}; })); // 2. DatabaseReader::get_one call on kPlainState returns empty value - EXPECT_CALL(transaction_, get_one(db::table::kPlainStateName, full_view(kZeroAddress))).WillOnce(InvokeWithoutArgs([]() -> Task { + EXPECT_CALL(transaction_, get_one(db::table::kPlainStateName, ByteView{kZeroAddress.bytes})).WillOnce(InvokeWithoutArgs([]() -> Task { co_return Bytes{}; })); @@ -87,7 +86,7 @@ TEST_CASE_METHOD(StateReaderTest, "StateReader::read_account") { co_return KeyValue{}; })); // 2. DatabaseReader::get_one call on kPlainState returns account data - EXPECT_CALL(transaction_, get_one(db::table::kPlainStateName, full_view(kZeroAddress))).WillOnce(InvokeWithoutArgs([]() -> Task { + EXPECT_CALL(transaction_, get_one(db::table::kPlainStateName, ByteView{kZeroAddress.bytes})).WillOnce(InvokeWithoutArgs([]() -> Task { co_return kEncodedAccount; })); @@ -107,7 +106,7 @@ TEST_CASE_METHOD(StateReaderTest, "StateReader::read_account") { // Set the call expectations: // 1. DatabaseReader::get call on kAccountHistory returns the account bitmap EXPECT_CALL(transaction_, get(db::table::kAccountHistoryName, _)).WillOnce(InvokeWithoutArgs([]() -> Task { - co_return KeyValue{Bytes{full_view(kZeroAddress)}, kEncodedAccountHistory}; + co_return KeyValue{Bytes{ByteView{kZeroAddress.bytes}}, kEncodedAccountHistory}; })); // 2. DatabaseReader::get_both_range call on kPlainAccountChangeSet returns the account data EXPECT_CALL(transaction_, get_both_range(db::table::kAccountChangeSetName, _, _)).WillOnce(InvokeWithoutArgs([]() -> Task> { @@ -133,7 +132,7 @@ TEST_CASE_METHOD(StateReaderTest, "StateReader::read_account") { co_return KeyValue{}; })); // 2. DatabaseReader::get_one call on kPlainState returns account data - EXPECT_CALL(transaction_, get_one(db::table::kPlainStateName, full_view(kZeroAddress))).WillOnce(InvokeWithoutArgs([]() -> Task { + EXPECT_CALL(transaction_, get_one(db::table::kPlainStateName, ByteView{kZeroAddress.bytes})).WillOnce(InvokeWithoutArgs([]() -> Task { co_return kEncodedAccountWithoutCodeHash; })); // 3. DatabaseReader::get_one call on kPlainContractCode returns account code hash diff --git a/silkworm/rpc/commands/engine_api.cpp b/silkworm/rpc/commands/engine_api.cpp index 622c9978bb..b962b43a64 100644 --- a/silkworm/rpc/commands/engine_api.cpp +++ b/silkworm/rpc/commands/engine_api.cpp @@ -22,7 +22,6 @@ #include #include -#include #include #include #include diff --git a/silkworm/rpc/commands/engine_api_test.cpp b/silkworm/rpc/commands/engine_api_test.cpp index 7cb8fbffd2..9197325396 100644 --- a/silkworm/rpc/commands/engine_api_test.cpp +++ b/silkworm/rpc/commands/engine_api_test.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -565,7 +566,7 @@ TEST_CASE_METHOD(EngineRpcApiTest, "engine_transitionConfigurationV1 OK: EL conf const silkworm::Bytes genesis_block_hash{*silkworm::from_hex("0000000000000000000000000000000000000000000000000000000000000000")}; const silkworm::ByteView genesis_block_key{genesis_block_hash}; EXPECT_CALL(*mock_cursor, seek_exact(genesis_block_key)).WillOnce(InvokeWithoutArgs([&]() -> Task { - co_return KeyValue{silkworm::Bytes{}, Bytes{byte_view_of_string(kChainConfig.to_json().dump())}}; + co_return KeyValue{silkworm::Bytes{}, Bytes{string_view_to_byte_view(kChainConfig.to_json().dump())}}; })); nlohmann::json reply; @@ -603,7 +604,7 @@ TEST_CASE_METHOD(EngineRpcApiTest, "engine_transitionConfigurationV1 OK: termina const silkworm::Bytes genesis_block_hash{*silkworm::from_hex("0000000000000000000000000000000000000000000000000000000000000000")}; const silkworm::ByteView genesis_block_key{genesis_block_hash}; EXPECT_CALL(*mock_cursor, seek_exact(genesis_block_key)).WillOnce(InvokeWithoutArgs([&]() -> Task { - co_return KeyValue{silkworm::Bytes{}, Bytes{byte_view_of_string(kChainConfig.to_json().dump())}}; + co_return KeyValue{silkworm::Bytes{}, Bytes{string_view_to_byte_view(kChainConfig.to_json().dump())}}; })); nlohmann::json reply; @@ -641,7 +642,7 @@ TEST_CASE_METHOD(EngineRpcApiTest, "engine_transitionConfigurationV1 KO: incorre const silkworm::Bytes genesis_block_hash{*silkworm::from_hex("0000000000000000000000000000000000000000000000000000000000000000")}; const silkworm::ByteView genesis_block_key{genesis_block_hash}; EXPECT_CALL(*mock_cursor, seek_exact(genesis_block_key)).WillOnce(InvokeWithoutArgs([&]() -> Task { - co_return KeyValue{silkworm::Bytes{}, Bytes{byte_view_of_string(kChainConfig.to_json().dump())}}; + co_return KeyValue{silkworm::Bytes{}, Bytes{string_view_to_byte_view(kChainConfig.to_json().dump())}}; })); nlohmann::json reply; @@ -678,7 +679,7 @@ TEST_CASE_METHOD(EngineRpcApiTest, "engine_transitionConfigurationV1 KO: EL does const silkworm::Bytes genesis_block_hash{*silkworm::from_hex("0000000000000000000000000000000000000000000000000000000000000000")}; const silkworm::ByteView genesis_block_key{genesis_block_hash}; EXPECT_CALL(*mock_cursor, seek_exact(genesis_block_key)).WillOnce(InvokeWithoutArgs([&]() -> Task { - co_return KeyValue{silkworm::Bytes{}, Bytes{byte_view_of_string(kChainConfigNoTerminalTotalDifficulty.to_json().dump())}}; + co_return KeyValue{silkworm::Bytes{}, Bytes{string_view_to_byte_view(kChainConfigNoTerminalTotalDifficulty.to_json().dump())}}; })); nlohmann::json reply; @@ -715,7 +716,7 @@ TEST_CASE_METHOD(EngineRpcApiTest, "engine_transitionConfigurationV1 KO: CL send const silkworm::Bytes genesis_block_hash{*silkworm::from_hex("0000000000000000000000000000000000000000000000000000000000000000")}; const silkworm::ByteView genesis_block_key{genesis_block_hash}; EXPECT_CALL(*mock_cursor, seek_exact(genesis_block_key)).WillOnce(InvokeWithoutArgs([&]() -> Task { - co_return KeyValue{silkworm::Bytes{}, Bytes{byte_view_of_string(kChainConfig.to_json().dump())}}; + co_return KeyValue{silkworm::Bytes{}, Bytes{string_view_to_byte_view(kChainConfig.to_json().dump())}}; })); nlohmann::json reply; @@ -752,7 +753,7 @@ TEST_CASE_METHOD(EngineRpcApiTest, "engine_transitionConfigurationV1 KO: CL send const silkworm::Bytes genesis_block_hash{*silkworm::from_hex("0000000000000000000000000000000000000000000000000000000000000000")}; const silkworm::ByteView genesis_block_key{genesis_block_hash}; EXPECT_CALL(*mock_cursor, seek_exact(genesis_block_key)).WillOnce(InvokeWithoutArgs([&]() -> Task { - co_return KeyValue{silkworm::Bytes{}, Bytes{byte_view_of_string(kChainConfig.to_json().dump())}}; + co_return KeyValue{silkworm::Bytes{}, Bytes{string_view_to_byte_view(kChainConfig.to_json().dump())}}; })); nlohmann::json reply; @@ -789,7 +790,7 @@ TEST_CASE_METHOD(EngineRpcApiTest, "engine_transitionConfigurationV1 OK: no matc const silkworm::Bytes genesis_block_hash{*silkworm::from_hex("0000000000000000000000000000000000000000000000000000000000000000")}; const silkworm::ByteView genesis_block_key{genesis_block_hash}; EXPECT_CALL(*mock_cursor, seek_exact(genesis_block_key)).WillOnce(InvokeWithoutArgs([&]() -> Task { - co_return KeyValue{silkworm::Bytes{}, Bytes{byte_view_of_string(kChainConfig.to_json().dump())}}; + co_return KeyValue{silkworm::Bytes{}, Bytes{string_view_to_byte_view(kChainConfig.to_json().dump())}}; })); nlohmann::json reply; diff --git a/silkworm/rpc/commands/eth_api.cpp b/silkworm/rpc/commands/eth_api.cpp index 2aa0ae9472..62a4a1ef2c 100644 --- a/silkworm/rpc/commands/eth_api.cpp +++ b/silkworm/rpc/commands/eth_api.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -125,7 +126,7 @@ Task EthereumRpcApi::handle_eth_syncing(const nlohmann::json& request, nlo for (std::size_t i{0}; i < sizeof(silkworm::db::stages::kAllStages) / sizeof(char*) - 1; i++) { // no unWind StageData current_stage; current_stage.stage_name = silkworm::db::stages::kAllStages[i]; - current_stage.block_number = to_quantity(co_await stages::get_sync_stage_progress(*tx, silkworm::bytes_of_string(current_stage.stage_name))); + current_stage.block_number = to_quantity(co_await stages::get_sync_stage_progress(*tx, string_to_bytes(current_stage.stage_name))); syncing_data.stages.push_back(current_stage); } reply = make_json_content(request, syncing_data); diff --git a/silkworm/rpc/commands/ots_api.cpp b/silkworm/rpc/commands/ots_api.cpp index 513290f4bc..0096e893fa 100644 --- a/silkworm/rpc/commands/ots_api.cpp +++ b/silkworm/rpc/commands/ots_api.cpp @@ -286,7 +286,7 @@ Task OtsRpcApi::handle_ots_get_transaction_by_sender_and_nonce(const nlohm try { auto account_history_cursor = co_await tx->cursor(db::table::kAccountHistoryName); auto account_change_set_cursor = co_await tx->cursor_dup_sort(db::table::kAccountChangeSetName); - auto sender_byte_view = full_view(sender); + const ByteView sender_byte_view{sender.bytes}; auto key_value = co_await account_history_cursor->seek(sender_byte_view); std::vector account_block_numbers; @@ -395,7 +395,7 @@ Task OtsRpcApi::handle_ots_get_contract_creator(const nlohmann::json& requ auto tx = co_await database_->begin(); try { - auto contract_address_byte_view = full_view(contract_address); + const ByteView contract_address_byte_view{contract_address.bytes}; auto plain_state_cursor = co_await tx->cursor(db::table::kPlainStateName); auto account_payload = co_await plain_state_cursor->seek(contract_address_byte_view); auto plain_state_account = Account::from_encoded_storage(account_payload.value); diff --git a/silkworm/rpc/commands/parity_api.cpp b/silkworm/rpc/commands/parity_api.cpp index 0a0a3982aa..be3431d589 100644 --- a/silkworm/rpc/commands/parity_api.cpp +++ b/silkworm/rpc/commands/parity_api.cpp @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -116,7 +115,7 @@ Task ParityRpcApi::handle_parity_list_storage_keys(const nlohmann::json& r std::optional account = co_await state_reader.read_account(address, block_number); if (!account) throw std::domain_error{"account not found"}; - Bytes seek_bytes = db::storage_prefix(full_view(address), account->incarnation); + Bytes seek_bytes = db::storage_prefix(address.bytes, 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(); Bytes seek_val = offset ? offset.value() : Bytes{}; diff --git a/silkworm/rpc/common/util.cpp b/silkworm/rpc/common/util.cpp index e5a2013784..0ea5e3ffd4 100644 --- a/silkworm/rpc/common/util.cpp +++ b/silkworm/rpc/common/util.cpp @@ -17,7 +17,6 @@ #include "util.hpp" #include -#include namespace silkworm { diff --git a/silkworm/rpc/core/account_dumper.cpp b/silkworm/rpc/core/account_dumper.cpp index 56ed3302a4..c8b4f199f7 100644 --- a/silkworm/rpc/core/account_dumper.cpp +++ b/silkworm/rpc/core/account_dumper.cpp @@ -59,13 +59,13 @@ Task AccountDumper::dump_accounts( std::vector collected_data; - AccountWalker::Collector collector = [&](silkworm::ByteView k, silkworm::ByteView v) { + AccountWalker::Collector collector = [&](ByteView k, ByteView v) { if (max_result > 0 && collected_data.size() >= static_cast(max_result)) { dump_accounts.next = bytes_to_address(k); return false; } - if (k.size() > silkworm::kAddressLength) { + if (k.size() > kAddressLength) { return true; } @@ -92,8 +92,8 @@ Task AccountDumper::load_accounts(const std::vector& collected_d for (const auto& kv : collected_data) { const auto address = bytes_to_address(kv.key); - auto account{silkworm::Account::from_encoded_storage(kv.value)}; - silkworm::success_or_throw(account); + auto account{Account::from_encoded_storage(kv.value)}; + success_or_throw(account); DumpAccount dump_account; dump_account.balance = account->balance; @@ -101,11 +101,11 @@ Task AccountDumper::load_accounts(const std::vector& collected_d dump_account.code_hash = account->code_hash; dump_account.incarnation = account->incarnation; - if (account->incarnation > 0 && account->code_hash == silkworm::kEmptyHash) { - const auto storage_key{db::storage_prefix(full_view(address), account->incarnation)}; + if (account->incarnation > 0 && account->code_hash == kEmptyHash) { + const auto storage_key{db::storage_prefix(address.bytes, 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); + if (code_hash.length() == kHashLength) { + std::memcpy(dump_account.code_hash.bytes, code_hash.data(), kHashLength); } } if (!exclude_code) { @@ -126,27 +126,26 @@ Task AccountDumper::load_storage(BlockNum block_number, DumpAccounts& dump auto& address = it.first; auto& account = it.second; - std::map collected_entries; - StorageWalker::AccountCollector collector = [&](const evmc::address& /*address*/, silkworm::ByteView loc, silkworm::ByteView data) { + std::map collected_entries; // TODO(canepat) switch to ByteView? + StorageWalker::AccountCollector collector = [&](const evmc::address& /*address*/, ByteView loc, ByteView data) { if (!account.storage.has_value()) { account.storage = Storage{}; } auto& storage = *account.storage; - storage[silkworm::to_bytes32(loc)] = data; - auto hash = hash_of(loc); - auto key = full_view(hash); - collected_entries[silkworm::Bytes{key}] = data; + storage[to_bytes32(loc)] = data; + const auto hash = hash_of(loc); + collected_entries[Bytes{hash.bytes, kHashLength}] = data; return true; }; co_await storage_walker.walk_of_storages(block_number, address, start_location, account.incarnation, collector); - silkworm::trie::HashBuilder hb; + trie::HashBuilder hb; for (const auto& [key, value] : collected_entries) { - silkworm::Bytes encoded{}; - silkworm::rlp::encode(encoded, value); - silkworm::Bytes unpacked = silkworm::trie::unpack_nibbles(key); + Bytes encoded{}; + rlp::encode(encoded, value); + Bytes unpacked = trie::unpack_nibbles(key); hb.add_leaf(unpacked, encoded); } diff --git a/silkworm/rpc/core/account_dumper.hpp b/silkworm/rpc/core/account_dumper.hpp index 190ff7f5ef..61f5e7c6ca 100644 --- a/silkworm/rpc/core/account_dumper.hpp +++ b/silkworm/rpc/core/account_dumper.hpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include diff --git a/silkworm/rpc/core/account_walker.cpp b/silkworm/rpc/core/account_walker.cpp index 0ba9110f0e..9e4e4eb260 100644 --- a/silkworm/rpc/core/account_walker.cpp +++ b/silkworm/rpc/core/account_walker.cpp @@ -17,7 +17,6 @@ #include "account_walker.hpp" #include -#include #include #include #include @@ -27,7 +26,7 @@ namespace silkworm::rpc { Task AccountWalker::walk_of_accounts(BlockNum block_number, const evmc::address& start_address, Collector& collector) { auto ps_cursor = co_await transaction_.cursor(db::table::kPlainStateName); - auto start_key = full_view(start_address); + const ByteView start_key{start_address.bytes}; auto ps_kv = co_await seek(*ps_cursor, start_key, kAddressLength); if (ps_kv.key.empty()) { co_return; diff --git a/silkworm/rpc/core/blocks.cpp b/silkworm/rpc/core/blocks.cpp index 6cad04a326..7654c54ecc 100644 --- a/silkworm/rpc/core/blocks.cpp +++ b/silkworm/rpc/core/blocks.cpp @@ -33,7 +33,7 @@ constexpr const char* kSafeBlockHash = "safeBlockHash"; using namespace db::chain; static Task get_forkchoice_block_number(db::kv::api::Transaction& tx, const char* block_hash_tag) { - const auto kv_pair = co_await tx.get(db::table::kLastForkchoiceName, bytes_of_string(block_hash_tag)); + const auto kv_pair = co_await tx.get(db::table::kLastForkchoiceName, string_to_bytes(block_hash_tag)); const auto block_hash_data = kv_pair.value; if (block_hash_data.empty()) { co_return 0; @@ -128,7 +128,7 @@ Task get_latest_executed_block_number(db::kv::api::Transaction& tx) { } Task get_latest_block_number(db::kv::api::Transaction& tx) { - const auto kv_pair = co_await tx.get(db::table::kLastForkchoiceName, bytes_of_string(kHeadBlockHash)); + const auto kv_pair = co_await tx.get(db::table::kLastForkchoiceName, string_to_bytes(kHeadBlockHash)); const auto head_block_hash_data = kv_pair.value; if (!head_block_hash_data.empty()) { const auto head_block_hash = to_bytes32(head_block_hash_data); diff --git a/silkworm/rpc/core/blocks_test.cpp b/silkworm/rpc/core/blocks_test.cpp index 984467fdfe..584506497e 100644 --- a/silkworm/rpc/core/blocks_test.cpp +++ b/silkworm/rpc/core/blocks_test.cpp @@ -37,7 +37,7 @@ using testing::_; using testing::InvokeWithoutArgs; static silkworm::Bytes kNumber{*silkworm::from_hex("00000000003D0900")}; -static silkworm::Bytes block_hash = silkworm::bytes_of_string(std::string("0x439816753229fc0736bf86a5048de4bc9fcdede8c91dadf88c828c76b2281dff")); +static silkworm::Bytes block_hash = string_to_bytes(std::string("0x439816753229fc0736bf86a5048de4bc9fcdede8c91dadf88c828c76b2281dff")); static silkworm::Bytes kHeader{*silkworm::from_hex( "f9025ca0209f062567c161c5f71b3f57a7de277b0e95c3455050b152d785ad" "7524ef8ee7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000" diff --git a/silkworm/rpc/core/estimate_gas_oracle.cpp b/silkworm/rpc/core/estimate_gas_oracle.cpp index 232732b0ea..04929ad5b9 100644 --- a/silkworm/rpc/core/estimate_gas_oracle.cpp +++ b/silkworm/rpc/core/estimate_gas_oracle.cpp @@ -19,7 +19,6 @@ #include #include -#include #include #include #include diff --git a/silkworm/rpc/core/evm_executor.cpp b/silkworm/rpc/core/evm_executor.cpp index 148ad03557..63650185fd 100644 --- a/silkworm/rpc/core/evm_executor.cpp +++ b/silkworm/rpc/core/evm_executor.cpp @@ -23,11 +23,11 @@ #include #include +#include #include #include #include #include -#include #include #include #include @@ -46,7 +46,7 @@ std::string ExecutionResult::error_message(bool full_error) const { } static Bytes build_abi_selector(const std::string& signature) { - const auto signature_hash = hash_of(byte_view_of_string(signature)); + const auto signature_hash = hash_of(string_view_to_byte_view(signature)); return {std::begin(signature_hash.bytes), std::begin(signature_hash.bytes) + 4}; } diff --git a/silkworm/rpc/core/override_state.cpp b/silkworm/rpc/core/override_state.cpp index 6850b6bd96..ef0a15e62e 100644 --- a/silkworm/rpc/core/override_state.cpp +++ b/silkworm/rpc/core/override_state.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include diff --git a/silkworm/rpc/core/receipts.cpp b/silkworm/rpc/core/receipts.cpp index 103b6d98c2..12bdb96c42 100644 --- a/silkworm/rpc/core/receipts.cpp +++ b/silkworm/rpc/core/receipts.cpp @@ -18,7 +18,6 @@ #include #include -#include #include #include #include @@ -61,7 +60,7 @@ Task> read_receipts(db::kv::api::Transaction& tx, const for (size_t i{0}; i < receipts.size(); i++) { // The tx hash can be calculated by the tx content itself auto tx_hash{transactions[i].hash()}; - receipts[i].tx_hash = silkworm::to_bytes32(full_view(tx_hash.bytes)); + receipts[i].tx_hash = to_bytes32(tx_hash.bytes); receipts[i].tx_index = uint32_t(i); receipts[i].block_hash = block_hash; diff --git a/silkworm/rpc/core/storage_walker.cpp b/silkworm/rpc/core/storage_walker.cpp index fc6920dcff..f07f4062b1 100644 --- a/silkworm/rpc/core/storage_walker.cpp +++ b/silkworm/rpc/core/storage_walker.cpp @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -32,17 +31,17 @@ namespace silkworm::rpc { -silkworm::Bytes make_key(const evmc::address& address, const evmc::bytes32& location) { - silkworm::Bytes res(silkworm::kAddressLength + silkworm::kHashLength, '\0'); - std::memcpy(&res[0], address.bytes, silkworm::kAddressLength); - std::memcpy(&res[silkworm::kAddressLength], location.bytes, silkworm::kHashLength); +Bytes make_key(const evmc::address& address, const evmc::bytes32& location) { + Bytes res(silkworm::kAddressLength + kHashLength, '\0'); + std::memcpy(&res[0], address.bytes, kAddressLength); + std::memcpy(&res[kAddressLength], location.bytes, kHashLength); return res; } struct StorageItem { - silkworm::Bytes key; - silkworm::Bytes sec_key; - silkworm::Bytes value; + Bytes key; + Bytes sec_key; + Bytes value; }; bool operator<(const StorageItem& k1, const StorageItem& k2) { @@ -174,7 +173,7 @@ Task StorageWalker::storage_range_at( const evmc::bytes32& start_location, size_t max_result, StorageCollector& collector) { - auto account_data = co_await transaction_.get_one(db::table::kPlainStateName, full_view(address)); + auto account_data = co_await transaction_.get_one(db::table::kPlainStateName, address.bytes); auto account = silkworm::Account::from_encoded_storage(account_data); silkworm::success_or_throw(account); @@ -192,7 +191,7 @@ Task StorageWalker::storage_range_at( StorageItem storage_item; storage_item.key = loc; - storage_item.sec_key = full_view(hash); + storage_item.sec_key = ByteView{hash.bytes}; storage_item.value = data; if (storage.find(storage_item) != storage.end()) { diff --git a/silkworm/rpc/ethbackend/remote_backend.cpp b/silkworm/rpc/ethbackend/remote_backend.cpp index 1f03eea9f6..9262c59c67 100644 --- a/silkworm/rpc/ethbackend/remote_backend.cpp +++ b/silkworm/rpc/ethbackend/remote_backend.cpp @@ -22,8 +22,8 @@ #include #include +#include #include -#include #include #include #include @@ -148,12 +148,12 @@ Task RemoteBackEnd::get_block(BlockNum block_number, const HashAsSpan& has request.set_block_height(block_number); request.set_allocated_block_hash(H256_from_bytes(hash).release()); const auto reply = co_await get_block_rpc.finish_on(executor_, request); - ByteView block_rlp{byte_view_of_string(reply.block_rlp())}; + ByteView block_rlp{string_view_to_byte_view(reply.block_rlp())}; if (const auto decode_result{rlp::decode(block_rlp, block)}; !decode_result) { co_return false; } if (read_senders) { - ByteView senders{byte_view_of_string(reply.senders())}; + ByteView senders{string_view_to_byte_view(reply.senders())}; if (senders.size() % kAddressLength == 0 && senders.size() / kAddressLength == block.transactions.size()) { std::vector sender_addresses; sender_addresses.reserve(block.transactions.size()); @@ -183,7 +183,7 @@ std::vector RemoteBackEnd::decode(const ::google::protobuf::RepeatedPtrFi std::vector encoded_transactions; encoded_transactions.reserve(static_cast(grpc_txs.size())); for (const auto& grpc_tx_string : grpc_txs) { - encoded_transactions.push_back(bytes_of_string(grpc_tx_string)); + encoded_transactions.push_back(string_to_bytes(grpc_tx_string)); } return encoded_transactions; } diff --git a/silkworm/rpc/ethdb/bitmap.cpp b/silkworm/rpc/ethdb/bitmap.cpp index 2775cb8233..6f9bdf0f0d 100644 --- a/silkworm/rpc/ethdb/bitmap.cpp +++ b/silkworm/rpc/ethdb/bitmap.cpp @@ -24,7 +24,6 @@ #include #include -#include #include #include diff --git a/silkworm/rpc/ethdb/walk.cpp b/silkworm/rpc/ethdb/walk.cpp index 8c4b975549..886478d4ad 100644 --- a/silkworm/rpc/ethdb/walk.cpp +++ b/silkworm/rpc/ethdb/walk.cpp @@ -16,7 +16,6 @@ #include "walk.hpp" -#include #include namespace silkworm::rpc::ethdb { diff --git a/silkworm/rpc/stagedsync/stages.hpp b/silkworm/rpc/stagedsync/stages.hpp index d9fa3da129..61b0182005 100644 --- a/silkworm/rpc/stagedsync/stages.hpp +++ b/silkworm/rpc/stagedsync/stages.hpp @@ -20,15 +20,15 @@ #include #include +#include #include -#include #include namespace silkworm::rpc::stages { -const Bytes kHeaders = silkworm::bytes_of_string(db::stages::kHeadersKey); -const Bytes kExecution = silkworm::bytes_of_string(db::stages::kExecutionKey); -const Bytes kFinish = silkworm::bytes_of_string(db::stages::kFinishKey); +const Bytes kHeaders = string_to_bytes(db::stages::kHeadersKey); +const Bytes kExecution = string_to_bytes(db::stages::kExecutionKey); +const Bytes kFinish = string_to_bytes(db::stages::kFinishKey); Task get_sync_stage_progress(db::kv::api::Transaction& tx, const Bytes& stage_key); diff --git a/silkworm/rpc/txpool/miner.cpp b/silkworm/rpc/txpool/miner.cpp index fa32277e23..c50b84dd1f 100644 --- a/silkworm/rpc/txpool/miner.cpp +++ b/silkworm/rpc/txpool/miner.cpp @@ -17,7 +17,6 @@ #include "miner.hpp" #include -#include #include #include #include diff --git a/silkworm/rpc/types/execution_payload.cpp b/silkworm/rpc/types/execution_payload.cpp index c0d6ec61a6..460ec03e81 100644 --- a/silkworm/rpc/types/execution_payload.cpp +++ b/silkworm/rpc/types/execution_payload.cpp @@ -18,7 +18,6 @@ #include #include -#include #include namespace silkworm::rpc { diff --git a/silkworm/rpc/types/receipt.cpp b/silkworm/rpc/types/receipt.cpp index bb5dfa2613..cd7087ec5e 100644 --- a/silkworm/rpc/types/receipt.cpp +++ b/silkworm/rpc/types/receipt.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -58,13 +57,13 @@ std::ostream& operator<<(std::ostream& out, const Receipt& r) { return out; } -silkworm::Bloom bloom_from_logs(const Logs& logs) { +Bloom bloom_from_logs(const Logs& logs) { SILK_TRACE << "bloom_from_logs #logs: " << logs.size(); - silkworm::Bloom bloom{}; + Bloom bloom{}; for (auto const& log : logs) { - silkworm::m3_2048(bloom, full_view(log.address)); + m3_2048(bloom, log.address.bytes); for (const auto& topic : log.topics) { - silkworm::m3_2048(bloom, full_view(topic)); + m3_2048(bloom, topic.bytes); } } SILK_TRACE << "bloom_from_logs bloom: " << silkworm::to_hex(full_view(bloom)); diff --git a/silkworm/sync/internals/types.hpp b/silkworm/sync/internals/types.hpp index b01ff2f4bc..e2052b0fac 100644 --- a/silkworm/sync/internals/types.hpp +++ b/silkworm/sync/internals/types.hpp @@ -36,12 +36,6 @@ using duration_t = std::chrono::system_clock::duration; using seconds_t = std::chrono::seconds; using milliseconds_t = std::chrono::milliseconds; -// stream operator << -inline std::ostream& operator<<(std::ostream& out, const silkworm::ByteView& bytes) { - out << silkworm::to_hex(bytes); - return out; -} - // Peers using PeerId = Bytes;