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
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{
437public:
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 bool 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
501public:
503 HostContext() = default;
504
508 HostContext(const evmc_host_interface& interface, evmc_host_context* ctx) noexcept
509 : host{&interface}, context{ctx}
510 {}
511
512 bool account_exists(const address& address) const noexcept final
513 {
514 return host->account_exists(context, &address);
515 }
516
517 bytes32 get_storage(const address& address, const bytes32& key) const noexcept final
518 {
519 return host->get_storage(context, &address, &key);
520 }
521
523 const bytes32& key,
524 const bytes32& value) noexcept final
525 {
526 return host->set_storage(context, &address, &key, &value);
527 }
528
529 uint256be get_balance(const address& address) const noexcept final
530 {
531 return host->get_balance(context, &address);
532 }
533
534 size_t get_code_size(const address& address) const noexcept final
535 {
536 return host->get_code_size(context, &address);
537 }
538
539 bytes32 get_code_hash(const address& address) const noexcept final
540 {
541 return host->get_code_hash(context, &address);
542 }
543
544 size_t copy_code(const address& address,
545 size_t code_offset,
546 uint8_t* buffer_data,
547 size_t buffer_size) const noexcept final
548 {
549 return host->copy_code(context, &address, code_offset, buffer_data, buffer_size);
550 }
551
552 bool selfdestruct(const address& addr, const address& beneficiary) noexcept final
553 {
554 return host->selfdestruct(context, &addr, &beneficiary);
555 }
556
557 Result call(const evmc_message& message) noexcept final
558 {
559 return Result{host->call(context, &message)};
560 }
561
563 evmc_tx_context get_tx_context() const noexcept final { return host->get_tx_context(context); }
564
565 bytes32 get_block_hash(int64_t number) const noexcept final
566 {
567 return host->get_block_hash(context, number);
568 }
569
570 void emit_log(const address& addr,
571 const uint8_t* data,
572 size_t data_size,
573 const bytes32 topics[],
574 size_t topics_count) noexcept final
575 {
576 host->emit_log(context, &addr, data, data_size, topics, topics_count);
577 }
578
580 {
581 return host->access_account(context, &address);
582 }
583
584 evmc_access_status access_storage(const address& address, const bytes32& key) noexcept final
585 {
586 return host->access_storage(context, &address, &key);
587 }
588};
589
590
596class Host : public HostInterface
597{
598public:
601 static const evmc_host_interface& get_interface() noexcept;
602
605 evmc_host_context* to_context() noexcept { return reinterpret_cast<evmc_host_context*>(this); }
606
611 template <typename DerivedClass = Host>
612 static DerivedClass* from_context(evmc_host_context* context) noexcept
613 {
614 // Get pointer of the Host base class.
615 auto* h = reinterpret_cast<Host*>(context);
616
617 // Additional downcast, only possible if DerivedClass inherits from Host.
618 return static_cast<DerivedClass*>(h);
619 }
620};
621
622
627class VM
628{
629public:
630 VM() noexcept = default;
631
633 explicit VM(evmc_vm* vm) noexcept : m_instance{vm} {}
634
636 ~VM() noexcept
637 {
638 if (m_instance != nullptr)
639 m_instance->destroy(m_instance);
640 }
641
642 VM(const VM&) = delete;
643 VM& operator=(const VM&) = delete;
644
646 VM(VM&& other) noexcept : m_instance{other.m_instance} { other.m_instance = nullptr; }
647
649 VM& operator=(VM&& other) noexcept
650 {
651 this->~VM();
652 m_instance = other.m_instance;
653 other.m_instance = nullptr;
654 return *this;
655 }
656
659 inline VM(evmc_vm* vm,
660 std::initializer_list<std::pair<const char*, const char*>> options) noexcept;
661
663 explicit operator bool() const noexcept { return m_instance != nullptr; }
664
666 bool is_abi_compatible() const noexcept { return m_instance->abi_version == EVMC_ABI_VERSION; }
667
669 char const* name() const noexcept { return m_instance->name; }
670
672 char const* version() const noexcept { return m_instance->version; }
673
675 bool has_capability(evmc_capabilities capability) const noexcept
676 {
677 return (get_capabilities() & static_cast<evmc_capabilities_flagset>(capability)) != 0;
678 }
679
682 {
683 return m_instance->get_capabilities(m_instance);
684 }
685
687 evmc_set_option_result set_option(const char name[], const char value[]) noexcept
688 {
689 return evmc_set_option(m_instance, name, value);
690 }
691
695 evmc_revision rev,
696 const evmc_message& msg,
697 const uint8_t* code,
698 size_t code_size) noexcept
699 {
700 return Result{m_instance->execute(m_instance, &host, ctx, rev, &msg, code, code_size)};
701 }
702
705 evmc_revision rev,
706 const evmc_message& msg,
707 const uint8_t* code,
708 size_t code_size) noexcept
709 {
710 return execute(Host::get_interface(), host.to_context(), rev, msg, code, code_size);
711 }
712
722 const evmc_message& msg,
723 const uint8_t* code,
724 size_t code_size) noexcept
725 {
726 return Result{
727 m_instance->execute(m_instance, nullptr, nullptr, rev, &msg, code, code_size)};
728 }
729
735 evmc_vm* get_raw_pointer() const noexcept { return m_instance; }
736
737private:
738 evmc_vm* m_instance = nullptr;
739};
740
741inline VM::VM(evmc_vm* vm,
742 std::initializer_list<std::pair<const char*, const char*>> options) noexcept
743 : m_instance{vm}
744{
745 // This constructor is implemented outside of the class definition to workaround a doxygen bug.
746 for (const auto& option : options)
747 set_option(option.first, option.second);
748}
749
750
751namespace internal
752{
753inline bool account_exists(evmc_host_context* h, const evmc_address* addr) noexcept
754{
755 return Host::from_context(h)->account_exists(*addr);
756}
757
758inline evmc_bytes32 get_storage(evmc_host_context* h,
759 const evmc_address* addr,
760 const evmc_bytes32* key) noexcept
761{
762 return Host::from_context(h)->get_storage(*addr, *key);
763}
764
765inline evmc_storage_status set_storage(evmc_host_context* h,
766 const evmc_address* addr,
767 const evmc_bytes32* key,
768 const evmc_bytes32* value) noexcept
769{
770 return Host::from_context(h)->set_storage(*addr, *key, *value);
771}
772
773inline evmc_uint256be get_balance(evmc_host_context* h, const evmc_address* addr) noexcept
774{
775 return Host::from_context(h)->get_balance(*addr);
776}
777
778inline size_t get_code_size(evmc_host_context* h, const evmc_address* addr) noexcept
779{
780 return Host::from_context(h)->get_code_size(*addr);
781}
782
783inline evmc_bytes32 get_code_hash(evmc_host_context* h, const evmc_address* addr) noexcept
784{
785 return Host::from_context(h)->get_code_hash(*addr);
786}
787
788inline size_t copy_code(evmc_host_context* h,
789 const evmc_address* addr,
790 size_t code_offset,
791 uint8_t* buffer_data,
792 size_t buffer_size) noexcept
793{
794 return Host::from_context(h)->copy_code(*addr, code_offset, buffer_data, buffer_size);
795}
796
797inline bool selfdestruct(evmc_host_context* h,
798 const evmc_address* addr,
799 const evmc_address* beneficiary) noexcept
800{
801 return Host::from_context(h)->selfdestruct(*addr, *beneficiary);
802}
803
804inline evmc_result call(evmc_host_context* h, const evmc_message* msg) noexcept
805{
806 return Host::from_context(h)->call(*msg).release_raw();
807}
808
809inline evmc_tx_context get_tx_context(evmc_host_context* h) noexcept
810{
811 return Host::from_context(h)->get_tx_context();
812}
813
814inline evmc_bytes32 get_block_hash(evmc_host_context* h, int64_t block_number) noexcept
815{
816 return Host::from_context(h)->get_block_hash(block_number);
817}
818
819inline void emit_log(evmc_host_context* h,
820 const evmc_address* addr,
821 const uint8_t* data,
822 size_t data_size,
823 const evmc_bytes32 topics[],
824 size_t num_topics) noexcept
825{
826 Host::from_context(h)->emit_log(*addr, data, data_size, static_cast<const bytes32*>(topics),
827 num_topics);
828}
829
830inline evmc_access_status access_account(evmc_host_context* h, const evmc_address* addr) noexcept
831{
832 return Host::from_context(h)->access_account(*addr);
833}
834
835inline evmc_access_status access_storage(evmc_host_context* h,
836 const evmc_address* addr,
837 const evmc_bytes32* key) noexcept
838{
839 return Host::from_context(h)->access_storage(*addr, *key);
840}
841} // namespace internal
842
844{
845 static constexpr evmc_host_interface interface = {
846 ::evmc::internal::account_exists, ::evmc::internal::get_storage,
847 ::evmc::internal::set_storage, ::evmc::internal::get_balance,
848 ::evmc::internal::get_code_size, ::evmc::internal::get_code_hash,
849 ::evmc::internal::copy_code, ::evmc::internal::selfdestruct,
850 ::evmc::internal::call, ::evmc::internal::get_tx_context,
851 ::evmc::internal::get_block_hash, ::evmc::internal::emit_log,
852 ::evmc::internal::access_account, ::evmc::internal::access_storage,
853 };
854 return interface;
855}
856} // namespace evmc
857
858
863inline std::ostream& operator<<(std::ostream& os, evmc_status_code status_code)
864{
865 return os << evmc::to_string(status_code);
866}
867
872inline std::ostream& operator<<(std::ostream& os, evmc_revision rev)
873{
874 return os << evmc::to_string(rev);
875}
876
877namespace std
878{
880template <>
881struct hash<evmc::address>
882{
884 constexpr size_t operator()(const evmc::address& s) const noexcept
885 {
886 using namespace evmc;
887 using namespace fnv;
888 return static_cast<size_t>(fnv1a_by64(
889 fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])), load64le(&s.bytes[8])),
890 load32le(&s.bytes[16])));
891 }
892};
893
895template <>
896struct hash<evmc::bytes32>
897{
899 constexpr size_t operator()(const evmc::bytes32& s) const noexcept
900 {
901 using namespace evmc;
902 using namespace fnv;
903 return static_cast<size_t>(
904 fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])),
905 load64le(&s.bytes[8])),
906 load64le(&s.bytes[16])),
907 load64le(&s.bytes[24])));
908 }
909};
910} // 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:539
bool account_exists(const address &address) const noexcept final
Check account existence callback function.
Definition: evmc.hpp:512
size_t get_code_size(const address &address) const noexcept final
Get code size callback function.
Definition: evmc.hpp:534
HostContext()=default
Default constructor for null Host context.
uint256be get_balance(const address &address) const noexcept final
Get balance callback function.
Definition: evmc.hpp:529
HostContext(const evmc_host_interface &interface, evmc_host_context *ctx) noexcept
Constructor from the EVMC Host primitives.
Definition: evmc.hpp:508
evmc_access_status access_storage(const address &address, const bytes32 &key) noexcept final
Access storage callback function.
Definition: evmc.hpp:584
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:544
Result call(const evmc_message &message) noexcept final
Call callback function.
Definition: evmc.hpp:557
bool selfdestruct(const address &addr, const address &beneficiary) noexcept final
Selfdestruct callback function.
Definition: evmc.hpp:552
evmc_access_status access_account(const address &address) noexcept final
Access account callback function.
Definition: evmc.hpp:579
bytes32 get_block_hash(int64_t number) const noexcept final
Get block hash callback function.
Definition: evmc.hpp:565
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:570
evmc_storage_status set_storage(const address &address, const bytes32 &key, const bytes32 &value) noexcept final
Set storage callback function.
Definition: evmc.hpp:522
evmc_tx_context get_tx_context() const noexcept final
Get transaction context callback function.
Definition: evmc.hpp:563
bytes32 get_storage(const address &address, const bytes32 &key) const noexcept final
Get storage callback function.
Definition: evmc.hpp:517
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 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 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:597
static DerivedClass * from_context(evmc_host_context *context) noexcept
Converts the opaque host context pointer back to the original Host object.
Definition: evmc.hpp:612
evmc_host_context * to_context() noexcept
Converts the Host object to the opaque host context pointer.
Definition: evmc.hpp:605
static const evmc_host_interface & get_interface() noexcept
Provides access to the global host interface.
Definition: evmc.hpp:843
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
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:425
evmc_address create_address
The address of the possibly created contract.
Definition: evmc.h:462
Result & operator=(Result &&other) noexcept
Move assignment operator.
Definition: evmc.hpp:409
The VM instance.
Definition: evmc.hpp:628
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:721
VM & operator=(VM &&other) noexcept
Move assignment operator.
Definition: evmc.hpp:649
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:687
~VM() noexcept
Destructor responsible for automatically destroying the VM instance.
Definition: evmc.hpp:636
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:704
bool is_abi_compatible() const noexcept
Checks whenever the VM instance is ABI compatible with the current EVMC API.
Definition: evmc.hpp:666
bool has_capability(evmc_capabilities capability) const noexcept
Checks if the VM has the given capability.
Definition: evmc.hpp:675
VM(VM &&other) noexcept
Move constructor.
Definition: evmc.hpp:646
evmc_vm * get_raw_pointer() const noexcept
Returns the pointer to C EVMC struct representing the VM.
Definition: evmc.hpp:735
char const * name() const noexcept
The name of the EVMC VM implementation.
Definition: evmc.hpp:669
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:693
char const * version() const noexcept
The version of the EVMC VM implementation, e.g.
Definition: evmc.hpp:672
evmc_capabilities_flagset get_capabilities() const noexcept
A method returning capabilities supported by the VM instance.
Definition: evmc.hpp:681
VM(evmc_vm *vm) noexcept
Converting constructor from evmc_vm.
Definition: evmc.hpp:633
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:863
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:845
evmc_capabilities
Possible capabilities of a VM.
Definition: evmc.h:1012
evmc_status_code
The execution status code.
Definition: evmc.h:260
uint32_t evmc_capabilities_flagset
Alias for unsigned integer representing a set of bit flags of EVMC capabilities.
Definition: evmc.h:1041
evmc_access_status
Access status per EIP-2929: Gas cost increases for state access opcodes.
Definition: evmc.h:725
evmc_storage_status
The effect of an attempt to modify a contract storage item.
Definition: evmc.h:529
evmc_revision
EVM revision.
Definition: evmc.h:876
@ 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:350
@ EVMC_LATEST_STABLE_REVISION
The latest known EVM revision with finalized specification.
Definition: evmc.h:978
@ EVMC_MAX_REVISION
The maximum EVM revision supported.
Definition: evmc.h:971
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:125
static const char * evmc_revision_to_string(enum evmc_revision rev)
Returns the name of the evmc_revision.
Definition: helpers.h:270
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:219
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:76
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:786
evmc_get_code_hash_fn get_code_hash
Get code hash callback function.
Definition: evmc.h:803
evmc_call_fn call
Call callback function.
Definition: evmc.h:812
evmc_selfdestruct_fn selfdestruct
Selfdestruct callback function.
Definition: evmc.h:809
evmc_get_storage_fn get_storage
Get storage callback function.
Definition: evmc.h:791
evmc_copy_code_fn copy_code
Copy code callback function.
Definition: evmc.h:806
evmc_get_code_size_fn get_code_size
Get code size callback function.
Definition: evmc.h:800
evmc_get_block_hash_fn get_block_hash
Get block hash callback function.
Definition: evmc.h:818
evmc_emit_log_fn emit_log
Emit log callback function.
Definition: evmc.h:821
evmc_account_exists_fn account_exists
Check account existence callback function.
Definition: evmc.h:788
evmc_access_account_fn access_account
Access account callback function.
Definition: evmc.h:824
evmc_get_balance_fn get_balance
Get balance callback function.
Definition: evmc.h:797
evmc_set_storage_fn set_storage
Set storage callback function.
Definition: evmc.h:794
evmc_get_tx_context_fn get_tx_context
Get transaction context callback function.
Definition: evmc.h:815
evmc_access_storage_fn access_storage
Access storage callback function.
Definition: evmc.h:827
The message describing an EVM call, including a zero-depth calls from a transaction origin.
Definition: evmc.h:97
The EVM code execution result.
Definition: evmc.h:392
const uint8_t * output_data
The reference to output data.
Definition: evmc.h:424
enum evmc_status_code status_code
The execution status code.
Definition: evmc.h:394
evmc_release_result_fn release
The method releasing all resources associated with the result object.
Definition: evmc.h:452
int64_t gas_refund
The refunded gas accumulated from this execution and its sub-calls.
Definition: evmc.h:410
size_t output_size
The size of the output data.
Definition: evmc.h:431
evmc_address create_address
The address of the possibly created contract.
Definition: evmc.h:462
int64_t gas_left
The amount of gas left after the execution.
Definition: evmc.h:402
The transaction and block data for execution.
Definition: evmc.h:195
The VM instance.
Definition: evmc.h:1061