TLA Line data Source code
1 : // evmone: Fast Ethereum Virtual Machine implementation
2 : // Copyright 2022 The evmone Authors.
3 : // SPDX-License-Identifier: Apache-2.0
4 :
5 : #pragma once
6 :
7 : #include "state.hpp"
8 : #include <optional>
9 : #include <unordered_set>
10 :
11 : namespace evmone::state
12 : {
13 : using evmc::uint256be;
14 :
15 : inline constexpr size_t max_code_size = 0x6000;
16 : inline constexpr size_t max_initcode_size = 2 * max_code_size;
17 :
18 : /// Computes the address of to-be-created contract with the CREATE scheme.
19 : ///
20 : /// Computes the new account address for the contract creation context of the CREATE instruction
21 : /// or a create transaction.
22 : /// This is defined by ππππ in Yellow Paper, 7. Contract Creation, (88-90), the case for ΞΆ = β
.
23 : ///
24 : /// @param sender The address of the message sender. YP: π .
25 : /// @param sender_nonce The sender's nonce before the increase. YP: π.
26 : /// @return The address computed with the CREATE scheme.
27 : [[nodiscard]] address compute_create_address(const address& sender, uint64_t sender_nonce) noexcept;
28 :
29 : /// Computes the address of to-be-created contract with the CREATE2 scheme.
30 : ///
31 : /// Computes the new account address for the contract creation context of the CREATE2 instruction.
32 : /// This is defined by ππππ in Yellow Paper, 7. Contract Creation, (88-90), the case for ΞΆ β β
.
33 : ///
34 : /// @param sender The address of the message sender. YP: π .
35 : /// @param salt The salt. YP: ΞΆ.
36 : /// @param init_code The contract creation init code. YP: π’.
37 : /// @return The address computed with the CREATE2 scheme.
38 : [[nodiscard]] address compute_create2_address(
39 : const address& sender, const bytes32& salt, bytes_view init_code) noexcept;
40 :
41 : class Host : public evmc::Host
42 : {
43 : evmc_revision m_rev;
44 : evmc::VM& m_vm;
45 : State& m_state;
46 : const BlockInfo& m_block;
47 : const Transaction& m_tx;
48 : std::vector<Log> m_logs;
49 :
50 : public:
51 CBC 53 : Host(evmc_revision rev, evmc::VM& vm, State& state, const BlockInfo& block,
52 : const Transaction& tx) noexcept
53 53 : : m_rev{rev}, m_vm{vm}, m_state{state}, m_block{block}, m_tx{tx}
54 53 : {}
55 :
56 53 : [[nodiscard]] std::vector<Log>&& take_logs() noexcept { return std::move(m_logs); }
57 :
58 : evmc::Result call(const evmc_message& msg) noexcept override;
59 :
60 : private:
61 : [[nodiscard]] bool account_exists(const address& addr) const noexcept override;
62 :
63 : [[nodiscard]] bytes32 get_storage(
64 : const address& addr, const bytes32& key) const noexcept override;
65 :
66 : evmc_storage_status set_storage(
67 : const address& addr, const bytes32& key, const bytes32& value) noexcept override;
68 :
69 : [[nodiscard]] evmc::bytes32 get_transient_storage(
70 : const address& addr, const bytes32& key) const noexcept override;
71 :
72 : void set_transient_storage(
73 : const address& addr, const bytes32& key, const bytes32& value) noexcept override;
74 :
75 : [[nodiscard]] uint256be get_balance(const address& addr) const noexcept override;
76 :
77 : [[nodiscard]] size_t get_code_size(const address& addr) const noexcept override;
78 :
79 : [[nodiscard]] bytes32 get_code_hash(const address& addr) const noexcept override;
80 :
81 : size_t copy_code(const address& addr, size_t code_offset, uint8_t* buffer_data,
82 : size_t buffer_size) const noexcept override;
83 :
84 : bool selfdestruct(const address& addr, const address& beneficiary) noexcept override;
85 :
86 : evmc::Result create(const evmc_message& msg) noexcept;
87 :
88 : [[nodiscard]] evmc_tx_context get_tx_context() const noexcept override;
89 :
90 : [[nodiscard]] bytes32 get_block_hash(int64_t block_number) const noexcept override;
91 :
92 : void emit_log(const address& addr, const uint8_t* data, size_t data_size,
93 : const bytes32 topics[], size_t topics_count) noexcept override;
94 :
95 : public:
96 : evmc_access_status access_account(const address& addr) noexcept override;
97 :
98 : private:
99 : evmc_access_status access_storage(const address& addr, const bytes32& key) noexcept override;
100 :
101 : /// Prepares message for execution.
102 : ///
103 : /// This contains mostly checks and logic related to the sender
104 : /// which may finally be moved to EVM.
105 : /// Any state modification is not reverted.
106 : /// @return Modified message or std::nullopt in case of EVM exception.
107 : std::optional<evmc_message> prepare_message(evmc_message msg);
108 :
109 : evmc::Result execute_message(const evmc_message& msg) noexcept;
110 : };
111 : } // namespace evmone::state
|