Skip to content

Commit

Permalink
fix(levm): change initial value of gas refund in sstore (#1530)
Browse files Browse the repository at this point in the history
**Motivation**

There was a problem with the refunded gas. It was not aligned correctly
in the subcontexts.

**Description**

- Use the gas refund that we have in the `env` as the value in `sstore`.
- Update `self.env.refunded_gas` with the new `gas_refunds` value.

With this change, we prevent losing track of gas refunds across
different contexts.
  • Loading branch information
damiramirez authored Dec 19, 2024
1 parent 4b7c965 commit e3620b0
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ impl VM {
)?;

// Gas Refunds
// TODO: Think about what to do in case of underflow of gas refunds (when we try to substract from it if the value is low)
let mut gas_refunds = U256::zero();
// Sync gas refund with global env, ensuring consistency accross contexts.
let mut gas_refunds = self.env.refunded_gas;
if new_storage_slot_value != storage_slot.current_value {
if storage_slot.current_value == storage_slot.original_value {
if !storage_slot.original_value.is_zero() && new_storage_slot_value.is_zero() {
Expand Down Expand Up @@ -210,14 +210,9 @@ impl VM {
}
};

self.env.refunded_gas = self
.env
.refunded_gas
.checked_add(gas_refunds)
.ok_or(VMError::GasLimitPriceProductOverflow)?;
self.env.refunded_gas = gas_refunds;

self.update_account_storage(current_call_frame.to, key, new_storage_slot_value)?;

Ok(OpcodeSuccess::Continue)
}

Expand Down

0 comments on commit e3620b0

Please sign in to comment.