Skip to content

Commit

Permalink
refactor(levm): make increase_consumed_gas a callframe method inste…
Browse files Browse the repository at this point in the history
…ad of a VM method. (#1843)

**Motivation**

`increase_consumed_gas` currently is a VM method. However, it receives a
mutable reference to a Callframe, modifies it and does not use any
information provided by the VM.

**Description**

Make `increase_consumed_gas` a Callframe, thus removing methods from the
VM itself.

Closes #1842

---------

Co-authored-by: fborello-lambda <[email protected]>
  • Loading branch information
lima-limon-inc and fborello-lambda authored Jan 30, 2025
1 parent 8610b4c commit 9b9721f
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 188 deletions.
25 changes: 24 additions & 1 deletion crates/vm/levm/src/call_frame.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
constants::STACK_LIMIT,
errors::{InternalError, VMError},
errors::{InternalError, OutOfGasError, VMError},
memory::Memory,
opcodes::Opcode,
utils::get_valid_jump_destinations,
Expand Down Expand Up @@ -144,4 +144,27 @@ impl CallFrame {
.ok_or(VMError::Internal(InternalError::PCOverflowed))?;
Ok(())
}

pub fn increment_pc(&mut self) -> Result<(), VMError> {
self.increment_pc_by(1)
}

pub fn pc(&self) -> usize {
self.pc
}

/// Increases gas consumption of CallFrame and Environment, returning an error if the callframe gas limit is reached.
pub fn increase_consumed_gas(&mut self, gas: u64) -> Result<(), VMError> {
let potential_consumed_gas = self
.gas_used
.checked_add(gas)
.ok_or(OutOfGasError::ConsumedGasOverflow)?;
if potential_consumed_gas > self.gas_limit {
return Err(VMError::OutOfGas(OutOfGasError::MaxGasLimitExceeded));
}

self.gas_used = potential_consumed_gas;

Ok(())
}
}
4 changes: 2 additions & 2 deletions crates/vm/levm/src/execution_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ impl VM {
Err(VMError::ContractOutputTooBig)
} else if contract_code.first().unwrap_or(&0) == &INVALID_CONTRACT_PREFIX {
Err(VMError::InvalidContractPrefix)
} else if self
.increase_consumed_gas(current_call_frame, code_deposit_cost)
} else if current_call_frame
.increase_consumed_gas(code_deposit_cost)
.is_err()
{
Err(VMError::OutOfGas(OutOfGasError::MaxGasLimitExceeded))
Expand Down
22 changes: 11 additions & 11 deletions crates/vm/levm/src/opcode_handlers/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::bitwise_comparison::checked_shift_right;
impl VM {
// ADD operation
pub fn op_add(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::ADD)?;
current_call_frame.increase_consumed_gas(gas_cost::ADD)?;

let augend = current_call_frame.stack.pop()?;
let addend = current_call_frame.stack.pop()?;
Expand All @@ -27,7 +27,7 @@ impl VM {

// SUB operation
pub fn op_sub(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SUB)?;
current_call_frame.increase_consumed_gas(gas_cost::SUB)?;

let minuend = current_call_frame.stack.pop()?;
let subtrahend = current_call_frame.stack.pop()?;
Expand All @@ -39,7 +39,7 @@ impl VM {

// MUL operation
pub fn op_mul(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::MUL)?;
current_call_frame.increase_consumed_gas(gas_cost::MUL)?;

let multiplicand = current_call_frame.stack.pop()?;
let multiplier = current_call_frame.stack.pop()?;
Expand All @@ -51,7 +51,7 @@ impl VM {

// DIV operation
pub fn op_div(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::DIV)?;
current_call_frame.increase_consumed_gas(gas_cost::DIV)?;

let dividend = current_call_frame.stack.pop()?;
let divisor = current_call_frame.stack.pop()?;
Expand All @@ -66,7 +66,7 @@ impl VM {

// SDIV operation
pub fn op_sdiv(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SDIV)?;
current_call_frame.increase_consumed_gas(gas_cost::SDIV)?;

let dividend = current_call_frame.stack.pop()?;
let divisor = current_call_frame.stack.pop()?;
Expand Down Expand Up @@ -97,7 +97,7 @@ impl VM {

// MOD operation
pub fn op_mod(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::MOD)?;
current_call_frame.increase_consumed_gas(gas_cost::MOD)?;

let dividend = current_call_frame.stack.pop()?;
let divisor = current_call_frame.stack.pop()?;
Expand All @@ -111,7 +111,7 @@ impl VM {

// SMOD operation
pub fn op_smod(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SMOD)?;
current_call_frame.increase_consumed_gas(gas_cost::SMOD)?;

let unchecked_dividend = current_call_frame.stack.pop()?;
let unchecked_divisor = current_call_frame.stack.pop()?;
Expand Down Expand Up @@ -148,7 +148,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::ADDMOD)?;
current_call_frame.increase_consumed_gas(gas_cost::ADDMOD)?;

let augend = current_call_frame.stack.pop()?;
let addend = current_call_frame.stack.pop()?;
Expand Down Expand Up @@ -184,7 +184,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::MULMOD)?;
current_call_frame.increase_consumed_gas(gas_cost::MULMOD)?;

let multiplicand = current_call_frame.stack.pop()?;
let multiplier = current_call_frame.stack.pop()?;
Expand Down Expand Up @@ -223,7 +223,7 @@ impl VM {

let gas_cost = gas_cost::exp(exponent)?;

self.increase_consumed_gas(current_call_frame, gas_cost)?;
current_call_frame.increase_consumed_gas(gas_cost)?;

let power = base.overflowing_pow(exponent).0;
current_call_frame.stack.push(power)?;
Expand All @@ -236,7 +236,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SIGNEXTEND)?;
current_call_frame.increase_consumed_gas(gas_cost::SIGNEXTEND)?;

let byte_size_minus_one = current_call_frame.stack.pop()?;
let value_to_extend = current_call_frame.stack.pop()?;
Expand Down
28 changes: 14 additions & 14 deletions crates/vm/levm/src/opcode_handlers/bitwise_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ethrex_core::U256;
impl VM {
// LT operation
pub fn op_lt(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::LT)?;
current_call_frame.increase_consumed_gas(gas_cost::LT)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let result = u256_from_bool(lho < rho);
Expand All @@ -24,7 +24,7 @@ impl VM {

// GT operation
pub fn op_gt(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::GT)?;
current_call_frame.increase_consumed_gas(gas_cost::GT)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let result = u256_from_bool(lho > rho);
Expand All @@ -35,7 +35,7 @@ impl VM {

// SLT operation (signed less than)
pub fn op_slt(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SLT)?;
current_call_frame.increase_consumed_gas(gas_cost::SLT)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let lho_is_negative = lho.bit(255);
Expand All @@ -54,7 +54,7 @@ impl VM {

// SGT operation (signed greater than)
pub fn op_sgt(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SGT)?;
current_call_frame.increase_consumed_gas(gas_cost::SGT)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let lho_is_negative = lho.bit(255);
Expand All @@ -73,7 +73,7 @@ impl VM {

// EQ operation (equality check)
pub fn op_eq(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::EQ)?;
current_call_frame.increase_consumed_gas(gas_cost::EQ)?;
let lho = current_call_frame.stack.pop()?;
let rho = current_call_frame.stack.pop()?;
let result = u256_from_bool(lho == rho);
Expand All @@ -88,7 +88,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::ISZERO)?;
current_call_frame.increase_consumed_gas(gas_cost::ISZERO)?;

let operand = current_call_frame.stack.pop()?;
let result = u256_from_bool(operand.is_zero());
Expand All @@ -100,7 +100,7 @@ impl VM {

// AND operation
pub fn op_and(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::AND)?;
current_call_frame.increase_consumed_gas(gas_cost::AND)?;
let a = current_call_frame.stack.pop()?;
let b = current_call_frame.stack.pop()?;
current_call_frame.stack.push(a & b)?;
Expand All @@ -110,7 +110,7 @@ impl VM {

// OR operation
pub fn op_or(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::OR)?;
current_call_frame.increase_consumed_gas(gas_cost::OR)?;
let a = current_call_frame.stack.pop()?;
let b = current_call_frame.stack.pop()?;
current_call_frame.stack.push(a | b)?;
Expand All @@ -120,7 +120,7 @@ impl VM {

// XOR operation
pub fn op_xor(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::XOR)?;
current_call_frame.increase_consumed_gas(gas_cost::XOR)?;
let a = current_call_frame.stack.pop()?;
let b = current_call_frame.stack.pop()?;
current_call_frame.stack.push(a ^ b)?;
Expand All @@ -130,7 +130,7 @@ impl VM {

// NOT operation
pub fn op_not(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::NOT)?;
current_call_frame.increase_consumed_gas(gas_cost::NOT)?;
let a = current_call_frame.stack.pop()?;
current_call_frame.stack.push(!a)?;

Expand All @@ -139,7 +139,7 @@ impl VM {

// BYTE operation
pub fn op_byte(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::BYTE)?;
current_call_frame.increase_consumed_gas(gas_cost::BYTE)?;
let op1 = current_call_frame.stack.pop()?;
let op2 = current_call_frame.stack.pop()?;
let byte_index = match op1.try_into() {
Expand Down Expand Up @@ -173,7 +173,7 @@ impl VM {

// SHL operation (shift left)
pub fn op_shl(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SHL)?;
current_call_frame.increase_consumed_gas(gas_cost::SHL)?;
let shift = current_call_frame.stack.pop()?;
let value = current_call_frame.stack.pop()?;

Expand All @@ -190,7 +190,7 @@ impl VM {

// SHR operation (shift right)
pub fn op_shr(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SHR)?;
current_call_frame.increase_consumed_gas(gas_cost::SHR)?;
let shift = current_call_frame.stack.pop()?;
let value = current_call_frame.stack.pop()?;

Expand All @@ -207,7 +207,7 @@ impl VM {

// SAR operation (arithmetic shift right)
pub fn op_sar(&mut self, current_call_frame: &mut CallFrame) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SAR)?;
current_call_frame.increase_consumed_gas(gas_cost::SAR)?;
let shift = current_call_frame.stack.pop()?;
let value = current_call_frame.stack.pop()?;
let res = if shift < U256::from(256) {
Expand Down
22 changes: 11 additions & 11 deletions crates/vm/levm/src/opcode_handlers/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::BLOCKHASH)?;
current_call_frame.increase_consumed_gas(gas_cost::BLOCKHASH)?;

let block_number = current_call_frame.stack.pop()?;

Expand Down Expand Up @@ -56,7 +56,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::COINBASE)?;
current_call_frame.increase_consumed_gas(gas_cost::COINBASE)?;

current_call_frame
.stack
Expand All @@ -70,7 +70,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::TIMESTAMP)?;
current_call_frame.increase_consumed_gas(gas_cost::TIMESTAMP)?;

current_call_frame.stack.push(self.env.timestamp)?;

Expand All @@ -82,7 +82,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::NUMBER)?;
current_call_frame.increase_consumed_gas(gas_cost::NUMBER)?;

current_call_frame.stack.push(self.env.block_number)?;

Expand All @@ -94,7 +94,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::PREVRANDAO)?;
current_call_frame.increase_consumed_gas(gas_cost::PREVRANDAO)?;

let randao = self.env.prev_randao.unwrap_or_default(); // Assuming block_env has been integrated
current_call_frame
Expand All @@ -109,7 +109,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::GASLIMIT)?;
current_call_frame.increase_consumed_gas(gas_cost::GASLIMIT)?;

current_call_frame
.stack
Expand All @@ -123,7 +123,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::CHAINID)?;
current_call_frame.increase_consumed_gas(gas_cost::CHAINID)?;

current_call_frame.stack.push(self.env.chain_id)?;

Expand All @@ -135,7 +135,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::SELFBALANCE)?;
current_call_frame.increase_consumed_gas(gas_cost::SELFBALANCE)?;

let balance = get_account(&mut self.cache, &self.db, current_call_frame.to)
.info
Expand All @@ -150,7 +150,7 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeResult, VMError> {
self.increase_consumed_gas(current_call_frame, gas_cost::BASEFEE)?;
current_call_frame.increase_consumed_gas(gas_cost::BASEFEE)?;

current_call_frame.stack.push(self.env.base_fee_per_gas)?;

Expand All @@ -168,7 +168,7 @@ impl VM {
return Err(VMError::InvalidOpcode);
}

self.increase_consumed_gas(current_call_frame, gas_cost::BLOBHASH)?;
current_call_frame.increase_consumed_gas(gas_cost::BLOBHASH)?;

let index = current_call_frame.stack.pop()?;

Expand Down Expand Up @@ -214,7 +214,7 @@ impl VM {
if self.env.fork < Fork::Cancun {
return Err(VMError::InvalidOpcode);
}
self.increase_consumed_gas(current_call_frame, gas_cost::BLOBBASEFEE)?;
current_call_frame.increase_consumed_gas(gas_cost::BLOBBASEFEE)?;

let blob_base_fee = self.get_blob_gasprice()?;

Expand Down
2 changes: 1 addition & 1 deletion crates/vm/levm/src/opcode_handlers/dup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl VM {
depth: usize,
) -> Result<OpcodeResult, VMError> {
// Increase the consumed gas
self.increase_consumed_gas(current_call_frame, gas_cost::DUPN)?;
current_call_frame.increase_consumed_gas(gas_cost::DUPN)?;

// Ensure the stack has enough elements to duplicate
if current_call_frame.stack.len() < depth {
Expand Down
Loading

0 comments on commit 9b9721f

Please sign in to comment.