diff --git a/include/evmc/helpers.hpp b/include/evmc/helpers.hpp index 9e404ad..d4624ae 100644 --- a/include/evmc/helpers.hpp +++ b/include/evmc/helpers.hpp @@ -1,6 +1,6 @@ /* EVMC: Ethereum Client-VM Connector API. - * Copyright 2018 The EVMC Authors. - * Licensed under the Apache License, Version 2.0. See the LICENSE file. + * Copyright 2019 The EVMC Authors. + * Licensed under the Apache License, Version 2.0. */ /** @@ -41,6 +41,18 @@ 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). +inline bool is_zero(const evmc_address& address) noexcept +{ + return address == evmc_address{}; +} + +/// Check if the hash is zero (all bytes are zeros). +inline bool is_zero(const evmc_bytes32& x) noexcept +{ + return x == evmc_bytes32{}; +} + /// FNV1a hash function with 64-bit result. inline uint64_t fnv1a_64(const uint8_t* ptr, size_t len) { diff --git a/test/unittests/test_helpers.cpp b/test/unittests/test_helpers.cpp index b199146..76cf740 100644 --- a/test/unittests/test_helpers.cpp +++ b/test/unittests/test_helpers.cpp @@ -1,6 +1,6 @@ // EVMC: Ethereum Client-VM Connector API. -// Copyright 2018 The EVMC Authors. -// Licensed under the Apache License, Version 2.0. See the LICENSE file. +// Copyright 2019 The EVMC Authors. +// Licensed under the Apache License, Version 2.0. #include @@ -27,3 +27,16 @@ TEST(helpers, maps) unordered_storage.emplace(*storage.begin()); EXPECT_EQ(unordered_storage.size(), 1); } + +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)); +}