Skip to content

Commit aaf3de4

Browse files
DiegoCivifmoletta
authored andcommitted
refactor(levm): use more descriptive names when popping call_frame (#2730)
**Motivation** Give a better description of call_frame related variables in run_execution(). **Description** - Use `executed_call_frame` in scenarios in which the callframe has already been executed. - Use `parent_call_frame` in scenarios in which a callframe has been popped before, to we are working with the previous one. <!-- Link to issues: Resolves #111, Resolves #222 --> Closes #2569
1 parent d48b627 commit aaf3de4

File tree

3 files changed

+44
-44
lines changed

3 files changed

+44
-44
lines changed

crates/vm/levm/src/execution_handlers.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,17 @@ impl<'a> VM<'a> {
150150

151151
pub fn handle_opcode_result(
152152
&mut self,
153-
current_call_frame: &mut CallFrame,
153+
executed_call_frame: &mut CallFrame,
154154
) -> Result<ExecutionReport, VMError> {
155155
let backup = self
156156
.backups
157157
.pop()
158158
.ok_or(VMError::Internal(InternalError::CouldNotPopCallframe))?;
159159
// On successful create check output validity
160-
if (self.is_create() && current_call_frame.depth == 0)
161-
|| current_call_frame.create_op_called
160+
if (self.is_create() && executed_call_frame.depth == 0)
161+
|| executed_call_frame.create_op_called
162162
{
163-
let contract_code = std::mem::take(&mut current_call_frame.output);
163+
let contract_code = std::mem::take(&mut executed_call_frame.output);
164164
let code_length = contract_code.len();
165165

166166
let code_length_u64: u64 = code_length
@@ -185,13 +185,13 @@ impl<'a> VM<'a> {
185185
.is_some_and(|val| val == &INVALID_CONTRACT_PREFIX)
186186
{
187187
Err(VMError::InvalidContractPrefix)
188-
} else if current_call_frame
188+
} else if executed_call_frame
189189
.increase_consumed_gas(code_deposit_cost)
190190
.is_err()
191191
{
192192
Err(VMError::OutOfGas(OutOfGasError::MaxGasLimitExceeded))
193193
} else {
194-
Ok(current_call_frame.to)
194+
Ok(executed_call_frame.to)
195195
};
196196

197197
match validate_create {
@@ -201,14 +201,14 @@ impl<'a> VM<'a> {
201201
}
202202
Err(error) => {
203203
// Revert if error
204-
current_call_frame.gas_used = current_call_frame.gas_limit;
205-
self.restore_state(backup, current_call_frame.call_frame_backup.clone())?;
204+
executed_call_frame.gas_used = executed_call_frame.gas_limit;
205+
self.restore_state(backup, executed_call_frame.call_frame_backup.clone())?;
206206

207207
return Ok(ExecutionReport {
208208
result: TxResult::Revert(error),
209-
gas_used: current_call_frame.gas_used,
209+
gas_used: executed_call_frame.gas_used,
210210
gas_refunded: self.env.refunded_gas,
211-
output: std::mem::take(&mut current_call_frame.output),
211+
output: std::mem::take(&mut executed_call_frame.output),
212212
logs: vec![],
213213
});
214214
}
@@ -217,17 +217,17 @@ impl<'a> VM<'a> {
217217

218218
Ok(ExecutionReport {
219219
result: TxResult::Success,
220-
gas_used: current_call_frame.gas_used,
220+
gas_used: executed_call_frame.gas_used,
221221
gas_refunded: self.env.refunded_gas,
222-
output: std::mem::take(&mut current_call_frame.output),
223-
logs: std::mem::take(&mut current_call_frame.logs),
222+
output: std::mem::take(&mut executed_call_frame.output),
223+
logs: std::mem::take(&mut executed_call_frame.logs),
224224
})
225225
}
226226

227227
pub fn handle_opcode_error(
228228
&mut self,
229229
error: VMError,
230-
current_call_frame: &mut CallFrame,
230+
executed_call_frame: &mut CallFrame,
231231
) -> Result<ExecutionReport, VMError> {
232232
let backup = self
233233
.backups
@@ -239,17 +239,17 @@ impl<'a> VM<'a> {
239239

240240
// Unless error is from Revert opcode, all gas is consumed
241241
if error != VMError::RevertOpcode {
242-
let left_gas = current_call_frame
242+
let left_gas = executed_call_frame
243243
.gas_limit
244-
.saturating_sub(current_call_frame.gas_used);
245-
current_call_frame.gas_used = current_call_frame.gas_used.saturating_add(left_gas);
244+
.saturating_sub(executed_call_frame.gas_used);
245+
executed_call_frame.gas_used = executed_call_frame.gas_used.saturating_add(left_gas);
246246
}
247247

248248
let refunded = backup.refunded_gas;
249-
let output = std::mem::take(&mut current_call_frame.output); // Bytes::new() if error is not RevertOpcode
250-
let gas_used = current_call_frame.gas_used;
249+
let output = std::mem::take(&mut executed_call_frame.output); // Bytes::new() if error is not RevertOpcode
250+
let gas_used = executed_call_frame.gas_used;
251251

252-
self.restore_state(backup, current_call_frame.call_frame_backup.clone())?;
252+
self.restore_state(backup, executed_call_frame.call_frame_backup.clone())?;
253253

254254
Ok(ExecutionReport {
255255
result: TxResult::Revert(error),

crates/vm/levm/src/opcode_handlers/system.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -863,50 +863,50 @@ impl<'a> VM<'a> {
863863

864864
pub fn handle_return(
865865
&mut self,
866-
call_frame: &CallFrame,
866+
executed_call_frame: &CallFrame,
867867
tx_report: &ExecutionReport,
868868
) -> Result<bool, VMError> {
869-
if call_frame.depth == 0 {
870-
self.call_frames.push(call_frame.clone());
869+
if executed_call_frame.depth == 0 {
870+
self.call_frames.push(executed_call_frame.clone());
871871
return Ok(false);
872872
}
873873
let retdata = self
874874
.return_data
875875
.pop()
876876
.ok_or(VMError::Internal(InternalError::CouldNotPopCallframe))?;
877877
if retdata.is_create {
878-
self.handle_return_create(call_frame, tx_report, retdata)?;
878+
self.handle_return_create(executed_call_frame, tx_report, retdata)?;
879879
} else {
880-
self.handle_return_call(call_frame, tx_report, retdata)?;
880+
self.handle_return_call(executed_call_frame, tx_report, retdata)?;
881881
}
882882
Ok(true)
883883
}
884884
pub fn handle_return_call(
885885
&mut self,
886-
call_frame: &CallFrame,
886+
executed_call_frame: &CallFrame,
887887
tx_report: &ExecutionReport,
888888
retdata: RetData,
889889
) -> Result<(), VMError> {
890890
// Return gas left from subcontext
891-
let gas_left_from_new_call_frame = call_frame
891+
let gas_left_from_new_call_frame = executed_call_frame
892892
.gas_limit
893893
.checked_sub(tx_report.gas_used)
894894
.ok_or(InternalError::GasOverflow)?;
895895
{
896-
let current_call_frame = self.current_call_frame_mut()?;
897-
current_call_frame.gas_used = current_call_frame
896+
let parent_call_frame = self.current_call_frame_mut()?;
897+
parent_call_frame.gas_used = parent_call_frame
898898
.gas_used
899899
.checked_sub(gas_left_from_new_call_frame)
900900
.ok_or(InternalError::GasOverflow)?;
901901

902-
current_call_frame.logs.extend(tx_report.logs.clone());
902+
parent_call_frame.logs.extend(tx_report.logs.clone());
903903
memory::try_store_range(
904-
&mut current_call_frame.memory,
904+
&mut parent_call_frame.memory,
905905
retdata.ret_offset,
906906
retdata.ret_size,
907907
&tx_report.output,
908908
)?;
909-
current_call_frame.sub_return_data = tx_report.output.clone();
909+
parent_call_frame.sub_return_data = tx_report.output.clone();
910910
}
911911

912912
// What to do, depending on TxResult
@@ -915,7 +915,7 @@ impl<'a> VM<'a> {
915915
self.current_call_frame_mut()?
916916
.stack
917917
.push(SUCCESS_FOR_CALL)?;
918-
self.merge_call_frame_backup_with_parent(&call_frame.call_frame_backup)?;
918+
self.merge_call_frame_backup_with_parent(&executed_call_frame.call_frame_backup)?;
919919
}
920920
TxResult::Revert(_) => {
921921
// Revert value transfer
@@ -932,7 +932,7 @@ impl<'a> VM<'a> {
932932
}
933933
pub fn handle_return_create(
934934
&mut self,
935-
call_frame: &CallFrame,
935+
executed_call_frame: &CallFrame,
936936
tx_report: &ExecutionReport,
937937
retdata: RetData,
938938
) -> Result<(), VMError> {
@@ -942,22 +942,22 @@ impl<'a> VM<'a> {
942942
.ok_or(InternalError::GasOverflow)?;
943943

944944
{
945-
let current_call_frame = self.current_call_frame_mut()?;
945+
let parent_call_frame = self.current_call_frame_mut()?;
946946
// Return reserved gas
947-
current_call_frame.gas_used = current_call_frame
947+
parent_call_frame.gas_used = parent_call_frame
948948
.gas_used
949949
.checked_sub(unused_gas)
950950
.ok_or(InternalError::GasOverflow)?;
951951

952-
current_call_frame.logs.extend(tx_report.logs.clone());
952+
parent_call_frame.logs.extend(tx_report.logs.clone());
953953
}
954954

955955
match tx_report.result.clone() {
956956
TxResult::Success => {
957957
self.current_call_frame_mut()?
958958
.stack
959959
.push(address_to_word(retdata.to))?;
960-
self.merge_call_frame_backup_with_parent(&call_frame.call_frame_backup)?;
960+
self.merge_call_frame_backup_with_parent(&executed_call_frame.call_frame_backup)?;
961961
}
962962
TxResult::Revert(err) => {
963963
// Return value to sender

crates/vm/levm/src/vm.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,24 +329,24 @@ impl<'a> VM<'a> {
329329
.current_call_frame_mut()?
330330
.increment_pc_by(pc_increment)?,
331331
Ok(OpcodeResult::Halt) => {
332-
let mut current_call_frame = self
332+
let mut executed_call_frame = self
333333
.call_frames
334334
.pop()
335335
.ok_or(VMError::Internal(InternalError::CouldNotPopCallframe))?;
336-
let report = self.handle_opcode_result(&mut current_call_frame)?;
337-
if self.handle_return(&current_call_frame, &report)? {
336+
let report = self.handle_opcode_result(&mut executed_call_frame)?;
337+
if self.handle_return(&executed_call_frame, &report)? {
338338
self.current_call_frame_mut()?.increment_pc_by(1)?;
339339
} else {
340340
return Ok(report);
341341
}
342342
}
343343
Err(error) => {
344-
let mut current_call_frame = self
344+
let mut executed_call_frame = self
345345
.call_frames
346346
.pop()
347347
.ok_or(VMError::Internal(InternalError::CouldNotPopCallframe))?;
348-
let report = self.handle_opcode_error(error, &mut current_call_frame)?;
349-
if self.handle_return(&current_call_frame, &report)? {
348+
let report = self.handle_opcode_error(error, &mut executed_call_frame)?;
349+
if self.handle_return(&executed_call_frame, &report)? {
350350
self.current_call_frame_mut()?.increment_pc_by(1)?;
351351
} else {
352352
return Ok(report);

0 commit comments

Comments
 (0)