aurora_evm/runtime/handler.rs
1use crate::prelude::*;
2use crate::{Capture, Context, CreateScheme, ExitError, ExitReason, Machine, Opcode};
3use primitive_types::{H160, H256, U256};
4
5/// Transfer from source to target, with given value.
6#[derive(Clone, Debug)]
7pub struct Transfer {
8 /// Source address.
9 pub source: H160,
10 /// Target address.
11 pub target: H160,
12 /// Transfer value.
13 pub value: U256,
14}
15
16/// EVM context handler.
17#[auto_impl::auto_impl(& mut, Box)]
18pub trait Handler {
19 /// Type of `CREATE` interrupt.
20 type CreateInterrupt;
21 /// Feedback value for `CREATE` interrupt.
22 type CreateFeedback;
23 /// Type of `CALL` interrupt.
24 type CallInterrupt;
25 /// Feedback value of `CALL` interrupt.
26 type CallFeedback;
27
28 /// Get balance of address.
29 fn balance(&self, address: H160) -> U256;
30 /// Get code size of address.
31 fn code_size(&mut self, address: H160) -> U256;
32 /// Get code hash of address.
33 fn code_hash(&mut self, address: H160) -> H256;
34 /// Get code of address.
35 fn code(&self, address: H160) -> Vec<u8>;
36 /// Get storage value of address at index.
37 fn storage(&self, address: H160, index: H256) -> H256;
38 /// Check if the storage of the address is empty.
39 fn is_empty_storage(&self, address: H160) -> bool;
40 /// Get original storage value of address at index.
41 fn original_storage(&self, address: H160, index: H256) -> H256;
42
43 /// Get the gas left value.
44 fn gas_left(&self) -> U256;
45 /// Get the gas price value.
46 fn gas_price(&self) -> U256;
47 /// Get execution origin.
48 fn origin(&self) -> H160;
49 /// Get environmental block hash.
50 fn block_hash(&self, number: U256) -> H256;
51 /// Get environmental block number.
52 fn block_number(&self) -> U256;
53 /// Get environmental coinbase.
54 fn block_coinbase(&self) -> H160;
55 /// Get environmental block timestamp.
56 fn block_timestamp(&self) -> U256;
57 /// Get environmental block difficulty.
58 fn block_difficulty(&self) -> U256;
59 /// Get environmental block randomness.
60 fn block_randomness(&self) -> Option<H256>;
61 /// Get environmental gas limit.
62 fn block_gas_limit(&self) -> U256;
63 /// Environmental block base fee.
64 fn block_base_fee_per_gas(&self) -> U256;
65 /// Get environmental chain ID.
66 fn chain_id(&self) -> U256;
67
68 /// Check whether an address exists.
69 fn exists(&self, address: H160) -> bool;
70 /// Check whether an address has already been deleted.
71 fn deleted(&self, address: H160) -> bool;
72 /// Checks if the address or (address, index) pair has been previously accessed
73 /// (or set in `accessed_addresses` / `accessed_storage_keys` via an access list
74 /// transaction).
75 /// References:
76 /// * <https://eips.ethereum.org/EIPS/eip-2929>
77 /// * <https://eips.ethereum.org/EIPS/eip-2930>
78 ///
79 /// # Errors
80 /// Return `ExitError`
81 fn is_cold(&mut self, address: H160, index: Option<H256>) -> bool;
82
83 /// Set storage value of address at index.
84 ///
85 /// # Errors
86 /// Return `ExitError`
87 fn set_storage(&mut self, address: H160, index: H256, value: H256) -> Result<(), ExitError>;
88 /// Create a log owned by address with given topics and data.
89 ///
90 /// # Errors
91 /// Return `ExitError`
92 fn log(&mut self, address: H160, topics: Vec<H256>, data: Vec<u8>) -> Result<(), ExitError>;
93 /// Mark an address to be deleted, with funds transferred to target.
94 ///
95 /// # Errors
96 /// Return `ExitError`
97 fn mark_delete(&mut self, address: H160, target: H160) -> Result<(), ExitError>;
98 /// Invoke a create operation.
99 fn create(
100 &mut self,
101 caller: H160,
102 scheme: CreateScheme,
103 value: U256,
104 init_code: Vec<u8>,
105 target_gas: Option<u64>,
106 ) -> Capture<(ExitReason, Vec<u8>), Self::CreateInterrupt>;
107 /// Feed in create feedback.
108 ///
109 /// # Errors
110 /// Return `ExitError`
111 fn create_feedback(
112 &mut self,
113 #[allow(clippy::used_underscore_binding)] _feedback: Self::CreateFeedback,
114 ) -> Result<(), ExitError> {
115 Ok(())
116 }
117 /// Invoke a call operation.
118 fn call(
119 &mut self,
120 code_address: H160,
121 transfer: Option<Transfer>,
122 input: Vec<u8>,
123 target_gas: Option<u64>,
124 is_static: bool,
125 context: Context,
126 ) -> Capture<(ExitReason, Vec<u8>), Self::CallInterrupt>;
127 /// Feed in call feedback.
128 ///
129 /// # Errors
130 /// Return `ExitError`
131 fn call_feedback(
132 &mut self,
133 #[allow(clippy::used_underscore_binding)] _feedback: Self::CallFeedback,
134 ) -> Result<(), ExitError> {
135 Ok(())
136 }
137 /// Handle other unknown external opcodes.
138 ///
139 /// # Errors
140 /// Return `ExitError`
141 fn other(
142 &mut self,
143 opcode: Opcode,
144 #[allow(clippy::used_underscore_binding)] _stack: &mut Machine,
145 ) -> Result<(), ExitError> {
146 Err(ExitError::InvalidCode(opcode))
147 }
148
149 /// Records some associated `ExternalOperation`.
150 ///
151 /// # Errors
152 /// Return `ExitError`
153 fn record_external_operation(&mut self, op: crate::ExternalOperation) -> Result<(), ExitError>;
154
155 /// Returns `None` if `Cancun` is not enabled.
156 /// CANCUN hard fork.
157 /// [EIP-4844]: Shard Blob Transactions
158 /// [EIP-7516]: BLOBBASEFEE instruction
159 fn blob_base_fee(&self) -> Option<u128>;
160 /// Get `blob_hash` from `blob_versioned_hashes` by index
161 /// [EIP-4844]: BLOBHASH - https://eips.ethereum.org/EIPS/eip-4844#opcode-to-get-versioned-hashes
162 fn get_blob_hash(&self, index: usize) -> Option<U256>;
163 /// Set tstorage value of address at index.
164 /// [EIP-1153]: Transient storage
165 ///
166 /// # Errors
167 /// Return `ExitError`
168 fn tstore(&mut self, address: H160, index: H256, value: U256) -> Result<(), ExitError>;
169 /// Get tstorage value of address at index.
170 /// [EIP-1153]: Transient storage
171 ///
172 /// # Errors
173 /// Return `ExitError`
174 fn tload(&mut self, address: H160, index: H256) -> Result<U256, ExitError>;
175
176 /// Return the target address of the authority delegation designation (EIP-7702).
177 fn get_authority_target(&mut self, address: H160) -> Option<H160>;
178
179 /// Get delegation designator code for the authority code.
180 /// EIP-7702
181 fn authority_code(&mut self, authority: H160) -> Vec<u8>;
182
183 /// Warm target according to EIP-2929
184 fn warm_target(&mut self, target: (H160, Option<H256>));
185}