Decentralised Art Server
High-performance C++ backend that exposes HTML interface and a secure REST API for managing Performative Transactions entities
 
Loading...
Searching...
No Matches
evm.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <string>
7#include <cstring>
8#include <filesystem>
9#include <vector>
10#include <expected>
11#include <fstream>
12#include <istream>
13#include <optional>
14
15// Undefine the conflicting macro
16#ifdef interface
17 #undef interface
18#endif
19
20#include <evmc/evmc.h>
21#include <evmc/evmc.hpp>
22#include <evmc/mocked_host.hpp>
23
24#include <evmc/hex.hpp>
25
26#include <evmone/evmone.h>
27
28#ifndef interface
29 #define interface __STRUCT__
30#endif
31
32#include <spdlog/spdlog.h>
33#include <absl/container/flat_hash_map.h>
34
35#include "async.hpp"
36#include "crypto.hpp"
37#include "chain.hpp"
38
39#include "evm_storage.hpp"
40#include "evm_formatter.hpp"
41
42namespace dcn::evm
43{
44 class EVM
45 {
46 public:
48
49 EVM(asio::io_context & io_context, evmc_revision rev, std::filesystem::path solc_path, std::filesystem::path pt_path);
50 ~EVM() = default;
51
52 EVM(const EVM&) = delete;
53 EVM& operator=(const EVM&) = delete;
54
55 EVM(EVM&&) = delete;
56 EVM& operator=(EVM&&) = delete;
57
58 asio::awaitable<bool> addAccount(chain::Address address, std::uint64_t initial_gas) noexcept;
59 asio::awaitable<bool> setGas(chain::Address address, std::uint64_t gas) noexcept;
60
61 asio::awaitable<bool> compile(std::filesystem::path code_path,
62 std::filesystem::path out_dir,
63 std::filesystem::path base_path = {},
64 std::filesystem::path includes = {}) const noexcept;
65
66 asio::awaitable<std::expected<chain::Address, chain::DeployError>> deploy(
67 std::istream & code_stream,
68 chain::Address sender,
69 std::vector<std::uint8_t> constructor_args,
70 std::uint64_t gas_limit,
71 std::uint64_t value) noexcept;
72
73 asio::awaitable<std::expected<chain::Address, chain::DeployError>> deploy(
74 std::filesystem::path code_path,
75 chain::Address sender,
76 std::vector<uint8_t> constructor_args,
77 std::uint64_t gas_limit,
78 std::uint64_t value) noexcept;
79
80 asio::awaitable<std::expected<std::vector<std::uint8_t>, chain::ExecuteError>> execute(
81 chain::Address sender,
82 chain::Address recipient,
83 std::vector<std::uint8_t> input_bytes,
84 std::uint64_t gas_limit,
85 std::uint64_t value) noexcept;
86
87 asio::awaitable<std::vector<EmittedLogRecord>> getLogsSince(std::uint64_t after_seq, std::size_t limit) noexcept;
88 asio::awaitable<std::int64_t> getHeadBlockNumber() noexcept;
89
90 chain::Address getRegistryAddress() const;
91 chain::Address getRunnerAddress() const;
92
93 const std::filesystem::path & getSolcPath() const;
94 const std::filesystem::path & getPTPath() const;
95
96 protected:
97 asio::awaitable<bool> loadPT();
98
99 private:
100 asio::strand<asio::io_context::executor_type> _strand;
101
102 evmc::VM _vm;
103 evmc_revision _rev;
104
105 std::filesystem::path _solc_path;
106 std::filesystem::path _pt_path;
107
108 EVMStorage _storage;
109
110 chain::Address _genesis_address;
111 chain::Address _console_log_address;
112
113 chain::Address _registry_address;
114 chain::Address _runner_address;
115 };
116
117 asio::awaitable<std::expected<std::vector<std::uint8_t>, chain::ExecuteError>> fetchOwner(EVM & evm, const chain::Address & address);
118
119 template<class T>
120 std::vector<std::uint8_t> encodeAsArg(const T & val);
121
122 template<>
123 std::vector<std::uint8_t> encodeAsArg<chain::Address>(const chain::Address & address);
124
125 template<>
126 std::vector<std::uint8_t> encodeAsArg<std::uint32_t>(const std::uint32_t & value);
127
128 template<>
129 std::vector<std::uint8_t> encodeAsArg<std::vector<std::uint32_t>>(const std::vector<std::uint32_t> & vec);
130
131 template<>
132 std::vector<std::uint8_t> encodeAsArg<std::vector<std::tuple<std::uint32_t, std::uint32_t>>>(const std::vector<std::tuple<std::uint32_t, std::uint32_t>>& vec);
133
134 template<>
135 std::vector<std::uint8_t> encodeAsArg<std::vector<std::tuple<std::uint32_t, std::uint32_t, std::uint32_t>>>(const std::vector<std::tuple<std::uint32_t, std::uint32_t, std::uint32_t>>& vec);
136
137 template<>
138 std::vector<std::uint8_t> encodeAsArg<std::string>(const std::string& str);
139}
Definition evm.hpp:45
asio::awaitable< std::int64_t > getHeadBlockNumber() noexcept
Definition evm.cpp:518
asio::awaitable< bool > loadPT()
Definition evm.cpp:524
const std::filesystem::path & getPTPath() const
Definition evm.cpp:247
chain::Address getRegistryAddress() const
Definition evm.cpp:232
asio::awaitable< bool > addAccount(chain::Address address, std::uint64_t initial_gas) noexcept
Definition evm.cpp:252
chain::Address getRunnerAddress() const
Definition evm.cpp:237
asio::awaitable< std::vector< EmittedLogRecord > > getLogsSince(std::uint64_t after_seq, std::size_t limit) noexcept
Definition evm.cpp:510
asio::awaitable< std::expected< chain::Address, chain::DeployError > > deploy(std::istream &code_stream, chain::Address sender, std::vector< std::uint8_t > constructor_args, std::uint64_t gas_limit, std::uint64_t value) noexcept
Definition evm.cpp:336
EVM(EVM &&)=delete
~EVM()=default
EVM & operator=(EVM &&)=delete
EVM(const EVM &)=delete
asio::awaitable< std::expected< std::vector< std::uint8_t >, chain::ExecuteError > > execute(chain::Address sender, chain::Address recipient, std::vector< std::uint8_t > input_bytes, std::uint64_t gas_limit, std::uint64_t value) noexcept
Definition evm.cpp:441
asio::awaitable< bool > compile(std::filesystem::path code_path, std::filesystem::path out_dir, std::filesystem::path base_path={}, std::filesystem::path includes={}) const noexcept
Definition evm.cpp:288
asio::awaitable< bool > setGas(chain::Address address, std::uint64_t gas) noexcept
Definition evm.cpp:274
const std::filesystem::path & getSolcPath() const
Definition evm.cpp:242
EVM & operator=(const EVM &)=delete
evmc::address Address
Definition address.hpp:18
Definition events_runtime.hpp:20
asio::awaitable< std::expected< std::vector< std::uint8_t >, chain::ExecuteError > > fetchOwner(EVM &evm, const chain::Address &address)
Definition evm.cpp:177
std::vector< std::uint8_t > encodeAsArg(const T &val)
Definition execute.hpp:21
Definition evm_storage.hpp:38