Skip to content

Commit

Permalink
core: fix Hash::from_hex (#1279)
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat authored Jun 24, 2023
1 parent 5f3f7ad commit 0bbf3a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
14 changes: 2 additions & 12 deletions silkworm/core/types/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <optional>
#include <span>
#include <string>

Expand All @@ -41,7 +42,7 @@ class Hash : public evmc::bytes32 {
static constexpr size_t length() { return sizeof(evmc::bytes32); }

[[nodiscard]] std::string to_hex() const { return silkworm::to_hex(*this); }
static Hash from_hex(const std::string& hex) { return {evmc::from_hex<Hash>(hex).value()}; }
static std::optional<Hash> from_hex(const std::string& hex) { return evmc::from_hex<Hash>(hex); }

// conversion to ByteView is handled in ByteView class,
// conversion operator Byte() { return {bytes, length()}; } is handled elsewhere
Expand All @@ -52,17 +53,6 @@ class Hash : public evmc::bytes32 {
using HashAsSpan = std::span<const uint8_t, kHashLength>;
using HashAsArray = const uint8_t (&)[kHashLength];

// RLP encoding: usually same as ByteView, some tricks for MSVC overload resolution
namespace rlp {
inline size_t length_hash(const Hash&) { return kHashLength + 1; }

void encode_hash(Bytes& to, const Hash& h);

// template <>
DecodingResult decode_hash(ByteView& from, Hash& to) noexcept;

} // namespace rlp

} // namespace silkworm

namespace std {
Expand Down
30 changes: 30 additions & 0 deletions silkworm/core/types/hash_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
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 "hash.hpp"

#include <catch2/catch.hpp>

namespace silkworm {

TEST_CASE("from_hex") {
CHECK(Hash::from_hex("foo") == std::nullopt);

const auto kHashValue{0x2d690516512020171c1ec870f6ff45398cc8609250326be89915fb538e7b_bytes32};
CHECK(Hash::from_hex("0x2d690516512020171c1ec870f6ff45398cc8609250326be89915fb538e7b") == kHashValue);
}

} // namespace silkworm
6 changes: 3 additions & 3 deletions silkworm/sync/packets/packet_coding_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TEST_CASE("NewBlockHashesPacket encoding") {
NewBlockHashesPacket packet;

NewBlockHash newBlock;
newBlock.hash = Hash::from_hex("eb2c33963824bf97d01cff8a65f00dc402fbf64f473cb4778a547ac08cebc354");
newBlock.hash = *Hash::from_hex("eb2c33963824bf97d01cff8a65f00dc402fbf64f473cb4778a547ac08cebc354");
newBlock.number = 12'420'112;
packet.push_back(newBlock);

Expand Down Expand Up @@ -1026,8 +1026,8 @@ TEST_CASE("GetBlockBodiesPacket (eth/66) encoding") {
GetBlockBodiesPacket66 packet;

packet.requestId = 0xae9405dbeebf3f01;
packet.request.push_back(Hash::from_hex("a36b1595c5acd878b63f83d3b62f6882edd27b757582f5319aebc17bc3e98246"));
packet.request.push_back(Hash::from_hex("9f20a871bf5151959fff4c88783bf4ef27b170a4cbe92b8f63ca1fe7d6ab829c"));
packet.request.push_back(*Hash::from_hex("a36b1595c5acd878b63f83d3b62f6882edd27b757582f5319aebc17bc3e98246"));
packet.request.push_back(*Hash::from_hex("9f20a871bf5151959fff4c88783bf4ef27b170a4cbe92b8f63ca1fe7d6ab829c"));

Bytes encoded;
rlp::encode(encoded, packet);
Expand Down

0 comments on commit 0bbf3a3

Please sign in to comment.