OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
host.hpp
Go to the documentation of this file.
1// Copyright (c) 2022 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef OPENCBDC_TX_SRC_PARSEC_AGENT_EVM_HOST_H_
7#define OPENCBDC_TX_SRC_PARSEC_AGENT_EVM_HOST_H_
8
12
13#include <evmc/evmc.hpp>
14#include <map>
15#include <set>
16
24 class evm_host : public evmc::Host {
25 public:
34 evm_host(std::shared_ptr<logging::log> log,
35 interface::try_lock_callback_type try_lock_callback,
36 evmc_tx_context tx_context,
37 evm_tx tx,
38 bool is_readonly_run,
39 interface::ticket_number_type ticket_number);
40
41 [[nodiscard]] auto
42 account_exists(const evmc::address& addr) const noexcept
43 -> bool override;
44
45 [[nodiscard]] auto get_storage(const evmc::address& addr,
46 const evmc::bytes32& key) const noexcept
47 -> evmc::bytes32 override final;
48
49 auto set_storage(const evmc::address& addr,
50 const evmc::bytes32& key,
51 const evmc::bytes32& value) noexcept
52 -> evmc_storage_status override final;
53
54 [[nodiscard]] auto
55 get_balance(const evmc::address& addr) const noexcept
56 -> evmc::uint256be override final;
57
58 [[nodiscard]] auto
59 get_code_size(const evmc::address& addr) const noexcept
60 -> size_t override final;
61
62 [[nodiscard]] auto
63 get_code_hash(const evmc::address& addr) const noexcept
64 -> evmc::bytes32 override final;
65
66 auto copy_code(const evmc::address& addr,
67 size_t code_offset,
68 uint8_t* buffer_data,
69 size_t buffer_size) const noexcept
70 -> size_t override final;
71
72 auto selfdestruct(const evmc::address& addr,
73 const evmc::address& beneficiary) noexcept
74 -> bool override final;
75
76 auto call(const evmc_message& msg) noexcept
77 -> evmc::Result override final;
78
79 [[nodiscard]] auto get_tx_context() const noexcept
80 -> evmc_tx_context override final;
81
82 [[nodiscard]] auto get_block_hash(int64_t number) const noexcept
83 -> evmc::bytes32 override final;
84
85 void emit_log(
86 const evmc::address& addr,
87 const uint8_t* data,
88 size_t data_size,
89 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
90 const evmc::bytes32 topics[],
91 size_t topics_count) noexcept override final;
92
93 auto access_account(const evmc::address& addr) noexcept
94 -> evmc_access_status override final;
95
96 auto access_storage(const evmc::address& addr,
97 const evmc::bytes32& key) noexcept
98 -> evmc_access_status override final;
99
101 = std::unordered_map<evmc::address, std::vector<evm_log>>;
102
108 auto get_log_index_keys() const -> std::vector<cbdc::buffer>;
109
113 auto get_state_updates() const
114 -> runtime_locking_shard::state_update_type;
115
119 auto should_retry() const -> bool;
120
125 void insert_account(const evmc::address& addr, const evm_account& acc);
126
130 void finalize(int64_t gas_left, int64_t gas_used);
131
134 void revert();
135
140 auto ticket_number_key(std::optional<interface::ticket_number_type> tn
141 = std::nullopt) const -> cbdc::buffer;
142
145 auto log_index_key(evmc::address addr,
146 std::optional<interface::ticket_number_type> tn
147 = std::nullopt) const -> cbdc::buffer;
148
149 private:
150 std::shared_ptr<logging::log> m_log;
151 runner::interface::try_lock_callback_type m_try_lock_callback;
152 mutable std::map<evmc::address,
153 std::pair<std::optional<evm_account>, bool>>
154 m_accounts;
155 mutable std::map<
156 evmc::address,
157 std::map<evmc::bytes32,
158 std::pair<std::optional<evmc::bytes32>, bool>>>
159 m_account_storage;
160 mutable std::map<evmc::address,
161 std::pair<std::optional<evm_account_code>, bool>>
162 m_account_code;
163 evmc_tx_context m_tx_context;
164 std::unique_ptr<evmc::VM> m_vm;
165 evm_tx m_tx;
166 bool m_is_readonly_run;
167
168 mutable std::set<evmc::address> m_accessed_addresses;
169 std::set<std::pair<evmc::address, evmc::bytes32>>
170 m_accessed_storage_keys;
171
172 mutable bool m_retry{false};
173
174 std::map<evmc::address, std::pair<std::optional<evm_account>, bool>>
175 m_init_state;
176
177 evm_tx_receipt m_receipt;
178 cbdc::buffer m_tx_id;
179
180 interface::ticket_number_type m_ticket_number;
181
182 [[nodiscard]] auto get_account(const evmc::address& addr,
183 bool write) const
184 -> std::optional<evm_account>;
185
186 [[nodiscard]] auto get_account_storage(const evmc::address& addr,
187 const evmc::bytes32& key,
188 bool write) const
189 -> std::optional<evmc::bytes32>;
190
191 [[nodiscard]] auto get_account_code(const evmc::address& addr,
192 bool write) const
193 -> std::optional<evm_account_code>;
194
195 auto get_sorted_logs() const
196 -> std::unordered_map<evmc::address, std::vector<evm_log>>;
197
198 void transfer(const evmc::address& from,
199 const evmc::address& to,
200 const evmc::uint256be& value);
201
202 static auto is_precompile(const evmc::address& addr) -> bool;
203
204 [[nodiscard]] auto get_key(const cbdc::buffer& key, bool write) const
205 -> std::optional<broker::value_type>;
206
207 auto create(const evmc_message& msg) noexcept -> evmc::Result;
208
209 auto execute(const evmc_message& msg,
210 const uint8_t* code,
211 size_t code_size) -> evmc::Result;
212 };
213}
214
215#endif
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
Implementation of the evmc::Host interface using PARSEC as the backend database.
Definition host.hpp:24
auto get_log_index_keys() const -> std::vector< cbdc::buffer >
Return the keys of the log indexes - these are sha256(addr, ticket) and will get value 1 - to indicat...
Definition host.cpp:460
auto should_retry() const -> bool
Returns whether the transaction needs to be retried due to a transient error.
Definition host.cpp:537
auto get_balance(const evmc::address &addr) const noexcept -> evmc::uint256be override final
Definition host.cpp:150
auto get_code_hash(const evmc::address &addr) const noexcept -> evmc::bytes32 override final
Definition host.cpp:175
auto get_tx_context() const noexcept -> evmc_tx_context override final
Definition host.cpp:375
void finalize(int64_t gas_left, int64_t gas_used)
Finalizes the state updates resulting from the transaction.
Definition host.cpp:576
void insert_account(const evmc::address &addr, const evm_account &acc)
Inserts an account into the host.
Definition host.cpp:541
void revert()
Set the state updates to revert the transaction changes due to a contract error.
Definition host.cpp:594
auto ticket_number_key(std::optional< interface::ticket_number_type > tn=std::nullopt) const -> cbdc::buffer
Return the key for the host's ticket number, which is the hash of the ticket number's serialized repr...
Definition host.cpp:427
auto get_storage(const evmc::address &addr, const evmc::bytes32 &key) const noexcept -> evmc::bytes32 override final
Definition host.cpp:95
auto account_exists(const evmc::address &addr) const noexcept -> bool override
Definition host.cpp:83
evm_host(std::shared_ptr< logging::log > log, interface::try_lock_callback_type try_lock_callback, evmc_tx_context tx_context, evm_tx tx, bool is_readonly_run, interface::ticket_number_type ticket_number)
Constructs a new host instance.
Definition host.cpp:23
auto copy_code(const evmc::address &addr, size_t code_offset, uint8_t *buffer_data, size_t buffer_size) const noexcept -> size_t override final
Definition host.cpp:192
auto access_storage(const evmc::address &addr, const evmc::bytes32 &key) noexcept -> evmc_access_status override final
Definition host.cpp:414
auto call(const evmc_message &msg) noexcept -> evmc::Result override final
Definition host.cpp:308
auto get_block_hash(int64_t number) const noexcept -> evmc::bytes32 override final
Definition host.cpp:379
void emit_log(const evmc::address &addr, const uint8_t *data, size_t data_size, const evmc::bytes32 topics[], size_t topics_count) noexcept override final
Definition host.cpp:386
auto log_index_key(evmc::address addr, std::optional< interface::ticket_number_type > tn=std::nullopt) const -> cbdc::buffer
Return the key for the indicator of the existence of logs for a particular address at a particular ti...
Definition host.cpp:443
auto set_storage(const evmc::address &addr, const evmc::bytes32 &key, const evmc::bytes32 &value) noexcept -> evmc_storage_status override final
Definition host.cpp:104
auto selfdestruct(const evmc::address &addr, const evmc::address &beneficiary) noexcept -> bool override final
Definition host.cpp:217
auto get_code_size(const evmc::address &addr) const noexcept -> size_t override final
Definition host.cpp:162
auto access_account(const evmc::address &addr) noexcept -> evmc_access_status override final
Definition host.cpp:404
auto get_state_updates() const -> runtime_locking_shard::state_update_type
Return the changes to the state resulting from transaction execution.
Definition host.cpp:481
std::unordered_map< evmc::address, std::vector< evm_log > > indexed_logs_type
Definition host.hpp:100
std::function< bool(broker::key_type, broker::lock_type, broker::interface::try_lock_callback_type)> try_lock_callback_type
Callback function type for acquiring locks during function execution.
parsec::ticket_machine::ticket_number_type ticket_number_type
Type alias for a ticket number.
std::vector< uint8_t > evm_account_code
Type alias for EVM account code.
@ transfer
Base token transfer.