Skip to content

Commit

Permalink
refactor(levm): gas cost in return and revert (#1571)
Browse files Browse the repository at this point in the history
**Motivation**

The gas costs are calculated inside the opcode function, we should
extract it to the `gas_costs` modules to keep consistency.

**Description**

- Create `gas_cost::exit_opcode()` function, to calculate the gas cost
of `op_return` and `op_revert`.

Closes #1431
  • Loading branch information
damiramirez authored Dec 27, 2024
1 parent bc8152f commit b6aeef1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions crates/vm/levm/src/gas_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ pub fn codecopy(
)
}

// Used in return and revert opcodes
pub fn exit_opcode(new_memory_size: usize, current_memory_size: usize) -> Result<u64, VMError> {
memory::expansion_cost(new_memory_size, current_memory_size)
}

pub fn returndatacopy(
new_memory_size: usize,
current_memory_size: usize,
Expand Down
18 changes: 10 additions & 8 deletions crates/vm/levm/src/opcode_handlers/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ impl VM {
}

let new_memory_size = calculate_memory_size(offset, size)?;
let current_memory_size = current_call_frame.memory.len();

let memory_expansion_cost =
memory::expansion_cost(new_memory_size, current_call_frame.memory.len())?;

self.increase_consumed_gas(current_call_frame, memory_expansion_cost)?;
self.increase_consumed_gas(
current_call_frame,
gas_cost::exit_opcode(new_memory_size, current_memory_size)?,
)?;

current_call_frame.output =
memory::load_range(&mut current_call_frame.memory, offset, size)?
Expand Down Expand Up @@ -405,11 +406,12 @@ impl VM {
.map_err(|_err| VMError::VeryLargeNumber)?;

let new_memory_size = calculate_memory_size(offset, size)?;
let current_memory_size = current_call_frame.memory.len();

let memory_expansion_cost: u64 =
memory::expansion_cost(new_memory_size, current_call_frame.memory.len())?;

self.increase_consumed_gas(current_call_frame, memory_expansion_cost)?;
self.increase_consumed_gas(
current_call_frame,
gas_cost::exit_opcode(new_memory_size, current_memory_size)?,
)?;

current_call_frame.output =
memory::load_range(&mut current_call_frame.memory, offset, size)?
Expand Down

0 comments on commit b6aeef1

Please sign in to comment.