11#include <initializer_list>
17 "latest stable revision ill-defined");
37 constexpr explicit address(uint64_t v) noexcept
50 static_cast<uint8_t
>(v >> 56),
51 static_cast<uint8_t
>(v >> 48),
52 static_cast<uint8_t
>(v >> 40),
53 static_cast<uint8_t
>(v >> 32),
54 static_cast<uint8_t
>(v >> 24),
55 static_cast<uint8_t
>(v >> 16),
56 static_cast<uint8_t
>(v >> 8),
57 static_cast<uint8_t
>(v >> 0)}}
61 inline constexpr explicit operator bool() const noexcept;
81 constexpr explicit bytes32(uint64_t v) noexcept
106 static_cast<uint8_t
>(v >> 56),
107 static_cast<uint8_t
>(v >> 48),
108 static_cast<uint8_t
>(v >> 40),
109 static_cast<uint8_t
>(v >> 32),
110 static_cast<uint8_t
>(v >> 24),
111 static_cast<uint8_t
>(v >> 16),
112 static_cast<uint8_t
>(v >> 8),
113 static_cast<uint8_t
>(v >> 0)}}
117 inline constexpr explicit operator bool() const noexcept;
128inline constexpr uint64_t
load64be(
const uint8_t* data)
noexcept
130 return (uint64_t{data[0]} << 56) | (uint64_t{data[1]} << 48) | (uint64_t{data[2]} << 40) |
131 (uint64_t{data[3]} << 32) | (uint64_t{data[4]} << 24) | (uint64_t{data[5]} << 16) |
132 (uint64_t{data[6]} << 8) | uint64_t{data[7]};
136inline constexpr uint64_t
load64le(
const uint8_t* data)
noexcept
138 return uint64_t{data[0]} | (uint64_t{data[1]} << 8) | (uint64_t{data[2]} << 16) |
139 (uint64_t{data[3]} << 24) | (uint64_t{data[4]} << 32) | (uint64_t{data[5]} << 40) |
140 (uint64_t{data[6]} << 48) | (uint64_t{data[7]} << 56);
144inline constexpr uint32_t
load32be(
const uint8_t* data)
noexcept
146 return (uint32_t{data[0]} << 24) | (uint32_t{data[1]} << 16) | (uint32_t{data[2]} << 8) |
151inline constexpr uint32_t
load32le(
const uint8_t* data)
noexcept
153 return uint32_t{data[0]} | (uint32_t{data[1]} << 8) | (uint32_t{data[2]} << 16) |
154 (uint32_t{data[3]} << 24);
159constexpr auto prime = 0x100000001b3;
163inline constexpr uint64_t
fnv1a_by64(uint64_t h, uint64_t x)
noexcept
165 return (h ^ x) *
prime;
263inline constexpr address::operator bool() const noexcept
274inline constexpr bytes32::operator bool() const noexcept
287constexpr T
parse(std::string_view s)
noexcept
289 return from_hex<T>(s).value();
293constexpr address operator""_address(
const char* s)
noexcept
295 return parse<address>(s);
299constexpr bytes32 operator""_bytes32(
const char* s)
noexcept
301 return parse<bytes32>(s);
305using namespace literals;
351 const uint8_t* _output_data,
352 size_t _output_size) noexcept
362 int64_t _gas_left = 0,
363 int64_t _gas_refund = 0) noexcept
452 const bytes32& value)
noexcept = 0;
466 uint8_t* buffer_data,
467 size_t buffer_size)
const noexcept = 0;
486 size_t num_topics) noexcept = 0;
496 const
bytes32& key) const noexcept = 0;
501 const
bytes32& value) noexcept = 0;
521 : host{&interface}, context{ctx}
536 const bytes32& value)
noexcept final
558 uint8_t* buffer_data,
559 size_t buffer_size)
const noexcept final
561 return host->
copy_code(context, &
address, code_offset, buffer_data, buffer_size);
566 return host->
selfdestruct(context, &addr, &beneficiary);
586 size_t topics_count)
noexcept final
588 host->
emit_log(context, &addr, data, data_size, topics, topics_count);
608 const bytes32& value)
noexcept final
635 template <
typename DerivedClass = Host>
639 auto* h =
reinterpret_cast<Host*
>(context);
642 return static_cast<DerivedClass*
>(h);
654 VM()
noexcept =
default;
662 if (m_instance !=
nullptr)
663 m_instance->destroy(m_instance);
666 VM(
const VM&) =
delete;
667 VM& operator=(
const VM&) =
delete;
670 VM(
VM&& other) noexcept : m_instance{other.m_instance} { other.m_instance =
nullptr; }
676 m_instance = other.m_instance;
677 other.m_instance =
nullptr;
684 std::initializer_list<std::pair<const char*, const char*>> options)
noexcept;
687 explicit operator bool() const noexcept {
return m_instance !=
nullptr; }
693 char const*
name() const noexcept {
return m_instance->name; }
696 char const*
version() const noexcept {
return m_instance->version; }
707 return m_instance->get_capabilities(m_instance);
722 size_t code_size)
noexcept
724 return Result{m_instance->execute(m_instance, &host, ctx, rev, &msg, code, code_size)};
732 size_t code_size)
noexcept
734 return execute(Host::get_interface(), host.to_context(), rev, msg, code, code_size);
748 size_t code_size)
noexcept
751 m_instance->execute(m_instance,
nullptr,
nullptr, rev, &msg, code, code_size)};
766 std::initializer_list<std::pair<const char*, const char*>> options) noexcept
770 for (
const auto& option : options)
771 set_option(option.first, option.second);
779 return Host::from_context(h)->account_exists(*addr);
786 return Host::from_context(h)->get_storage(*addr, *key);
794 return Host::from_context(h)->set_storage(*addr, *key, *value);
799 return Host::from_context(h)->get_balance(*addr);
804 return Host::from_context(h)->get_code_size(*addr);
809 return Host::from_context(h)->get_code_hash(*addr);
815 uint8_t* buffer_data,
816 size_t buffer_size)
noexcept
818 return Host::from_context(h)->copy_code(*addr, code_offset, buffer_data, buffer_size);
825 return Host::from_context(h)->selfdestruct(*addr, *beneficiary);
830 return Host::from_context(h)->call(*msg).release_raw();
835 return Host::from_context(h)->get_tx_context();
840 return Host::from_context(h)->get_block_hash(block_number);
848 size_t num_topics)
noexcept
850 Host::from_context(h)->emit_log(*addr, data, data_size,
static_cast<const bytes32*
>(topics),
856 return Host::from_context(h)->access_account(*addr);
863 return Host::from_context(h)->access_storage(*addr, *key);
870 return Host::from_context(h)->get_transient_storage(*addr, *key);
878 Host::from_context(h)->set_transient_storage(*addr, *key, *value);
885 ::evmc::internal::account_exists,
886 ::evmc::internal::get_storage,
887 ::evmc::internal::set_storage,
888 ::evmc::internal::get_balance,
889 ::evmc::internal::get_code_size,
890 ::evmc::internal::get_code_hash,
891 ::evmc::internal::copy_code,
892 ::evmc::internal::selfdestruct,
893 ::evmc::internal::call,
894 ::evmc::internal::get_tx_context,
895 ::evmc::internal::get_block_hash,
896 ::evmc::internal::emit_log,
897 ::evmc::internal::access_account,
898 ::evmc::internal::access_storage,
899 ::evmc::internal::get_transient_storage,
900 ::evmc::internal::set_transient_storage,
929struct hash<
evmc::address>
932 constexpr size_t operator()(
const evmc::address& s)
const noexcept
934 using namespace evmc;
947 constexpr size_t operator()(
const evmc::bytes32& s)
const noexcept
949 using namespace evmc;
951 return static_cast<size_t>(
Wrapper around EVMC host context / host interface.
bytes32 get_code_hash(const address &address) const noexcept final
Get code hash callback function.
bool account_exists(const address &address) const noexcept final
Check account existence callback function.
size_t get_code_size(const address &address) const noexcept final
Get code size callback function.
HostContext()=default
Default constructor for null Host context.
uint256be get_balance(const address &address) const noexcept final
Get balance callback function.
HostContext(const evmc_host_interface &interface, evmc_host_context *ctx) noexcept
Constructor from the EVMC Host primitives.
evmc_access_status access_storage(const address &address, const bytes32 &key) noexcept final
Access storage callback function.
size_t copy_code(const address &address, size_t code_offset, uint8_t *buffer_data, size_t buffer_size) const noexcept final
Copy code callback function.
Result call(const evmc_message &message) noexcept final
Call callback function.
bool selfdestruct(const address &addr, const address &beneficiary) noexcept final
Selfdestruct callback function.
evmc_access_status access_account(const address &address) noexcept final
Access account callback function.
void set_transient_storage(const address &address, const bytes32 &key, const bytes32 &value) noexcept final
Set transient storage callback function.
bytes32 get_block_hash(int64_t number) const noexcept final
Get block hash callback function.
void emit_log(const address &addr, const uint8_t *data, size_t data_size, const bytes32 topics[], size_t topics_count) noexcept final
Emit log callback function.
evmc_storage_status set_storage(const address &address, const bytes32 &key, const bytes32 &value) noexcept final
Set storage callback function.
bytes32 get_transient_storage(const address &address, const bytes32 &key) const noexcept final
Get transient storage callback function.
evmc_tx_context get_tx_context() const noexcept final
Get transaction context callback function.
bytes32 get_storage(const address &address, const bytes32 &key) const noexcept final
Get storage callback function.
virtual bytes32 get_transient_storage(const address &addr, const bytes32 &key) const noexcept=0
Get transient storage callback function.
virtual bytes32 get_storage(const address &addr, const bytes32 &key) const noexcept=0
Get storage callback function.
virtual Result call(const evmc_message &msg) noexcept=0
Call callback function.
virtual void emit_log(const address &addr, const uint8_t *data, size_t data_size, const bytes32 topics[], size_t num_topics) noexcept=0
Emit log callback function.
virtual bool selfdestruct(const address &addr, const address &beneficiary) noexcept=0
Selfdestruct callback function.
virtual evmc_tx_context get_tx_context() const noexcept=0
Get transaction context callback function.
virtual bytes32 get_code_hash(const address &addr) const noexcept=0
Get code hash callback function.
virtual size_t get_code_size(const address &addr) const noexcept=0
Get code size callback function.
virtual evmc_storage_status set_storage(const address &addr, const bytes32 &key, const bytes32 &value) noexcept=0
Set storage callback function.
virtual bytes32 get_block_hash(int64_t block_number) const noexcept=0
Get block hash callback function.
virtual evmc_access_status access_account(const address &addr) noexcept=0
Access account callback function.
virtual evmc_access_status access_storage(const address &addr, const bytes32 &key) noexcept=0
Access storage callback function.
virtual uint256be get_balance(const address &addr) const noexcept=0
Get balance callback function.
virtual size_t copy_code(const address &addr, size_t code_offset, uint8_t *buffer_data, size_t buffer_size) const noexcept=0
Copy code callback function.
virtual void set_transient_storage(const address &addr, const bytes32 &key, const bytes32 &value) noexcept=0
Set transient storage callback function.
virtual bool account_exists(const address &addr) const noexcept=0
Check account existence callback function.
Abstract class to be used by Host implementations.
static DerivedClass * from_context(evmc_host_context *context) noexcept
Converts the opaque host context pointer back to the original Host object.
evmc_host_context * to_context() noexcept
Converts the Host object to the opaque host context pointer.
static const evmc_host_interface & get_interface() noexcept
Provides access to the global host interface.
The EVM code execution result.
Result(evmc_status_code _status_code, int64_t _gas_left, int64_t _gas_refund, const uint8_t *_output_data, size_t _output_size) noexcept
Creates the result from the provided arguments.
~Result() noexcept
Destructor responsible for automatically releasing attached resources.
evmc_result & raw() noexcept
Access the result object as a referenced to evmc_result.
Result(evmc_status_code _status_code=EVMC_INTERNAL_ERROR, int64_t _gas_left=0, int64_t _gas_refund=0) noexcept
Creates the result without output.
Result(const evmc_result &res) noexcept
Converting constructor from raw evmc_result.
Result(Result &&other) noexcept
Move constructor.
Result(evmc_status_code _status_code, int64_t _gas_left, int64_t _gas_refund, const evmc_address &_create_address) noexcept
Creates the result of contract creation.
evmc_result release_raw() noexcept
Releases the ownership and returns the raw copy of evmc_result.
evmc_address create_address
The address of the possibly created contract.
Result & operator=(Result &&other) noexcept
Move assignment operator.
const evmc_result & raw() const noexcept
Access the result object as a const referenced to evmc_result.
Result execute(evmc_revision rev, const evmc_message &msg, const uint8_t *code, size_t code_size) noexcept
Executes code without the Host context.
VM & operator=(VM &&other) noexcept
Move assignment operator.
evmc_set_option_result set_option(const char name[], const char value[]) noexcept
Sets the option for the VM, if the feature is supported by the VM.
~VM() noexcept
Destructor responsible for automatically destroying the VM instance.
Result execute(Host &host, evmc_revision rev, const evmc_message &msg, const uint8_t *code, size_t code_size) noexcept
Convenient variant of the VM::execute() that takes reference to evmc::Host class.
bool is_abi_compatible() const noexcept
Checks whenever the VM instance is ABI compatible with the current EVMC API.
bool has_capability(evmc_capabilities capability) const noexcept
Checks if the VM has the given capability.
VM(VM &&other) noexcept
Move constructor.
evmc_vm * get_raw_pointer() const noexcept
Returns the pointer to C EVMC struct representing the VM.
char const * name() const noexcept
The name of the EVMC VM implementation.
Result execute(const evmc_host_interface &host, evmc_host_context *ctx, evmc_revision rev, const evmc_message &msg, const uint8_t *code, size_t code_size) noexcept
Executes code in the VM instance.
char const * version() const noexcept
The version of the EVMC VM implementation, e.g.
evmc_capabilities_flagset get_capabilities() const noexcept
A method returning capabilities supported by the VM instance.
VM(evmc_vm *vm) noexcept
Converting constructor from evmc_vm.
constexpr auto offset_basis
The 64-bit FNV offset basis.
std::ostream & operator<<(std::ostream &os, evmc_status_code status_code)
"Stream out" operator implementation for evmc_status_code.
constexpr uint64_t fnv1a_by64(uint64_t h, uint64_t x) noexcept
The hashing transformation for 64-bit inputs based on the FNV-1a formula.
constexpr T parse(std::string_view s) noexcept
Converts a raw literal into value of type T.
constexpr auto prime
The 64-bit FNV prime number.
evmc_set_option_result
Possible outcomes of evmc_set_option.
evmc_capabilities
Possible capabilities of a VM.
evmc_status_code
The execution status code.
uint32_t evmc_capabilities_flagset
Alias for unsigned integer representing a set of bit flags of EVMC capabilities.
evmc_access_status
Access status per EIP-2929: Gas cost increases for state access opcodes.
evmc_storage_status
The effect of an attempt to modify a contract storage item.
evmc_revision
EVM revision.
@ EVMC_ABI_VERSION
The EVMC ABI version number of the interface declared in this file.
@ EVMC_INTERNAL_ERROR
EVM implementation generic internal error.
@ EVMC_LATEST_STABLE_REVISION
The latest known EVM revision with finalized specification.
@ EVMC_MAX_REVISION
The maximum EVM revision supported.
static struct evmc_result evmc_make_result(enum evmc_status_code status_code, int64_t gas_left, int64_t gas_refund, const uint8_t *output_data, size_t output_size)
Creates the result from the provided arguments.
static const char * evmc_revision_to_string(enum evmc_revision rev)
Returns the name of the evmc_revision.
static const char * evmc_status_code_to_string(enum evmc_status_code status_code)
Returns text representation of the evmc_status_code.
static enum evmc_set_option_result evmc_set_option(struct evmc_vm *vm, char const *name, char const *value)
Sets the option for the VM, if the feature is supported by the VM.
EVMC C++ API - wrappers and bindings for C++.
constexpr bool operator<(const address &a, const address &b) noexcept
The "less than" comparison operator for the evmc::address type.
constexpr uint64_t load64be(const uint8_t *data) noexcept
Loads 64 bits / 8 bytes of data from the given data array in big-endian order.
constexpr uint32_t load32be(const uint8_t *data) noexcept
Loads 32 bits / 4 bytes of data from the given data array in big-endian order.
constexpr bool operator==(const address &a, const address &b) noexcept
The "equal to" comparison operator for the evmc::address type.
constexpr bool operator>(const address &a, const address &b) noexcept
The "greater than" comparison operator for the evmc::address type.
const char * to_string(evmc_status_code status_code) noexcept
Returns text representation of the evmc_status_code.
std::basic_string_view< unsigned char, byte_traits< unsigned char > > bytes_view
String view of unsigned chars representing bytes.
constexpr auto make_result
Alias for evmc_make_result().
constexpr bool operator>=(const address &a, const address &b) noexcept
The "greater than or equal to" comparison operator for the evmc::address type.
constexpr bool operator<=(const address &a, const address &b) noexcept
The "less than or equal to" comparison operator for the evmc::address type.
constexpr uint64_t load64le(const uint8_t *data) noexcept
Loads 64 bits / 8 bytes of data from the given data array in little-endian order.
constexpr bool operator!=(const address &a, const address &b) noexcept
The "not equal to" comparison operator for the evmc::address type.
constexpr uint32_t load32le(const uint8_t *data) noexcept
Loads 32 bits / 4 bytes of data from the given data array in little-endian order.
constexpr bool is_zero(const address &a) noexcept
Checks if the given address is the zero address.
The big-endian 160-bit hash suitable for keeping an Ethereum address.
constexpr address(evmc_address init={}) noexcept
Default and converting constructor.
constexpr address(uint64_t v) noexcept
Converting constructor from unsigned integer value.
The fixed size array of 32 bytes for storing 256-bit EVM values.
constexpr bytes32(evmc_bytes32 init={}) noexcept
Default and converting constructor.
constexpr bytes32(uint64_t v) noexcept
Converting constructor from unsigned integer value.
Big-endian 160-bit hash suitable for keeping an Ethereum address.
uint8_t bytes[20]
The 20 bytes of the hash.
The fixed size array of 32 bytes.
uint8_t bytes[32]
The 32 bytes.
The opaque data type representing the Host execution context.
evmc_get_code_hash_fn get_code_hash
Get code hash callback function.
evmc_call_fn call
Call callback function.
evmc_selfdestruct_fn selfdestruct
Selfdestruct callback function.
evmc_get_storage_fn get_storage
Get storage callback function.
evmc_copy_code_fn copy_code
Copy code callback function.
evmc_get_code_size_fn get_code_size
Get code size callback function.
evmc_get_transient_storage_fn get_transient_storage
Get transient storage callback function.
evmc_set_transient_storage_fn set_transient_storage
Set transient storage callback function.
evmc_get_block_hash_fn get_block_hash
Get block hash callback function.
evmc_emit_log_fn emit_log
Emit log callback function.
evmc_account_exists_fn account_exists
Check account existence callback function.
evmc_access_account_fn access_account
Access account callback function.
evmc_get_balance_fn get_balance
Get balance callback function.
evmc_set_storage_fn set_storage
Set storage callback function.
evmc_get_tx_context_fn get_tx_context
Get transaction context callback function.
evmc_access_storage_fn access_storage
Access storage callback function.
The message describing an EVM call, including a zero-depth calls from a transaction origin.
The EVM code execution result.
const uint8_t * output_data
The reference to output data.
enum evmc_status_code status_code
The execution status code.
evmc_release_result_fn release
The method releasing all resources associated with the result object.
int64_t gas_refund
The refunded gas accumulated from this execution and its sub-calls.
size_t output_size
The size of the output data.
evmc_address create_address
The address of the possibly created contract.
int64_t gas_left
The amount of gas left after the execution.
The transaction and block data for execution.