Skip to content

Commit 2524914

Browse files
authored
stop popping callframe unnecessarily (#2584)
**Motivation** <!-- Why does this pull request exist? What are its goals? --> - stop popping callframe in finalize execution **Description** <!-- A clear and concise general description of the changes this PR introduces --> <!-- Link to issues: Resolves #111, Resolves #222 --> Closes #issue_number
1 parent acf7d82 commit 2524914

File tree

4 files changed

+12
-19
lines changed

4 files changed

+12
-19
lines changed

crates/vm/levm/src/hooks/default_hook.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{
22
account::Account,
3-
call_frame::CallFrame,
43
constants::*,
54
errors::{ExecutionReport, InternalError, TxValidationError, VMError},
65
gas_cost::{self, STANDARD_TOKEN_COST, TOTAL_COST_FLOOR_PER_TOKEN},
@@ -304,19 +303,21 @@ impl Hook for DefaultHook {
304303
fn finalize_execution(
305304
&self,
306305
vm: &mut VM<'_>,
307-
initial_call_frame: &CallFrame,
308306
report: &mut ExecutionReport,
309307
) -> Result<(), VMError> {
310-
let sender_address = initial_call_frame.msg_sender;
308+
let sender_address = vm.current_call_frame()?.msg_sender;
311309

312310
// 1. Undo value transfer if Tx reverted
313311
if !report.is_success() {
314312
// In a create if Tx was reverted the account won't even exist by this point.
315313
if !vm.is_create() {
316-
vm.decrease_account_balance(initial_call_frame.to, initial_call_frame.msg_value)?;
314+
vm.decrease_account_balance(
315+
vm.current_call_frame()?.to,
316+
vm.current_call_frame()?.msg_value,
317+
)?;
317318
}
318319

319-
vm.increase_account_balance(sender_address, initial_call_frame.msg_value)?;
320+
vm.increase_account_balance(sender_address, vm.current_call_frame()?.msg_value)?;
320321
}
321322

322323
// 2. Return unused gas + gas refunds to the sender.
@@ -343,7 +344,7 @@ impl Hook for DefaultHook {
343344
.ok_or(VMError::Internal(InternalError::UndefinedState(-2)))?;
344345

345346
let actual_gas_used = if vm.env.config.fork >= Fork::Prague {
346-
let minimum_gas_consumed = vm.get_min_gas_used(initial_call_frame)?;
347+
let minimum_gas_consumed = vm.get_min_gas_used(vm.current_call_frame()?)?;
347348
exec_gas_consumed.max(minimum_gas_consumed)
348349
} else {
349350
exec_gas_consumed

crates/vm/levm/src/hooks/hook.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::{
2-
call_frame::CallFrame,
32
errors::{ExecutionReport, VMError},
43
vm::VM,
54
};
@@ -10,7 +9,6 @@ pub trait Hook {
109
fn finalize_execution(
1110
&self,
1211
vm: &mut VM<'_>,
13-
initial_call_frame: &CallFrame,
1412
report: &mut ExecutionReport,
1513
) -> Result<(), VMError>;
1614
}

crates/vm/levm/src/hooks/l2_hook.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,19 @@ impl Hook for L2Hook {
193193
fn finalize_execution(
194194
&self,
195195
vm: &mut crate::vm::VM<'_>,
196-
initial_call_frame: &crate::call_frame::CallFrame,
197196
report: &mut crate::errors::ExecutionReport,
198197
) -> Result<(), crate::errors::VMError> {
199198
// POST-EXECUTION Changes
200-
let sender_address = initial_call_frame.msg_sender;
199+
let sender_address = vm.current_call_frame()?.msg_sender;
201200

202201
// 1. Undo value transfer if Tx reverted
203202
if !report.is_success() {
204203
// In a create if Tx was reverted the account won't even exist by this point.
205204
if !vm.is_create() {
206-
vm.decrease_account_balance(initial_call_frame.to, initial_call_frame.msg_value)?;
205+
vm.decrease_account_balance(vm.current_call_frame()?.to, vm.current_call_frame()?.msg_value)?;
207206
}
208207

209-
vm.increase_account_balance(sender_address, initial_call_frame.msg_value)?;
208+
vm.increase_account_balance(sender_address, vm.current_call_frame()?.msg_value)?;
210209
}
211210

212211
// 2. Return unused gas + gas refunds to the sender.
@@ -233,7 +232,7 @@ impl Hook for L2Hook {
233232
.ok_or(VMError::Internal(InternalError::UndefinedState(-2)))?;
234233

235234
let actual_gas_used = if vm.env.config.fork >= Fork::Prague {
236-
let minimum_gas_consumed = vm.get_min_gas_used(initial_call_frame)?;
235+
let minimum_gas_consumed = vm.get_min_gas_used(vm.current_call_frame()?)?;
237236
exec_gas_consumed.max(minimum_gas_consumed)
238237
} else {
239238
exec_gas_consumed

crates/vm/levm/src/vm.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,9 @@ impl<'a> VM<'a> {
587587
// NOTE: ATTOW the default hook is created in VM::new(), so
588588
// (in theory) _at least_ the default finalize execution should
589589
// run
590-
let call_frame = self
591-
.call_frames
592-
.pop()
593-
.ok_or(VMError::Internal(InternalError::CouldNotPopCallframe))?;
594590
for hook in self.hooks.clone() {
595-
hook.finalize_execution(self, &call_frame, report)?;
591+
hook.finalize_execution(self, report)?;
596592
}
597-
self.call_frames.push(call_frame);
598593

599594
Ok(())
600595
}

0 commit comments

Comments
 (0)