Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions specs/opcode/54SLOAD_55SSTORE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/zkevm_specs/evm/execution/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
Expand Down
13 changes: 6 additions & 7 deletions tests/evm/test_sstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_sstore(
1,
CallContextFieldTag.RwCounterEndOfReversion,
0,
0 if result else 16,
0 if result else 15,
0,
0,
0,
Expand Down Expand Up @@ -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,
Expand All @@ -218,7 +217,7 @@ def test_sstore(
0,
),
(
15,
14,
RW.Write,
RWTableTag.TxAccessListAccountStorage,
tx.id,
Expand All @@ -230,7 +229,7 @@ def test_sstore(
0,
),
(
16,
15,
RW.Write,
RWTableTag.AccountStorage,
tx.callee_address,
Expand Down Expand Up @@ -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,
Expand Down