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

Deprecate helpers.hpp #358

Merged
merged 2 commits into from
Jul 24, 2019
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
In C++ API `evmc::result::raw()` renamed to `evmc::result::release_raw()`.
- Changed: [[#311](https://github.com/ethereum/evmc/pull/311)]
In `evmc_load_and_create()` the `error_code` is optional (can be `NULL`).
- Deprecated: [[#358](https://github.com/ethereum/evmc/pull/358)]
The usage of `evmc/helpers.hpp` has been deprecated. Use `evmc/evmc.hpp`
which provides the same features.
- Fixed:
[[#261](https://github.com/ethereum/evmc/issues/261),
[#263](https://github.com/ethereum/evmc/pull/263)]
Expand Down
16 changes: 12 additions & 4 deletions include/evmc/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,47 @@
#include <functional>

/// The comparator for std::map<evmc_address, ...>.
EVMC_DEPRECATED
inline bool operator<(const evmc_address& a, const evmc_address& b)
{
return std::memcmp(a.bytes, b.bytes, sizeof(a.bytes)) < 0;
}

/// The comparator for std::map<evmc_bytes32, ...>.
EVMC_DEPRECATED
inline bool operator<(const evmc_bytes32& a, const evmc_bytes32& b)
{
return std::memcmp(a.bytes, b.bytes, sizeof(a.bytes)) < 0;
}

/// The comparator for equality.
EVMC_DEPRECATED
inline bool operator==(const evmc_address& a, const evmc_address& b)
{
return std::memcmp(a.bytes, b.bytes, sizeof(a.bytes)) == 0;
}

/// The comparator for equality.
EVMC_DEPRECATED
inline bool operator==(const evmc_bytes32& a, const evmc_bytes32& b)
{
return std::memcmp(a.bytes, b.bytes, sizeof(a.bytes)) == 0;
}

/// Check if the address is zero (all bytes are zeros).
EVMC_DEPRECATED
inline bool is_zero(const evmc_address& address) noexcept
{
return address == evmc_address{};
constexpr auto zero = evmc_address{};
return std::memcmp(address.bytes, zero.bytes, sizeof(zero.bytes)) == 0;
}

/// Check if the hash is zero (all bytes are zeros).
EVMC_DEPRECATED
inline bool is_zero(const evmc_bytes32& x) noexcept
{
return x == evmc_bytes32{};
constexpr auto zero = evmc_bytes32{};
return std::memcmp(x.bytes, zero.bytes, sizeof(zero.bytes)) == 0;
}

/// Parameters for the fnv1a hash function, specialized by the hash result size (size_t).
Expand Down Expand Up @@ -98,7 +106,7 @@ namespace std
{
/// Hash operator template specialization for evmc_address needed for unordered containers.
template <>
struct hash<evmc_address>
struct EVMC_DEPRECATED hash<evmc_address>
{
/// Hash operator using FNV1a.
size_t operator()(const evmc_address& s) const noexcept
Expand All @@ -109,7 +117,7 @@ struct hash<evmc_address>

/// Hash operator template needed for std::unordered_set and others using hashes.
template <>
struct hash<evmc_bytes32>
struct EVMC_DEPRECATED hash<evmc_bytes32>
{
/// Hash operator using FNV1a.
size_t operator()(const evmc_bytes32& s) const noexcept
Expand Down
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_executable(
evmc-unittests
loader_mock.h
test_cpp.cpp
test_deprecated.cpp
test_helpers.cpp
test_instructions.cpp
test_loader.cpp
Expand Down
50 changes: 50 additions & 0 deletions test/unittests/test_deprecated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// EVMC: Ethereum Client-VM Connector API.
// Copyright 2019 The EVMC Authors.
// Licensed under the Apache License, Version 2.0.

#include <evmc/helpers.hpp>

#include <gtest/gtest.h>
#include <map>
#include <unordered_map>

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

TEST(helpers, fnv1a)
{
const uint8_t text[] = {'E', 'V', 'M', 'C'};
const auto h = fnv1a(text, sizeof(text));
EXPECT_EQ(h, sizeof(size_t) == 8 ? 0x15e05d6d22fed89a : 0xffaa6a9a);
}

TEST(helpers, is_zero)
{
auto a = evmc_address{};
EXPECT_TRUE(is_zero(a));
a.bytes[0] = 1;
EXPECT_FALSE(is_zero(a));

auto b = evmc_bytes32{};
EXPECT_TRUE(is_zero(b));
b.bytes[0] = 1;
EXPECT_FALSE(is_zero(b));
}

TEST(helpers, maps)
{
std::map<evmc_address, bool> addresses;
addresses[{}] = true;
ASSERT_EQ(addresses.size(), 1);

std::unordered_map<evmc_address, bool> unordered_addresses;
unordered_addresses.emplace(*addresses.begin());
EXPECT_EQ(unordered_addresses.size(), 1);

std::map<evmc_bytes32, bool> storage;
storage[{}] = true;
ASSERT_EQ(storage.size(), 1);

std::unordered_map<evmc_bytes32, bool> unordered_storage;
unordered_storage.emplace(*storage.begin());
EXPECT_EQ(unordered_storage.size(), 1);
}
22 changes: 0 additions & 22 deletions test/unittests/test_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Licensed under the Apache License, Version 2.0.

#include <evmc/helpers.h>
#include <evmc/helpers.hpp>

#include <gtest/gtest.h>

Expand All @@ -27,27 +26,6 @@ static constexpr size_t optionalDataSize =
static_assert(optionalDataSize >= sizeof(evmc_result_optional_storage),
"evmc_result's optional data space is too small");


TEST(helpers, fnv1a)
{
const uint8_t text[] = {'E', 'V', 'M', 'C'};
const auto h = fnv1a(text, sizeof(text));
EXPECT_EQ(h, sizeof(size_t) == 8 ? 0x15e05d6d22fed89a : 0xffaa6a9a);
}

TEST(helpers, is_zero)
chfast marked this conversation as resolved.
Show resolved Hide resolved
{
auto a = evmc_address{};
EXPECT_TRUE(is_zero(a));
a.bytes[0] = 1;
EXPECT_FALSE(is_zero(a));

auto b = evmc_bytes32{};
EXPECT_TRUE(is_zero(b));
b.bytes[0] = 1;
EXPECT_FALSE(is_zero(b));
}

TEST(helpers, release_result)
{
auto r1 = evmc_result{};
Expand Down