Skip to content

Commit

Permalink
Implement delegatecall gas consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
maximopalopoli committed Nov 8, 2024
1 parent bcd5ff8 commit 18bf85c
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions crates/vm/levm/src/opcode_handlers/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ impl VM {
}

// DELEGATECALL operation
// TODO: https://github.com/lambdaclass/lambda_ethereum_rust/issues/1086
// TODO: add tests
pub fn op_delegatecall(
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<OpcodeSuccess, VMError> {
let gas = current_call_frame.stack.pop()?;
let code_address = word_to_address(current_call_frame.stack.pop()?);
let args_offset = current_call_frame
let args_offset: usize = current_call_frame
.stack
.pop()?
.try_into()
Expand All @@ -236,7 +236,7 @@ impl VM {
.pop()?
.try_into()
.map_err(|_err| VMError::VeryLargeNumber)?;
let ret_offset = current_call_frame
let ret_offset: usize = current_call_frame
.stack
.pop()?
.try_into()
Expand All @@ -252,6 +252,30 @@ impl VM {
let to = current_call_frame.to;
let is_static = current_call_frame.is_static;

// Gas consumed
let memory_byte_size = args_offset
.checked_add(args_size)
.and_then(|src_sum| {
ret_offset
.checked_add(ret_size)
.map(|dest_sum| src_sum.max(dest_sum))
})
.ok_or(VMError::OverflowInArithmeticOp)?;
let memory_expansion_cost = current_call_frame.memory.expansion_cost(memory_byte_size)?;

let access_cost = if self.cache.is_account_cached(&code_address) {
WARM_ADDRESS_ACCESS_COST
} else {
self.cache_from_db(&code_address);
COLD_ADDRESS_ACCESS_COST
};

let gas_cost = memory_expansion_cost
.checked_add(access_cost)
.ok_or(VMError::GasCostOverflow)?;

self.increase_consumed_gas(current_call_frame, gas_cost)?;

self.generic_call(
current_call_frame,
gas,
Expand Down

0 comments on commit 18bf85c

Please sign in to comment.