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  byte_traits
 The char traits for byte-like types. 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 = std::basic_string< unsigned char, byte_traits< unsigned char > >
 String of unsigned chars representing bytes.
 
using bytes_view = std::basic_string_view< unsigned char, byte_traits< unsigned char > >
 String view of unsigned chars representing bytes.
 
using uint256be = bytes32
 The alias for evmc::bytes32 to represent a big-endian 256-bit integer.
 

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

using evmc::bytes = typedef std::basic_string<unsigned char, byte_traits<unsigned char> >

String of unsigned chars representing bytes.

Definition at line 89 of file bytes.hpp.

◆ bytes_view

using evmc::bytes_view = typedef std::basic_string_view<unsigned char, byte_traits<unsigned char> >

String view of unsigned chars representing bytes.

Definition at line 92 of file bytes.hpp.

◆ uint256be

using evmc::uint256be = typedef bytes32

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

Definition at line 124 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 58 of file hex.hpp.

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

◆ 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 101 of file hex.hpp.

102{
103 bytes bs;
104 bs.reserve(hex.size() / 2);
105 if (!from_hex(hex.begin(), hex.end(), std::back_inserter(bs)))
106 return {};
107 return bs;
108}
std::string hex(uint8_t b) noexcept
Encode a byte to a hex string.
Definition: hex.hpp:16
constexpr bool from_hex(InputIt begin, InputIt end, OutputIt out) noexcept
Decodes hex-encoded sequence of characters.
Definition: hex.hpp:58
std::basic_string< unsigned char, byte_traits< unsigned char > > bytes
String of unsigned chars representing bytes.
Definition: bytes.hpp:89

◆ 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 116 of file hex.hpp.

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

◆ 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 138 of file hex.hpp.

139{
140 bytes bs;
141 if (!from_hex(skip_space_iterator{begin, end}, skip_space_iterator{end, end},
142 std::back_inserter(bs)))
143 return {};
144 return bs;
145}
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 148 of file hex.hpp.

149{
150 return from_spaced_hex(hex.begin(), hex.end());
151}
std::optional< bytes > from_spaced_hex(InputIterator begin, InputIterator end) noexcept
Decodes hex encoded string to bytes.
Definition: hex.hpp:138

◆ hex() [1/2]

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

Encodes bytes as hex string.

Definition at line 23 of file hex.hpp.

24{
25 std::string str;
26 str.reserve(bs.size() * 2);
27 for (const auto b : bs)
28 str += hex(b);
29 return str;
30}

◆ hex() [2/2]

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

Encode a byte to a hex string.

Definition at line 16 of file hex.hpp.

17{
18 static constexpr auto hex_digits = "0123456789abcdef";
19 return {hex_digits[b >> 4], hex_digits[b & 0xf]};
20}

◆ 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 258 of file evmc.hpp.

259{
260 return a == address{};
261}
The big-endian 160-bit hash suitable for keeping an Ethereum address.
Definition: evmc.hpp:27

◆ 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 269 of file evmc.hpp.

270{
271 return a == bytes32{};
272}
The fixed size array of 32 bytes for storing 256-bit EVM values.
Definition: evmc.hpp:71

◆ 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 144 of file evmc.hpp.

145{
146 return (uint32_t{data[0]} << 24) | (uint32_t{data[1]} << 16) | (uint32_t{data[2]} << 8) |
147 uint32_t{data[3]};
148}

◆ 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 151 of file evmc.hpp.

152{
153 return uint32_t{data[0]} | (uint32_t{data[1]} << 8) | (uint32_t{data[2]} << 16) |
154 (uint32_t{data[3]} << 24);
155}

◆ 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 128 of file evmc.hpp.

129{
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]};
133}

◆ 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 136 of file evmc.hpp.

137{
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);
141}

◆ 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 179 of file evmc.hpp.

180{
181 return !(a == b);
182}

◆ 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 222 of file evmc.hpp.

223{
224 return !(a == b);
225}

◆ 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 185 of file evmc.hpp.

186{
187 return load64be(&a.bytes[0]) < load64be(&b.bytes[0]) ||
188 (load64be(&a.bytes[0]) == load64be(&b.bytes[0]) &&
189 (load64be(&a.bytes[8]) < load64be(&b.bytes[8]) ||
190 (load64be(&a.bytes[8]) == load64be(&b.bytes[8]) &&
191 load32be(&a.bytes[16]) < load32be(&b.bytes[16]))));
192}
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:128
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:144
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 228 of file evmc.hpp.

229{
230 return load64be(&a.bytes[0]) < load64be(&b.bytes[0]) ||
231 (load64be(&a.bytes[0]) == load64be(&b.bytes[0]) &&
232 (load64be(&a.bytes[8]) < load64be(&b.bytes[8]) ||
233 (load64be(&a.bytes[8]) == load64be(&b.bytes[8]) &&
234 (load64be(&a.bytes[16]) < load64be(&b.bytes[16]) ||
235 (load64be(&a.bytes[16]) == load64be(&b.bytes[16]) &&
236 load64be(&a.bytes[24]) < load64be(&b.bytes[24]))))));
237}
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 201 of file evmc.hpp.

202{
203 return !(b < a);
204}

◆ 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 246 of file evmc.hpp.

247{
248 return !(b < a);
249}

◆ 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 171 of file evmc.hpp.

172{
173 return load64le(&a.bytes[0]) == load64le(&b.bytes[0]) &&
174 load64le(&a.bytes[8]) == load64le(&b.bytes[8]) &&
175 load32le(&a.bytes[16]) == load32le(&b.bytes[16]);
176}
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:136
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:151

◆ 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 213 of file evmc.hpp.

214{
215 return load64le(&a.bytes[0]) == load64le(&b.bytes[0]) &&
216 load64le(&a.bytes[8]) == load64le(&b.bytes[8]) &&
217 load64le(&a.bytes[16]) == load64le(&b.bytes[16]) &&
218 load64le(&a.bytes[24]) == load64le(&b.bytes[24]);
219}

◆ 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 195 of file evmc.hpp.

196{
197 return b < a;
198}

◆ 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 240 of file evmc.hpp.

241{
242 return b < a;
243}

◆ 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 207 of file evmc.hpp.

208{
209 return !(a < b);
210}

◆ 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 252 of file evmc.hpp.

253{
254 return !(a < b);
255}

◆ to_string() [1/2]

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

Returns the name of the evmc_revision.

Definition at line 315 of file evmc.hpp.

316{
317 return evmc_revision_to_string(rev);
318}
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 309 of file evmc.hpp.

310{
311 return evmc_status_code_to_string(status_code);
312}
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 85 of file hex.hpp.

86{
87 struct noop_output_iterator
88 {
89 uint8_t sink = {};
90 uint8_t& operator*() noexcept { return sink; }
91 noop_output_iterator operator++(int) noexcept { return *this; } // NOLINT(cert-dcl21-cpp)
92 };
93
94 return from_hex(hex.begin(), hex.end(), noop_output_iterator{});
95}

Variable Documentation

◆ make_result

constexpr auto evmc::make_result = evmc_make_result
constexpr

Alias for evmc_make_result().

Definition at line 322 of file evmc.hpp.