From 378fa531595c290b757dea14443bc2a4f8565064 Mon Sep 17 00:00:00 2001 From: Maximo Palopoli <96491141+maximopalopoli@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:04:04 -0300 Subject: [PATCH] fix(levm): fix gas refunds (#1410) **Motivation** The gas refunds had a bug in the sstore calculation. Fixes all the tests from `stShift` folder. **Description** Also, this PR sums the refunds to the sender's balance. --- .../levm/src/opcode_handlers/stack_memory_storage_flow.rs | 2 +- crates/vm/levm/src/vm.rs | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs b/crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs index c58e66dd2..e0d5f6ada 100644 --- a/crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs +++ b/crates/vm/levm/src/opcode_handlers/stack_memory_storage_flow.rs @@ -178,7 +178,7 @@ impl VM { let mut gas_refunds = U256::zero(); 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() { + if !storage_slot.original_value.is_zero() && new_storage_slot_value.is_zero() { gas_refunds = gas_refunds .checked_add(U256::from(4800)) .ok_or(VMError::GasRefundsOverflow)?; diff --git a/crates/vm/levm/src/vm.rs b/crates/vm/levm/src/vm.rs index 7a2a58a43..63e7cc5ce 100644 --- a/crates/vm/levm/src/vm.rs +++ b/crates/vm/levm/src/vm.rs @@ -626,6 +626,12 @@ impl VM { .checked_mul(self.env.gas_price) .ok_or(VMError::GasLimitPriceProductOverflow)?, )?; + self.increase_account_balance( + sender, + U256::from(report.gas_refunded) + .checked_mul(self.env.gas_price) + .ok_or(VMError::GasLimitPriceProductOverflow)?, + )?; // Send coinbase fee let priority_fee_per_gas = self