From 168c77457fc8960c59efeb273a01062fdf7c32cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 26 Nov 2019 23:27:56 +0100 Subject: [PATCH 1/2] examples: Rewrite get_storage() not to create empty entries --- examples/example_host.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/example_host.cpp b/examples/example_host.cpp index ff1c987..547cf6d 100644 --- a/examples/example_host.cpp +++ b/examples/example_host.cpp @@ -59,9 +59,13 @@ public: evmc::bytes32 get_storage(const evmc::address& addr, const evmc::bytes32& key) noexcept final { - auto it = accounts.find(addr); - if (it != accounts.end()) - return it->second.storage[key]; + const auto account_iter = accounts.find(addr); + if (account_iter == accounts.end()) + return {}; + + const auto storage_iter = account_iter->second.storage.find(key); + if (storage_iter != account_iter->second.storage.end()) + return storage_iter->second; return {}; } From d8ab2489dd791213794d47f8cceee37419f2dad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 26 Nov 2019 21:49:56 +0100 Subject: [PATCH 2/2] cpp: Add const to HostInterface --- CHANGELOG.md | 2 ++ examples/example_host.cpp | 19 ++++++++++--------- include/evmc/evmc.hpp | 34 +++++++++++++++++----------------- include/evmc/mocked_host.hpp | 22 +++++++++++----------- test/unittests/test_cpp.cpp | 19 +++++++++++-------- 5 files changed, 51 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4df0b3..aee78eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning]. - In the Rust bindings mark read-only functions in `ExecutionContext` as non-mutating. [#444](https://github.com/ethereum/evmc/pull/444) +- In the C++ `HostInterface` the logically read-only methods are marked with `const`. + [#461](https://github.com/ethereum/evmc/pull/461) ## [7.0.0] „Istanbul Ready” — 2019-11-11 diff --git a/examples/example_host.cpp b/examples/example_host.cpp index 547cf6d..11c3aea 100644 --- a/examples/example_host.cpp +++ b/examples/example_host.cpp @@ -24,7 +24,7 @@ struct account std::vector code; std::map storage; - virtual evmc::bytes32 code_hash() + virtual evmc::bytes32 code_hash() const { // Extremely dumb "hash" function. evmc::bytes32 ret{}; @@ -52,12 +52,13 @@ public: ExampleHost(evmc_tx_context& _tx_context, evmc::accounts& _accounts) noexcept : accounts{_accounts}, tx_context{_tx_context} {}; - bool account_exists(const evmc::address& addr) noexcept final + bool account_exists(const evmc::address& addr) const noexcept final { return accounts.find(addr) != accounts.end(); } - evmc::bytes32 get_storage(const evmc::address& addr, const evmc::bytes32& key) noexcept final + evmc::bytes32 get_storage(const evmc::address& addr, const evmc::bytes32& key) const + noexcept final { const auto account_iter = accounts.find(addr); if (account_iter == accounts.end()) @@ -80,7 +81,7 @@ public: return (prev_value == value) ? EVMC_STORAGE_UNCHANGED : EVMC_STORAGE_MODIFIED; } - evmc::uint256be get_balance(const evmc::address& addr) noexcept final + evmc::uint256be get_balance(const evmc::address& addr) const noexcept final { auto it = accounts.find(addr); if (it != accounts.end()) @@ -88,7 +89,7 @@ public: return {}; } - size_t get_code_size(const evmc::address& addr) noexcept final + size_t get_code_size(const evmc::address& addr) const noexcept final { auto it = accounts.find(addr); if (it != accounts.end()) @@ -96,7 +97,7 @@ public: return 0; } - evmc::bytes32 get_code_hash(const evmc::address& addr) noexcept final + evmc::bytes32 get_code_hash(const evmc::address& addr) const noexcept final { auto it = accounts.find(addr); if (it != accounts.end()) @@ -107,7 +108,7 @@ public: size_t copy_code(const evmc::address& addr, size_t code_offset, uint8_t* buffer_data, - size_t buffer_size) noexcept final + size_t buffer_size) const noexcept final { const auto it = accounts.find(addr); if (it == accounts.end()) @@ -136,9 +137,9 @@ public: return {EVMC_REVERT, msg.gas, msg.input_data, msg.input_size}; } - evmc_tx_context get_tx_context() noexcept final { return tx_context; } + evmc_tx_context get_tx_context() const noexcept final { return tx_context; } - evmc::bytes32 get_block_hash(int64_t number) noexcept final + evmc::bytes32 get_block_hash(int64_t number) const noexcept final { const int64_t current_block_number = get_tx_context().block_number; diff --git a/include/evmc/evmc.hpp b/include/evmc/evmc.hpp index 7acfd1a..2c7597f 100644 --- a/include/evmc/evmc.hpp +++ b/include/evmc/evmc.hpp @@ -342,10 +342,10 @@ public: virtual ~HostInterface() noexcept = default; /// @copydoc evmc_host_interface::account_exists - virtual bool account_exists(const address& addr) noexcept = 0; + virtual bool account_exists(const address& addr) const noexcept = 0; /// @copydoc evmc_host_interface::get_storage - virtual bytes32 get_storage(const address& addr, const bytes32& key) noexcept = 0; + virtual bytes32 get_storage(const address& addr, const bytes32& key) const noexcept = 0; /// @copydoc evmc_host_interface::set_storage virtual evmc_storage_status set_storage(const address& addr, @@ -353,19 +353,19 @@ public: const bytes32& value) noexcept = 0; /// @copydoc evmc_host_interface::get_balance - virtual uint256be get_balance(const address& addr) noexcept = 0; + virtual uint256be get_balance(const address& addr) const noexcept = 0; /// @copydoc evmc_host_interface::get_code_size - virtual size_t get_code_size(const address& addr) noexcept = 0; + virtual size_t get_code_size(const address& addr) const noexcept = 0; /// @copydoc evmc_host_interface::get_code_hash - virtual bytes32 get_code_hash(const address& addr) noexcept = 0; + virtual bytes32 get_code_hash(const address& addr) const noexcept = 0; /// @copydoc evmc_host_interface::copy_code virtual size_t copy_code(const address& addr, size_t code_offset, uint8_t* buffer_data, - size_t buffer_size) noexcept = 0; + size_t buffer_size) const noexcept = 0; /// @copydoc evmc_host_interface::selfdestruct virtual void selfdestruct(const address& addr, const address& beneficiary) noexcept = 0; @@ -374,10 +374,10 @@ public: virtual result call(const evmc_message& msg) noexcept = 0; /// @copydoc evmc_host_interface::get_tx_context - virtual evmc_tx_context get_tx_context() noexcept = 0; + virtual evmc_tx_context get_tx_context() const noexcept = 0; /// @copydoc evmc_host_interface::get_block_hash - virtual bytes32 get_block_hash(int64_t block_number) noexcept = 0; + virtual bytes32 get_block_hash(int64_t block_number) const noexcept = 0; /// @copydoc evmc_host_interface::emit_log virtual void emit_log(const address& addr, @@ -395,7 +395,7 @@ class HostContext : public HostInterface { const evmc_host_interface* host = nullptr; evmc_host_context* context = nullptr; - evmc_tx_context tx_context = {}; + mutable evmc_tx_context tx_context = {}; public: /// Default constructor for null Host context. @@ -408,12 +408,12 @@ public: : host{&interface}, context{ctx} {} - bool account_exists(const address& address) noexcept final + bool account_exists(const address& address) const noexcept final { return host->account_exists(context, &address); } - bytes32 get_storage(const address& address, const bytes32& key) noexcept final + bytes32 get_storage(const address& address, const bytes32& key) const noexcept final { return host->get_storage(context, &address, &key); } @@ -425,17 +425,17 @@ public: return host->set_storage(context, &address, &key, &value); } - uint256be get_balance(const address& address) noexcept final + uint256be get_balance(const address& address) const noexcept final { return host->get_balance(context, &address); } - size_t get_code_size(const address& address) noexcept final + size_t get_code_size(const address& address) const noexcept final { return host->get_code_size(context, &address); } - bytes32 get_code_hash(const address& address) noexcept final + bytes32 get_code_hash(const address& address) const noexcept final { return host->get_code_hash(context, &address); } @@ -443,7 +443,7 @@ public: size_t copy_code(const address& address, size_t code_offset, uint8_t* buffer_data, - size_t buffer_size) noexcept final + size_t buffer_size) const noexcept final { return host->copy_code(context, &address, code_offset, buffer_data, buffer_size); } @@ -464,14 +464,14 @@ public: /// by assuming that the block timestamp should never be zero. /// /// @return The cached transaction context. - evmc_tx_context get_tx_context() noexcept final + evmc_tx_context get_tx_context() const noexcept final { if (tx_context.block_timestamp == 0) tx_context = host->get_tx_context(context); return tx_context; } - bytes32 get_block_hash(int64_t number) noexcept final + bytes32 get_block_hash(int64_t number) const noexcept final { return host->get_block_hash(context, number); } diff --git a/include/evmc/mocked_host.hpp b/include/evmc/mocked_host.hpp index 6be2e49..181de73 100644 --- a/include/evmc/mocked_host.hpp +++ b/include/evmc/mocked_host.hpp @@ -108,10 +108,10 @@ public: evmc_result call_result = {}; /// The record of all block numbers for which get_block_hash() was called. - std::vector recorded_blockhashes; + mutable std::vector recorded_blockhashes; /// The record of all account accesses. - std::vector
recorded_account_accesses; + mutable std::vector
recorded_account_accesses; /// The maximum number of entries in recorded_account_accesses record. /// This is arbitrary value useful in fuzzing when we don't want the record to explode. @@ -136,7 +136,7 @@ protected: /// Record an account access. /// @param addr The address of the accessed account. - void record_account_access(const address& addr) + void record_account_access(const address& addr) const { if (recorded_account_accesses.empty()) recorded_account_accesses.reserve(max_recorded_account_accesses); @@ -146,14 +146,14 @@ protected: } /// Returns true if an account exists (EVMC Host method). - bool account_exists(const address& addr) noexcept override + bool account_exists(const address& addr) const noexcept override { record_account_access(addr); return accounts.count(addr) != 0; } /// Get the account's storage value at the given key (EVMC Host method). - bytes32 get_storage(const address& addr, const bytes32& key) noexcept override + bytes32 get_storage(const address& addr, const bytes32& key) const noexcept override { record_account_access(addr); @@ -204,7 +204,7 @@ protected: } /// Get the account's balance (EVMC Host method). - uint256be get_balance(const address& addr) noexcept override + uint256be get_balance(const address& addr) const noexcept override { record_account_access(addr); const auto it = accounts.find(addr); @@ -215,7 +215,7 @@ protected: } /// Get the account's code size (EVMC host method). - size_t get_code_size(const address& addr) noexcept override + size_t get_code_size(const address& addr) const noexcept override { record_account_access(addr); const auto it = accounts.find(addr); @@ -225,7 +225,7 @@ protected: } /// Get the account's code hash (EVMC host method). - bytes32 get_code_hash(const address& addr) noexcept override + bytes32 get_code_hash(const address& addr) const noexcept override { record_account_access(addr); const auto it = accounts.find(addr); @@ -238,7 +238,7 @@ protected: size_t copy_code(const address& addr, size_t code_offset, uint8_t* buffer_data, - size_t buffer_size) noexcept override + size_t buffer_size) const noexcept override { record_account_access(addr); const auto it = accounts.find(addr); @@ -290,10 +290,10 @@ protected: } /// Get transaction context (EVMC host method). - evmc_tx_context get_tx_context() noexcept override { return tx_context; } + evmc_tx_context get_tx_context() const noexcept override { return tx_context; } /// Get the block header hash (EVMC host method). - bytes32 get_block_hash(int64_t block_number) noexcept override + bytes32 get_block_hash(int64_t block_number) const noexcept override { recorded_blockhashes.emplace_back(block_number); return block_hash; diff --git a/test/unittests/test_cpp.cpp b/test/unittests/test_cpp.cpp index 0ed28cc..396ecfb 100644 --- a/test/unittests/test_cpp.cpp +++ b/test/unittests/test_cpp.cpp @@ -20,9 +20,9 @@ class NullHost : public evmc::Host { public: - bool account_exists(const evmc::address&) noexcept final { return false; } + bool account_exists(const evmc::address&) const noexcept final { return false; } - evmc::bytes32 get_storage(const evmc::address&, const evmc::bytes32&) noexcept final + evmc::bytes32 get_storage(const evmc::address&, const evmc::bytes32&) const noexcept final { return {}; } @@ -34,21 +34,24 @@ public: return {}; } - evmc::uint256be get_balance(const evmc::address&) noexcept final { return {}; } + evmc::uint256be get_balance(const evmc::address&) const noexcept final { return {}; } - size_t get_code_size(const evmc::address&) noexcept final { return 0; } + size_t get_code_size(const evmc::address&) const noexcept final { return 0; } - evmc::bytes32 get_code_hash(const evmc::address&) noexcept final { return {}; } + evmc::bytes32 get_code_hash(const evmc::address&) const noexcept final { return {}; } - size_t copy_code(const evmc::address&, size_t, uint8_t*, size_t) noexcept final { return 0; } + size_t copy_code(const evmc::address&, size_t, uint8_t*, size_t) const noexcept final + { + return 0; + } void selfdestruct(const evmc::address&, const evmc::address&) noexcept final {} evmc::result call(const evmc_message&) noexcept final { return evmc::result{evmc_result{}}; } - evmc_tx_context get_tx_context() noexcept final { return {}; } + evmc_tx_context get_tx_context() const noexcept final { return {}; } - evmc::bytes32 get_block_hash(int64_t) noexcept final { return {}; } + evmc::bytes32 get_block_hash(int64_t) const noexcept final { return {}; } void emit_log(const evmc::address&, const uint8_t*,