EVMC
evmc.hpp
Go to the documentation of this file.
1// EVMC: Ethereum Client-VM Connector API.
2// Copyright 2018 The EVMC Authors.
3// Licensed under the Apache License, Version 2.0.
4#pragma once
5
6#include <evmc/evmc.h>
7#include <evmc/helpers.h>
8#include <evmc/hex.hpp>
9
10#include <functional>
11#include <initializer_list>
12#include <ostream>
13#include <string_view>
14#include <utility>
15
17 "latest stable revision ill-defined");
18
21namespace evmc
22{
27{
31 constexpr address(evmc_address init = {}) noexcept : evmc_address{init} {}
32
37 constexpr explicit address(uint64_t v) noexcept
38 : evmc_address{{0,
39 0,
40 0,
41 0,
42 0,
43 0,
44 0,
45 0,
46 0,
47 0,
48 0,
49 0,
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)}}
58 {}
59
61 inline constexpr explicit operator bool() const noexcept;
62
64 inline constexpr operator bytes_view() const noexcept { return {bytes, sizeof(bytes)}; }
65};
66
71{
75 constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}
76
81 constexpr explicit bytes32(uint64_t v) noexcept
82 : evmc_bytes32{{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 0,
100 0,
101 0,
102 0,
103 0,
104 0,
105 0,
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)}}
114 {}
115
117 inline constexpr explicit operator bool() const noexcept;
118
120 inline constexpr operator bytes_view() const noexcept { return {bytes, sizeof(bytes)}; }
121};
122
125
126
128inline constexpr uint64_t load64be(const uint8_t* data) noexcept
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}
134
136inline constexpr uint64_t load64le(const uint8_t* data) noexcept
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}
142
144inline constexpr uint32_t load32be(const uint8_t* data) noexcept
145{
146 return (uint32_t{data[0]} << 24) | (uint32_t{data[1]} << 16) | (uint32_t{data[2]} << 8) |
147 uint32_t{data[3]};
148}
149
151inline constexpr uint32_t load32le(const uint8_t* data) noexcept
152{
153 return uint32_t{data[0]} | (uint32_t{data[1]} << 8) | (uint32_t{data[2]} << 16) |
154 (uint32_t{data[3]} << 24);
155}
156
157namespace fnv
158{
159constexpr auto prime = 0x100000001b3;
160constexpr auto offset_basis = 0xcbf29ce484222325;
161
163inline constexpr uint64_t fnv1a_by64(uint64_t h, uint64_t x) noexcept
164{
165 return (h ^ x) * prime;
166}
167} // namespace fnv
168
169
171inline constexpr bool operator==(const address& a, const address& b) noexcept
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}
177
179inline constexpr bool operator!=(const address& a, const address& b) noexcept
180{
181 return !(a == b);
182}
183
185inline constexpr bool operator<(const address& a, const address& b) noexcept
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}
193
195inline constexpr bool operator>(const address& a, const address& b) noexcept
196{
197 return b < a;
198}
199
201inline constexpr bool operator<=(const address& a, const address& b) noexcept
202{
203 return !(b < a);
204}
205
207inline constexpr bool operator>=(const address& a, const address& b) noexcept
208{
209 return !(a < b);
210}
211
213inline constexpr bool operator==(const bytes32& a, const bytes32& b) noexcept
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}
220
222inline constexpr bool operator!=(const bytes32& a, const bytes32& b) noexcept
223{
224 return !(a == b);
225}
226
228inline constexpr bool operator<(const bytes32& a, const bytes32& b) noexcept
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}
238
240inline constexpr bool operator>(const bytes32& a, const bytes32& b) noexcept
241{
242 return b < a;
243}
244
246inline constexpr bool operator<=(const bytes32& a, const bytes32& b) noexcept
247{
248 return !(b < a);
249}
250
252inline constexpr bool operator>=(const bytes32& a, const bytes32& b) noexcept
253{
254 return !(a < b);
255}
256
258inline constexpr bool is_zero(const address& a) noexcept
259{
260 return a == address{};
261}
262
263inline constexpr address::operator bool() const noexcept
264{
265 return !is_zero(*this);
266}
267
269inline constexpr bool is_zero(const bytes32& a) noexcept
270{
271 return a == bytes32{};
272}
273
274inline constexpr bytes32::operator bool() const noexcept
275{
276 return !is_zero(*this);
277}
278
279namespace literals
280{
286template <typename T>
287constexpr T parse(std::string_view s) noexcept
288{
289 return from_hex<T>(s).value();
290}
291
293constexpr address operator""_address(const char* s) noexcept
294{
295 return parse<address>(s);
296}
297
299constexpr bytes32 operator""_bytes32(const char* s) noexcept
300{
301 return parse<bytes32>(s);
302}
303} // namespace literals
304
305using namespace literals;
306
307
309inline const char* to_string(evmc_status_code status_code) noexcept
310{
311 return evmc_status_code_to_string(status_code);
312}
313
315inline const char* to_string(evmc_revision rev) noexcept
316{
317 return evmc_revision_to_string(rev);
318}
319
320
323
328class Result : private evmc_result
329{
330public:
337
348 explicit Result(evmc_status_code _status_code,
349 int64_t _gas_left,
350 int64_t _gas_refund,
351 const uint8_t* _output_data,
352 size_t _output_size) noexcept
353 : evmc_result{make_result(_status_code, _gas_left, _gas_refund, _output_data, _output_size)}
354 {}
355
362 int64_t _gas_left = 0,
363 int64_t _gas_refund = 0) noexcept
364 : evmc_result{make_result(_status_code, _gas_left, _gas_refund, nullptr, 0)}
365 {}
366
373 explicit Result(evmc_status_code _status_code,
374 int64_t _gas_left,
375 int64_t _gas_refund,
376 const evmc_address& _create_address) noexcept
377 : evmc_result{make_result(_status_code, _gas_left, _gas_refund, nullptr, 0)}
378 {
379 create_address = _create_address;
380 }
381
385 explicit Result(const evmc_result& res) noexcept : evmc_result{res} {}
386
388 ~Result() noexcept
389 {
390 if (release != nullptr)
391 release(this);
392 }
393
395 Result(Result&& other) noexcept : evmc_result{other}
396 {
397 other.release = nullptr; // Disable releasing of the rvalue object.
398 }
399
406 Result& operator=(Result&& other) noexcept
407 {
408 this->~Result(); // Release this object.
409 static_cast<evmc_result&>(*this) = other; // Copy data.
410 other.release = nullptr; // Disable releasing of the rvalue object.
411 return *this;
412 }
413
415 evmc_result& raw() noexcept { return *this; }
416
418 const evmc_result& raw() const noexcept { return *this; }
419
429 {
430 const auto out = evmc_result{*this}; // Copy data.
431 this->release = nullptr; // Disable releasing of this object.
432 return out;
433 }
434};
435
436
439{
440public:
441 virtual ~HostInterface() noexcept = default;
442
444 virtual bool account_exists(const address& addr) const noexcept = 0;
445
447 virtual bytes32 get_storage(const address& addr, const bytes32& key) const noexcept = 0;
448
451 const bytes32& key,
452 const bytes32& value) noexcept = 0;
453
455 virtual uint256be get_balance(const address& addr) const noexcept = 0;
456
458 virtual size_t get_code_size(const address& addr) const noexcept = 0;
459
461 virtual bytes32 get_code_hash(const address& addr) const noexcept = 0;
462
464 virtual size_t copy_code(const address& addr,
465 size_t code_offset,
466 uint8_t* buffer_data,
467 size_t buffer_size) const noexcept = 0;
468
470 virtual bool selfdestruct(const address& addr, const address& beneficiary) noexcept = 0;
471
473 virtual Result call(const evmc_message& msg) noexcept = 0;
474
476 virtual evmc_tx_context get_tx_context() const noexcept = 0;
477
479 virtual bytes32 get_block_hash(int64_t block_number) const noexcept = 0;
480
482 virtual void emit_log(const address& addr,
483 const uint8_t* data,
484 size_t data_size,
485 const bytes32 topics[],
486 size_t num_topics) noexcept = 0;
487
489 virtual evmc_access_status access_account(const address& addr) noexcept = 0;
490
492 virtual evmc_access_status access_storage(const address& addr, const bytes32& key) noexcept = 0;
493
496 const bytes32& key) const noexcept = 0;
497
499 virtual void set_transient_storage(const address& addr,
500 const bytes32& key,
501 const bytes32& value) noexcept = 0;
502};
503
504
509{
510 const evmc_host_interface* host = nullptr;
511 evmc_host_context* context = nullptr;
512
513public:
515 HostContext() = default;
516
520 HostContext(const evmc_host_interface& interface, evmc_host_context* ctx) noexcept
521 : host{&interface}, context{ctx}
522 {}
523
524 bool account_exists(const address& address) const noexcept final
525 {
526 return host->account_exists(context, &address);
527 }
528
529 bytes32 get_storage(const address& address, const bytes32& key) const noexcept final
530 {
531 return host->get_storage(context, &address, &key);
532 }
533
535 const bytes32& key,
536 const bytes32& value) noexcept final
537 {
538 return host->set_storage(context, &address, &key, &value);
539 }
540
541 uint256be get_balance(const address& address) const noexcept final
542 {
543 return host->get_balance(context, &address);
544 }
545
546 size_t get_code_size(const address& address) const noexcept final
547 {
548 return host->get_code_size(context, &address);
549 }
550
551 bytes32 get_code_hash(const address& address) const noexcept final
552 {
553 return host->get_code_hash(context, &address);
554 }
555
556 size_t copy_code(const address& address,
557 size_t code_offset,
558 uint8_t* buffer_data,
559 size_t buffer_size) const noexcept final
560 {
561 return host->copy_code(context, &address, code_offset, buffer_data, buffer_size);
562 }
563
564 bool selfdestruct(const address& addr, const address& beneficiary) noexcept final
565 {
566 return host->selfdestruct(context, &addr, &beneficiary);
567 }
568
569 Result call(const evmc_message& message) noexcept final
570 {
571 return Result{host->call(context, &message)};
572 }
573
575 evmc_tx_context get_tx_context() const noexcept final { return host->get_tx_context(context); }
576
577 bytes32 get_block_hash(int64_t number) const noexcept final
578 {
579 return host->get_block_hash(context, number);
580 }
581
582 void emit_log(const address& addr,
583 const uint8_t* data,
584 size_t data_size,
585 const bytes32 topics[],
586 size_t topics_count) noexcept final
587 {
588 host->emit_log(context, &addr, data, data_size, topics, topics_count);
589 }
590
592 {
593 return host->access_account(context, &address);
594 }
595
596 evmc_access_status access_storage(const address& address, const bytes32& key) noexcept final
597 {
598 return host->access_storage(context, &address, &key);
599 }
600
601 bytes32 get_transient_storage(const address& address, const bytes32& key) const noexcept final
602 {
603 return host->get_transient_storage(context, &address, &key);
604 }
605
607 const bytes32& key,
608 const bytes32& value) noexcept final
609 {
610 host->set_transient_storage(context, &address, &key, &value);
611 }
612};
613
614
620class Host : public HostInterface
621{
622public:
625 static const evmc_host_interface& get_interface() noexcept;
626
629 evmc_host_context* to_context() noexcept { return reinterpret_cast<evmc_host_context*>(this); }
630
635 template <typename DerivedClass = Host>
636 static DerivedClass* from_context(evmc_host_context* context) noexcept
637 {
638 // Get pointer of the Host base class.
639 auto* h = reinterpret_cast<Host*>(context);
640
641 // Additional downcast, only possible if DerivedClass inherits from Host.
642 return static_cast<DerivedClass*>(h);
643 }
644};
645
646
651class VM
652{
653public:
654 VM() noexcept = default;
655
657 explicit VM(evmc_vm* vm) noexcept : m_instance{vm} {}
658
660 ~VM() noexcept
661 {
662 if (m_instance != nullptr)
663 m_instance->destroy(m_instance);
664 }
665
666 VM(const VM&) = delete;
667 VM& operator=(const VM&) = delete;
668
670 VM(VM&& other) noexcept : m_instance{other.m_instance} { other.m_instance = nullptr; }
671
673 VM& operator=(VM&& other) noexcept
674 {
675 this->~VM();
676 m_instance = other.m_instance;
677 other.m_instance = nullptr;
678 return *this;
679 }
680
683 inline VM(evmc_vm* vm,
684 std::initializer_list<std::pair<const char*, const char*>> options) noexcept;
685
687 explicit operator bool() const noexcept { return m_instance != nullptr; }
688
690 bool is_abi_compatible() const noexcept { return m_instance->abi_version == EVMC_ABI_VERSION; }
691
693 char const* name() const noexcept { return m_instance->name; }
694
696 char const* version() const noexcept { return m_instance->version; }
697
699 bool has_capability(evmc_capabilities capability) const noexcept
700 {
701 return (get_capabilities() & static_cast<evmc_capabilities_flagset>(capability)) != 0;
702 }
703
706 {
707 return m_instance->get_capabilities(m_instance);
708 }
709
711 evmc_set_option_result set_option(const char name[], const char value[]) noexcept
712 {
713 return evmc_set_option(m_instance, name, value);
714 }
715
719 evmc_revision rev,
720 const evmc_message& msg,
721 const uint8_t* code,
722 size_t code_size) noexcept
723 {
724 return Result{m_instance->execute(m_instance, &host, ctx, rev, &msg, code, code_size)};
725 }
726
729 evmc_revision rev,
730 const evmc_message& msg,
731 const uint8_t* code,
732 size_t code_size) noexcept
733 {
734 return execute(Host::get_interface(), host.to_context(), rev, msg, code, code_size);
735 }
736
746 const evmc_message& msg,
747 const uint8_t* code,
748 size_t code_size) noexcept
749 {
750 return Result{
751 m_instance->execute(m_instance, nullptr, nullptr, rev, &msg, code, code_size)};
752 }
753
759 evmc_vm* get_raw_pointer() const noexcept { return m_instance; }
760
761private:
762 evmc_vm* m_instance = nullptr;
763};
764
765inline VM::VM(evmc_vm* vm,
766 std::initializer_list<std::pair<const char*, const char*>> options) noexcept
767 : m_instance{vm}
768{
769 // This constructor is implemented outside of the class definition to workaround a doxygen bug.
770 for (const auto& option : options)
771 set_option(option.first, option.second);
772}
773
774
775namespace internal
776{
777inline bool account_exists(evmc_host_context* h, const evmc_address* addr) noexcept
778{
779 return Host::from_context(h)->account_exists(*addr);
780}
781
782inline evmc_bytes32 get_storage(evmc_host_context* h,
783 const evmc_address* addr,
784 const evmc_bytes32* key) noexcept
785{
786 return Host::from_context(h)->get_storage(*addr, *key);
787}
788
789inline evmc_storage_status set_storage(evmc_host_context* h,
790 const evmc_address* addr,
791 const evmc_bytes32* key,
792 const evmc_bytes32* value) noexcept
793{
794 return Host::from_context(h)->set_storage(*addr, *key, *value);
795}
796
797inline evmc_uint256be get_balance(evmc_host_context* h, const evmc_address* addr) noexcept
798{
799 return Host::from_context(h)->get_balance(*addr);
800}
801
802inline size_t get_code_size(evmc_host_context* h, const evmc_address* addr) noexcept
803{
804 return Host::from_context(h)->get_code_size(*addr);
805}
806
807inline evmc_bytes32 get_code_hash(evmc_host_context* h, const evmc_address* addr) noexcept
808{
809 return Host::from_context(h)->get_code_hash(*addr);
810}
811
812inline size_t copy_code(evmc_host_context* h,
813 const evmc_address* addr,
814 size_t code_offset,
815 uint8_t* buffer_data,
816 size_t buffer_size) noexcept
817{
818 return Host::from_context(h)->copy_code(*addr, code_offset, buffer_data, buffer_size);
819}
820
821inline bool selfdestruct(evmc_host_context* h,
822 const evmc_address* addr,
823 const evmc_address* beneficiary) noexcept
824{
825 return Host::from_context(h)->selfdestruct(*addr, *beneficiary);
826}
827
828inline evmc_result call(evmc_host_context* h, const evmc_message* msg) noexcept
829{
830 return Host::from_context(h)->call(*msg).release_raw();
831}
832
833inline evmc_tx_context get_tx_context(evmc_host_context* h) noexcept
834{
835 return Host::from_context(h)->get_tx_context();
836}
837
838inline evmc_bytes32 get_block_hash(evmc_host_context* h, int64_t block_number) noexcept
839{
840 return Host::from_context(h)->get_block_hash(block_number);
841}
842
843inline void emit_log(evmc_host_context* h,
844 const evmc_address* addr,
845 const uint8_t* data,
846 size_t data_size,
847 const evmc_bytes32 topics[],
848 size_t num_topics) noexcept
849{
850 Host::from_context(h)->emit_log(*addr, data, data_size, static_cast<const bytes32*>(topics),
851 num_topics);
852}
853
854inline evmc_access_status access_account(evmc_host_context* h, const evmc_address* addr) noexcept
855{
856 return Host::from_context(h)->access_account(*addr);
857}
858
859inline evmc_access_status access_storage(evmc_host_context* h,
860 const evmc_address* addr,
861 const evmc_bytes32* key) noexcept
862{
863 return Host::from_context(h)->access_storage(*addr, *key);
864}
865
866inline evmc_bytes32 get_transient_storage(evmc_host_context* h,
867 const evmc_address* addr,
868 const evmc_bytes32* key) noexcept
869{
870 return Host::from_context(h)->get_transient_storage(*addr, *key);
871}
872
873inline void set_transient_storage(evmc_host_context* h,
874 const evmc_address* addr,
875 const evmc_bytes32* key,
876 const evmc_bytes32* value) noexcept
877{
878 Host::from_context(h)->set_transient_storage(*addr, *key, *value);
879}
880} // namespace internal
881
883{
884 static constexpr evmc_host_interface interface = {
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,
901 };
902 return interface;
903}
904} // namespace evmc
905
906
911inline std::ostream& operator<<(std::ostream& os, evmc_status_code status_code)
912{
913 return os << evmc::to_string(status_code);
914}
915
920inline std::ostream& operator<<(std::ostream& os, evmc_revision rev)
921{
922 return os << evmc::to_string(rev);
923}
924
925namespace std
926{
928template <>
929struct hash<evmc::address>
930{
932 constexpr size_t operator()(const evmc::address& s) const noexcept
933 {
934 using namespace evmc;
935 using namespace fnv;
936 return static_cast<size_t>(fnv1a_by64(
937 fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])), load64le(&s.bytes[8])),
938 load32le(&s.bytes[16])));
939 }
940};
941
943template <>
944struct hash<evmc::bytes32>
945{
947 constexpr size_t operator()(const evmc::bytes32& s) const noexcept
948 {
949 using namespace evmc;
950 using namespace fnv;
951 return static_cast<size_t>(
952 fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])),
953 load64le(&s.bytes[8])),
954 load64le(&s.bytes[16])),
955 load64le(&s.bytes[24])));
956 }
957};
958} // namespace std
Wrapper around EVMC host context / host interface.
Definition: evmc.hpp:509
bytes32 get_code_hash(const address &address) const noexcept final
Get code hash callback function.
Definition: evmc.hpp:551
bool account_exists(const address &address) const noexcept final
Check account existence callback function.
Definition: evmc.hpp:524
size_t get_code_size(const address &address) const noexcept final
Get code size callback function.
Definition: evmc.hpp:546
HostContext()=default
Default constructor for null Host context.
uint256be get_balance(const address &address) const noexcept final
Get balance callback function.
Definition: evmc.hpp:541
HostContext(const evmc_host_interface &interface, evmc_host_context *ctx) noexcept
Constructor from the EVMC Host primitives.
Definition: evmc.hpp:520
evmc_access_status access_storage(const address &address, const bytes32 &key) noexcept final
Access storage callback function.
Definition: evmc.hpp:596
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:556
Result call(const evmc_message &message) noexcept final
Call callback function.
Definition: evmc.hpp:569
bool selfdestruct(const address &addr, const address &beneficiary) noexcept final
Selfdestruct callback function.
Definition: evmc.hpp:564
evmc_access_status access_account(const address &address) noexcept final
Access account callback function.
Definition: evmc.hpp:591
void set_transient_storage(const address &address, const bytes32 &key, const bytes32 &value) noexcept final
Set transient storage callback function.
Definition: evmc.hpp:606
bytes32 get_block_hash(int64_t number) const noexcept final
Get block hash callback function.
Definition: evmc.hpp:577
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:582
evmc_storage_status set_storage(const address &address, const bytes32 &key, const bytes32 &value) noexcept final
Set storage callback function.
Definition: evmc.hpp:534
bytes32 get_transient_storage(const address &address, const bytes32 &key) const noexcept final
Get transient storage callback function.
Definition: evmc.hpp:601
evmc_tx_context get_tx_context() const noexcept final
Get transaction context callback function.
Definition: evmc.hpp:575
bytes32 get_storage(const address &address, const bytes32 &key) const noexcept final
Get storage callback function.
Definition: evmc.hpp:529
The EVMC Host interface.
Definition: evmc.hpp:439
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.
Definition: evmc.hpp:621
static DerivedClass * from_context(evmc_host_context *context) noexcept
Converts the opaque host context pointer back to the original Host object.
Definition: evmc.hpp:636
evmc_host_context * to_context() noexcept
Converts the Host object to the opaque host context pointer.
Definition: evmc.hpp:629
static const evmc_host_interface & get_interface() noexcept
Provides access to the global host interface.
Definition: evmc.hpp:882
The EVM code execution result.
Definition: evmc.hpp:329
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.
Definition: evmc.hpp:348
~Result() noexcept
Destructor responsible for automatically releasing attached resources.
Definition: evmc.hpp:388
evmc_result & raw() noexcept
Access the result object as a referenced to evmc_result.
Definition: evmc.hpp:415
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.
Definition: evmc.hpp:361
Result(const evmc_result &res) noexcept
Converting constructor from raw evmc_result.
Definition: evmc.hpp:385
Result(Result &&other) noexcept
Move constructor.
Definition: evmc.hpp:395
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.
Definition: evmc.hpp:373
evmc_result release_raw() noexcept
Releases the ownership and returns the raw copy of evmc_result.
Definition: evmc.hpp:428
evmc_address create_address
The address of the possibly created contract.
Definition: evmc.h:486
Result & operator=(Result &&other) noexcept
Move assignment operator.
Definition: evmc.hpp:406
const evmc_result & raw() const noexcept
Access the result object as a const referenced to evmc_result.
Definition: evmc.hpp:418
The VM instance.
Definition: evmc.hpp:652
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:745
VM & operator=(VM &&other) noexcept
Move assignment operator.
Definition: evmc.hpp:673
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:711
~VM() noexcept
Destructor responsible for automatically destroying the VM instance.
Definition: evmc.hpp:660
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:728
bool is_abi_compatible() const noexcept
Checks whenever the VM instance is ABI compatible with the current EVMC API.
Definition: evmc.hpp:690
bool has_capability(evmc_capabilities capability) const noexcept
Checks if the VM has the given capability.
Definition: evmc.hpp:699
VM(VM &&other) noexcept
Move constructor.
Definition: evmc.hpp:670
evmc_vm * get_raw_pointer() const noexcept
Returns the pointer to C EVMC struct representing the VM.
Definition: evmc.hpp:759
char const * name() const noexcept
The name of the EVMC VM implementation.
Definition: evmc.hpp:693
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:717
char const * version() const noexcept
The version of the EVMC VM implementation, e.g.
Definition: evmc.hpp:696
evmc_capabilities_flagset get_capabilities() const noexcept
A method returning capabilities supported by the VM instance.
Definition: evmc.hpp:705
VM(evmc_vm *vm) noexcept
Converting constructor from evmc_vm.
Definition: evmc.hpp:657
constexpr auto offset_basis
The 64-bit FNV offset basis.
Definition: evmc.hpp:160
std::ostream & operator<<(std::ostream &os, evmc_status_code status_code)
"Stream out" operator implementation for evmc_status_code.
Definition: evmc.hpp:911
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:163
constexpr T parse(std::string_view s) noexcept
Converts a raw literal into value of type T.
Definition: evmc.hpp:287
constexpr auto prime
The 64-bit FNV prime number.
Definition: evmc.hpp:159
evmc_set_option_result
Possible outcomes of evmc_set_option.
Definition: evmc.h:910
evmc_capabilities
Possible capabilities of a VM.
Definition: evmc.h:1092
evmc_status_code
The execution status code.
Definition: evmc.h:284
uint32_t evmc_capabilities_flagset
Alias for unsigned integer representing a set of bit flags of EVMC capabilities.
Definition: evmc.h:1121
evmc_access_status
Access status per EIP-2929: Gas cost increases for state access opcodes.
Definition: evmc.h:784
evmc_storage_status
The effect of an attempt to modify a contract storage item.
Definition: evmc.h:569
evmc_revision
EVM revision.
Definition: evmc.h:941
@ EVMC_ABI_VERSION
The EVMC ABI version number of the interface declared in this file.
Definition: evmc.h:47
@ EVMC_INTERNAL_ERROR
EVM implementation generic internal error.
Definition: evmc.h:374
@ EVMC_LATEST_STABLE_REVISION
The latest known EVM revision with finalized specification.
Definition: evmc.h:1058
@ EVMC_MAX_REVISION
The maximum EVM revision supported.
Definition: evmc.h:1051
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.
Definition: helpers.h:127
static const char * evmc_revision_to_string(enum evmc_revision rev)
Returns the name of the evmc_revision.
Definition: helpers.h:272
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
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:78
EVMC C++ API - wrappers and bindings for C++.
Definition: bytes.hpp:12
constexpr bool operator<(const address &a, const address &b) noexcept
The "less than" comparison operator for the evmc::address type.
Definition: evmc.hpp:185
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
constexpr bool operator==(const address &a, const address &b) noexcept
The "equal to" comparison operator for the evmc::address type.
Definition: evmc.hpp:171
constexpr bool operator>(const address &a, const address &b) noexcept
The "greater than" comparison operator for the evmc::address type.
Definition: evmc.hpp:195
const char * to_string(evmc_status_code status_code) noexcept
Returns text representation of the evmc_status_code.
Definition: evmc.hpp:309
std::basic_string_view< unsigned char, byte_traits< unsigned char > > bytes_view
String view of unsigned chars representing bytes.
Definition: bytes.hpp:92
constexpr auto make_result
Alias for evmc_make_result().
Definition: evmc.hpp:322
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:207
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:201
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 bool operator!=(const address &a, const address &b) noexcept
The "not equal to" comparison operator for the evmc::address type.
Definition: evmc.hpp:179
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
constexpr bool is_zero(const address &a) noexcept
Checks if the given address is the zero address.
Definition: evmc.hpp:258
The big-endian 160-bit hash suitable for keeping an Ethereum address.
Definition: evmc.hpp:27
constexpr address(evmc_address init={}) noexcept
Default and converting constructor.
Definition: evmc.hpp:31
constexpr address(uint64_t v) noexcept
Converting constructor from unsigned integer value.
Definition: evmc.hpp:37
The fixed size array of 32 bytes for storing 256-bit EVM values.
Definition: evmc.hpp:71
constexpr bytes32(evmc_bytes32 init={}) noexcept
Default and converting constructor.
Definition: evmc.hpp:75
constexpr bytes32(uint64_t v) noexcept
Converting constructor from unsigned integer value.
Definition: evmc.hpp:81
Big-endian 160-bit hash suitable for keeping an Ethereum address.
Definition: evmc.h:69
uint8_t bytes[20]
The 20 bytes of the hash.
Definition: evmc.h:71
The fixed size array of 32 bytes.
Definition: evmc.h:57
uint8_t bytes[32]
The 32 bytes.
Definition: evmc.h:59
The opaque data type representing the Host execution context.
The Host interface.
Definition: evmc.h:845
evmc_get_code_hash_fn get_code_hash
Get code hash callback function.
Definition: evmc.h:862
evmc_call_fn call
Call callback function.
Definition: evmc.h:871
evmc_selfdestruct_fn selfdestruct
Selfdestruct callback function.
Definition: evmc.h:868
evmc_get_storage_fn get_storage
Get storage callback function.
Definition: evmc.h:850
evmc_copy_code_fn copy_code
Copy code callback function.
Definition: evmc.h:865
evmc_get_code_size_fn get_code_size
Get code size callback function.
Definition: evmc.h:859
evmc_get_transient_storage_fn get_transient_storage
Get transient storage callback function.
Definition: evmc.h:889
evmc_set_transient_storage_fn set_transient_storage
Set transient storage callback function.
Definition: evmc.h:892
evmc_get_block_hash_fn get_block_hash
Get block hash callback function.
Definition: evmc.h:877
evmc_emit_log_fn emit_log
Emit log callback function.
Definition: evmc.h:880
evmc_account_exists_fn account_exists
Check account existence callback function.
Definition: evmc.h:847
evmc_access_account_fn access_account
Access account callback function.
Definition: evmc.h:883
evmc_get_balance_fn get_balance
Get balance callback function.
Definition: evmc.h:856
evmc_set_storage_fn set_storage
Set storage callback function.
Definition: evmc.h:853
evmc_get_tx_context_fn get_tx_context
Get transaction context callback function.
Definition: evmc.h:874
evmc_access_storage_fn access_storage
Access storage callback function.
Definition: evmc.h:886
The message describing an EVM call, including a zero-depth calls from a transaction origin.
Definition: evmc.h:98
The EVM code execution result.
Definition: evmc.h:416
const uint8_t * output_data
The reference to output data.
Definition: evmc.h:448
enum evmc_status_code status_code
The execution status code.
Definition: evmc.h:418
evmc_release_result_fn release
The method releasing all resources associated with the result object.
Definition: evmc.h:476
int64_t gas_refund
The refunded gas accumulated from this execution and its sub-calls.
Definition: evmc.h:434
size_t output_size
The size of the output data.
Definition: evmc.h:455
evmc_address create_address
The address of the possibly created contract.
Definition: evmc.h:486
int64_t gas_left
The amount of gas left after the execution.
Definition: evmc.h:426
The transaction and block data for execution.
Definition: evmc.h:214
The VM instance.
Definition: evmc.h:1141