EVMC
evmc Namespace Reference

EVMC C++ API - wrappers and bindings for C++. More...

Classes

struct  address
 The big-endian 160-bit hash suitable for keeping an Ethereum address. More...
 
struct  bytes32
 The fixed size array of 32 bytes for storing 256-bit EVM values. More...
 
struct  filter_iterator
 The filter iterator adaptor creates a view of an iterator range in which some elements of the range are skipped. More...
 
class  Host
 Abstract class to be used by Host implementations. More...
 
class  HostContext
 Wrapper around EVMC host context / host interface. More...
 
class  HostInterface
 The EVMC Host interface. More...
 
struct  MockedAccount
 Mocked account. More...
 
class  MockedHost
 Mocked EVMC Host implementation. More...
 
class  Result
 The EVM code execution result. More...
 
struct  skip_space_iterator
 The input filter iterator which skips whitespace characters from the base input iterator. More...
 
struct  StorageValue
 Extended value (with original value and access flag) for account storage. More...
 
class  VM
 The VM instance. More...
 

Typedefs

using bytes_view = std::basic_string_view< uint8_t >
 String view of uint8_t chars.
 
using uint256be = bytes32
 The alias for evmc::bytes32 to represent a big-endian 256-bit integer.
 
using bytes = std::basic_string< uint8_t >
 String of uint8_t chars.
 

Functions

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 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 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 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 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 "not equal to" comparison operator for the evmc::address type.
 
constexpr bool operator< (const address &a, const address &b) noexcept
 The "less than" 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.
 
constexpr bool operator<= (const address &a, const address &b) noexcept
 The "less than or equal to" comparison operator for the evmc::address type.
 
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 bytes32 &a, const bytes32 &b) noexcept
 The "equal to" comparison operator for the evmc::bytes32 type.
 
constexpr bool operator!= (const bytes32 &a, const bytes32 &b) noexcept
 The "not equal to" comparison operator for the evmc::bytes32 type.
 
constexpr bool operator< (const bytes32 &a, const bytes32 &b) noexcept
 The "less than" comparison operator for the evmc::bytes32 type.
 
constexpr bool operator> (const bytes32 &a, const bytes32 &b) noexcept
 The "greater than" comparison operator for the evmc::bytes32 type.
 
constexpr bool operator<= (const bytes32 &a, const bytes32 &b) noexcept
 The "less than or equal to" comparison operator for the evmc::bytes32 type.
 
constexpr bool operator>= (const bytes32 &a, const bytes32 &b) noexcept
 The "greater than or equal to" comparison operator for the evmc::bytes32 type.
 
constexpr bool is_zero (const address &a) noexcept
 Checks if the given address is the zero address.
 
constexpr bool is_zero (const bytes32 &a) noexcept
 Checks if the given bytes32 object has all zero bytes.
 
const char * to_string (evmc_status_code status_code) noexcept
 Returns text representation of the evmc_status_code.
 
const char * to_string (evmc_revision rev) noexcept
 Returns the name of the evmc_revision.
 
constexpr bool isspace (char ch) noexcept
 The constexpr variant of std::isspace().
 
constexpr bool is_not_space (char ch) noexcept
 Checks if a character is not a white space.
 
template<typename BaseIterator >
 skip_space_iterator (BaseIterator, BaseIterator) -> skip_space_iterator< BaseIterator >
 Class template argument deduction guide.
 
std::string hex (uint8_t b) noexcept
 Encode a byte to a hex string.
 
std::string hex (bytes_view bs)
 Encodes bytes as hex string.
 
template<typename InputIt , typename OutputIt >
constexpr bool from_hex (InputIt begin, InputIt end, OutputIt out) noexcept
 Decodes hex-encoded sequence of characters.
 
bool validate_hex (std::string_view hex) noexcept
 Validates hex encoded string.
 
std::optional< bytesfrom_hex (std::string_view hex)
 Decodes hex encoded string to bytes.
 
template<typename T >
constexpr std::optional< T > from_hex (std::string_view s) noexcept
 Decodes hex-encoded string into custom type T with .bytes array of uint8_t.
 
template<typename InputIterator >
std::optional< bytesfrom_spaced_hex (InputIterator begin, InputIterator end) noexcept
 Decodes hex encoded string to bytes.
 
std::optional< bytesfrom_spaced_hex (std::string_view hex) noexcept
 Decodes hex encoded string to bytes.
 

Variables

constexpr auto make_result = evmc_make_result
 Alias for evmc_make_result().
 

Detailed Description

EVMC C++ API - wrappers and bindings for C++.

Typedef Documentation

◆ bytes

typedef std::basic_string< uint8_t > evmc::bytes

String of uint8_t chars.

The string of bytes.

Definition at line 15 of file hex.hpp.

◆ bytes_view

typedef std::basic_string_view< uint8_t > evmc::bytes_view

String view of uint8_t chars.

Definition at line 24 of file evmc.hpp.

◆ uint256be

using evmc::uint256be = typedef bytes32

The alias for evmc::bytes32 to represent a big-endian 256-bit integer.

Definition at line 127 of file evmc.hpp.

Function Documentation

◆ from_hex() [1/3]

template<typename InputIt , typename OutputIt >
constexpr bool evmc::from_hex ( InputIt  begin,
InputIt  end,
OutputIt  out 
)
inlineconstexprnoexcept

Decodes hex-encoded sequence of characters.

It is guaranteed that the output will not be longer than half of the input length.

Parameters
beginThe input begin iterator. It only must satisfy input iterator concept.
endThe input end iterator. It only must satisfy input iterator concept.
outThe output iterator. It must satisfy output iterator concept.
Returns
True if successful, false if input is invalid hex.

Definition at line 64 of file hex.hpp.

65{
66 int hi_nibble = -1; // Init with invalid value, should never be used.
67 size_t i = 0;
68 for (auto it = begin; it != end; ++it, ++i)
69 {
70 const auto h = *it;
71 const int v = evmc::internal::from_hex_digit(h);
72 if (v < 0)
73 {
74 if (i == 1 && hi_nibble == 0 && h == 'x') // 0x prefix
75 continue;
76 return false;
77 }
78
79 if (i % 2 == 0)
80 hi_nibble = v << 4;
81 else
82 *out++ = static_cast<uint8_t>(hi_nibble | v);
83 }
84
85 return i % 2 == 0;
86}

◆ from_hex() [2/3]

std::optional< bytes > evmc::from_hex ( std::string_view  hex)
inline

Decodes hex encoded string to bytes.

In case the input is invalid the returned value is std::nullopt. This can happen if a non-hex digit or odd number of digits is encountered.

Definition at line 107 of file hex.hpp.

108{
109 bytes bs;
110 bs.reserve(hex.size() / 2);
111 if (!from_hex(hex.begin(), hex.end(), std::back_inserter(bs)))
112 return {};
113 return bs;
114}
std::basic_string< uint8_t > bytes
String of uint8_t chars.
Definition: hex.hpp:15
std::string hex(uint8_t b) noexcept
Encode a byte to a hex string.
Definition: hex.hpp:22
constexpr bool from_hex(InputIt begin, InputIt end, OutputIt out) noexcept
Decodes hex-encoded sequence of characters.
Definition: hex.hpp:64

◆ from_hex() [3/3]

template<typename T >
constexpr std::optional< T > evmc::from_hex ( std::string_view  s)
constexprnoexcept

Decodes hex-encoded string into custom type T with .bytes array of uint8_t.

When the input is smaller than the result type, the result is padded with zeros on the left (the result bytes of lowest indices are filled with zeros). TODO: Support optional left alignment.

Definition at line 122 of file hex.hpp.

123{
124 // Omit the optional 0x prefix.
125 if (s.size() >= 2 && s[0] == '0' && s[1] == 'x')
126 s.remove_prefix(2);
127
128 T r{}; // The T must have .bytes array. This may be lifted if std::bit_cast is available.
129 constexpr auto num_out_bytes = std::size(r.bytes);
130 const auto num_in_bytes = s.length() / 2;
131 if (num_in_bytes > num_out_bytes)
132 return {};
133 if (!from_hex(s.begin(), s.end(), &r.bytes[num_out_bytes - num_in_bytes]))
134 return {};
135 return r;
136}

◆ from_spaced_hex() [1/2]

template<typename InputIterator >
std::optional< bytes > evmc::from_spaced_hex ( InputIterator  begin,
InputIterator  end 
)
noexcept

Decodes hex encoded string to bytes.

The whitespace in the input is ignored.

In case the input is invalid the returned value is std::nullopt. This can happen if a non-hex digit or odd number of digits is encountered. The whitespace (as defined by std::isspace) in the input is ignored.

Definition at line 144 of file hex.hpp.

145{
146 bytes bs;
147 if (!from_hex(skip_space_iterator{begin, end}, skip_space_iterator{end, end},
148 std::back_inserter(bs)))
149 return {};
150 return bs;
151}
The input filter iterator which skips whitespace characters from the base input iterator.

◆ from_spaced_hex() [2/2]

std::optional< bytes > evmc::from_spaced_hex ( std::string_view  hex)
inlinenoexcept

Decodes hex encoded string to bytes.

The whitespace in the input is ignored.

In case the input is invalid the returned value is std::nullopt. This can happen if a non-hex digit or odd number of digits is encountered. The whitespace (as defined by std::isspace) in the input is ignored.

Definition at line 154 of file hex.hpp.

155{
156 return from_spaced_hex(hex.begin(), hex.end());
157}
std::optional< bytes > from_spaced_hex(InputIterator begin, InputIterator end) noexcept
Decodes hex encoded string to bytes.
Definition: hex.hpp:144

◆ hex() [1/2]

std::string evmc::hex ( bytes_view  bs)
inline

Encodes bytes as hex string.

Definition at line 29 of file hex.hpp.

30{
31 std::string str;
32 str.reserve(bs.size() * 2);
33 for (const auto b : bs)
34 str += hex(b);
35 return str;
36}

◆ hex() [2/2]

std::string evmc::hex ( uint8_t  b)
inlinenoexcept

Encode a byte to a hex string.

Definition at line 22 of file hex.hpp.

23{
24 static constexpr auto hex_digits = "0123456789abcdef";
25 return {hex_digits[b >> 4], hex_digits[b & 0xf]};
26}

◆ is_not_space()

constexpr bool evmc::is_not_space ( char  ch)
inlineconstexprnoexcept

Checks if a character is not a white space.

Definition at line 18 of file filter_iterator.hpp.

19{
20 return !isspace(ch);
21}
constexpr bool isspace(char ch) noexcept
The constexpr variant of std::isspace().

◆ is_zero() [1/2]

constexpr bool evmc::is_zero ( const address a)
inlineconstexprnoexcept

Checks if the given address is the zero address.

Definition at line 261 of file evmc.hpp.

262{
263 return a == address{};
264}
The big-endian 160-bit hash suitable for keeping an Ethereum address.
Definition: evmc.hpp:30

◆ is_zero() [2/2]

constexpr bool evmc::is_zero ( const bytes32 a)
inlineconstexprnoexcept

Checks if the given bytes32 object has all zero bytes.

Definition at line 272 of file evmc.hpp.

273{
274 return a == bytes32{};
275}
The fixed size array of 32 bytes for storing 256-bit EVM values.
Definition: evmc.hpp:74

◆ isspace()

constexpr bool evmc::isspace ( char  ch)
inlineconstexprnoexcept

The constexpr variant of std::isspace().

Definition at line 11 of file filter_iterator.hpp.

12{
13 // Implementation taken from LLVM's libc.
14 return ch == ' ' || (static_cast<unsigned>(ch) - '\t') < 5;
15}

◆ load32be()

constexpr uint32_t evmc::load32be ( const uint8_t *  data)
inlineconstexprnoexcept

Loads 32 bits / 4 bytes of data from the given data array in big-endian order.

Definition at line 147 of file evmc.hpp.

148{
149 return (uint32_t{data[0]} << 24) | (uint32_t{data[1]} << 16) | (uint32_t{data[2]} << 8) |
150 uint32_t{data[3]};
151}

◆ load32le()

constexpr uint32_t evmc::load32le ( const uint8_t *  data)
inlineconstexprnoexcept

Loads 32 bits / 4 bytes of data from the given data array in little-endian order.

Definition at line 154 of file evmc.hpp.

155{
156 return uint32_t{data[0]} | (uint32_t{data[1]} << 8) | (uint32_t{data[2]} << 16) |
157 (uint32_t{data[3]} << 24);
158}

◆ load64be()

constexpr uint64_t evmc::load64be ( const uint8_t *  data)
inlineconstexprnoexcept

Loads 64 bits / 8 bytes of data from the given data array in big-endian order.

Definition at line 131 of file evmc.hpp.

132{
133 return (uint64_t{data[0]} << 56) | (uint64_t{data[1]} << 48) | (uint64_t{data[2]} << 40) |
134 (uint64_t{data[3]} << 32) | (uint64_t{data[4]} << 24) | (uint64_t{data[5]} << 16) |
135 (uint64_t{data[6]} << 8) | uint64_t{data[7]};
136}

◆ load64le()

constexpr uint64_t evmc::load64le ( const uint8_t *  data)
inlineconstexprnoexcept

Loads 64 bits / 8 bytes of data from the given data array in little-endian order.

Definition at line 139 of file evmc.hpp.

140{
141 return uint64_t{data[0]} | (uint64_t{data[1]} << 8) | (uint64_t{data[2]} << 16) |
142 (uint64_t{data[3]} << 24) | (uint64_t{data[4]} << 32) | (uint64_t{data[5]} << 40) |
143 (uint64_t{data[6]} << 48) | (uint64_t{data[7]} << 56);
144}

◆ operator!=() [1/2]

constexpr bool evmc::operator!= ( const address a,
const address b 
)
inlineconstexprnoexcept

The "not equal to" comparison operator for the evmc::address type.

Definition at line 182 of file evmc.hpp.

183{
184 return !(a == b);
185}

◆ operator!=() [2/2]

constexpr bool evmc::operator!= ( const bytes32 a,
const bytes32 b 
)
inlineconstexprnoexcept

The "not equal to" comparison operator for the evmc::bytes32 type.

Definition at line 225 of file evmc.hpp.

226{
227 return !(a == b);
228}

◆ operator<() [1/2]

constexpr bool evmc::operator< ( const address a,
const address b 
)
inlineconstexprnoexcept

The "less than" comparison operator for the evmc::address type.

Definition at line 188 of file evmc.hpp.

189{
190 return load64be(&a.bytes[0]) < load64be(&b.bytes[0]) ||
191 (load64be(&a.bytes[0]) == load64be(&b.bytes[0]) &&
192 (load64be(&a.bytes[8]) < load64be(&b.bytes[8]) ||
193 (load64be(&a.bytes[8]) == load64be(&b.bytes[8]) &&
194 load32be(&a.bytes[16]) < load32be(&b.bytes[16]))));
195}
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.
Definition: evmc.hpp:131
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.
Definition: evmc.hpp:147
uint8_t bytes[20]
The 20 bytes of the hash.
Definition: evmc.h:71

◆ operator<() [2/2]

constexpr bool evmc::operator< ( const bytes32 a,
const bytes32 b 
)
inlineconstexprnoexcept

The "less than" comparison operator for the evmc::bytes32 type.

Definition at line 231 of file evmc.hpp.

232{
233 return load64be(&a.bytes[0]) < load64be(&b.bytes[0]) ||
234 (load64be(&a.bytes[0]) == load64be(&b.bytes[0]) &&
235 (load64be(&a.bytes[8]) < load64be(&b.bytes[8]) ||
236 (load64be(&a.bytes[8]) == load64be(&b.bytes[8]) &&
237 (load64be(&a.bytes[16]) < load64be(&b.bytes[16]) ||
238 (load64be(&a.bytes[16]) == load64be(&b.bytes[16]) &&
239 load64be(&a.bytes[24]) < load64be(&b.bytes[24]))))));
240}
uint8_t bytes[32]
The 32 bytes.
Definition: evmc.h:59

◆ operator<=() [1/2]

constexpr bool evmc::operator<= ( const address a,
const address b 
)
inlineconstexprnoexcept

The "less than or equal to" comparison operator for the evmc::address type.

Definition at line 204 of file evmc.hpp.

205{
206 return !(b < a);
207}

◆ operator<=() [2/2]

constexpr bool evmc::operator<= ( const bytes32 a,
const bytes32 b 
)
inlineconstexprnoexcept

The "less than or equal to" comparison operator for the evmc::bytes32 type.

Definition at line 249 of file evmc.hpp.

250{
251 return !(b < a);
252}

◆ operator==() [1/2]

constexpr bool evmc::operator== ( const address a,
const address b 
)
inlineconstexprnoexcept

The "equal to" comparison operator for the evmc::address type.

Definition at line 174 of file evmc.hpp.

175{
176 return load64le(&a.bytes[0]) == load64le(&b.bytes[0]) &&
177 load64le(&a.bytes[8]) == load64le(&b.bytes[8]) &&
178 load32le(&a.bytes[16]) == load32le(&b.bytes[16]);
179}
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.
Definition: evmc.hpp:139
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.
Definition: evmc.hpp:154

◆ operator==() [2/2]

constexpr bool evmc::operator== ( const bytes32 a,
const bytes32 b 
)
inlineconstexprnoexcept

The "equal to" comparison operator for the evmc::bytes32 type.

Definition at line 216 of file evmc.hpp.

217{
218 return load64le(&a.bytes[0]) == load64le(&b.bytes[0]) &&
219 load64le(&a.bytes[8]) == load64le(&b.bytes[8]) &&
220 load64le(&a.bytes[16]) == load64le(&b.bytes[16]) &&
221 load64le(&a.bytes[24]) == load64le(&b.bytes[24]);
222}

◆ operator>() [1/2]

constexpr bool evmc::operator> ( const address a,
const address b 
)
inlineconstexprnoexcept

The "greater than" comparison operator for the evmc::address type.

Definition at line 198 of file evmc.hpp.

199{
200 return b < a;
201}

◆ operator>() [2/2]

constexpr bool evmc::operator> ( const bytes32 a,
const bytes32 b 
)
inlineconstexprnoexcept

The "greater than" comparison operator for the evmc::bytes32 type.

Definition at line 243 of file evmc.hpp.

244{
245 return b < a;
246}

◆ operator>=() [1/2]

constexpr bool evmc::operator>= ( const address a,
const address b 
)
inlineconstexprnoexcept

The "greater than or equal to" comparison operator for the evmc::address type.

Definition at line 210 of file evmc.hpp.

211{
212 return !(a < b);
213}

◆ operator>=() [2/2]

constexpr bool evmc::operator>= ( const bytes32 a,
const bytes32 b 
)
inlineconstexprnoexcept

The "greater than or equal to" comparison operator for the evmc::bytes32 type.

Definition at line 255 of file evmc.hpp.

256{
257 return !(a < b);
258}

◆ to_string() [1/2]

const char * evmc::to_string ( evmc_revision  rev)
inlinenoexcept

Returns the name of the evmc_revision.

Definition at line 318 of file evmc.hpp.

319{
320 return evmc_revision_to_string(rev);
321}
static const char * evmc_revision_to_string(enum evmc_revision rev)
Returns the name of the evmc_revision.
Definition: helpers.h:272

◆ to_string() [2/2]

const char * evmc::to_string ( evmc_status_code  status_code)
inlinenoexcept

Returns text representation of the evmc_status_code.

Definition at line 312 of file evmc.hpp.

313{
314 return evmc_status_code_to_string(status_code);
315}
static const char * evmc_status_code_to_string(enum evmc_status_code status_code)
Returns text representation of the evmc_status_code.
Definition: helpers.h:221

◆ validate_hex()

bool evmc::validate_hex ( std::string_view  hex)
inlinenoexcept

Validates hex encoded string.

Returns
True if the input is valid hex.

Definition at line 91 of file hex.hpp.

92{
93 struct noop_output_iterator
94 {
95 uint8_t sink = {};
96 uint8_t& operator*() noexcept { return sink; }
97 noop_output_iterator operator++(int) noexcept { return *this; } // NOLINT(cert-dcl21-cpp)
98 };
99
100 return from_hex(hex.begin(), hex.end(), noop_output_iterator{});
101}

Variable Documentation

◆ make_result

constexpr auto evmc::make_result = evmc_make_result
constexpr

Alias for evmc_make_result().

Definition at line 325 of file evmc.hpp.