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 <string>
5#include <cstring>
6#include <filesystem>
7#include <vector>
8#include <expected>
9#include <fstream>
10#include <istream>
11#include <optional>
12
13// Undefine the conflicting macro
14#ifdef interface
15 #undef interface
16#endif
17
18#include <evmc/evmc.h>
19#include <evmc/evmc.hpp>
20#include <evmc/mocked_host.hpp>
21
22#include <evmc/hex.hpp>
23
24#include <evmone/evmone.h>
25
26#ifndef interface
27 #define interface __STRUCT__
28#endif
29
30#include <spdlog/spdlog.h>
31#include <absl/container/flat_hash_map.h>
32
33#include "async.hpp"
34#include "crypto.hpp"
35#include "chain.hpp"
36
37#include "evm_storage.hpp"
38#include "evm_formatter.hpp"
39
40namespace dcn::evm
41{
42 class EVM
43 {
44 public:
45 EVM(asio::io_context & io_context, evmc_revision rev, std::filesystem::path solc_path, std::filesystem::path pt_path);
46 ~EVM() = default;
47
48 EVM(const EVM&) = delete;
49 EVM& operator=(const EVM&) = delete;
50
51 EVM(EVM&&) = delete;
52 EVM& operator=(EVM&&) = delete;
53
54 asio::awaitable<bool> addAccount(chain::Address address, std::uint64_t initial_gas) noexcept;
55 asio::awaitable<bool> setGas(chain::Address address, std::uint64_t gas) noexcept;
56
57 asio::awaitable<bool> compile(std::filesystem::path code_path,
58 std::filesystem::path out_dir,
59 std::filesystem::path base_path = {},
60 std::filesystem::path includes = {}) const noexcept;
61
62 asio::awaitable<std::expected<chain::Address, chain::DeployError>> deploy(
63 std::istream & code_stream,
64 chain::Address sender,
65 std::vector<std::uint8_t> constructor_args,
66 std::uint64_t gas_limit,
67 std::uint64_t value) noexcept;
68
69 asio::awaitable<std::expected<chain::Address, chain::DeployError>> deploy(
70 std::filesystem::path code_path,
71 chain::Address sender,
72 std::vector<uint8_t> constructor_args,
73 std::uint64_t gas_limit,
74 std::uint64_t value) noexcept;
75
76 asio::awaitable<std::expected<std::vector<std::uint8_t>, chain::ExecuteError>> execute(
77 chain::Address sender,
78 chain::Address recipient,
79 std::vector<std::uint8_t> input_bytes,
80 std::uint64_t gas_limit,
81 std::uint64_t value) noexcept;
82
85
86 const std::filesystem::path & getSolcPath() const;
87 const std::filesystem::path & getPTPath() const;
88
89 protected:
90 asio::awaitable<bool> loadPT();
91
92 private:
93 asio::strand<asio::io_context::executor_type> _strand;
94
95 evmc::VM _vm;
96 evmc_revision _rev;
97
98 std::filesystem::path _solc_path;
99 std::filesystem::path _pt_path;
100
101 EVMStorage _storage;
102
103 chain::Address _genesis_address;
104 chain::Address _console_log_address;
105
106 chain::Address _registry_address;
107 chain::Address _runner_address;
108 };
109
110 asio::awaitable<std::expected<std::vector<std::uint8_t>, chain::ExecuteError>> fetchOwner(EVM & evm, const chain::Address & address);
111
112 template<class T>
113 std::vector<std::uint8_t> encodeAsArg(const T & val);
114
115 template<>
116 std::vector<std::uint8_t> encodeAsArg<chain::Address>(const chain::Address & address);
117
118 template<>
119 std::vector<std::uint8_t> encodeAsArg<std::uint32_t>(const std::uint32_t & value);
120
121 template<>
122 std::vector<std::uint8_t> encodeAsArg<std::vector<std::uint32_t>>(const std::vector<std::uint32_t> & vec);
123
124 template<>
125 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);
126
127 template<>
128 std::vector<std::uint8_t> encodeAsArg<std::string>(const std::string& str);
129}
Definition evm.hpp:43
asio::awaitable< bool > loadPT()
Definition evm.cpp:469
const std::filesystem::path & getPTPath() const
Definition evm.cpp:206
chain::Address getRegistryAddress() const
Definition evm.cpp:191
asio::awaitable< bool > addAccount(chain::Address address, std::uint64_t initial_gas) noexcept
Definition evm.cpp:211
chain::Address getRunnerAddress() const
Definition evm.cpp:196
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:295
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:400
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:247
asio::awaitable< bool > setGas(chain::Address address, std::uint64_t gas) noexcept
Definition evm.cpp:233
const std::filesystem::path & getSolcPath() const
Definition evm.cpp:201
EVM & operator=(const EVM &)=delete
evmc::address Address
Definition address.hpp:18
Definition evm.hpp:41
std::vector< std::uint8_t > encodeAsArg< std::string >(const std::string &str)
Definition evm.cpp:115
asio::awaitable< std::expected< std::vector< std::uint8_t >, chain::ExecuteError > > fetchOwner(EVM &evm, const chain::Address &address)
Definition evm.cpp:136
std::vector< std::uint8_t > encodeAsArg(const T &val)
std::vector< std::uint8_t > encodeAsArg< std::uint32_t >(const std::uint32_t &value)
Definition evm.cpp:32
std::vector< std::uint8_t > encodeAsArg< chain::Address >(const chain::Address &address)
Definition evm.cpp:24
Definition execute.hpp:21