EVMC
mocked_host.hpp
1// EVMC: Ethereum Client-VM Connector API.
2// Copyright 2019 The EVMC Authors.
3// Licensed under the Apache License, Version 2.0.
4#pragma once
5
6#include <evmc/evmc.hpp>
7#include <algorithm>
8#include <cassert>
9#include <string>
10#include <unordered_map>
11#include <vector>
12
13namespace evmc
14{
16using bytes = std::basic_string<uint8_t>;
17
20{
23
26
29
31 StorageValue() noexcept = default;
32
34 StorageValue(const bytes32& _value, // NOLINT(hicpp-explicit-conversions)
35 evmc_access_status _access_status = EVMC_ACCESS_COLD) noexcept
36 : current{_value}, original{_value}, access_status{_access_status}
37 {}
38
40 StorageValue(const bytes32& _value,
41 const bytes32& _original,
42 evmc_access_status _access_status = EVMC_ACCESS_COLD) noexcept
43 : current{_value}, original{_original}, access_status{_access_status}
44 {}
45};
46
49{
51 int nonce = 0;
52
55
58
61
63 std::unordered_map<bytes32, StorageValue> storage;
64
66 std::unordered_map<bytes32, bytes32> transient_storage;
67
69 void set_balance(uint64_t x) noexcept
70 {
72 for (std::size_t i = 0; i < sizeof(x); ++i)
73 balance.bytes[sizeof(balance) - 1 - i] = static_cast<uint8_t>(x >> (8 * i));
74 }
75};
76
78class MockedHost : public Host
79{
80public:
83 {
86
89
91 std::vector<bytes32> topics;
92
94 bool operator==(const log_record& other) const noexcept
95 {
96 return creator == other.creator && data == other.data && topics == other.topics;
97 }
98 };
99
101 std::unordered_map<address, MockedAccount> accounts;
102
105
108
111
113 mutable std::vector<int64_t> recorded_blockhashes;
114
116 mutable std::vector<address> recorded_account_accesses;
117
120 static constexpr auto max_recorded_account_accesses = 200;
121
123 std::vector<evmc_message> recorded_calls;
124
127 static constexpr auto max_recorded_calls = 100;
128
130 std::vector<log_record> recorded_logs;
131
134 std::unordered_map<address, std::vector<address>> recorded_selfdestructs;
135
136private:
138 std::vector<bytes> m_recorded_calls_inputs;
139
142 void record_account_access(const address& addr) const
143 {
144 if (recorded_account_accesses.empty())
146
148 recorded_account_accesses.emplace_back(addr);
149 }
150
151public:
153 bool account_exists(const address& addr) const noexcept override
154 {
155 record_account_access(addr);
156 return accounts.count(addr) != 0;
157 }
158
160 bytes32 get_storage(const address& addr, const bytes32& key) const noexcept override
161 {
162 record_account_access(addr);
163
164 const auto account_iter = accounts.find(addr);
165 if (account_iter == accounts.end())
166 return {};
167
168 const auto storage_iter = account_iter->second.storage.find(key);
169 if (storage_iter != account_iter->second.storage.end())
170 return storage_iter->second.current;
171 return {};
172 }
173
176 const bytes32& key,
177 const bytes32& value) noexcept override
178 {
179 record_account_access(addr);
180
181 // Get the reference to the storage entry value.
182 // This will create the account in case it was not present.
183 // This is convenient for unit testing and standalone EVM execution to preserve the
184 // storage values after the execution terminates.
185 auto& s = accounts[addr].storage[key];
186
187 // Follow the EIP-2200 specification as closely as possible.
188 // https://eips.ethereum.org/EIPS/eip-2200
189 // Warning: this is not the most efficient implementation. The storage status can be
190 // figured out by combining only 4 checks:
191 // - original != current (dirty)
192 // - original == value (restored)
193 // - current != 0
194 // - value != 0
195 const auto status = [&original = s.original, &current = s.current, &value]() {
196 // Clause 1 is irrelevant:
197 // 1. "If gasleft is less than or equal to gas stipend,
198 // fail the current call frame with ‘out of gas’ exception"
199
200 // 2. "If current value equals new value (this is a no-op)"
201 if (current == value)
202 {
203 // "SLOAD_GAS is deducted"
205 }
206 // 3. "If current value does not equal new value"
207 else
208 {
209 // 3.1. "If original value equals current value
210 // (this storage slot has not been changed by the current execution context)"
211 if (original == current)
212 {
213 // 3.1.1 "If original value is 0"
214 if (is_zero(original))
215 {
216 // "SSTORE_SET_GAS is deducted"
217 return EVMC_STORAGE_ADDED;
218 }
219 // 3.1.2 "Otherwise"
220 else
221 {
222 // "SSTORE_RESET_GAS gas is deducted"
223 auto st = EVMC_STORAGE_MODIFIED;
224
225 // "If new value is 0"
226 if (is_zero(value))
227 {
228 // "add SSTORE_CLEARS_SCHEDULE gas to refund counter"
230 }
231
232 return st;
233 }
234 }
235 // 3.2. "If original value does not equal current value
236 // (this storage slot is dirty),
237 // SLOAD_GAS gas is deducted.
238 // Apply both of the following clauses."
239 else
240 {
241 // Because we need to apply "both following clauses"
242 // we first collect information which clause is triggered
243 // then assign status code to combination of these clauses.
244 enum
245 {
246 None = 0,
247 RemoveClearsSchedule = 1 << 0,
248 AddClearsSchedule = 1 << 1,
249 RestoredBySet = 1 << 2,
250 RestoredByReset = 1 << 3,
251 };
252 int triggered_clauses = None;
253
254 // 3.2.1. "If original value is not 0"
255 if (!is_zero(original))
256 {
257 // 3.2.1.1. "If current value is 0"
258 if (is_zero(current))
259 {
260 // "(also means that new value is not 0)"
261 assert(!is_zero(value));
262 // "remove SSTORE_CLEARS_SCHEDULE gas from refund counter"
263 triggered_clauses |= RemoveClearsSchedule;
264 }
265 // 3.2.1.2. "If new value is 0"
266 if (is_zero(value))
267 {
268 // "(also means that current value is not 0)"
269 assert(!is_zero(current));
270 // "add SSTORE_CLEARS_SCHEDULE gas to refund counter"
271 triggered_clauses |= AddClearsSchedule;
272 }
273 }
274
275 // 3.2.2. "If original value equals new value (this storage slot is reset)"
276 // Except: we use term 'storage slot restored'.
277 if (original == value)
278 {
279 // 3.2.2.1. "If original value is 0"
280 if (is_zero(original))
281 {
282 // "add SSTORE_SET_GAS - SLOAD_GAS to refund counter"
283 triggered_clauses |= RestoredBySet;
284 }
285 // 3.2.2.2. "Otherwise"
286 else
287 {
288 // "add SSTORE_RESET_GAS - SLOAD_GAS gas to refund counter"
289 triggered_clauses |= RestoredByReset;
290 }
291 }
292
293 switch (triggered_clauses)
294 {
295 case RemoveClearsSchedule:
297 case AddClearsSchedule:
299 case RemoveClearsSchedule | RestoredByReset:
301 case RestoredBySet:
303 case RestoredByReset:
305 case None:
307 default:
308 assert(false); // Other combinations are impossible.
309 return evmc_storage_status{};
310 }
311 }
312 }
313 }();
314
315 s.current = value; // Finally update the current storage value.
316 return status;
317 }
318
320 uint256be get_balance(const address& addr) const noexcept override
321 {
322 record_account_access(addr);
323 const auto it = accounts.find(addr);
324 if (it == accounts.end())
325 return {};
326
327 return it->second.balance;
328 }
329
331 size_t get_code_size(const address& addr) const noexcept override
332 {
333 record_account_access(addr);
334 const auto it = accounts.find(addr);
335 if (it == accounts.end())
336 return 0;
337 return it->second.code.size();
338 }
339
341 bytes32 get_code_hash(const address& addr) const noexcept override
342 {
343 record_account_access(addr);
344 const auto it = accounts.find(addr);
345 if (it == accounts.end())
346 return {};
347 return it->second.codehash;
348 }
349
351 size_t copy_code(const address& addr,
352 size_t code_offset,
353 uint8_t* buffer_data,
354 size_t buffer_size) const noexcept override
355 {
356 record_account_access(addr);
357 const auto it = accounts.find(addr);
358 if (it == accounts.end())
359 return 0;
360
361 const auto& code = it->second.code;
362
363 if (code_offset >= code.size())
364 return 0;
365
366 const auto n = std::min(buffer_size, code.size() - code_offset);
367
368 if (n > 0)
369 std::copy_n(&code[code_offset], n, buffer_data);
370 return n;
371 }
372
374 bool selfdestruct(const address& addr, const address& beneficiary) noexcept override
375 {
376 record_account_access(addr);
377 auto& beneficiaries = recorded_selfdestructs[addr];
378 beneficiaries.emplace_back(beneficiary);
379 return beneficiaries.size() == 1;
380 }
381
383 Result call(const evmc_message& msg) noexcept override
384 {
385 record_account_access(msg.recipient);
386
387 if (recorded_calls.empty())
388 {
390 m_recorded_calls_inputs.reserve(max_recorded_calls); // Iterators will not invalidate.
391 }
392
394 {
395 recorded_calls.emplace_back(msg);
396 auto& call_msg = recorded_calls.back();
397 if (call_msg.input_size > 0)
398 {
399 m_recorded_calls_inputs.emplace_back(call_msg.input_data, call_msg.input_size);
400 const auto& input_copy = m_recorded_calls_inputs.back();
401 call_msg.input_data = input_copy.data();
402 }
403 }
404 return Result{call_result};
405 }
406
408 evmc_tx_context get_tx_context() const noexcept override { return tx_context; }
409
411 bytes32 get_block_hash(int64_t block_number) const noexcept override
412 {
413 recorded_blockhashes.emplace_back(block_number);
414 return block_hash;
415 }
416
418 void emit_log(const address& addr,
419 const uint8_t* data,
420 size_t data_size,
421 const bytes32 topics[],
422 size_t topics_count) noexcept override
423 {
424 recorded_logs.push_back({addr, {data, data_size}, {topics, topics + topics_count}});
425 }
426
443 evmc_access_status access_account(const address& addr) noexcept override
444 {
445 // Check if the address have been already accessed.
446 const auto already_accessed =
447 std::find(recorded_account_accesses.begin(), recorded_account_accesses.end(), addr) !=
449
450 record_account_access(addr);
451
452 // Accessing precompiled contracts is always warm.
453 if (addr >= 0x0000000000000000000000000000000000000001_address &&
454 addr <= 0x0000000000000000000000000000000000000009_address)
455 return EVMC_ACCESS_WARM;
456
457 return already_accessed ? EVMC_ACCESS_WARM : EVMC_ACCESS_COLD;
458 }
459
474 evmc_access_status access_storage(const address& addr, const bytes32& key) noexcept override
475 {
476 auto& value = accounts[addr].storage[key];
477 const auto access_status = value.access_status;
478 value.access_status = EVMC_ACCESS_WARM;
479 return access_status;
480 }
481
487 bytes32 get_transient_storage(const address& addr, const bytes32& key) const noexcept override
488 {
489 record_account_access(addr);
490
491 const auto account_iter = accounts.find(addr);
492 if (account_iter == accounts.end())
493 return {};
494
495 const auto storage_iter = account_iter->second.transient_storage.find(key);
496 if (storage_iter != account_iter->second.transient_storage.end())
497 return storage_iter->second;
498 return {};
499 }
500
507 const bytes32& key,
508 const bytes32& value) noexcept override
509 {
510 record_account_access(addr);
511 accounts[addr].transient_storage[key] = value;
512 }
513};
514} // namespace evmc
Abstract class to be used by Host implementations.
Definition: evmc.hpp:624
Mocked EVMC Host implementation.
Definition: mocked_host.hpp:79
size_t get_code_size(const address &addr) const noexcept override
Get the account's code size (EVMC host method).
size_t copy_code(const address &addr, size_t code_offset, uint8_t *buffer_data, size_t buffer_size) const noexcept override
Copy the account's code to the given buffer (EVMC host method).
uint256be get_balance(const address &addr) const noexcept override
Get the account's balance (EVMC Host method).
void emit_log(const address &addr, const uint8_t *data, size_t data_size, const bytes32 topics[], size_t topics_count) noexcept override
Emit LOG (EVMC host method).
evmc_tx_context get_tx_context() const noexcept override
Get transaction context (EVMC host method).
evmc_result call_result
The call result to be returned by the call() method.
bytes32 block_hash
The block header hash value to be returned by get_block_hash().
evmc_access_status access_account(const address &addr) noexcept override
Record an account access.
bool account_exists(const address &addr) const noexcept override
Returns true if an account exists (EVMC Host method).
static constexpr auto max_recorded_account_accesses
The maximum number of entries in recorded_account_accesses record.
std::unordered_map< address, MockedAccount > accounts
The set of all accounts in the Host, organized by their addresses.
Result call(const evmc_message &msg) noexcept override
Call/create other contract (EVMC host method).
bytes32 get_storage(const address &addr, const bytes32 &key) const noexcept override
Get the account's storage value at the given key (EVMC Host method).
std::vector< log_record > recorded_logs
The record of all LOGs passed to the emit_log() method.
evmc_access_status access_storage(const address &addr, const bytes32 &key) noexcept override
Access the account's storage value at the given key.
bytes32 get_code_hash(const address &addr) const noexcept override
Get the account's code hash (EVMC host method).
static constexpr auto max_recorded_calls
The maximum number of entries in recorded_calls record.
bytes32 get_transient_storage(const address &addr, const bytes32 &key) const noexcept override
Get account's transient storage.
evmc_storage_status set_storage(const address &addr, const bytes32 &key, const bytes32 &value) noexcept override
Set the account's storage value (EVMC Host method).
bool selfdestruct(const address &addr, const address &beneficiary) noexcept override
Selfdestruct the account (EVMC host method).
std::unordered_map< address, std::vector< address > > recorded_selfdestructs
The record of all SELFDESTRUCTs from the selfdestruct() method as a map selfdestructed_address => [be...
std::vector< int64_t > recorded_blockhashes
The record of all block numbers for which get_block_hash() was called.
bytes32 get_block_hash(int64_t block_number) const noexcept override
Get the block header hash (EVMC host method).
evmc_tx_context tx_context
The EVMC transaction context to be returned by get_tx_context().
void set_transient_storage(const address &addr, const bytes32 &key, const bytes32 &value) noexcept override
Set account's transient storage.
std::vector< address > recorded_account_accesses
The record of all account accesses.
std::vector< evmc_message > recorded_calls
The record of all call messages requested in the call() method.
The EVM code execution result.
Definition: evmc.hpp:332
evmc_access_status
Access status per EIP-2929: Gas cost increases for state access opcodes.
Definition: evmc.h:784
evmc_storage_status
The effect of an attempt to modify a contract storage item.
Definition: evmc.h:569
@ EVMC_ACCESS_COLD
The entry hasn't been accessed before – it's the first access.
Definition: evmc.h:788
@ EVMC_ACCESS_WARM
The entry is already in accessed_addresses or accessed_storage_keys.
Definition: evmc.h:793
@ EVMC_STORAGE_ADDED_DELETED
A storage item is deleted by changing the current dirty nonzero to the original zero value.
Definition: evmc.h:633
@ EVMC_STORAGE_MODIFIED_RESTORED
A storage item is modified by changing the current dirty nonzero to the original nonzero value other ...
Definition: evmc.h:640
@ EVMC_STORAGE_DELETED_RESTORED
A storage item is added by changing the current dirty zero to the original value.
Definition: evmc.h:626
@ EVMC_STORAGE_ADDED
A new storage item is added by changing the current clean zero to a nonzero value.
Definition: evmc.h:591
@ EVMC_STORAGE_MODIFIED_DELETED
A storage item is deleted by changing the current dirty nonzero to the zero value and the original va...
Definition: evmc.h:619
@ EVMC_STORAGE_ASSIGNED
The new/same value is assigned to the storage item without affecting the cost structure.
Definition: evmc.h:584
@ EVMC_STORAGE_DELETED
A storage item is deleted by changing the current clean nonzero to the zero value.
Definition: evmc.h:598
@ EVMC_STORAGE_DELETED_ADDED
A storage item is added by changing the current dirty zero to a nonzero value other than the original...
Definition: evmc.h:612
@ EVMC_STORAGE_MODIFIED
A storage item is modified by changing the current clean nonzero to other nonzero value.
Definition: evmc.h:605
EVMC C++ API - wrappers and bindings for C++.
Definition: evmc.hpp:22
std::basic_string< uint8_t > bytes
String of uint8_t chars.
Definition: hex.hpp:15
constexpr bool is_zero(const address &a) noexcept
Checks if the given address is the zero address.
Definition: evmc.hpp:261
Mocked account.
Definition: mocked_host.hpp:49
bytes32 codehash
The code hash. Can be a value not related to the actual code.
Definition: mocked_host.hpp:57
std::unordered_map< bytes32, StorageValue > storage
The account storage map.
Definition: mocked_host.hpp:63
int nonce
The account nonce.
Definition: mocked_host.hpp:51
bytes code
The account code.
Definition: mocked_host.hpp:54
uint256be balance
The account balance.
Definition: mocked_host.hpp:60
std::unordered_map< bytes32, bytes32 > transient_storage
The account transient storage.
Definition: mocked_host.hpp:66
void set_balance(uint64_t x) noexcept
Helper method for setting balance by numeric type.
Definition: mocked_host.hpp:69
address creator
The address of the account which created the log.
Definition: mocked_host.hpp:85
bool operator==(const log_record &other) const noexcept
Equal operator.
Definition: mocked_host.hpp:94
std::vector< bytes32 > topics
The log topics.
Definition: mocked_host.hpp:91
bytes data
The data attached to the log.
Definition: mocked_host.hpp:88
Extended value (with original value and access flag) for account storage.
Definition: mocked_host.hpp:20
bytes32 original
The original storage value.
Definition: mocked_host.hpp:25
evmc_access_status access_status
Is the storage key cold or warm.
Definition: mocked_host.hpp:28
StorageValue() noexcept=default
Default constructor.
StorageValue(const bytes32 &_value, const bytes32 &_original, evmc_access_status _access_status=EVMC_ACCESS_COLD) noexcept
Constructor with original value and optional access status.
Definition: mocked_host.hpp:40
bytes32 current
The current storage value.
Definition: mocked_host.hpp:22
The big-endian 160-bit hash suitable for keeping an Ethereum address.
Definition: evmc.hpp:30
The fixed size array of 32 bytes for storing 256-bit EVM values.
Definition: evmc.hpp:74
uint8_t bytes[32]
The 32 bytes.
Definition: evmc.h:59
The message describing an EVM call, including a zero-depth calls from a transaction origin.
Definition: evmc.h:98
The EVM code execution result.
Definition: evmc.h:416
The transaction and block data for execution.
Definition: evmc.h:214