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{
24using bytes_view = std::basic_string_view<uint8_t>;
25
30{
34 constexpr address(evmc_address init = {}) noexcept : evmc_address{init} {}
35
40 constexpr explicit address(uint64_t v) noexcept
41 : evmc_address{{0,
42 0,
43 0,
44 0,
45 0,
46 0,
47 0,
48 0,
49 0,
50 0,
51 0,
52 0,
53 static_cast<uint8_t>(v >> 56),
54 static_cast<uint8_t>(v >> 48),
55 static_cast<uint8_t>(v >> 40),
56 static_cast<uint8_t>(v >> 32),
57 static_cast<uint8_t>(v >> 24),
58 static_cast<uint8_t>(v >> 16),
59 static_cast<uint8_t>(v >> 8),
60 static_cast<uint8_t>(v >> 0)}}
61 {}
62
64 inline constexpr explicit operator bool() const noexcept;
65
67 inline constexpr operator bytes_view() const noexcept { return {bytes, sizeof(bytes)}; }
68};
69
74{
78 constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}
79
84 constexpr explicit bytes32(uint64_t v) noexcept
85 : evmc_bytes32{{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 0,
107 0,
108 0,
109 static_cast<uint8_t>(v >> 56),
110 static_cast<uint8_t>(v >> 48),
111 static_cast<uint8_t>(v >> 40),
112 static_cast<uint8_t>(v >> 32),
113 static_cast<uint8_t>(v >> 24),
114 static_cast<uint8_t>(v >> 16),
115 static_cast<uint8_t>(v >> 8),
116 static_cast<uint8_t>(v >> 0)}}
117 {}
118
120 inline constexpr explicit operator bool() const noexcept;
121
123 inline constexpr operator bytes_view() const noexcept { return {bytes, sizeof(bytes)}; }
124};
125
128
129
131inline constexpr uint64_t load64be(const uint8_t* data) noexcept
132{
133 return (uint64_t{data[0]} << 56) | (uint64_t{data[1]} << 48) | (uint64_t{data[2]} << 40) |
134 (uint64_t{data[3]} << 32) | (uint64_t{data[4]} << 24) | (uint64_t{data[5]} << 16) |
135 (uint64_t{data[6]} << 8) | uint64_t{data[7]};
136}
137
139inline constexpr uint64_t load64le(const uint8_t* data) noexcept
140{
141 return uint64_t{data[0]} | (uint64_t{data[1]} << 8) | (uint64_t{data[2]} << 16) |
142 (uint64_t{data[3]} << 24) | (uint64_t{data[4]} << 32) | (uint64_t{data[5]} << 40) |
143 (uint64_t{data[6]} << 48) | (uint64_t{data[7]} << 56);
144}
145
147inline constexpr uint32_t load32be(const uint8_t* data) noexcept
148{
149 return (uint32_t{data[0]} << 24) | (uint32_t{data[1]} << 16) | (uint32_t{data[2]} << 8) |
150 uint32_t{data[3]};
151}
152
154inline constexpr uint32_t load32le(const uint8_t* data) noexcept
155{
156 return uint32_t{data[0]} | (uint32_t{data[1]} << 8) | (uint32_t{data[2]} << 16) |
157 (uint32_t{data[3]} << 24);
158}
159
160namespace fnv
161{
162constexpr auto prime = 0x100000001b3;
163constexpr auto offset_basis = 0xcbf29ce484222325;
164
166inline constexpr uint64_t fnv1a_by64(uint64_t h, uint64_t x) noexcept
167{
168 return (h ^ x) * prime;
169}
170} // namespace fnv
171
172
174inline constexpr bool operator==(const address& a, const address& b) noexcept
175{
176 return load64le(&a.bytes[0]) == load64le(&b.bytes[0]) &&
177 load64le(&a.bytes[8]) == load64le(&b.bytes[8]) &&
178 load32le(&a.bytes[16]) == load32le(&b.bytes[16]);
179}
180
182inline constexpr bool operator!=(const address& a, const address& b) noexcept
183{
184 return !(a == b);
185}
186
188inline constexpr bool operator<(const address& a, const address& b) noexcept
189{
190 return load64be(&a.bytes[0]) < load64be(&b.bytes[0]) ||
191 (load64be(&a.bytes[0]) == load64be(&b.bytes[0]) &&
192 (load64be(&a.bytes[8]) < load64be(&b.bytes[8]) ||
193 (load64be(&a.bytes[8]) == load64be(&b.bytes[8]) &&
194 load32be(&a.bytes[16]) < load32be(&b.bytes[16]))));
195}
196
198inline constexpr bool operator>(const address& a, const address& b) noexcept
199{
200 return b < a;
201}
202
204inline constexpr bool operator<=(const address& a, const address& b) noexcept
205{
206 return !(b < a);
207}
208
210inline constexpr bool operator>=(const address& a, const address& b) noexcept
211{
212 return !(a < b);
213}
214
216inline constexpr bool operator==(const bytes32& a, const bytes32& b) noexcept
217{
218 return load64le(&a.bytes[0]) == load64le(&b.bytes[0]) &&
219 load64le(&a.bytes[8]) == load64le(&b.bytes[8]) &&
220 load64le(&a.bytes[16]) == load64le(&b.bytes[16]) &&
221 load64le(&a.bytes[24]) == load64le(&b.bytes[24]);
222}
223
225inline constexpr bool operator!=(const bytes32& a, const bytes32& b) noexcept
226{
227 return !(a == b);
228}
229
231inline constexpr bool operator<(const bytes32& a, const bytes32& b) noexcept
232{
233 return load64be(&a.bytes[0]) < load64be(&b.bytes[0]) ||
234 (load64be(&a.bytes[0]) == load64be(&b.bytes[0]) &&
235 (load64be(&a.bytes[8]) < load64be(&b.bytes[8]) ||
236 (load64be(&a.bytes[8]) == load64be(&b.bytes[8]) &&
237 (load64be(&a.bytes[16]) < load64be(&b.bytes[16]) ||
238 (load64be(&a.bytes[16]) == load64be(&b.bytes[16]) &&
239 load64be(&a.bytes[24]) < load64be(&b.bytes[24]))))));
240}
241
243inline constexpr bool operator>(const bytes32& a, const bytes32& b) noexcept
244{
245 return b < a;
246}
247
249inline constexpr bool operator<=(const bytes32& a, const bytes32& b) noexcept
250{
251 return !(b < a);
252}
253
255inline constexpr bool operator>=(const bytes32& a, const bytes32& b) noexcept
256{
257 return !(a < b);
258}
259
261inline constexpr bool is_zero(const address& a) noexcept
262{
263 return a == address{};
264}
265
266inline constexpr address::operator bool() const noexcept
267{
268 return !is_zero(*this);
269}
270
272inline constexpr bool is_zero(const bytes32& a) noexcept
273{
274 return a == bytes32{};
275}
276
277inline constexpr bytes32::operator bool() const noexcept
278{
279 return !is_zero(*this);
280}
281
282namespace literals
283{
289template <typename T>
290constexpr T parse(std::string_view s) noexcept
291{
292 return from_hex<T>(s).value();
293}
294
296constexpr address operator""_address(const char* s) noexcept
297{
298 return parse<address>(s);
299}
300
302constexpr bytes32 operator""_bytes32(const char* s) noexcept
303{
304 return parse<bytes32>(s);
305}
306} // namespace literals
307
308using namespace literals;
309
310
312inline const char* to_string(evmc_status_code status_code) noexcept
313{
314 return evmc_status_code_to_string(status_code);
315}
316
318inline const char* to_string(evmc_revision rev) noexcept
319{
320 return evmc_revision_to_string(rev);
321}
322
323
326
331class Result : private evmc_result
332{
333public:
340
351 explicit Result(evmc_status_code _status_code,
352 int64_t _gas_left,
353 int64_t _gas_refund,
354 const uint8_t* _output_data,
355 size_t _output_size) noexcept
356 : evmc_result{make_result(_status_code, _gas_left, _gas_refund, _output_data, _output_size)}
357 {}
358
365 int64_t _gas_left = 0,
366 int64_t _gas_refund = 0) noexcept
367 : evmc_result{make_result(_status_code, _gas_left, _gas_refund, nullptr, 0)}
368 {}
369
376 explicit Result(evmc_status_code _status_code,
377 int64_t _gas_left,
378 int64_t _gas_refund,
379 const evmc_address& _create_address) noexcept
380 : evmc_result{make_result(_status_code, _gas_left, _gas_refund, nullptr, 0)}
381 {
382 create_address = _create_address;
383 }
384
388 explicit Result(const evmc_result& 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
418 evmc_result& raw() noexcept { return *this; }
419
421 const evmc_result& raw() const noexcept { return *this; }
422
432 {
433 const auto out = evmc_result{*this}; // Copy data.
434 this->release = nullptr; // Disable releasing of this object.
435 return out;
436 }
437};
438
439
442{
443public:
444 virtual ~HostInterface() noexcept = default;
445
447 virtual bool account_exists(const address& addr) const noexcept = 0;
448
450 virtual bytes32 get_storage(const address& addr, const bytes32& key) const noexcept = 0;
451
454 const bytes32& key,
455 const bytes32& value) noexcept = 0;
456
458 virtual uint256be get_balance(const address& addr) const noexcept = 0;
459
461 virtual size_t get_code_size(const address& addr) const noexcept = 0;
462
464 virtual bytes32 get_code_hash(const address& addr) const noexcept = 0;
465
467 virtual size_t copy_code(const address& addr,
468 size_t code_offset,
469 uint8_t* buffer_data,
470 size_t buffer_size) const noexcept = 0;
471
473 virtual bool selfdestruct(const address& addr, const address& beneficiary) noexcept = 0;
474
476 virtual Result call(const evmc_message& msg) noexcept = 0;
477
479 virtual evmc_tx_context get_tx_context() const noexcept = 0;
480
482 virtual bytes32 get_block_hash(int64_t block_number) const noexcept = 0;
483
485 virtual void emit_log(const address& addr,
486 const uint8_t* data,
487 size_t data_size,
488 const bytes32 topics[],
489 size_t num_topics) noexcept = 0;
490
492 virtual evmc_access_status access_account(const address& addr) noexcept = 0;
493
495 virtual evmc_access_status access_storage(const address& addr, const bytes32& key) noexcept = 0;
496
499 const bytes32& key) const noexcept = 0;
500
502 virtual void set_transient_storage(const address& addr,
503 const bytes32& key,
504 const bytes32& value) noexcept = 0;
505};
506
507
512{
513 const evmc_host_interface* host = nullptr;
514 evmc_host_context* context = nullptr;
515
516public:
518 HostContext() = default;
519
523 HostContext(const evmc_host_interface& interface, evmc_host_context* ctx) noexcept
524 : host{&interface}, context{ctx}
525 {}
526
527 bool account_exists(const address& address) const noexcept final
528 {
529 return host->account_exists(context, &address);
530 }
531
532 bytes32 get_storage(const address& address, const bytes32& key) const noexcept final
533 {
534 return host->get_storage(context, &address, &key);
535 }
536
538 const bytes32& key,
539 const bytes32& value) noexcept final
540 {
541 return host->set_storage(context, &address, &key, &value);
542 }
543
544 uint256be get_balance(const address& address) const noexcept final
545 {
546 return host->get_balance(context, &address);
547 }
548
549 size_t get_code_size(const address& address) const noexcept final
550 {
551 return host->get_code_size(context, &address);
552 }
553
554 bytes32 get_code_hash(const address& address) const noexcept final
555 {
556 return host->get_code_hash(context, &address);
557 }
558
559 size_t copy_code(const address& address,
560 size_t code_offset,
561 uint8_t* buffer_data,
562 size_t buffer_size) const noexcept final
563 {
564 return host->copy_code(context, &address, code_offset, buffer_data, buffer_size);
565 }
566
567 bool selfdestruct(const address& addr, const address& beneficiary) noexcept final
568 {
569 return host->selfdestruct(context, &addr, &beneficiary);
570 }
571
572 Result call(const evmc_message& message) noexcept final
573 {
574 return Result{host->call(context, &message)};
575 }
576
578 evmc_tx_context get_tx_context() const noexcept final { return host->get_tx_context(context); }
579
580 bytes32 get_block_hash(int64_t number) const noexcept final
581 {
582 return host->get_block_hash(context, number);
583 }
584
585 void emit_log(const address& addr,
586 const uint8_t* data,
587 size_t data_size,
588 const bytes32 topics[],
589 size_t topics_count) noexcept final
590 {
591 host->emit_log(context, &addr, data, data_size, topics, topics_count);
592 }
593
595 {
596 return host->access_account(context, &address);
597 }
598
599 evmc_access_status access_storage(const address& address, const bytes32& key) noexcept final
600 {
601 return host->access_storage(context, &address, &key);
602 }
603
604 bytes32 get_transient_storage(const address& address, const bytes32& key) const noexcept final
605 {
606 return host->get_transient_storage(context, &address, &key);
607 }
608
610 const bytes32& key,
611 const bytes32& value) noexcept final
612 {
613 host->set_transient_storage(context, &address, &key, &value);
614 }
615};
616
617
623class Host : public HostInterface
624{
625public:
628 static const evmc_host_interface& get_interface() noexcept;
629
632 evmc_host_context* to_context() noexcept { return reinterpret_cast<evmc_host_context*>(this); }
633
638 template <typename DerivedClass = Host>
639 static DerivedClass* from_context(evmc_host_context* context) noexcept
640 {
641 // Get pointer of the Host base class.
642 auto* h = reinterpret_cast<Host*>(context);
643
644 // Additional downcast, only possible if DerivedClass inherits from Host.
645 return static_cast<DerivedClass*>(h);
646 }
647};
648
649
654class VM
655{
656public:
657 VM() noexcept = default;
658
660 explicit VM(evmc_vm* vm) noexcept : m_instance{vm} {}
661
663 ~VM() noexcept
664 {
665 if (m_instance != nullptr)
666 m_instance->destroy(m_instance);
667 }
668
669 VM(const VM&) = delete;
670 VM& operator=(const VM&) = delete;
671
673 VM(VM&& other) noexcept : m_instance{other.m_instance} { other.m_instance = nullptr; }
674
676 VM& operator=(VM&& other) noexcept
677 {
678 this->~VM();
679 m_instance = other.m_instance;
680 other.m_instance = nullptr;
681 return *this;
682 }
683
686 inline VM(evmc_vm* vm,
687 std::initializer_list<std::pair<const char*, const char*>> options) noexcept;
688
690 explicit operator bool() const noexcept { return m_instance != nullptr; }
691
693 bool is_abi_compatible() const noexcept { return m_instance->abi_version == EVMC_ABI_VERSION; }
694
696 char const* name() const noexcept { return m_instance->name; }
697
699 char const* version() const noexcept { return m_instance->version; }
700
702 bool has_capability(evmc_capabilities capability) const noexcept
703 {
704 return (get_capabilities() & static_cast<evmc_capabilities_flagset>(capability)) != 0;
705 }
706
709 {
710 return m_instance->get_capabilities(m_instance);
711 }
712
714 evmc_set_option_result set_option(const char name[], const char value[]) noexcept
715 {
716 return evmc_set_option(m_instance, name, value);
717 }
718
722 evmc_revision rev,
723 const evmc_message& msg,
724 const uint8_t* code,
725 size_t code_size) noexcept
726 {
727 return Result{m_instance->execute(m_instance, &host, ctx, rev, &msg, code, code_size)};
728 }
729
732 evmc_revision rev,
733 const evmc_message& msg,
734 const uint8_t* code,
735 size_t code_size) noexcept
736 {
737 return execute(Host::get_interface(), host.to_context(), rev, msg, code, code_size);
738 }
739
749 const evmc_message& msg,
750 const uint8_t* code,
751 size_t code_size) noexcept
752 {
753 return Result{
754 m_instance->execute(m_instance, nullptr, nullptr, rev, &msg, code, code_size)};
755 }
756
762 evmc_vm* get_raw_pointer() const noexcept { return m_instance; }
763
764private:
765 evmc_vm* m_instance = nullptr;
766};
767
768inline VM::VM(evmc_vm* vm,
769 std::initializer_list<std::pair<const char*, const char*>> options) noexcept
770 : m_instance{vm}
771{
772 // This constructor is implemented outside of the class definition to workaround a doxygen bug.
773 for (const auto& option : options)
774 set_option(option.first, option.second);
775}
776
777
778namespace internal
779{
780inline bool account_exists(evmc_host_context* h, const evmc_address* addr) noexcept
781{
782 return Host::from_context(h)->account_exists(*addr);
783}
784
785inline evmc_bytes32 get_storage(evmc_host_context* h,
786 const evmc_address* addr,
787 const evmc_bytes32* key) noexcept
788{
789 return Host::from_context(h)->get_storage(*addr, *key);
790}
791
792inline evmc_storage_status set_storage(evmc_host_context* h,
793 const evmc_address* addr,
794 const evmc_bytes32* key,
795 const evmc_bytes32* value) noexcept
796{
797 return Host::from_context(h)->set_storage(*addr, *key, *value);
798}
799
800inline evmc_uint256be get_balance(evmc_host_context* h, const evmc_address* addr) noexcept
801{
802 return Host::from_context(h)->get_balance(*addr);
803}
804
805inline size_t get_code_size(evmc_host_context* h, const evmc_address* addr) noexcept
806{
807 return Host::from_context(h)->get_code_size(*addr);
808}
809
810inline evmc_bytes32 get_code_hash(evmc_host_context* h, const evmc_address* addr) noexcept
811{
812 return Host::from_context(h)->get_code_hash(*addr);
813}
814
815inline size_t copy_code(evmc_host_context* h,
816 const evmc_address* addr,
817 size_t code_offset,
818 uint8_t* buffer_data,
819 size_t buffer_size) noexcept
820{
821 return Host::from_context(h)->copy_code(*addr, code_offset, buffer_data, buffer_size);
822}
823
824inline bool selfdestruct(evmc_host_context* h,
825 const evmc_address* addr,
826 const evmc_address* beneficiary) noexcept
827{
828 return Host::from_context(h)->selfdestruct(*addr, *beneficiary);
829}
830
831inline evmc_result call(evmc_host_context* h, const evmc_message* msg) noexcept
832{
833 return Host::from_context(h)->call(*msg).release_raw();
834}
835
836inline evmc_tx_context get_tx_context(evmc_host_context* h) noexcept
837{
838 return Host::from_context(h)->get_tx_context();
839}
840
841inline evmc_bytes32 get_block_hash(evmc_host_context* h, int64_t block_number) noexcept
842{
843 return Host::from_context(h)->get_block_hash(block_number);
844}
845
846inline void emit_log(evmc_host_context* h,
847 const evmc_address* addr,
848 const uint8_t* data,
849 size_t data_size,
850 const evmc_bytes32 topics[],
851 size_t num_topics) noexcept
852{
853 Host::from_context(h)->emit_log(*addr, data, data_size, static_cast<const bytes32*>(topics),
854 num_topics);
855}
856
857inline evmc_access_status access_account(evmc_host_context* h, const evmc_address* addr) noexcept
858{
859 return Host::from_context(h)->access_account(*addr);
860}
861
862inline evmc_access_status access_storage(evmc_host_context* h,
863 const evmc_address* addr,
864 const evmc_bytes32* key) noexcept
865{
866 return Host::from_context(h)->access_storage(*addr, *key);
867}
868
869inline evmc_bytes32 get_transient_storage(evmc_host_context* h,
870 const evmc_address* addr,
871 const evmc_bytes32* key) noexcept
872{
873 return Host::from_context(h)->get_transient_storage(*addr, *key);
874}
875
876inline void set_transient_storage(evmc_host_context* h,
877 const evmc_address* addr,
878 const evmc_bytes32* key,
879 const evmc_bytes32* value) noexcept
880{
881 Host::from_context(h)->set_transient_storage(*addr, *key, *value);
882}
883} // namespace internal
884
886{
887 static constexpr evmc_host_interface interface = {
888 ::evmc::internal::account_exists,
889 ::evmc::internal::get_storage,
890 ::evmc::internal::set_storage,
891 ::evmc::internal::get_balance,
892 ::evmc::internal::get_code_size,
893 ::evmc::internal::get_code_hash,
894 ::evmc::internal::copy_code,
895 ::evmc::internal::selfdestruct,
896 ::evmc::internal::call,
897 ::evmc::internal::get_tx_context,
898 ::evmc::internal::get_block_hash,
899 ::evmc::internal::emit_log,
900 ::evmc::internal::access_account,
901 ::evmc::internal::access_storage,
902 ::evmc::internal::get_transient_storage,
903 ::evmc::internal::set_transient_storage,
904 };
905 return interface;
906}
907} // namespace evmc
908
909
914inline std::ostream& operator<<(std::ostream& os, evmc_status_code status_code)
915{
916 return os << evmc::to_string(status_code);
917}
918
923inline std::ostream& operator<<(std::ostream& os, evmc_revision rev)
924{
925 return os << evmc::to_string(rev);
926}
927
928namespace std
929{
931template <>
932struct hash<evmc::address>
933{
935 constexpr size_t operator()(const evmc::address& s) const noexcept
936 {
937 using namespace evmc;
938 using namespace fnv;
939 return static_cast<size_t>(fnv1a_by64(
940 fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])), load64le(&s.bytes[8])),
941 load32le(&s.bytes[16])));
942 }
943};
944
946template <>
947struct hash<evmc::bytes32>
948{
950 constexpr size_t operator()(const evmc::bytes32& s) const noexcept
951 {
952 using namespace evmc;
953 using namespace fnv;
954 return static_cast<size_t>(
955 fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])),
956 load64le(&s.bytes[8])),
957 load64le(&s.bytes[16])),
958 load64le(&s.bytes[24])));
959 }
960};
961} // namespace std
Wrapper around EVMC host context / host interface.
Definition: evmc.hpp:512
bytes32 get_code_hash(const address &address) const noexcept final
Get code hash callback function.
Definition: evmc.hpp:554
bool account_exists(const address &address) const noexcept final
Check account existence callback function.
Definition: evmc.hpp:527
size_t get_code_size(const address &address) const noexcept final
Get code size callback function.
Definition: evmc.hpp:549
HostContext()=default
Default constructor for null Host context.
uint256be get_balance(const address &address) const noexcept final
Get balance callback function.
Definition: evmc.hpp:544
HostContext(const evmc_host_interface &interface, evmc_host_context *ctx) noexcept
Constructor from the EVMC Host primitives.
Definition: evmc.hpp:523
evmc_access_status access_storage(const address &address, const bytes32 &key) noexcept final
Access storage callback function.
Definition: evmc.hpp:599
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:559
Result call(const evmc_message &message) noexcept final
Call callback function.
Definition: evmc.hpp:572
bool selfdestruct(const address &addr, const address &beneficiary) noexcept final
Selfdestruct callback function.
Definition: evmc.hpp:567
evmc_access_status access_account(const address &address) noexcept final
Access account callback function.
Definition: evmc.hpp:594
void set_transient_storage(const address &address, const bytes32 &key, const bytes32 &value) noexcept final
Set transient storage callback function.
Definition: evmc.hpp:609
bytes32 get_block_hash(int64_t number) const noexcept final
Get block hash callback function.
Definition: evmc.hpp:580
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:585
evmc_storage_status set_storage(const address &address, const bytes32 &key, const bytes32 &value) noexcept final
Set storage callback function.
Definition: evmc.hpp:537
bytes32 get_transient_storage(const address &address, const bytes32 &key) const noexcept final
Get transient storage callback function.
Definition: evmc.hpp:604
evmc_tx_context get_tx_context() const noexcept final
Get transaction context callback function.
Definition: evmc.hpp:578
bytes32 get_storage(const address &address, const bytes32 &key) const noexcept final
Get storage callback function.
Definition: evmc.hpp:532
The EVMC Host interface.
Definition: evmc.hpp:442
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:624
static DerivedClass * from_context(evmc_host_context *context) noexcept
Converts the opaque host context pointer back to the original Host object.
Definition: evmc.hpp:639
evmc_host_context * to_context() noexcept
Converts the Host object to the opaque host context pointer.
Definition: evmc.hpp:632
static const evmc_host_interface & get_interface() noexcept
Provides access to the global host interface.
Definition: evmc.hpp:885
The EVM code execution result.
Definition: evmc.hpp:332
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:351
~Result() noexcept
Destructor responsible for automatically releasing attached resources.
Definition: evmc.hpp:391
evmc_result & raw() noexcept
Access the result object as a referenced to evmc_result.
Definition: evmc.hpp:418
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:364
Result(const evmc_result &res) noexcept
Converting constructor from raw evmc_result.
Definition: evmc.hpp:388
Result(Result &&other) noexcept
Move constructor.
Definition: evmc.hpp:398
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:376
evmc_result release_raw() noexcept
Releases the ownership and returns the raw copy of evmc_result.
Definition: evmc.hpp:431
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:409
const evmc_result & raw() const noexcept
Access the result object as a const referenced to evmc_result.
Definition: evmc.hpp:421
The VM instance.
Definition: evmc.hpp:655
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:748
VM & operator=(VM &&other) noexcept
Move assignment operator.
Definition: evmc.hpp:676
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:714
~VM() noexcept
Destructor responsible for automatically destroying the VM instance.
Definition: evmc.hpp:663
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:731
bool is_abi_compatible() const noexcept
Checks whenever the VM instance is ABI compatible with the current EVMC API.
Definition: evmc.hpp:693
bool has_capability(evmc_capabilities capability) const noexcept
Checks if the VM has the given capability.
Definition: evmc.hpp:702
VM(VM &&other) noexcept
Move constructor.
Definition: evmc.hpp:673
evmc_vm * get_raw_pointer() const noexcept
Returns the pointer to C EVMC struct representing the VM.
Definition: evmc.hpp:762
char const * name() const noexcept
The name of the EVMC VM implementation.
Definition: evmc.hpp:696
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:720
char const * version() const noexcept
The version of the EVMC VM implementation, e.g.
Definition: evmc.hpp:699
evmc_capabilities_flagset get_capabilities() const noexcept
A method returning capabilities supported by the VM instance.
Definition: evmc.hpp:708
VM(evmc_vm *vm) noexcept
Converting constructor from evmc_vm.
Definition: evmc.hpp:660
constexpr auto offset_basis
The 64-bit FNV offset basis.
Definition: evmc.hpp:163
std::ostream & operator<<(std::ostream &os, evmc_status_code status_code)
"Stream out" operator implementation for evmc_status_code.
Definition: evmc.hpp:914
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:166
constexpr T parse(std::string_view s) noexcept
Converts a raw literal into value of type T.
Definition: evmc.hpp:290
constexpr auto prime
The 64-bit FNV prime number.
Definition: evmc.hpp:162
evmc_set_option_result
Possible outcomes of evmc_set_option.
Definition: evmc.h:910
evmc_capabilities
Possible capabilities of a VM.
Definition: evmc.h:1085
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:1114
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:1051
@ EVMC_MAX_REVISION
The maximum EVM revision supported.
Definition: evmc.h:1044
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: evmc.hpp:22
constexpr bool operator<(const address &a, const address &b) noexcept
The "less than" comparison operator for the evmc::address type.
Definition: evmc.hpp:188
constexpr uint64_t load64be(const uint8_t *data) noexcept
Loads 64 bits / 8 bytes of data from the given data array in big-endian order.
Definition: evmc.hpp:131
constexpr uint32_t load32be(const uint8_t *data) noexcept
Loads 32 bits / 4 bytes of data from the given data array in big-endian order.
Definition: evmc.hpp:147
constexpr bool operator==(const address &a, const address &b) noexcept
The "equal to" comparison operator for the evmc::address type.
Definition: evmc.hpp:174
constexpr bool operator>(const address &a, const address &b) noexcept
The "greater than" comparison operator for the evmc::address type.
Definition: evmc.hpp:198
const char * to_string(evmc_status_code status_code) noexcept
Returns text representation of the evmc_status_code.
Definition: evmc.hpp:312
std::basic_string_view< uint8_t > bytes_view
String view of uint8_t chars.
Definition: evmc.hpp:24
constexpr auto make_result
Alias for evmc_make_result().
Definition: evmc.hpp:325
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:210
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:204
constexpr uint64_t load64le(const uint8_t *data) noexcept
Loads 64 bits / 8 bytes of data from the given data array in little-endian order.
Definition: evmc.hpp:139
constexpr bool operator!=(const address &a, const address &b) noexcept
The "not equal to" comparison operator for the evmc::address type.
Definition: evmc.hpp:182
constexpr uint32_t load32le(const uint8_t *data) noexcept
Loads 32 bits / 4 bytes of data from the given data array in little-endian order.
Definition: evmc.hpp:154
constexpr bool is_zero(const address &a) noexcept
Checks if the given address is the zero address.
Definition: evmc.hpp:261
The big-endian 160-bit hash suitable for keeping an Ethereum address.
Definition: evmc.hpp:30
constexpr address(evmc_address init={}) noexcept
Default and converting constructor.
Definition: evmc.hpp:34
constexpr address(uint64_t v) noexcept
Converting constructor from unsigned integer value.
Definition: evmc.hpp:40
The fixed size array of 32 bytes for storing 256-bit EVM values.
Definition: evmc.hpp:74
constexpr bytes32(evmc_bytes32 init={}) noexcept
Default and converting constructor.
Definition: evmc.hpp:78
constexpr bytes32(uint64_t v) noexcept
Converting constructor from unsigned integer value.
Definition: evmc.hpp:84
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:1134