EVMC
evmc.hpp
Go to the documentation of this file.
1 /* EVMC: Ethereum Client-VM Connector API.
2  * Copyright 2018-2020 The EVMC Authors.
3  * Licensed under the Apache License, Version 2.0.
4  */
5 #pragma once
6 
7 #include <evmc/evmc.h>
8 #include <evmc/helpers.h>
9 
10 #include <functional>
11 #include <initializer_list>
12 #include <ostream>
13 #include <utility>
14 
17 namespace evmc
18 {
23 {
27  constexpr address(evmc_address init = {}) noexcept : evmc_address{init} {}
28 
33  constexpr explicit address(uint64_t v) noexcept
34  : evmc_address{{0,
35  0,
36  0,
37  0,
38  0,
39  0,
40  0,
41  0,
42  0,
43  0,
44  0,
45  0,
46  static_cast<uint8_t>(v >> 56),
47  static_cast<uint8_t>(v >> 48),
48  static_cast<uint8_t>(v >> 40),
49  static_cast<uint8_t>(v >> 32),
50  static_cast<uint8_t>(v >> 24),
51  static_cast<uint8_t>(v >> 16),
52  static_cast<uint8_t>(v >> 8),
53  static_cast<uint8_t>(v >> 0)}}
54  {}
55 
57  inline constexpr explicit operator bool() const noexcept;
58 };
59 
64 {
68  constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}
69 
74  constexpr explicit bytes32(uint64_t v) noexcept
75  : evmc_bytes32{{0,
76  0,
77  0,
78  0,
79  0,
80  0,
81  0,
82  0,
83  0,
84  0,
85  0,
86  0,
87  0,
88  0,
89  0,
90  0,
91  0,
92  0,
93  0,
94  0,
95  0,
96  0,
97  0,
98  0,
99  static_cast<uint8_t>(v >> 56),
100  static_cast<uint8_t>(v >> 48),
101  static_cast<uint8_t>(v >> 40),
102  static_cast<uint8_t>(v >> 32),
103  static_cast<uint8_t>(v >> 24),
104  static_cast<uint8_t>(v >> 16),
105  static_cast<uint8_t>(v >> 8),
106  static_cast<uint8_t>(v >> 0)}}
107  {}
108 
110  constexpr inline explicit operator bool() const noexcept;
111 };
112 
115 
116 
118 inline constexpr uint64_t load64be(const uint8_t* data) noexcept
119 {
120  return (uint64_t{data[0]} << 56) | (uint64_t{data[1]} << 48) | (uint64_t{data[2]} << 40) |
121  (uint64_t{data[3]} << 32) | (uint64_t{data[4]} << 24) | (uint64_t{data[5]} << 16) |
122  (uint64_t{data[6]} << 8) | uint64_t{data[7]};
123 }
124 
126 inline constexpr uint64_t load64le(const uint8_t* data) noexcept
127 {
128  return uint64_t{data[0]} | (uint64_t{data[1]} << 8) | (uint64_t{data[2]} << 16) |
129  (uint64_t{data[3]} << 24) | (uint64_t{data[4]} << 32) | (uint64_t{data[5]} << 40) |
130  (uint64_t{data[6]} << 48) | (uint64_t{data[7]} << 56);
131 }
132 
134 inline constexpr uint32_t load32be(const uint8_t* data) noexcept
135 {
136  return (uint32_t{data[0]} << 24) | (uint32_t{data[1]} << 16) | (uint32_t{data[2]} << 8) |
137  uint32_t{data[3]};
138 }
139 
141 inline constexpr uint32_t load32le(const uint8_t* data) noexcept
142 {
143  return uint32_t{data[0]} | (uint32_t{data[1]} << 8) | (uint32_t{data[2]} << 16) |
144  (uint32_t{data[3]} << 24);
145 }
146 
147 namespace fnv
148 {
149 constexpr auto prime = 0x100000001b3;
150 constexpr auto offset_basis = 0xcbf29ce484222325;
151 
153 inline constexpr uint64_t fnv1a_by64(uint64_t h, uint64_t x) noexcept
154 {
155  return (h ^ x) * prime;
156 }
157 } // namespace fnv
158 
159 
161 inline constexpr bool operator==(const address& a, const address& b) noexcept
162 {
163  return load64le(&a.bytes[0]) == load64le(&b.bytes[0]) &&
164  load64le(&a.bytes[8]) == load64le(&b.bytes[8]) &&
165  load32le(&a.bytes[16]) == load32le(&b.bytes[16]);
166 }
167 
169 inline constexpr bool operator!=(const address& a, const address& b) noexcept
170 {
171  return !(a == b);
172 }
173 
175 inline constexpr bool operator<(const address& a, const address& b) noexcept
176 {
177  return load64be(&a.bytes[0]) < load64be(&b.bytes[0]) ||
178  (load64be(&a.bytes[0]) == load64be(&b.bytes[0]) &&
179  (load64be(&a.bytes[8]) < load64be(&b.bytes[8]) ||
180  (load64be(&a.bytes[8]) == load64be(&b.bytes[8]) &&
181  load32be(&a.bytes[16]) < load32be(&b.bytes[16]))));
182 }
183 
185 inline constexpr bool operator>(const address& a, const address& b) noexcept
186 {
187  return b < a;
188 }
189 
191 inline constexpr bool operator<=(const address& a, const address& b) noexcept
192 {
193  return !(b < a);
194 }
195 
197 inline constexpr bool operator>=(const address& a, const address& b) noexcept
198 {
199  return !(a < b);
200 }
201 
203 inline constexpr bool operator==(const bytes32& a, const bytes32& b) noexcept
204 {
205  return load64le(&a.bytes[0]) == load64le(&b.bytes[0]) &&
206  load64le(&a.bytes[8]) == load64le(&b.bytes[8]) &&
207  load64le(&a.bytes[16]) == load64le(&b.bytes[16]) &&
208  load64le(&a.bytes[24]) == load64le(&b.bytes[24]);
209 }
210 
212 inline constexpr bool operator!=(const bytes32& a, const bytes32& b) noexcept
213 {
214  return !(a == b);
215 }
216 
218 inline constexpr bool operator<(const bytes32& a, const bytes32& b) noexcept
219 {
220  return load64be(&a.bytes[0]) < load64be(&b.bytes[0]) ||
221  (load64be(&a.bytes[0]) == load64be(&b.bytes[0]) &&
222  (load64be(&a.bytes[8]) < load64be(&b.bytes[8]) ||
223  (load64be(&a.bytes[8]) == load64be(&b.bytes[8]) &&
224  (load64be(&a.bytes[16]) < load64be(&b.bytes[16]) ||
225  (load64be(&a.bytes[16]) == load64be(&b.bytes[16]) &&
226  load64be(&a.bytes[24]) < load64be(&b.bytes[24]))))));
227 }
228 
230 inline constexpr bool operator>(const bytes32& a, const bytes32& b) noexcept
231 {
232  return b < a;
233 }
234 
236 inline constexpr bool operator<=(const bytes32& a, const bytes32& b) noexcept
237 {
238  return !(b < a);
239 }
240 
242 inline constexpr bool operator>=(const bytes32& a, const bytes32& b) noexcept
243 {
244  return !(a < b);
245 }
246 
248 inline constexpr bool is_zero(const address& a) noexcept
249 {
250  return a == address{};
251 }
252 
253 inline constexpr address::operator bool() const noexcept
254 {
255  return !is_zero(*this);
256 }
257 
259 inline constexpr bool is_zero(const bytes32& a) noexcept
260 {
261  return a == bytes32{};
262 }
263 
264 inline constexpr bytes32::operator bool() const noexcept
265 {
266  return !is_zero(*this);
267 }
268 
269 namespace literals
270 {
271 namespace internal
272 {
273 constexpr int from_hex(char c) noexcept
274 {
275  return (c >= 'a' && c <= 'f') ? c - ('a' - 10) :
276  (c >= 'A' && c <= 'F') ? c - ('A' - 10) :
277  c - '0';
278 }
279 
280 constexpr uint8_t byte(const char* s, size_t i) noexcept
281 {
282  return static_cast<uint8_t>((from_hex(s[2 * i]) << 4) | from_hex(s[2 * i + 1]));
283 }
284 
285 template <typename T>
286 T from_hex(const char*) noexcept;
287 
288 template <>
289 constexpr bytes32 from_hex<bytes32>(const char* s) noexcept
290 {
291  return {
292  {{byte(s, 0), byte(s, 1), byte(s, 2), byte(s, 3), byte(s, 4), byte(s, 5), byte(s, 6),
293  byte(s, 7), byte(s, 8), byte(s, 9), byte(s, 10), byte(s, 11), byte(s, 12), byte(s, 13),
294  byte(s, 14), byte(s, 15), byte(s, 16), byte(s, 17), byte(s, 18), byte(s, 19), byte(s, 20),
295  byte(s, 21), byte(s, 22), byte(s, 23), byte(s, 24), byte(s, 25), byte(s, 26), byte(s, 27),
296  byte(s, 28), byte(s, 29), byte(s, 30), byte(s, 31)}}};
297 }
298 
299 template <>
300 constexpr address from_hex<address>(const char* s) noexcept
301 {
302  return {
303  {{byte(s, 0), byte(s, 1), byte(s, 2), byte(s, 3), byte(s, 4), byte(s, 5), byte(s, 6),
304  byte(s, 7), byte(s, 8), byte(s, 9), byte(s, 10), byte(s, 11), byte(s, 12), byte(s, 13),
305  byte(s, 14), byte(s, 15), byte(s, 16), byte(s, 17), byte(s, 18), byte(s, 19)}}};
306 }
307 
308 template <typename T, char... c>
309 constexpr T from_literal() noexcept
310 {
311  constexpr auto size = sizeof...(c);
312  constexpr char literal[] = {c...};
313  constexpr bool is_simple_zero = size == 1 && literal[0] == '0';
314 
315  static_assert(is_simple_zero || (literal[0] == '0' && literal[1] == 'x'),
316  "literal must be in hexadecimal notation");
317  static_assert(is_simple_zero || size == 2 * sizeof(T) + 2,
318  "literal must match the result type size");
319 
320  return is_simple_zero ? T{} : from_hex<T>(&literal[2]);
321 }
322 } // namespace internal
323 
325 template <char... c>
326 constexpr address operator""_address() noexcept
327 {
328  return internal::from_literal<address, c...>();
329 }
330 
332 template <char... c>
333 constexpr bytes32 operator""_bytes32() noexcept
334 {
335  return internal::from_literal<bytes32, c...>();
336 }
337 } // namespace literals
338 
339 using namespace literals;
340 
341 
343 inline const char* to_string(evmc_status_code status_code) noexcept
344 {
345  return evmc_status_code_to_string(status_code);
346 }
347 
349 inline const char* to_string(evmc_revision rev) noexcept
350 {
351  return evmc_revision_to_string(rev);
352 }
353 
354 
356 constexpr auto make_result = evmc_make_result;
357 
362 class result : private evmc_result
363 {
364 public:
366  using evmc_result::gas_left;
370 
380  result(evmc_status_code _status_code,
381  int64_t _gas_left,
382  const uint8_t* _output_data,
383  size_t _output_size) noexcept
384  : evmc_result{make_result(_status_code, _gas_left, _output_data, _output_size)}
385  {}
386 
388  explicit result(evmc_result const& res) noexcept : evmc_result{res} {}
389 
391  ~result() noexcept
392  {
393  if (release != nullptr)
394  release(this);
395  }
396 
398  result(result&& other) noexcept : evmc_result{other}
399  {
400  other.release = nullptr; // Disable releasing of the rvalue object.
401  }
402 
409  result& operator=(result&& other) noexcept
410  {
411  this->~result(); // Release this object.
412  static_cast<evmc_result&>(*this) = other; // Copy data.
413  other.release = nullptr; // Disable releasing of the rvalue object.
414  return *this;
415  }
416 
426  {
427  const auto out = evmc_result{*this}; // Copy data.
428  this->release = nullptr; // Disable releasing of this object.
429  return out;
430  }
431 };
432 
433 
436 {
437 public:
438  virtual ~HostInterface() noexcept = default;
439 
441  virtual bool account_exists(const address& addr) const noexcept = 0;
442 
444  virtual bytes32 get_storage(const address& addr, const bytes32& key) const noexcept = 0;
445 
448  const bytes32& key,
449  const bytes32& value) noexcept = 0;
450 
452  virtual uint256be get_balance(const address& addr) const noexcept = 0;
453 
455  virtual size_t get_code_size(const address& addr) const noexcept = 0;
456 
458  virtual bytes32 get_code_hash(const address& addr) const noexcept = 0;
459 
461  virtual size_t copy_code(const address& addr,
462  size_t code_offset,
463  uint8_t* buffer_data,
464  size_t buffer_size) const noexcept = 0;
465 
467  virtual void selfdestruct(const address& addr, const address& beneficiary) noexcept = 0;
468 
470  virtual result call(const evmc_message& msg) noexcept = 0;
471 
473  virtual evmc_tx_context get_tx_context() const noexcept = 0;
474 
476  virtual bytes32 get_block_hash(int64_t block_number) const noexcept = 0;
477 
479  virtual void emit_log(const address& addr,
480  const uint8_t* data,
481  size_t data_size,
482  const bytes32 topics[],
483  size_t num_topics) noexcept = 0;
484 
486  virtual evmc_access_status access_account(const address& addr) noexcept = 0;
487 
489  virtual evmc_access_status access_storage(const address& addr, const bytes32& key) noexcept = 0;
490 };
491 
492 
497 {
498  const evmc_host_interface* host = nullptr;
499  evmc_host_context* context = nullptr;
500  mutable evmc_tx_context tx_context = {};
501 
502 public:
504  HostContext() = default;
505 
509  HostContext(const evmc_host_interface& interface, evmc_host_context* ctx) noexcept
510  : host{&interface}, context{ctx}
511  {}
512 
513  bool account_exists(const address& address) const noexcept final
514  {
515  return host->account_exists(context, &address);
516  }
517 
518  bytes32 get_storage(const address& address, const bytes32& key) const noexcept final
519  {
520  return host->get_storage(context, &address, &key);
521  }
522 
524  const bytes32& key,
525  const bytes32& value) noexcept final
526  {
527  return host->set_storage(context, &address, &key, &value);
528  }
529 
530  uint256be get_balance(const address& address) const noexcept final
531  {
532  return host->get_balance(context, &address);
533  }
534 
535  size_t get_code_size(const address& address) const noexcept final
536  {
537  return host->get_code_size(context, &address);
538  }
539 
540  bytes32 get_code_hash(const address& address) const noexcept final
541  {
542  return host->get_code_hash(context, &address);
543  }
544 
545  size_t copy_code(const address& address,
546  size_t code_offset,
547  uint8_t* buffer_data,
548  size_t buffer_size) const noexcept final
549  {
550  return host->copy_code(context, &address, code_offset, buffer_data, buffer_size);
551  }
552 
553  void selfdestruct(const address& addr, const address& beneficiary) noexcept final
554  {
555  host->selfdestruct(context, &addr, &beneficiary);
556  }
557 
558  result call(const evmc_message& message) noexcept final
559  {
560  return result{host->call(context, &message)};
561  }
562 
569  evmc_tx_context get_tx_context() const noexcept final
570  {
571  if (tx_context.block_timestamp == 0)
572  tx_context = host->get_tx_context(context);
573  return tx_context;
574  }
575 
576  bytes32 get_block_hash(int64_t number) const noexcept final
577  {
578  return host->get_block_hash(context, number);
579  }
580 
581  void emit_log(const address& addr,
582  const uint8_t* data,
583  size_t data_size,
584  const bytes32 topics[],
585  size_t topics_count) noexcept final
586  {
587  host->emit_log(context, &addr, data, data_size, topics, topics_count);
588  }
589 
591  {
592  return host->access_account(context, &address);
593  }
594 
595  evmc_access_status access_storage(const address& address, const bytes32& key) noexcept final
596  {
597  return host->access_storage(context, &address, &key);
598  }
599 };
600 
601 
607 class Host : public HostInterface
608 {
609 public:
612  static const evmc_host_interface& get_interface() noexcept;
613 
616  evmc_host_context* to_context() noexcept { return reinterpret_cast<evmc_host_context*>(this); }
617 
622  template <typename DerivedClass = Host>
623  static DerivedClass* from_context(evmc_host_context* context) noexcept
624  {
625  // Get pointer of the Host base class.
626  auto* h = reinterpret_cast<Host*>(context);
627 
628  // Additional downcast, only possible if DerivedClass inherits from Host.
629  return static_cast<DerivedClass*>(h);
630  }
631 };
632 
633 
638 class VM
639 {
640 public:
641  VM() noexcept = default;
642 
644  explicit VM(evmc_vm* vm) noexcept : m_instance{vm} {}
645 
647  ~VM() noexcept
648  {
649  if (m_instance != nullptr)
650  m_instance->destroy(m_instance);
651  }
652 
653  VM(const VM&) = delete;
654  VM& operator=(const VM&) = delete;
655 
657  VM(VM&& other) noexcept : m_instance{other.m_instance} { other.m_instance = nullptr; }
658 
660  VM& operator=(VM&& other) noexcept
661  {
662  this->~VM();
663  m_instance = other.m_instance;
664  other.m_instance = nullptr;
665  return *this;
666  }
667 
670  inline VM(evmc_vm* vm,
671  std::initializer_list<std::pair<const char*, const char*>> options) noexcept;
672 
674  explicit operator bool() const noexcept { return m_instance != nullptr; }
675 
677  bool is_abi_compatible() const noexcept { return m_instance->abi_version == EVMC_ABI_VERSION; }
678 
680  char const* name() const noexcept { return m_instance->name; }
681 
683  char const* version() const noexcept { return m_instance->version; }
684 
686  bool has_capability(evmc_capabilities capability) const noexcept
687  {
688  return (get_capabilities() & static_cast<evmc_capabilities_flagset>(capability)) != 0;
689  }
690 
693  {
694  return m_instance->get_capabilities(m_instance);
695  }
696 
698  evmc_set_option_result set_option(const char name[], const char value[]) noexcept
699  {
700  return evmc_set_option(m_instance, name, value);
701  }
702 
705  evmc_host_context* ctx,
706  evmc_revision rev,
707  const evmc_message& msg,
708  const uint8_t* code,
709  size_t code_size) noexcept
710  {
711  return result{m_instance->execute(m_instance, &host, ctx, rev, &msg, code, code_size)};
712  }
713 
716  evmc_revision rev,
717  const evmc_message& msg,
718  const uint8_t* code,
719  size_t code_size) noexcept
720  {
721  return execute(Host::get_interface(), host.to_context(), rev, msg, code, code_size);
722  }
723 
733  const evmc_message& msg,
734  const uint8_t* code,
735  size_t code_size) noexcept
736  {
737  return result{
738  m_instance->execute(m_instance, nullptr, nullptr, rev, &msg, code, code_size)};
739  }
740 
746  evmc_vm* get_raw_pointer() const noexcept { return m_instance; }
747 
748 private:
749  evmc_vm* m_instance = nullptr;
750 };
751 
752 inline VM::VM(evmc_vm* vm,
753  std::initializer_list<std::pair<const char*, const char*>> options) noexcept
754  : m_instance{vm}
755 {
756  // This constructor is implemented outside of the class definition to workaround a doxygen bug.
757  for (const auto& option : options)
758  set_option(option.first, option.second);
759 }
760 
761 
762 namespace internal
763 {
764 inline bool account_exists(evmc_host_context* h, const evmc_address* addr) noexcept
765 {
766  return Host::from_context(h)->account_exists(*addr);
767 }
768 
769 inline evmc_bytes32 get_storage(evmc_host_context* h,
770  const evmc_address* addr,
771  const evmc_bytes32* key) noexcept
772 {
773  return Host::from_context(h)->get_storage(*addr, *key);
774 }
775 
776 inline evmc_storage_status set_storage(evmc_host_context* h,
777  const evmc_address* addr,
778  const evmc_bytes32* key,
779  const evmc_bytes32* value) noexcept
780 {
781  return Host::from_context(h)->set_storage(*addr, *key, *value);
782 }
783 
784 inline evmc_uint256be get_balance(evmc_host_context* h, const evmc_address* addr) noexcept
785 {
786  return Host::from_context(h)->get_balance(*addr);
787 }
788 
789 inline size_t get_code_size(evmc_host_context* h, const evmc_address* addr) noexcept
790 {
791  return Host::from_context(h)->get_code_size(*addr);
792 }
793 
794 inline evmc_bytes32 get_code_hash(evmc_host_context* h, const evmc_address* addr) noexcept
795 {
796  return Host::from_context(h)->get_code_hash(*addr);
797 }
798 
799 inline size_t copy_code(evmc_host_context* h,
800  const evmc_address* addr,
801  size_t code_offset,
802  uint8_t* buffer_data,
803  size_t buffer_size) noexcept
804 {
805  return Host::from_context(h)->copy_code(*addr, code_offset, buffer_data, buffer_size);
806 }
807 
808 inline void selfdestruct(evmc_host_context* h,
809  const evmc_address* addr,
810  const evmc_address* beneficiary) noexcept
811 {
812  Host::from_context(h)->selfdestruct(*addr, *beneficiary);
813 }
814 
815 inline evmc_result call(evmc_host_context* h, const evmc_message* msg) noexcept
816 {
817  return Host::from_context(h)->call(*msg).release_raw();
818 }
819 
820 inline evmc_tx_context get_tx_context(evmc_host_context* h) noexcept
821 {
822  return Host::from_context(h)->get_tx_context();
823 }
824 
825 inline evmc_bytes32 get_block_hash(evmc_host_context* h, int64_t block_number) noexcept
826 {
827  return Host::from_context(h)->get_block_hash(block_number);
828 }
829 
830 inline void emit_log(evmc_host_context* h,
831  const evmc_address* addr,
832  const uint8_t* data,
833  size_t data_size,
834  const evmc_bytes32 topics[],
835  size_t num_topics) noexcept
836 {
837  Host::from_context(h)->emit_log(*addr, data, data_size, static_cast<const bytes32*>(topics),
838  num_topics);
839 }
840 
841 inline evmc_access_status access_account(evmc_host_context* h, const evmc_address* addr) noexcept
842 {
843  return Host::from_context(h)->access_account(*addr);
844 }
845 
846 inline evmc_access_status access_storage(evmc_host_context* h,
847  const evmc_address* addr,
848  const evmc_bytes32* key) noexcept
849 {
850  return Host::from_context(h)->access_storage(*addr, *key);
851 }
852 } // namespace internal
853 
854 inline const evmc_host_interface& Host::get_interface() noexcept
855 {
856  static constexpr evmc_host_interface interface{
857  ::evmc::internal::account_exists, ::evmc::internal::get_storage,
858  ::evmc::internal::set_storage, ::evmc::internal::get_balance,
859  ::evmc::internal::get_code_size, ::evmc::internal::get_code_hash,
860  ::evmc::internal::copy_code, ::evmc::internal::selfdestruct,
861  ::evmc::internal::call, ::evmc::internal::get_tx_context,
862  ::evmc::internal::get_block_hash, ::evmc::internal::emit_log,
863  ::evmc::internal::access_account, ::evmc::internal::access_storage,
864  };
865  return interface;
866 }
867 } // namespace evmc
868 
869 
874 inline std::ostream& operator<<(std::ostream& os, evmc_status_code status_code)
875 {
876  return os << evmc::to_string(status_code);
877 }
878 
883 inline std::ostream& operator<<(std::ostream& os, evmc_revision rev)
884 {
885  return os << evmc::to_string(rev);
886 }
887 
888 namespace std
889 {
891 template <>
892 struct hash<evmc::address>
893 {
895  constexpr size_t operator()(const evmc::address& s) const noexcept
896  {
897  using namespace evmc;
898  using namespace fnv;
899  return static_cast<size_t>(fnv1a_by64(
900  fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])), load64le(&s.bytes[8])),
901  load32le(&s.bytes[16])));
902  }
903 };
904 
906 template <>
907 struct hash<evmc::bytes32>
908 {
910  constexpr size_t operator()(const evmc::bytes32& s) const noexcept
911  {
912  using namespace evmc;
913  using namespace fnv;
914  return static_cast<size_t>(
915  fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])),
916  load64le(&s.bytes[8])),
917  load64le(&s.bytes[16])),
918  load64le(&s.bytes[24])));
919  }
920 };
921 } // namespace std
Wrapper around EVMC host context / host interface.
Definition: evmc.hpp:497
bytes32 get_code_hash(const address &address) const noexcept final
Get code hash callback function.
Definition: evmc.hpp:540
bool account_exists(const address &address) const noexcept final
Check account existence callback function.
Definition: evmc.hpp:513
void selfdestruct(const address &addr, const address &beneficiary) noexcept final
Selfdestruct callback function.
Definition: evmc.hpp:553
size_t get_code_size(const address &address) const noexcept final
Get code size callback function.
Definition: evmc.hpp:535
HostContext()=default
Default constructor for null Host context.
uint256be get_balance(const address &address) const noexcept final
Get balance callback function.
Definition: evmc.hpp:530
HostContext(const evmc_host_interface &interface, evmc_host_context *ctx) noexcept
Constructor from the EVMC Host primitives.
Definition: evmc.hpp:509
evmc_access_status access_storage(const address &address, const bytes32 &key) noexcept final
Access storage callback function.
Definition: evmc.hpp:595
result call(const evmc_message &message) noexcept final
Call callback function.
Definition: evmc.hpp:558
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.
Definition: evmc.hpp:545
evmc_access_status access_account(const address &address) noexcept final
Access account callback function.
Definition: evmc.hpp:590
bytes32 get_block_hash(int64_t number) const noexcept final
Get block hash callback function.
Definition: evmc.hpp:576
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.
Definition: evmc.hpp:581
evmc_storage_status set_storage(const address &address, const bytes32 &key, const bytes32 &value) noexcept final
Set storage callback function.
Definition: evmc.hpp:523
evmc_tx_context get_tx_context() const noexcept final
Get transaction context callback function.
Definition: evmc.hpp:569
bytes32 get_storage(const address &address, const bytes32 &key) const noexcept final
Get storage callback function.
Definition: evmc.hpp:518
The EVMC Host interface.
Definition: evmc.hpp:436
virtual bytes32 get_storage(const address &addr, const bytes32 &key) const noexcept=0
Get storage callback function.
virtual evmc_tx_context get_tx_context() const noexcept=0
Get transaction context callback function.
virtual result call(const evmc_message &msg) noexcept=0
Call 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 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 bool account_exists(const address &addr) const noexcept=0
Check account existence callback function.
virtual void selfdestruct(const address &addr, const address &beneficiary) noexcept=0
Selfdestruct callback function.
Abstract class to be used by Host implementations.
Definition: evmc.hpp:608
evmc_host_context * to_context() noexcept
Converts the Host object to the opaque host context pointer.
Definition: evmc.hpp:616
static const evmc_host_interface & get_interface() noexcept
Provides access to the global host interface.
Definition: evmc.hpp:854
static DerivedClass * from_context(evmc_host_context *context) noexcept
Converts the opaque host context pointer back to the original Host object.
Definition: evmc.hpp:623
The VM instance.
Definition: evmc.hpp:639
char const * version() const noexcept
The version of the EVMC VM implementation, e.g.
Definition: evmc.hpp:683
result execute(evmc_revision rev, const evmc_message &msg, const uint8_t *code, size_t code_size) noexcept
Executes code without the Host context.
Definition: evmc.hpp:732
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.
Definition: evmc.hpp:715
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.
Definition: evmc.hpp:698
evmc_vm * get_raw_pointer() const noexcept
Returns the pointer to C EVMC struct representing the VM.
Definition: evmc.hpp:746
VM & operator=(VM &&other) noexcept
Move assignment operator.
Definition: evmc.hpp:660
~VM() noexcept
Destructor responsible for automatically destroying the VM instance.
Definition: evmc.hpp:647
bool is_abi_compatible() const noexcept
Checks whenever the VM instance is ABI compatible with the current EVMC API.
Definition: evmc.hpp:677
bool has_capability(evmc_capabilities capability) const noexcept
Checks if the VM has the given capability.
Definition: evmc.hpp:686
VM(VM &&other) noexcept
Move constructor.
Definition: evmc.hpp:657
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.
Definition: evmc.hpp:704
evmc_capabilities_flagset get_capabilities() const noexcept
A method returning capabilities supported by the VM instance.
Definition: evmc.hpp:692
VM(evmc_vm *vm) noexcept
Converting constructor from evmc_vm.
Definition: evmc.hpp:644
char const * name() const noexcept
The name of the EVMC VM implementation.
Definition: evmc.hpp:680
The EVM code execution result.
Definition: evmc.hpp:363
result & operator=(result &&other) noexcept
Move assignment operator.
Definition: evmc.hpp:409
evmc_result release_raw() noexcept
Releases the ownership and returns the raw copy of evmc_result.
Definition: evmc.hpp:425
result(evmc_result const &res) noexcept
Converting constructor from raw evmc_result.
Definition: evmc.hpp:388
result(evmc_status_code _status_code, int64_t _gas_left, const uint8_t *_output_data, size_t _output_size) noexcept
Creates the result from the provided arguments.
Definition: evmc.hpp:380
result(result &&other) noexcept
Move constructor.
Definition: evmc.hpp:398
~result() noexcept
Destructor responsible for automatically releasing attached resources.
Definition: evmc.hpp:391
EVMC: Ethereum Client-VM Connector API.
std::ostream & operator<<(std::ostream &os, evmc_status_code status_code)
"Stream out" operator implementation for evmc_status_code.
Definition: evmc.hpp:874
constexpr auto offset_basis
The 64-bit FNV offset basis.
Definition: evmc.hpp:150
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.
Definition: evmc.hpp:153
constexpr auto prime
The 64-bit FNV prime number.
Definition: evmc.hpp:149
evmc_set_option_result
Possible outcomes of evmc_set_option.
Definition: evmc.h:731
evmc_capabilities
Possible capabilities of a VM.
Definition: evmc.h:870
evmc_status_code
The execution status code.
Definition: evmc.h:213
uint32_t evmc_capabilities_flagset
Alias for unsigned integer representing a set of bit flags of EVMC capabilities.
Definition: evmc.h:899
evmc_access_status
Access status per EIP-2929: Gas cost increases for state access opcodes.
Definition: evmc.h:611
evmc_storage_status
The effect of an attempt to modify a contract storage item.
Definition: evmc.h:464
evmc_revision
EVM revision.
Definition: evmc.h:762
@ EVMC_ABI_VERSION
The EVMC ABI version number of the interface declared in this file.
Definition: evmc.h:47
static struct evmc_result evmc_make_result(enum evmc_status_code status_code, int64_t gas_left, const uint8_t *output_data, size_t output_size)
Creates the result from the provided arguments.
Definition: helpers.h:121
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:213
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.
Definition: helpers.h:73
static const char * evmc_revision_to_string(enum evmc_revision rev)
Returns the name of the evmc_revision.
Definition: helpers.h:264
EVMC Helpers.
EVMC C++ API - wrappers and bindings for C++.
Definition: evmc.hpp:18
constexpr bool operator<(const address &a, const address &b) noexcept
The "less than" comparison operator for the evmc::address type.
Definition: evmc.hpp:175
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:118
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:134
constexpr bool operator==(const address &a, const address &b) noexcept
The "equal to" comparison operator for the evmc::address type.
Definition: evmc.hpp:161
constexpr bool operator>(const address &a, const address &b) noexcept
The "greater than" comparison operator for the evmc::address type.
Definition: evmc.hpp:185
bytes from_hex(std::string_view hex)
Decodes hex encoded string to bytes.
constexpr auto make_result
Alias for evmc_make_result().
Definition: evmc.hpp:356
constexpr bool operator>=(const address &a, const address &b) noexcept
The "greater than or equal to" comparison operator for the evmc::address type.
Definition: evmc.hpp:197
const char * to_string(evmc_status_code status_code) noexcept
Returns text representation of the evmc_status_code.
Definition: evmc.hpp:343
constexpr bool operator<=(const address &a, const address &b) noexcept
The "less than or equal to" comparison operator for the evmc::address type.
Definition: evmc.hpp:191
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:126
constexpr bool operator!=(const address &a, const address &b) noexcept
The "not equal to" comparison operator for the evmc::address type.
Definition: evmc.hpp:169
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:141
constexpr bool is_zero(const address &a) noexcept
Checks if the given address is the zero address.
Definition: evmc.hpp:248
The big-endian 160-bit hash suitable for keeping an Ethereum address.
Definition: evmc.hpp:23
constexpr address(evmc_address init={}) noexcept
Default and converting constructor.
Definition: evmc.hpp:27
constexpr address(uint64_t v) noexcept
Converting constructor from unsigned integer value.
Definition: evmc.hpp:33
The fixed size array of 32 bytes for storing 256-bit EVM values.
Definition: evmc.hpp:64
constexpr bytes32(evmc_bytes32 init={}) noexcept
Default and converting constructor.
Definition: evmc.hpp:68
constexpr bytes32(uint64_t v) noexcept
Converting constructor from unsigned integer value.
Definition: evmc.hpp:74
Big-endian 160-bit hash suitable for keeping an Ethereum address.
Definition: evmc.h:69
The fixed size array of 32 bytes.
Definition: evmc.h:57
The opaque data type representing the Host execution context.
The Host interface.
Definition: evmc.h:672
evmc_get_code_hash_fn get_code_hash
Get code hash callback function.
Definition: evmc.h:689
evmc_call_fn call
Call callback function.
Definition: evmc.h:698
evmc_selfdestruct_fn selfdestruct
Selfdestruct callback function.
Definition: evmc.h:695
evmc_get_storage_fn get_storage
Get storage callback function.
Definition: evmc.h:677
evmc_copy_code_fn copy_code
Copy code callback function.
Definition: evmc.h:692
evmc_get_code_size_fn get_code_size
Get code size callback function.
Definition: evmc.h:686
evmc_get_block_hash_fn get_block_hash
Get block hash callback function.
Definition: evmc.h:704
evmc_emit_log_fn emit_log
Emit log callback function.
Definition: evmc.h:707
evmc_account_exists_fn account_exists
Check account existence callback function.
Definition: evmc.h:674
evmc_access_account_fn access_account
Access account callback function.
Definition: evmc.h:710
evmc_get_balance_fn get_balance
Get balance callback function.
Definition: evmc.h:683
evmc_set_storage_fn set_storage
Set storage callback function.
Definition: evmc.h:680
evmc_get_tx_context_fn get_tx_context
Get transaction context callback function.
Definition: evmc.h:701
evmc_access_storage_fn access_storage
Access storage callback function.
Definition: evmc.h:713
The message describing an EVM call, including a zero-depth calls from a transaction origin.
Definition: evmc.h:96
The EVM code execution result.
Definition: evmc.h:345
const uint8_t * output_data
The reference to output data.
Definition: evmc.h:368
enum evmc_status_code status_code
The execution status code.
Definition: evmc.h:347
evmc_release_result_fn release
The method releasing all resources associated with the result object.
Definition: evmc.h:396
size_t output_size
The size of the output data.
Definition: evmc.h:375
evmc_address create_address
The address of the contract created by create instructions.
Definition: evmc.h:407
int64_t gas_left
The amount of gas left after the execution.
Definition: evmc.h:355
The transaction and block data for execution.
Definition: evmc.h:148
int64_t block_timestamp
The block timestamp.
Definition: evmc.h:153
The VM instance.
Definition: evmc.h:919