diff --git a/specs/opcode/54SLOAD_55SSTORE.md b/specs/opcode/54SLOAD_55SSTORE.md index 02cf50e2b..24deba2e3 100644 --- a/specs/opcode/54SLOAD_55SSTORE.md +++ b/specs/opcode/54SLOAD_55SSTORE.md @@ -23,12 +23,12 @@ - 2 stack operations - 1 storage reads - 1 access_list write - - `SSTORE`: +11 + - `SSTORE`: +10 - 4 call_context read - 2 stack operations - 2 storage reads/writes - 1 access_list write - - 2 gas_refund reads/writes + - 1 gas_refund reads/writes - stack_pointer - `SLOAD`: remains the same - `SSTORE`: -2 @@ -79,7 +79,7 @@ - `value` is pushed on top of the stack - storage: The 32 bytes of `value` are read from storage at `key` - access_list: Write as `true` for `key` - - `SSTORE`: 11 busmapping lookups + - `SSTORE`: 10 busmapping lookups - call_context: - `tx_id`: Read the `tx_id` for this tx. - `rw_counter_end_of_reversion`: Read the `rw_counter_end` if this tx get reverted. @@ -93,7 +93,6 @@ - The 32 bytes of new `value` are written to storage at `key` - access_list: Write as `true` for `key` - gas_refund: - - Read the accumulated gas_refund for this tx - Write the new accumulated gas_refund for this tx ## Exceptions diff --git a/src/zkevm_specs/evm/execution/storage.py b/src/zkevm_specs/evm/execution/storage.py index 4253449be..42492e6bc 100644 --- a/src/zkevm_specs/evm/execution/storage.py +++ b/src/zkevm_specs/evm/execution/storage.py @@ -66,23 +66,23 @@ def sstore(instruction: Instruction): tx_id, callee_address, storage_key, is_persistent, rw_counter_end_of_reversion ) - gas_refund = instruction.tx_refund_read(tx_id) + gas_refund, gas_refund_prev = instruction.tx_refund_write_with_reversion(tx_id, is_persistent, rw_counter_end_of_reversion) + new_gas_refund = gas_refund_prev if current_value != new_value: if original_value == current_value: if original_value != 0 and new_value == 0: - gas_refund = gas_refund + SSTORE_CLEARS_SCHEDULE + new_gas_refund = new_gas_refund + SSTORE_CLEARS_SCHEDULE else: if original_value != 0: if current_value == 0: - gas_refund = gas_refund - SSTORE_CLEARS_SCHEDULE + new_gas_refund = new_gas_refund - SSTORE_CLEARS_SCHEDULE if new_value == 0: - gas_refund = gas_refund + SSTORE_CLEARS_SCHEDULE + new_gas_refund = new_gas_refund + SSTORE_CLEARS_SCHEDULE if original_value == new_value: if original_value == 0: - gas_refund = gas_refund + SSTORE_SET_GAS - SLOAD_GAS + new_gas_refund = new_gas_refund + SSTORE_SET_GAS - SLOAD_GAS else: - gas_refund = gas_refund + SSTORE_RESET_GAS - SLOAD_GAS - new_gas_refund, _ = instruction.tx_refund_write_with_reversion(tx_id, is_persistent, rw_counter_end_of_reversion) + new_gas_refund = new_gas_refund + SSTORE_RESET_GAS - SLOAD_GAS instruction.constrain_equal(gas_refund, new_gas_refund) # TODO: Use intrinsic gas (EIP 2028, 2930) @@ -101,7 +101,7 @@ def sstore(instruction: Instruction): instruction.step_state_transition_in_same_context( opcode, - rw_counter=Transition.delta(11), + rw_counter=Transition.delta(10), program_counter=Transition.delta(1), stack_pointer=Transition.delta(2), state_write_counter=Transition.delta(3), diff --git a/tests/evm/test_sstore.py b/tests/evm/test_sstore.py index 11c73981e..7b6de1276 100644 --- a/tests/evm/test_sstore.py +++ b/tests/evm/test_sstore.py @@ -142,7 +142,7 @@ def test_sstore( 1, CallContextFieldTag.RwCounterEndOfReversion, 0, - 0 if result else 16, + 0 if result else 15, 0, 0, 0, @@ -198,15 +198,14 @@ def test_sstore( 0, 0, ), - (10, RW.Read, RWTableTag.TxRefund, tx.id, 0, 0, old_gas_refund, 0, 0, 0), - (11, RW.Write, RWTableTag.TxRefund, tx.id, 0, 0, gas_refund, old_gas_refund, 0, 0), + (10, RW.Write, RWTableTag.TxRefund, tx.id, 0, 0, gas_refund, old_gas_refund, 0, 0), ] + ( [] if result else [ ( - 14, + 13, RW.Write, RWTableTag.TxRefund, tx.id, @@ -218,7 +217,7 @@ def test_sstore( 0, ), ( - 15, + 14, RW.Write, RWTableTag.TxAccessListAccountStorage, tx.id, @@ -230,7 +229,7 @@ def test_sstore( 0, ), ( - 16, + 15, RW.Write, RWTableTag.AccountStorage, tx.callee_address, @@ -264,7 +263,7 @@ def test_sstore( ), StepState( execution_state=ExecutionState.STOP if result else ExecutionState.REVERT, - rw_counter=12, + rw_counter=11, call_id=1, is_root=True, is_create=False,