ethrex_levm/
execution_handlers.rs1use crate::{
2 constants::*,
3 errors::{ContextResult, ExceptionalHalt, InternalError, TxResult, VMError},
4 gas_cost::{CODE_DEPOSIT_COST, CODE_DEPOSIT_REGULAR_COST_PER_WORD},
5 utils::create_eth_transfer_log,
6 vm::VM,
7};
8
9use bytes::Bytes;
10use ethrex_common::types::{Code, Fork};
11
12impl<'a> VM<'a> {
13 pub fn handle_precompile_result(
14 precompile_result: Result<Bytes, VMError>,
15 gas_limit: u64,
16 gas_remaining: u64,
17 ) -> Result<ContextResult, VMError> {
18 match precompile_result {
19 Ok(output) => {
20 let gas_used = gas_limit
21 .checked_sub(gas_remaining)
22 .ok_or(InternalError::Underflow)?;
23 Ok(ContextResult {
24 result: TxResult::Success,
25 gas_used,
26 gas_spent: gas_used, output,
28 })
29 }
30 Err(error) => {
31 if error.should_propagate() {
32 return Err(error);
33 }
34
35 Ok(ContextResult {
36 result: TxResult::Revert(error),
37 gas_used: gas_limit,
38 gas_spent: gas_limit, output: Bytes::new(),
40 })
41 }
42 }
43 }
44
45 #[cold] pub fn handle_opcode_result(&mut self) -> Result<ContextResult, VMError> {
47 if self.is_create()? {
49 let validate_create = self.validate_contract_creation();
50
51 if let Err(error) = validate_create {
52 if error.should_propagate() {
53 return Err(error);
54 }
55
56 if self.env.config.fork >= Fork::Amsterdam {
61 let entry = self.current_call_frame.state_gas_used_at_entry;
62 self.refill_frame_state_gas(entry)?;
63 }
64
65 let callframe = &mut self.current_call_frame;
67 callframe.gas_remaining = 0;
68
69 #[expect(clippy::as_conversions, reason = "remaining gas conversion")]
70 let gas_used = callframe
71 .gas_limit
72 .checked_sub(callframe.gas_remaining as u64)
73 .ok_or(InternalError::Underflow)?;
74 return Ok(ContextResult {
75 result: TxResult::Revert(error),
76 gas_used,
77 gas_spent: gas_used, output: Bytes::new(),
79 });
80 }
81
82 let contract_address = self.current_call_frame.to;
84 let code = self.current_call_frame.output.clone();
85 self.update_account_bytecode(contract_address, Code::from_bytecode(code, self.crypto))?;
86 }
87
88 #[expect(clippy::as_conversions, reason = "remaining gas conversion")]
89 let gas_used = {
90 let callframe = &mut self.current_call_frame;
91 callframe
92 .gas_limit
93 .checked_sub(callframe.gas_remaining as u64)
94 .ok_or(InternalError::Underflow)?
95 };
96 Ok(ContextResult {
97 result: TxResult::Success,
98 gas_used,
99 gas_spent: gas_used, output: std::mem::take(&mut self.current_call_frame.output),
101 })
102 }
103
104 #[cold] pub fn handle_opcode_error(&mut self, error: VMError) -> Result<ContextResult, VMError> {
106 if error.should_propagate() {
107 return Err(error);
108 }
109
110 if self.env.config.fork >= Fork::Amsterdam {
115 let entry = self.current_call_frame.state_gas_used_at_entry;
116 self.refill_frame_state_gas(entry)?;
117 }
118
119 let callframe = &mut self.current_call_frame;
120
121 if !error.is_revert_opcode() {
123 callframe.gas_remaining = 0;
124 }
125
126 #[expect(clippy::as_conversions, reason = "remaining gas conversion")]
127 let gas_used = callframe
128 .gas_limit
129 .checked_sub(callframe.gas_remaining as u64)
130 .ok_or(InternalError::Underflow)?;
131 Ok(ContextResult {
132 result: TxResult::Revert(error),
133 gas_used,
134 gas_spent: gas_used, output: std::mem::take(&mut callframe.output),
136 })
137 }
138
139 pub fn handle_create_transaction(&mut self) -> Result<Option<ContextResult>, VMError> {
141 let new_contract_address = self.current_call_frame.to;
142
143 if let Some(recorder) = self.db.bal_recorder.as_mut() {
146 recorder.record_touched_address(new_contract_address);
147 }
148
149 let new_account = self.get_account_mut(new_contract_address)?;
150
151 if new_account.create_would_collide() {
152 debug_assert_eq!(
159 self.state_gas_spill, 0,
160 "create collision must occur before any execution state gas spills"
161 );
162 debug_assert_eq!(
163 self.current_call_frame.frame_state_gas_spilled, 0,
164 "create collision must occur before any per-frame state gas spills"
165 );
166
167 self.current_call_frame.gas_remaining = 0;
173 return Ok(Some(ContextResult {
174 result: TxResult::Revert(ExceptionalHalt::AddressAlreadyOccupied.into()),
175 gas_used: self.env.gas_limit,
176 gas_spent: self.env.gas_limit, output: Bytes::new(),
178 }));
179 }
180
181 self.created_target_alive = !new_account.is_empty();
190
191 let value = self.current_call_frame.msg_value;
192 self.increase_account_balance(new_contract_address, value)?;
193
194 if self.env.config.fork >= Fork::Amsterdam && !value.is_zero() {
197 let log = create_eth_transfer_log(self.env.origin, new_contract_address, value);
198 self.substate.add_log(log);
199 }
200
201 self.increment_account_nonce(new_contract_address)?;
202
203 Ok(None)
204 }
205
206 fn validate_contract_creation(&mut self) -> Result<(), VMError> {
208 let fork = self.env.config.fork;
209 let code = &self.current_call_frame.output;
210
211 let code_length: u64 = code
212 .len()
213 .try_into()
214 .map_err(|_| InternalError::TypeConversion)?;
215
216 if code.first().is_some_and(|v| v == &EOF_PREFIX) {
218 return Err(ExceptionalHalt::InvalidContractPrefix.into());
219 }
220
221 if fork >= Fork::Amsterdam {
226 if code_length > AMSTERDAM_MAX_CODE_SIZE {
228 return Err(ExceptionalHalt::ContractOutputTooBig.into());
229 }
230
231 let words = code_length.div_ceil(32);
232 let regular = words
233 .checked_mul(CODE_DEPOSIT_REGULAR_COST_PER_WORD)
234 .ok_or(InternalError::Overflow)?;
235 let state = code_length
236 .checked_mul(self.cost_per_state_byte)
237 .ok_or(InternalError::Overflow)?;
238
239 self.current_call_frame.increase_consumed_gas(regular)?;
241 if state > 0 {
242 self.increase_state_gas(state)?;
243 }
244 } else {
245 if code_length > MAX_CODE_SIZE {
247 return Err(ExceptionalHalt::ContractOutputTooBig.into());
248 }
249 let regular = code_length
250 .checked_mul(CODE_DEPOSIT_COST)
251 .ok_or(InternalError::Overflow)?;
252 self.current_call_frame.increase_consumed_gas(regular)?;
253 }
254
255 Ok(())
256 }
257}