Merge pull request #461 from ethereum/cpp_host_interface_const

cpp: Add const to HostInterface
This commit is contained in:
Paweł Bylica 2019-11-27 17:53:46 +01:00 committed by GitHub
commit 95c01b648b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 48 deletions

View File

@ -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

View File

@ -24,7 +24,7 @@ struct account
std::vector<uint8_t> code;
std::map<evmc::bytes32, evmc::bytes32> storage;
virtual evmc::bytes32 code_hash()
virtual evmc::bytes32 code_hash() const
{
// Extremely dumb "hash" function.
evmc::bytes32 ret{};
@ -52,16 +52,21 @@ 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
{
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 {};
}
@ -76,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())
@ -84,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())
@ -92,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())
@ -103,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())
@ -132,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;

View File

@ -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);
}

View File

@ -108,10 +108,10 @@ public:
evmc_result call_result = {};
/// The record of all block numbers for which get_block_hash() was called.
std::vector<int64_t> recorded_blockhashes;
mutable std::vector<int64_t> recorded_blockhashes;
/// The record of all account accesses.
std::vector<address> recorded_account_accesses;
mutable std::vector<address> 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;

View File

@ -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*,