From 7e4db85487603359563596e9c5fb84f25be882f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 24 Jul 2019 12:57:45 +0200 Subject: [PATCH 1/2] cpp: Deprecate helpers.hpp --- CHANGELOG.md | 3 +++ include/evmc/helpers.hpp | 16 ++++++++++++---- test/unittests/test_helpers.cpp | 22 ---------------------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd45f5417..87017b031 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)] diff --git a/include/evmc/helpers.hpp b/include/evmc/helpers.hpp index c4f55e940..f865b0185 100644 --- a/include/evmc/helpers.hpp +++ b/include/evmc/helpers.hpp @@ -18,39 +18,47 @@ #include /// The comparator for std::map. +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_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). @@ -98,7 +106,7 @@ namespace std { /// Hash operator template specialization for evmc_address needed for unordered containers. template <> -struct hash +struct EVMC_DEPRECATED hash { /// Hash operator using FNV1a. size_t operator()(const evmc_address& s) const noexcept @@ -109,7 +117,7 @@ struct hash /// Hash operator template needed for std::unordered_set and others using hashes. template <> -struct hash +struct EVMC_DEPRECATED hash { /// Hash operator using FNV1a. size_t operator()(const evmc_bytes32& s) const noexcept diff --git a/test/unittests/test_helpers.cpp b/test/unittests/test_helpers.cpp index e6b8ac676..201286b80 100644 --- a/test/unittests/test_helpers.cpp +++ b/test/unittests/test_helpers.cpp @@ -3,7 +3,6 @@ // Licensed under the Apache License, Version 2.0. #include -#include #include @@ -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) -{ - 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{}; From 87f2c680cbb30e9d804dd56dc39a18547b8f1153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 24 Jul 2019 13:05:17 +0200 Subject: [PATCH 2/2] test: Bring back the unit tests of deprecated features --- test/unittests/CMakeLists.txt | 1 + test/unittests/test_deprecated.cpp | 50 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 test/unittests/test_deprecated.cpp diff --git a/test/unittests/CMakeLists.txt b/test/unittests/CMakeLists.txt index 957ad0dfd..200c2537b 100644 --- a/test/unittests/CMakeLists.txt +++ b/test/unittests/CMakeLists.txt @@ -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 diff --git a/test/unittests/test_deprecated.cpp b/test/unittests/test_deprecated.cpp new file mode 100644 index 000000000..46539d95d --- /dev/null +++ b/test/unittests/test_deprecated.cpp @@ -0,0 +1,50 @@ +// EVMC: Ethereum Client-VM Connector API. +// Copyright 2019 The EVMC Authors. +// Licensed under the Apache License, Version 2.0. + +#include + +#include +#include +#include + +#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 addresses; + addresses[{}] = true; + ASSERT_EQ(addresses.size(), 1); + + std::unordered_map unordered_addresses; + unordered_addresses.emplace(*addresses.begin()); + EXPECT_EQ(unordered_addresses.size(), 1); + + std::map storage; + storage[{}] = true; + ASSERT_EQ(storage.size(), 1); + + std::unordered_map unordered_storage; + unordered_storage.emplace(*storage.begin()); + EXPECT_EQ(unordered_storage.size(), 1); +}