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:
339
349 explicit Result(evmc_status_code _status_code,
350 int64_t _gas_left,
351 const uint8_t* _output_data,
352 size_t _output_size) noexcept
353 : evmc_result{make_result(_status_code, _gas_left, _output_data, _output_size)}
354 {}
355
361 int64_t _gas_left = 0) noexcept
362 : evmc_result{make_result(_status_code, _gas_left, nullptr, 0)}
363 {}
364
370 explicit Result(evmc_status_code _status_code,
371 int64_t _gas_left,
372 const evmc_address& _create_address) noexcept
373 : evmc_result{make_result(_status_code, _gas_left, nullptr, 0)}
374 {
375 create_address = _create_address;
376 }
377
381 explicit Result(const evmc_result& res) noexcept : evmc_result{res} {}
382
384 ~Result() noexcept
385 {
386 if (release != nullptr)
387 release(this);
388 }
389
391 Result(Result&& other) noexcept : evmc_result{other}
392 {
393 other.release = nullptr; // Disable releasing of the rvalue object.
394 }
395
402 Result& operator=(Result&& other) noexcept
403 {
404 this->~Result(); // Release this object.
405 static_cast<evmc_result&>(*this) = other; // Copy data.
406 other.release = nullptr; // Disable releasing of the rvalue object.
407 return *this;
408 }
409
419 {
420 const auto out = evmc_result{*this}; // Copy data.
421 this->release = nullptr; // Disable releasing of this object.
422 return out;
423 }
424};
425
426
429{
430public:
431 virtual ~HostInterface() noexcept = default;
432
434 virtual bool account_exists(const address& addr) const noexcept = 0;
435
437 virtual bytes32 get_storage(const address& addr, const bytes32& key) const noexcept = 0;
438
441 const bytes32& key,
442 const bytes32& value) noexcept = 0;
443
445 virtual uint256be get_balance(const address& addr) const noexcept = 0;
446
448 virtual size_t get_code_size(const address& addr) const noexcept = 0;
449
451 virtual bytes32 get_code_hash(const address& addr) const noexcept = 0;
452
454 virtual size_t copy_code(const address& addr,
455 size_t code_offset,
456 uint8_t* buffer_data,
457 size_t buffer_size) const noexcept = 0;
458
460 virtual bool selfdestruct(const address& addr, const address& beneficiary) noexcept = 0;
461
463 virtual Result call(const evmc_message& msg) noexcept = 0;
464
466 virtual evmc_tx_context get_tx_context() const noexcept = 0;
467
469 virtual bytes32 get_block_hash(int64_t block_number) const noexcept = 0;
470
472 virtual void emit_log(const address& addr,
473 const uint8_t* data,
474 size_t data_size,
475 const bytes32 topics[],
476 size_t num_topics) noexcept = 0;
477
479 virtual evmc_access_status access_account(const address& addr) noexcept = 0;
480
482 virtual evmc_access_status access_storage(const address& addr, const bytes32& key) noexcept = 0;
483};
484
485
490{
491 const evmc_host_interface* host = nullptr;
492 evmc_host_context* context = nullptr;
493
494public:
496 HostContext() = default;
497
501 HostContext(const evmc_host_interface& interface, evmc_host_context* ctx) noexcept
502 : host{&interface}, context{ctx}
503 {}
504
505 bool account_exists(const address& address) const noexcept final
506 {
507 return host->account_exists(context, &address);
508 }
509
510 bytes32 get_storage(const address& address, const bytes32& key) const noexcept final
511 {
512 return host->get_storage(context, &address, &key);
513 }
514
516 const bytes32& key,
517 const bytes32& value) noexcept final
518 {
519 return host->set_storage(context, &address, &key, &value);
520 }
521
522 uint256be get_balance(const address& address) const noexcept final
523 {
524 return host->get_balance(context, &address);
525 }
526
527 size_t get_code_size(const address& address) const noexcept final
528 {
529 return host->get_code_size(context, &address);
530 }
531
532 bytes32 get_code_hash(const address& address) const noexcept final
533 {
534 return host->get_code_hash(context, &address);
535 }
536
537 size_t copy_code(const address& address,
538 size_t code_offset,
539 uint8_t* buffer_data,
540 size_t buffer_size) const noexcept final
541 {
542 return host->copy_code(context, &address, code_offset, buffer_data, buffer_size);
543 }
544
545 bool selfdestruct(const address& addr, const address& beneficiary) noexcept final
546 {
547 return host->selfdestruct(context, &addr, &beneficiary);
548 }
549
550 Result call(const evmc_message& message) noexcept final
551 {
552 return Result{host->call(context, &message)};
553 }
554
556 evmc_tx_context get_tx_context() const noexcept final { return host->get_tx_context(context); }
557
558 bytes32 get_block_hash(int64_t number) const noexcept final
559 {
560 return host->get_block_hash(context, number);
561 }
562
563 void emit_log(const address& addr,
564 const uint8_t* data,
565 size_t data_size,
566 const bytes32 topics[],
567 size_t topics_count) noexcept final
568 {
569 host->emit_log(context, &addr, data, data_size, topics, topics_count);
570 }
571
573 {
574 return host->access_account(context, &address);
575 }
576
577 evmc_access_status access_storage(const address& address, const bytes32& key) noexcept final
578 {
579 return host->access_storage(context, &address, &key);
580 }
581};
582
583
589class Host : public HostInterface
590{
591public:
594 static const evmc_host_interface& get_interface() noexcept;
595
598 evmc_host_context* to_context() noexcept { return reinterpret_cast<evmc_host_context*>(this); }
599
604 template <typename DerivedClass = Host>
605 static DerivedClass* from_context(evmc_host_context* context) noexcept
606 {
607 // Get pointer of the Host base class.
608 auto* h = reinterpret_cast<Host*>(context);
609
610 // Additional downcast, only possible if DerivedClass inherits from Host.
611 return static_cast<DerivedClass*>(h);
612 }
613};
614
615
620class VM
621{
622public:
623 VM() noexcept = default;
624
626 explicit VM(evmc_vm* vm) noexcept : m_instance{vm} {}
627
629 ~VM() noexcept
630 {
631 if (m_instance != nullptr)
632 m_instance->destroy(m_instance);
633 }
634
635 VM(const VM&) = delete;
636 VM& operator=(const VM&) = delete;
637
639 VM(VM&& other) noexcept : m_instance{other.m_instance} { other.m_instance = nullptr; }
640
642 VM& operator=(VM&& other) noexcept
643 {
644 this->~VM();
645 m_instance = other.m_instance;
646 other.m_instance = nullptr;
647 return *this;
648 }
649
652 inline VM(evmc_vm* vm,
653 std::initializer_list<std::pair<const char*, const char*>> options) noexcept;
654
656 explicit operator bool() const noexcept { return m_instance != nullptr; }
657
659 bool is_abi_compatible() const noexcept { return m_instance->abi_version == EVMC_ABI_VERSION; }
660
662 char const* name() const noexcept { return m_instance->name; }
663
665 char const* version() const noexcept { return m_instance->version; }
666
668 bool has_capability(evmc_capabilities capability) const noexcept
669 {
670 return (get_capabilities() & static_cast<evmc_capabilities_flagset>(capability)) != 0;
671 }
672
675 {
676 return m_instance->get_capabilities(m_instance);
677 }
678
680 evmc_set_option_result set_option(const char name[], const char value[]) noexcept
681 {
682 return evmc_set_option(m_instance, name, value);
683 }
684
688 evmc_revision rev,
689 const evmc_message& msg,
690 const uint8_t* code,
691 size_t code_size) noexcept
692 {
693 return Result{m_instance->execute(m_instance, &host, ctx, rev, &msg, code, code_size)};
694 }
695
698 evmc_revision rev,
699 const evmc_message& msg,
700 const uint8_t* code,
701 size_t code_size) noexcept
702 {
703 return execute(Host::get_interface(), host.to_context(), rev, msg, code, code_size);
704 }
705
715 const evmc_message& msg,
716 const uint8_t* code,
717 size_t code_size) noexcept
718 {
719 return Result{
720 m_instance->execute(m_instance, nullptr, nullptr, rev, &msg, code, code_size)};
721 }
722
728 evmc_vm* get_raw_pointer() const noexcept { return m_instance; }
729
730private:
731 evmc_vm* m_instance = nullptr;
732};
733
734inline VM::VM(evmc_vm* vm,
735 std::initializer_list<std::pair<const char*, const char*>> options) noexcept
736 : m_instance{vm}
737{
738 // This constructor is implemented outside of the class definition to workaround a doxygen bug.
739 for (const auto& option : options)
740 set_option(option.first, option.second);
741}
742
743
744namespace internal
745{
746inline bool account_exists(evmc_host_context* h, const evmc_address* addr) noexcept
747{
748 return Host::from_context(h)->account_exists(*addr);
749}
750
752 const evmc_address* addr,
753 const evmc_bytes32* key) noexcept
754{
755 return Host::from_context(h)->get_storage(*addr, *key);
756}
757
759 const evmc_address* addr,
760 const evmc_bytes32* key,
761 const evmc_bytes32* value) noexcept
762{
763 return Host::from_context(h)->set_storage(*addr, *key, *value);
764}
765
766inline evmc_uint256be get_balance(evmc_host_context* h, const evmc_address* addr) noexcept
767{
768 return Host::from_context(h)->get_balance(*addr);
769}
770
771inline size_t get_code_size(evmc_host_context* h, const evmc_address* addr) noexcept
772{
773 return Host::from_context(h)->get_code_size(*addr);
774}
775
776inline evmc_bytes32 get_code_hash(evmc_host_context* h, const evmc_address* addr) noexcept
777{
778 return Host::from_context(h)->get_code_hash(*addr);
779}
780
781inline size_t copy_code(evmc_host_context* h,
782 const evmc_address* addr,
783 size_t code_offset,
784 uint8_t* buffer_data,
785 size_t buffer_size) noexcept
786{
787 return Host::from_context(h)->copy_code(*addr, code_offset, buffer_data, buffer_size);
788}
789
790inline bool selfdestruct(evmc_host_context* h,
791 const evmc_address* addr,
792 const evmc_address* beneficiary) noexcept
793{
794 return Host::from_context(h)->selfdestruct(*addr, *beneficiary);
795}
796
797inline evmc_result call(evmc_host_context* h, const evmc_message* msg) noexcept
798{
799 return Host::from_context(h)->call(*msg).release_raw();
800}
801
803{
804 return Host::from_context(h)->get_tx_context();
805}
806
807inline evmc_bytes32 get_block_hash(evmc_host_context* h, int64_t block_number) noexcept
808{
809 return Host::from_context(h)->get_block_hash(block_number);
810}
811
812inline void emit_log(evmc_host_context* h,
813 const evmc_address* addr,
814 const uint8_t* data,
815 size_t data_size,
816 const evmc_bytes32 topics[],
817 size_t num_topics) noexcept
818{
819 Host::from_context(h)->emit_log(*addr, data, data_size, static_cast<const bytes32*>(topics),
820 num_topics);
821}
822
824{
825 return Host::from_context(h)->access_account(*addr);
826}
827
829 const evmc_address* addr,
830 const evmc_bytes32* key) noexcept
831{
832 return Host::from_context(h)->access_storage(*addr, *key);
833}
834} // namespace internal
835
837{
838 static constexpr evmc_host_interface interface = {
839 ::evmc::internal::account_exists, ::evmc::internal::get_storage,
840 ::evmc::internal::set_storage, ::evmc::internal::get_balance,
841 ::evmc::internal::get_code_size, ::evmc::internal::get_code_hash,
842 ::evmc::internal::copy_code, ::evmc::internal::selfdestruct,
843 ::evmc::internal::call, ::evmc::internal::get_tx_context,
844 ::evmc::internal::get_block_hash, ::evmc::internal::emit_log,
845 ::evmc::internal::access_account, ::evmc::internal::access_storage,
846 };
847 return interface;
848}
849} // namespace evmc
850
851
856inline std::ostream& operator<<(std::ostream& os, evmc_status_code status_code)
857{
858 return os << evmc::to_string(status_code);
859}
860
865inline std::ostream& operator<<(std::ostream& os, evmc_revision rev)
866{
867 return os << evmc::to_string(rev);
868}
869
870namespace std
871{
873template <>
874struct hash<evmc::address>
875{
877 constexpr size_t operator()(const evmc::address& s) const noexcept
878 {
879 using namespace evmc;
880 using namespace fnv;
881 return static_cast<size_t>(fnv1a_by64(
882 fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])), load64le(&s.bytes[8])),
883 load32le(&s.bytes[16])));
884 }
885};
886
888template <>
889struct hash<evmc::bytes32>
890{
892 constexpr size_t operator()(const evmc::bytes32& s) const noexcept
893 {
894 using namespace evmc;
895 using namespace fnv;
896 return static_cast<size_t>(
897 fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64le(&s.bytes[0])),
898 load64le(&s.bytes[8])),
899 load64le(&s.bytes[16])),
900 load64le(&s.bytes[24])));
901 }
902};
903} // namespace std
Wrapper around EVMC host context / host interface.
Definition: evmc.hpp:490
bytes32 get_code_hash(const address &address) const noexcept final
Get code hash callback function.
Definition: evmc.hpp:532
bool account_exists(const address &address) const noexcept final
Check account existence callback function.
Definition: evmc.hpp:505
size_t get_code_size(const address &address) const noexcept final
Get code size callback function.
Definition: evmc.hpp:527
HostContext()=default
Default constructor for null Host context.
uint256be get_balance(const address &address) const noexcept final
Get balance callback function.
Definition: evmc.hpp:522
HostContext(const evmc_host_interface &interface, evmc_host_context *ctx) noexcept
Constructor from the EVMC Host primitives.
Definition: evmc.hpp:501
evmc_access_status access_storage(const address &address, const bytes32 &key) noexcept final
Access storage callback function.
Definition: evmc.hpp:577
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:537
Result call(const evmc_message &message) noexcept final
Call callback function.
Definition: evmc.hpp:550
bool selfdestruct(const address &addr, const address &beneficiary) noexcept final
Selfdestruct callback function.
Definition: evmc.hpp:545
evmc_access_status access_account(const address &address) noexcept final
Access account callback function.
Definition: evmc.hpp:572
bytes32 get_block_hash(int64_t number) const noexcept final
Get block hash callback function.
Definition: evmc.hpp:558
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:563
evmc_storage_status set_storage(const address &address, const bytes32 &key, const bytes32 &value) noexcept final
Set storage callback function.
Definition: evmc.hpp:515
evmc_tx_context get_tx_context() const noexcept final
Get transaction context callback function.
Definition: evmc.hpp:556
bytes32 get_storage(const address &address, const bytes32 &key) const noexcept final
Get storage callback function.
Definition: evmc.hpp:510
The EVMC Host interface.
Definition: evmc.hpp:429
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:590
static DerivedClass * from_context(evmc_host_context *context) noexcept
Converts the opaque host context pointer back to the original Host object.
Definition: evmc.hpp:605
evmc_host_context * to_context() noexcept
Converts the Host object to the opaque host context pointer.
Definition: evmc.hpp:598
static const evmc_host_interface & get_interface() noexcept
Provides access to the global host interface.
Definition: evmc.hpp:836
The EVM code execution result.
Definition: evmc.hpp:332
~Result() noexcept
Destructor responsible for automatically releasing attached resources.
Definition: evmc.hpp:384
Result(const evmc_result &res) noexcept
Converting constructor from raw evmc_result.
Definition: evmc.hpp:381
Result(evmc_status_code _status_code, int64_t _gas_left, const evmc_address &_create_address) noexcept
Creates the result of contract creation.
Definition: evmc.hpp:370
Result(Result &&other) noexcept
Move constructor.
Definition: evmc.hpp:391
evmc_result release_raw() noexcept
Releases the ownership and returns the raw copy of evmc_result.
Definition: evmc.hpp:418
Result(evmc_status_code _status_code, int64_t _gas_left, const uint8_t *_output_data, size_t _output_size) noexcept
Creates the result from the provided arguments.
Definition: evmc.hpp:349
evmc_address create_address
The address of the possibly created contract.
Definition: evmc.h:453
Result & operator=(Result &&other) noexcept
Move assignment operator.
Definition: evmc.hpp:402
Result(evmc_status_code _status_code=EVMC_INTERNAL_ERROR, int64_t _gas_left=0) noexcept
Creates the result without output.
Definition: evmc.hpp:360
The VM instance.
Definition: evmc.hpp:621
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:714
VM & operator=(VM &&other) noexcept
Move assignment operator.
Definition: evmc.hpp:642
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:680
~VM() noexcept
Destructor responsible for automatically destroying the VM instance.
Definition: evmc.hpp:629
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:697
bool is_abi_compatible() const noexcept
Checks whenever the VM instance is ABI compatible with the current EVMC API.
Definition: evmc.hpp:659
bool has_capability(evmc_capabilities capability) const noexcept
Checks if the VM has the given capability.
Definition: evmc.hpp:668
VM(VM &&other) noexcept
Move constructor.
Definition: evmc.hpp:639
evmc_vm * get_raw_pointer() const noexcept
Returns the pointer to C EVMC struct representing the VM.
Definition: evmc.hpp:728
char const * name() const noexcept
The name of the EVMC VM implementation.
Definition: evmc.hpp:662
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:686
char const * version() const noexcept
The version of the EVMC VM implementation, e.g.
Definition: evmc.hpp:665
evmc_capabilities_flagset get_capabilities() const noexcept
A method returning capabilities supported by the VM instance.
Definition: evmc.hpp:674
VM(evmc_vm *vm) noexcept
Converting constructor from evmc_vm.
Definition: evmc.hpp:626
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:856
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:836
evmc_capabilities
Possible capabilities of a VM.
Definition: evmc.h:1003
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:1032
evmc_access_status
Access status per EIP-2929: Gas cost increases for state access opcodes.
Definition: evmc.h:716
evmc_storage_status
The effect of an attempt to modify a contract storage item.
Definition: evmc.h:520
evmc_revision
EVM revision.
Definition: evmc.h:867
@ 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:969
@ EVMC_MAX_REVISION
The maximum EVM revision supported.
Definition: evmc.h:962
static struct evmc_result evmc_make_result(enum evmc_status_code status_code, int64_t gas_left, const uint8_t *output_data, size_t output_size)
Creates the result from the provided arguments.
Definition: helpers.h:124
static const char * evmc_revision_to_string(enum evmc_revision rev)
Returns the name of the evmc_revision.
Definition: helpers.h:267
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:216
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:777
evmc_get_code_hash_fn get_code_hash
Get code hash callback function.
Definition: evmc.h:794
evmc_call_fn call
Call callback function.
Definition: evmc.h:803
evmc_selfdestruct_fn selfdestruct
Selfdestruct callback function.
Definition: evmc.h:800
evmc_get_storage_fn get_storage
Get storage callback function.
Definition: evmc.h:782
evmc_copy_code_fn copy_code
Copy code callback function.
Definition: evmc.h:797
evmc_get_code_size_fn get_code_size
Get code size callback function.
Definition: evmc.h:791
evmc_get_block_hash_fn get_block_hash
Get block hash callback function.
Definition: evmc.h:809
evmc_emit_log_fn emit_log
Emit log callback function.
Definition: evmc.h:812
evmc_account_exists_fn account_exists
Check account existence callback function.
Definition: evmc.h:779
evmc_access_account_fn access_account
Access account callback function.
Definition: evmc.h:815
evmc_get_balance_fn get_balance
Get balance callback function.
Definition: evmc.h:788
evmc_set_storage_fn set_storage
Set storage callback function.
Definition: evmc.h:785
evmc_get_tx_context_fn get_tx_context
Get transaction context callback function.
Definition: evmc.h:806
evmc_access_storage_fn access_storage
Access storage callback function.
Definition: evmc.h:818
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:415
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:443
size_t output_size
The size of the output data.
Definition: evmc.h:422
evmc_address create_address
The address of the possibly created contract.
Definition: evmc.h:453
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:1052