From 1b0a6f1062306991755a65faade4fd60d1e11be4 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Mon, 24 Jan 2022 17:19:01 +0800 Subject: [PATCH 1/6] update --- specs/opcode/54SLOAD_55SSTORE.md | 91 ++++++++ src/zkevm_specs/evm/execution/__init__.py | 2 + src/zkevm_specs/evm/execution/gas.py | 6 + src/zkevm_specs/evm/execution/storage.py | 107 +++++++++ tests/evm/test_sload.py | 171 ++++++++++++++ tests/evm/test_sstore.py | 269 ++++++++++++++++++++++ 6 files changed, 646 insertions(+) create mode 100644 specs/opcode/54SLOAD_55SSTORE.md create mode 100644 src/zkevm_specs/evm/execution/gas.py create mode 100644 src/zkevm_specs/evm/execution/storage.py create mode 100644 tests/evm/test_sload.py create mode 100644 tests/evm/test_sstore.py diff --git a/specs/opcode/54SLOAD_55SSTORE.md b/specs/opcode/54SLOAD_55SSTORE.md new file mode 100644 index 000000000..c498fa63c --- /dev/null +++ b/specs/opcode/54SLOAD_55SSTORE.md @@ -0,0 +1,91 @@ +# SLOAD & SSTORE op code + +## Variables definition + +| Name | Value | +| - | - | +| COLD_SLOAD_COST | 2100 | +| WARM_STORAGE_READ_COST | 100 | +| SLOAD_GAS | 100 | +| SSTORE_SET_GAS | 20000 | +| SSTORE_RESET_GAS | 2900 | +| SSTORE_CLEARS_SCHEDULE | 15000 | + +## Constraints + +1. opcodeId checks + 1. opId === OpcodeId(0x54) for `SLOAD` + 2. opId === OpcodeId(0x55) for `SSTORE` +2. state transition: + - gc + - `SLOAD`: +5 (2 stack operations + 1 storage reads + 2 access_list reads/writes) + - `SSTORE`: +8 + + 2 stack operations + + 2 storage reads/writes + + 2 access_list reads/writes + + 2 gas_refund reads/writes + - stack_pointer + - `SLOAD`: remains the same + - `SSTORE`: -2 + - pc + 1 + - state_write_counter + - `SLOAD`: +1 (access_list) + - `SSTORE`: +3 (for storage, access_list & gas_refund respectively) + - gas: + - `SLOAD`: + + the accessed address is warm: gas + WARM_STORAGE_READ_COST + + the accessed address is cold: gas + COLD_SLOAD_COST + - `SSTORE`: + + the accessed address is warm: + * `current_value == new_value`: gas + SLOAD_GAS + * `current_value != new_value`: + - `original_value == current_value`: + - `original_value == 0`: gas + SSTORE_SET_GAS + - `original_value != 0`: gas + SSTORE_RESET_GAS + - `original_value != current_value`: gas + SLOAD_GAS + + the accessed address is cold: + * `current_value == new_value`: gas + SLOAD_GAS + COLD_SLOAD_COST + * `current_value != new_value`: + - `original_value == current_value`: + - `original_value == 0`: gas + SSTORE_SET_GAS + COLD_SLOAD_COST + - `original_value != 0`: gas + SSTORE_RESET_GAS + COLD_SLOAD_COST + - `original_value != current_value`: gas + SLOAD_GAS + COLD_SLOAD_COST + * gas_refund: + - `SSTORE`: + + `current_value != new_value`: + * `original_value == current_value`: + * `original_value != 0` && `new_value == 0`: gas_refund + SSTORE_CLEARS_SCHEDULE + * `original_value != current_value`: + * `original_value != 0`: + - `current_value == 0`: gas_refund - SSTORE_CLEARS_SCHEDULE + - `new_value == 0`: gas_refund + SSTORE_CLEARS_SCHEDULE + * `original_value == new_value`: + - `original_value == 0`: gas_refund + SSTORE_SET_GAS - SLOAD_GAS + - `original_value != 0`: gas_refund + SSTORE_RESET_GAS - SLOAD_GAS +3. lookups: + - `SLOAD`: 5 busmapping lookups + - stack: + - `address` is popped off the top of the stack + - `value` is pushed on top of the stack + - storage: The 32 bytes of `value` are read from storage at `address` + - access_list: Whether the address is warm (accessed before), mark as warm afterward + - `SSTORE`: 8 busmapping lookups + - stack: + - `address` is popped off the top of the stack + - `value` is popped off the top of the stack + - storage: + - Read the orignal_value and the current_value at `address` + - The 32 bytes of new `value` are written to storage at `address` + - access_list: Whether the address is warm (accessed before), mark as warm afterward + - gas_refund: + + Read the accumulated gas_refund for this tx + + Write the new accumulated gas_refund for this tx + +## Exceptions + +1. gas out: remaining gas is not enough +2. stack underflow: + - the stack is empty: `1024 == stack_pointer` + - only for `SSTORE`: contains a single value: `1023 == stack_pointer` +3. context error + - only for `SSTORE`: the current execution context is from a `STATICCALL` (since Byzantium fork). diff --git a/src/zkevm_specs/evm/execution/__init__.py b/src/zkevm_specs/evm/execution/__init__.py index 9a8e3deab..6c4eecdfa 100644 --- a/src/zkevm_specs/evm/execution/__init__.py +++ b/src/zkevm_specs/evm/execution/__init__.py @@ -16,6 +16,8 @@ from .slt_sgt import * from .callvalue import * from .calldatasize import * +from .gas import * +from .storage import * EXECUTION_STATE_IMPL: Dict[ExecutionState, Callable] = { diff --git a/src/zkevm_specs/evm/execution/gas.py b/src/zkevm_specs/evm/execution/gas.py new file mode 100644 index 000000000..6904f7db3 --- /dev/null +++ b/src/zkevm_specs/evm/execution/gas.py @@ -0,0 +1,6 @@ +COLD_SLOAD_COST = 2100 +WARM_STORAGE_READ_COST = 100 +SLOAD_GAS = 100 +SSTORE_SET_GAS = 20000 +SSTORE_RESET_GAS = 2900 +SSTORE_CLEARS_SCHEDULE = 15000 diff --git a/src/zkevm_specs/evm/execution/storage.py b/src/zkevm_specs/evm/execution/storage.py new file mode 100644 index 000000000..0573ffbb8 --- /dev/null +++ b/src/zkevm_specs/evm/execution/storage.py @@ -0,0 +1,107 @@ +from ..instruction import Instruction, Transition +from ..opcode import Opcode +from ..table import CallContextFieldTag, TxContextFieldTag +from .gas import ( + COLD_SLOAD_COST, + WARM_STORAGE_READ_COST, + SLOAD_GAS, + SSTORE_SET_GAS, + SSTORE_RESET_GAS, + SSTORE_CLEARS_SCHEDULE, +) + + +def sload(instruction: Instruction): + opcode = instruction.opcode_lookup(True) + instruction.constrain_equal(opcode, Opcode.SLOAD) + + tx_id = instruction.call_context_lookup(CallContextFieldTag.TxId) + rw_counter_end_of_reversion = instruction.call_context_lookup(CallContextFieldTag.RWCounterEndOfReversion) + is_persistent = instruction.call_context_lookup(CallContextFieldTag.IsPersistent) + callee_address = instruction.tx_lookup(tx_id, TxContextFieldTag.CalleeAddress) + + storage_slot = instruction.stack_pop() + warm, _ = instruction.access_list_storage_slot_read(tx_id, callee_address, storage_slot) + + # TODO: Use intrinsic gas (EIP 2028, 2930) + dynamic_gas_cost = WARM_STORAGE_READ_COST if warm else COLD_SLOAD_COST + + instruction.storage_slot_read(callee_address, storage_slot) + instruction.add_storage_slot_to_access_list_with_reversion( + tx_id, callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion + ) + instruction.stack_push() + + instruction.constrain_same_context_state_transition( + opcode, + rw_counter=Transition.delta(5), + program_counter=Transition.delta(1), + stack_pointer=Transition.delta(0), + state_write_counter=Transition.delta(1), + dynamic_gas_cost=dynamic_gas_cost, + ) + + +def sstore(instruction: Instruction): + opcode = instruction.opcode_lookup(True) + instruction.constrain_equal(opcode, Opcode.SSTORE) + + tx_id = instruction.call_context_lookup(CallContextFieldTag.TxId) + rw_counter_end_of_reversion = instruction.call_context_lookup(CallContextFieldTag.RWCounterEndOfReversion) + is_persistent = instruction.call_context_lookup(CallContextFieldTag.IsPersistent) + callee_address = instruction.tx_lookup(tx_id, TxContextFieldTag.CalleeAddress) + + storage_slot = instruction.stack_pop() + new_value = instruction.stack_pop() + warm, _ = instruction.access_list_storage_slot_read(tx_id, callee_address, storage_slot) + current_value, _, txid, original_value = instruction.storage_slot_read(callee_address, storage_slot) + instruction.constrain_equal(tx_id, txid) + + # TODO: Use intrinsic gas (EIP 2028, 2930) + if current_value == new_value: + dynamic_gas_cost = SLOAD_GAS + else: + if original_value == current_value: + if original_value == 0: + dynamic_gas_cost = SSTORE_SET_GAS + else: + dynamic_gas_cost = SSTORE_RESET_GAS + else: + dynamic_gas_cost = SLOAD_GAS + if not warm: + dynamic_gas_cost = dynamic_gas_cost + COLD_SLOAD_COST + + gas_refund, _ = instruction.gas_refund_read(tx_id) + 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 + else: + if original_value != 0: + if current_value == 0: + gas_refund = gas_refund - SSTORE_CLEARS_SCHEDULE + if new_value == 0: + gas_refund = gas_refund + SSTORE_CLEARS_SCHEDULE + if original_value == new_value: + if original_value == 0: + gas_refund = gas_refund + SSTORE_SET_GAS - SLOAD_GAS + else: + gas_refund = gas_refund + SSTORE_RESET_GAS - SLOAD_GAS + + instruction.storage_slot_write_with_reversion( + callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion + ) + instruction.add_storage_slot_to_access_list_with_reversion( + tx_id, callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion + ) + new_gas_refund, _ = instruction.gas_refund_write_with_reversion(tx_id, is_persistent, rw_counter_end_of_reversion) + instruction.constrain_equal(gas_refund, new_gas_refund) + + instruction.constrain_same_context_state_transition( + opcode, + rw_counter=Transition.delta(8), + program_counter=Transition.delta(1), + stack_pointer=Transition.delta(2), + state_write_counter=Transition.delta(3), + dynamic_gas_cost=dynamic_gas_cost, + ) diff --git a/tests/evm/test_sload.py b/tests/evm/test_sload.py new file mode 100644 index 000000000..ab55cfd4e --- /dev/null +++ b/tests/evm/test_sload.py @@ -0,0 +1,171 @@ +import pytest + +from zkevm_specs.evm import ( + ExecutionState, + StepState, + verify_steps, + Tables, + RWTableTag, + RW, + CallContextFieldTag, + Transaction, + Block, + Bytecode, +) +from zkevm_specs.evm.execution.params import COLD_SLOAD_COST, WARM_STORAGE_READ_COST +from zkevm_specs.util import rand_fp, rand_address, RLC + +TESTING_DATA = ( + ( + Transaction(caller_address=rand_address(), callee_address=rand_address()), + bytes([i for i in range(32, 0, -1)]), + False, + True, + ), + ( + Transaction(caller_address=rand_address(), callee_address=rand_address()), + bytes([i for i in range(32, 0, -1)]), + True, + True, + ), + ( + Transaction(caller_address=rand_address(), callee_address=rand_address()), + bytes([i for i in range(32, 0, -1)]), + False, + False, + ), + ( + Transaction(caller_address=rand_address(), callee_address=rand_address()), + bytes([i for i in range(32, 0, -1)]), + True, + False, + ), +) + + +@pytest.mark.parametrize("tx, slot_be_bytes, warm, result", TESTING_DATA) +def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): + randomness = rand_fp() + + storage_slot = RLC(bytes(reversed(slot_be_bytes)), randomness) + + block = Block() + + # PUSH32 STORAGE_SLOT SLOAD STOP + bytecode = Bytecode(f"7f{slot_be_bytes.hex()}5400") + bytecode_hash = RLC(bytecode.hash(), randomness) + + value = 2 + value_prev = 0 + value_committed = 0 + + tables = Tables( + block_table=set(block.table_assignments(rlc_store)), + tx_table=set(tx.table_assignments(rlc_store)), + bytecode_table=set(bytecode.table_assignments(rlc_store)), + rw_table=set( + [ + (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0), + ( + 10, + RW.Read, + RWTableTag.CallContext, + 1, + CallContextFieldTag.RWCounterEndOfReversion, + 0, + 0 if result else 19, + 0, + 0, + 0, + ), + (11, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsPersistent, 0, result, 0, 0, 0), + (12, RW.Read, RWTableTag.Stack, 1, 1023, 0, storage_slot, 0, 0, 0), + ( + 13, + RW.Read, + RWTableTag.TxAccessListStorageSlot, + tx.id, + tx.callee_address, + storage_slot, + 1 if warm else 0, + 0, + 0, + 0, + ), + ( + 14, + RW.Read, + RWTableTag.AccountStorage, + tx.callee_address, + storage_slot, + 0, + value, + value_prev, + tx.id, + value_committed, + ), + ( + 15, + RW.Write, + RWTableTag.TxAccessListStorageSlot, + tx.id, + tx.callee_address, + storage_slot, + 1, + 1 if warm else 0, + 0, + 0, + ), + (16, RW.Write, RWTableTag.Stack, 1, 1023, 0, value, 0, 0, 0), + ] + + ( + [] + if result + else [ + ( + 19, + RW.Write, + RWTableTag.TxAccessListStorageSlot, + tx.id, + tx.callee_address, + storage_slot, + 1 if warm else 0, + 1, + 0, + 0, + ), + ] + ) + ), + ) + + verify_steps( + rlc_store=rlc_store, + tables=tables, + steps=[ + StepState( + execution_state=ExecutionState.SLOAD, + rw_counter=9, + call_id=1, + is_root=True, + is_create=False, + opcode_source=bytecode_hash, + program_counter=33, + stack_pointer=1023, + state_write_counter=0, + gas_left=WARM_STORAGE_READ_COST if warm else COLD_SLOAD_COST, + ), + StepState( + execution_state=ExecutionState.STOP if result else ExecutionState.REVERT, + rw_counter=14, + call_id=1, + is_root=True, + is_create=False, + opcode_source=bytecode_hash, + program_counter=34, + stack_pointer=1023, + state_write_counter=1, + gas_left=0, + ), + ], + ) diff --git a/tests/evm/test_sstore.py b/tests/evm/test_sstore.py new file mode 100644 index 000000000..a7ccec3a4 --- /dev/null +++ b/tests/evm/test_sstore.py @@ -0,0 +1,269 @@ +import pytest + +from zkevm_specs.evm import ( + ExecutionState, + StepState, + verify_steps, + Tables, + RWTableTag, + RW, + CallContextFieldTag, + Transaction, + Block, + Bytecode, +) +from zkevm_specs.evm.execution.params import ( + COLD_SLOAD_COST, + WARM_STORAGE_READ_COST, + SLOAD_GAS, + SSTORE_SET_GAS, + SSTORE_RESET_GAS, + SSTORE_CLEARS_SCHEDULE, +) +from zkevm_specs.util import rand_fp, rand_address, RLC + + +def gen_test_cases(): + value_cases = [ + [bytes([i for i in range(0, 32, 1)]), 0, -1], # value_prev == value + # "value_prev != value, original_value == value_prev, original_value == 0" case is skipped because inconvenient to generate for now + [ + bytes([i for i in range(0, 32, 1)]), + -1, + -1, + ], # value_prev != value, original_value == value_prev, original_value != 0 + [bytes([i for i in range(0, 32, 1)]), -1, -2], # value_prev != value, original_value != value_prev + [ + bytes([i for i in range(0, 32, 1)]), + -1, + 0, + ], # value_prev != value, original_value != value_prev, value == original_value + ] + warm_cases = [False, True] + persist_cases = [True, False] + + test_cases = [] + for value_case in value_cases: + for warm_case in warm_cases: + for persist_case in persist_cases: + test_cases.append( + ( + Transaction(caller_address=rand_address(), callee_address=rand_address()), # tx + bytes([i for i in range(32, 0, -1)]), # storage_slot + value_case[0], # new_value + value_case[1], # value_prev_diff + value_case[2], # original_value_diff + warm_case, # is_warm_storage_slot + persist_case, # is_not_reverted + ) + ) + return test_cases + + +TESTING_DATA = gen_test_cases() + + +@pytest.mark.parametrize( + "tx, slot_be_bytes, value_be_bytes, value_prev_diff, original_value_diff, warm, result", TESTING_DATA +) +def test_sstore( + tx: Transaction, + slot_be_bytes: bytes, + value_be_bytes: bytes, + value_prev_diff: int, + original_value_diff: int, + warm: bool, + result: bool, +): + randomness = rand_fp() + + storage_slot = RLC(bytes(reversed(slot_be_bytes)), randomness) + value = RLC(bytes(reversed(value_be_bytes)), randomness) + value_prev = value + value_prev_diff + original_value = value + original_value_diff + + block = Block() + + # PUSH32 STORAGE_SLOT PUSH32 VALUE SSTORE STOP + bytecode = Bytecode(f"7f{slot_be_bytes.hex()}7f{value_be_bytes.hex()}5500") + bytecode_hash = RLC(bytecode.hash(), randomness) + + if value_prev == value: + expected_gas_cost = SLOAD_GAS + else: + if original_value == value_prev: + if original_value == 0: + expected_gas_cost = SSTORE_SET_GAS + else: + expected_gas_cost = SSTORE_RESET_GAS + else: + expected_gas_cost = SLOAD_GAS + if not warm: + expected_gas_cost = expected_gas_cost + COLD_SLOAD_COST + + old_gas_refund = 15000 + gas_refund = old_gas_refund + if value_prev != value: + if original_value == value_prev: + if original_value != 0 and value == 0: + gas_refund = gas_refund + SSTORE_CLEARS_SCHEDULE + else: + if original_value != 0: + if value_prev == 0: + gas_refund = gas_refund - SSTORE_CLEARS_SCHEDULE + if value == 0: + gas_refund = gas_refund + SSTORE_CLEARS_SCHEDULE + if original_value == value: + if original_value == 0: + gas_refund = gas_refund + SSTORE_SET_GAS - SLOAD_GAS + else: + gas_refund = gas_refund + SSTORE_RESET_GAS - SLOAD_GAS + + tables = Tables( + block_table=set(block.table_assignments(rlc_store)), + tx_table=set(tx.table_assignments(rlc_store)), + bytecode_table=set(bytecode.table_assignments(rlc_store)), + rw_table=set( + [ + (1, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0), + ( + 2, + RW.Read, + RWTableTag.CallContext, + 1, + CallContextFieldTag.RWCounterEndOfReversion, + 0, + 0 if result else 16, + 0, + 0, + 0, + ), + (3, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsPersistent, 0, result, 0, 0, 0), + (4, RW.Read, RWTableTag.Stack, 1, 1022, 0, storage_slot, 0, 0, 0), + (5, RW.Read, RWTableTag.Stack, 1, 1023, 0, value, 0, 0, 0), + ( + 6, + RW.Read, + RWTableTag.TxAccessListStorageSlot, + tx.id, + tx.callee_address, + storage_slot, + 1 if warm else 0, + 0, + 0, + 0, + ), + ( + 7, + RW.Read, + RWTableTag.AccountStorage, + tx.callee_address, + storage_slot, + 0, + value_prev, + original_value, + tx.id, + original_value, + ), + (8, RW.Read, RWTableTag.TxRefund, tx.id, 0, 0, old_gas_refund, 0, 0, 0), + ( + 9, + RW.Write, + RWTableTag.AccountStorage, + tx.callee_address, + storage_slot, + 0, + value, + value_prev, + tx.id, + original_value, + ), + ( + 10, + RW.Write, + RWTableTag.TxAccessListStorageSlot, + tx.id, + tx.callee_address, + storage_slot, + 1, + 1 if warm else 0, + 0, + 0, + ), + (11, RW.Write, RWTableTag.TxRefund, tx.id, 0, 0, gas_refund, old_gas_refund, 0, 0), + ] + + ( + [] + if result + else [ + ( + 14, + RW.Write, + RWTableTag.TxRefund, + tx.id, + 0, + 0, + old_gas_refund, + gas_refund, + 0, + 0, + ), + ( + 15, + RW.Write, + RWTableTag.TxAccessListStorageSlot, + tx.id, + tx.callee_address, + storage_slot, + 1 if warm else 0, + 1, + 0, + 0, + ), + ( + 16, + RW.Write, + RWTableTag.AccountStorage, + tx.callee_address, + storage_slot, + 0, + value_prev, + value, + tx.id, + original_value, + ), + ] + ) + ), + ) + + verify_steps( + rlc_store=rlc_store, + tables=tables, + steps=[ + StepState( + execution_state=ExecutionState.SSTORE, + rw_counter=1, + call_id=1, + is_root=True, + is_create=False, + opcode_source=bytecode_hash, + program_counter=66, + stack_pointer=1022, + state_write_counter=0, + gas_left=expected_gas_cost, + ), + StepState( + execution_state=ExecutionState.STOP if result else ExecutionState.REVERT, + rw_counter=9, + call_id=1, + is_root=True, + is_create=False, + opcode_source=bytecode_hash, + program_counter=67, + stack_pointer=1024, + state_write_counter=3, + gas_left=0, + ), + ], + ) From cbf96cb063a1d62f84f29e3972e9a3d3d34d8f39 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Mon, 24 Jan 2022 17:29:52 +0800 Subject: [PATCH 2/6] fix test_sload --- src/zkevm_specs/evm/execution/__init__.py | 2 ++ src/zkevm_specs/evm/execution/storage.py | 30 +++++++++++------------ src/zkevm_specs/evm/instruction.py | 17 +++++++++++++ tests/evm/test_sload.py | 24 +++++++++--------- tests/evm/test_sstore.py | 14 +++++------ 5 files changed, 52 insertions(+), 35 deletions(-) diff --git a/src/zkevm_specs/evm/execution/__init__.py b/src/zkevm_specs/evm/execution/__init__.py index 6c4eecdfa..f6fe4958f 100644 --- a/src/zkevm_specs/evm/execution/__init__.py +++ b/src/zkevm_specs/evm/execution/__init__.py @@ -33,4 +33,6 @@ ExecutionState.JUMPI: jumpi, ExecutionState.PUSH: push, ExecutionState.SCMP: scmp, + ExecutionState.SLOAD: sload, + ExecutionState.SSTORE: sstore, } diff --git a/src/zkevm_specs/evm/execution/storage.py b/src/zkevm_specs/evm/execution/storage.py index 0573ffbb8..deeb9204f 100644 --- a/src/zkevm_specs/evm/execution/storage.py +++ b/src/zkevm_specs/evm/execution/storage.py @@ -16,23 +16,23 @@ def sload(instruction: Instruction): instruction.constrain_equal(opcode, Opcode.SLOAD) tx_id = instruction.call_context_lookup(CallContextFieldTag.TxId) - rw_counter_end_of_reversion = instruction.call_context_lookup(CallContextFieldTag.RWCounterEndOfReversion) + rw_counter_end_of_reversion = instruction.call_context_lookup(CallContextFieldTag.RwCounterEndOfReversion) is_persistent = instruction.call_context_lookup(CallContextFieldTag.IsPersistent) - callee_address = instruction.tx_lookup(tx_id, TxContextFieldTag.CalleeAddress) + tx_callee_address = instruction.tx_context_lookup(tx_id, TxContextFieldTag.CalleeAddress) storage_slot = instruction.stack_pop() - warm, _ = instruction.access_list_storage_slot_read(tx_id, callee_address, storage_slot) + warm, _ = instruction.access_list_account_storage_read(tx_id, tx_callee_address, storage_slot) # TODO: Use intrinsic gas (EIP 2028, 2930) dynamic_gas_cost = WARM_STORAGE_READ_COST if warm else COLD_SLOAD_COST - instruction.storage_slot_read(callee_address, storage_slot) - instruction.add_storage_slot_to_access_list_with_reversion( - tx_id, callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion + instruction.account_storage_read(tx_callee_address, storage_slot) + instruction.add_account_storage_to_access_list_with_reversion( + tx_id, tx_callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion ) instruction.stack_push() - instruction.constrain_same_context_state_transition( + instruction.step_state_transition_in_same_context( opcode, rw_counter=Transition.delta(5), program_counter=Transition.delta(1), @@ -47,14 +47,14 @@ def sstore(instruction: Instruction): instruction.constrain_equal(opcode, Opcode.SSTORE) tx_id = instruction.call_context_lookup(CallContextFieldTag.TxId) - rw_counter_end_of_reversion = instruction.call_context_lookup(CallContextFieldTag.RWCounterEndOfReversion) + rw_counter_end_of_reversion = instruction.call_context_lookup(CallContextFieldTag.RwCounterEndOfReversion) is_persistent = instruction.call_context_lookup(CallContextFieldTag.IsPersistent) - callee_address = instruction.tx_lookup(tx_id, TxContextFieldTag.CalleeAddress) + tx_callee_address = instruction.tx_context_lookup(tx_id, TxContextFieldTag.CalleeAddress) storage_slot = instruction.stack_pop() new_value = instruction.stack_pop() - warm, _ = instruction.access_list_storage_slot_read(tx_id, callee_address, storage_slot) - current_value, _, txid, original_value = instruction.storage_slot_read(callee_address, storage_slot) + warm, _ = instruction.access_list_account_storage_read(tx_id, tx_callee_address, storage_slot) + current_value, _, txid, original_value = instruction.account_storage_read(tx_callee_address, storage_slot) instruction.constrain_equal(tx_id, txid) # TODO: Use intrinsic gas (EIP 2028, 2930) @@ -89,15 +89,15 @@ def sstore(instruction: Instruction): gas_refund = gas_refund + SSTORE_RESET_GAS - SLOAD_GAS instruction.storage_slot_write_with_reversion( - callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion + tx_callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion ) - instruction.add_storage_slot_to_access_list_with_reversion( - tx_id, callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion + instruction.add_account_storage_to_access_list_with_reversion( + tx_id, tx_callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion ) new_gas_refund, _ = instruction.gas_refund_write_with_reversion(tx_id, is_persistent, rw_counter_end_of_reversion) instruction.constrain_equal(gas_refund, new_gas_refund) - instruction.constrain_same_context_state_transition( + instruction.step_state_transition_in_same_context( opcode, rw_counter=Transition.delta(8), program_counter=Transition.delta(1), diff --git a/src/zkevm_specs/evm/instruction.py b/src/zkevm_specs/evm/instruction.py index d9a570682..9d10dceff 100644 --- a/src/zkevm_specs/evm/instruction.py +++ b/src/zkevm_specs/evm/instruction.py @@ -508,6 +508,10 @@ def sub_balance_with_reversion( self.constrain_zero(carry) return balance, balance_prev + def account_storage_read(self, account_address: int, storage_key: int) -> Tuple[int, int]: + row = self.rw_lookup(RW.Read, RWTableTag.AccountStorage, [account_address, storage_key, 0]) + return row[-4], row[-3] + def add_account_to_access_list( self, tx_id: int, @@ -537,6 +541,19 @@ def add_account_to_access_list_with_reversion( ) return row[-4] - row[-3] + def access_list_account_storage_read( + self, + tx_id: int, + account_address: int, + storage_key: int, + ) -> Tuple[int, int]: + row = self.rw_lookup( + RW.Read, + RWTableTag.TxAccessListAccountStorage, + [tx_id, account_address, storage_key], + ) + return row[-4], row[-3] + def add_account_storage_to_access_list( self, tx_id: int, diff --git a/tests/evm/test_sload.py b/tests/evm/test_sload.py index ab55cfd4e..2bd0535a4 100644 --- a/tests/evm/test_sload.py +++ b/tests/evm/test_sload.py @@ -49,10 +49,8 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): storage_slot = RLC(bytes(reversed(slot_be_bytes)), randomness) - block = Block() - # PUSH32 STORAGE_SLOT SLOAD STOP - bytecode = Bytecode(f"7f{slot_be_bytes.hex()}5400") + bytecode = Bytecode().push32(slot_be_bytes).sload().stop() bytecode_hash = RLC(bytecode.hash(), randomness) value = 2 @@ -60,9 +58,9 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): value_committed = 0 tables = Tables( - block_table=set(block.table_assignments(rlc_store)), - tx_table=set(tx.table_assignments(rlc_store)), - bytecode_table=set(bytecode.table_assignments(rlc_store)), + block_table=set(Block().table_assignments(randomness)), + tx_table=set(tx.table_assignments(randomness)), + bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0), @@ -71,7 +69,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): RW.Read, RWTableTag.CallContext, 1, - CallContextFieldTag.RWCounterEndOfReversion, + CallContextFieldTag.RwCounterEndOfReversion, 0, 0 if result else 19, 0, @@ -83,7 +81,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): ( 13, RW.Read, - RWTableTag.TxAccessListStorageSlot, + RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, storage_slot, @@ -107,7 +105,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): ( 15, RW.Write, - RWTableTag.TxAccessListStorageSlot, + RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, storage_slot, @@ -125,7 +123,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): ( 19, RW.Write, - RWTableTag.TxAccessListStorageSlot, + RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, storage_slot, @@ -140,7 +138,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): ) verify_steps( - rlc_store=rlc_store, + randomness=randomness, tables=tables, steps=[ StepState( @@ -149,7 +147,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): call_id=1, is_root=True, is_create=False, - opcode_source=bytecode_hash, + code_source=bytecode_hash, program_counter=33, stack_pointer=1023, state_write_counter=0, @@ -161,7 +159,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): call_id=1, is_root=True, is_create=False, - opcode_source=bytecode_hash, + code_source=bytecode_hash, program_counter=34, stack_pointer=1023, state_write_counter=1, diff --git a/tests/evm/test_sstore.py b/tests/evm/test_sstore.py index a7ccec3a4..5c5b13b28 100644 --- a/tests/evm/test_sstore.py +++ b/tests/evm/test_sstore.py @@ -131,7 +131,7 @@ def test_sstore( RW.Read, RWTableTag.CallContext, 1, - CallContextFieldTag.RWCounterEndOfReversion, + CallContextFieldTag.RwCounterEndOfReversion, 0, 0 if result else 16, 0, @@ -144,7 +144,7 @@ def test_sstore( ( 6, RW.Read, - RWTableTag.TxAccessListStorageSlot, + RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, storage_slot, @@ -181,7 +181,7 @@ def test_sstore( ( 10, RW.Write, - RWTableTag.TxAccessListStorageSlot, + RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, storage_slot, @@ -211,7 +211,7 @@ def test_sstore( ( 15, RW.Write, - RWTableTag.TxAccessListStorageSlot, + RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, storage_slot, @@ -238,7 +238,7 @@ def test_sstore( ) verify_steps( - rlc_store=rlc_store, + randomness=randomness, tables=tables, steps=[ StepState( @@ -247,7 +247,7 @@ def test_sstore( call_id=1, is_root=True, is_create=False, - opcode_source=bytecode_hash, + code_source=bytecode_hash, program_counter=66, stack_pointer=1022, state_write_counter=0, @@ -259,7 +259,7 @@ def test_sstore( call_id=1, is_root=True, is_create=False, - opcode_source=bytecode_hash, + code_source=bytecode_hash, program_counter=67, stack_pointer=1024, state_write_counter=3, From 77208d09ddc1f26daaff0a16dcbdf94124059016 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Mon, 24 Jan 2022 21:54:56 +0800 Subject: [PATCH 3/6] t# This is a combination of 2 commits. fix test_sstore --- src/zkevm_specs/evm/execution/storage.py | 6 +-- src/zkevm_specs/evm/instruction.py | 35 ++++++++++++++++- tests/evm/test_sload.py | 1 - tests/evm/test_sstore.py | 49 ++++++++++++++---------- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/zkevm_specs/evm/execution/storage.py b/src/zkevm_specs/evm/execution/storage.py index deeb9204f..4793fc305 100644 --- a/src/zkevm_specs/evm/execution/storage.py +++ b/src/zkevm_specs/evm/execution/storage.py @@ -71,7 +71,7 @@ def sstore(instruction: Instruction): if not warm: dynamic_gas_cost = dynamic_gas_cost + COLD_SLOAD_COST - gas_refund, _ = instruction.gas_refund_read(tx_id) + gas_refund = instruction.tx_refund_read(tx_id) if current_value != new_value: if original_value == current_value: if original_value != 0 and new_value == 0: @@ -88,13 +88,13 @@ def sstore(instruction: Instruction): else: gas_refund = gas_refund + SSTORE_RESET_GAS - SLOAD_GAS - instruction.storage_slot_write_with_reversion( + instruction.account_storage_write_with_reversion( tx_callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion ) instruction.add_account_storage_to_access_list_with_reversion( tx_id, tx_callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion ) - new_gas_refund, _ = instruction.gas_refund_write_with_reversion(tx_id, is_persistent, rw_counter_end_of_reversion) + new_gas_refund, _ = instruction.tx_refund_write_with_reversion(tx_id, is_persistent, rw_counter_end_of_reversion) instruction.constrain_equal(gas_refund, new_gas_refund) instruction.step_state_transition_in_same_context( diff --git a/src/zkevm_specs/evm/instruction.py b/src/zkevm_specs/evm/instruction.py index 9d10dceff..e4d0fa746 100644 --- a/src/zkevm_specs/evm/instruction.py +++ b/src/zkevm_specs/evm/instruction.py @@ -429,6 +429,22 @@ def tx_refund_read(self, tx_id) -> int: row = self.rw_lookup(RW.Read, RWTableTag.TxRefund, [tx_id]) return row[-4] + def tx_refund_write_with_reversion( + self, + tx_id: int, + is_persistent: bool, + rw_counter_end_of_reversion: int, + state_write_counter: Optional[int] = None, + ) -> Tuple[int, int]: + row = self.state_write_with_reversion( + RWTableTag.TxRefund, + [tx_id], + is_persistent, + rw_counter_end_of_reversion, + state_write_counter, + ) + return row[-4], row[-3] + def account_read(self, account_address: int, account_field_tag: AccountFieldTag) -> int: row = self.rw_lookup(RW.Read, RWTableTag.Account, [account_address, account_field_tag]) return row[-4] @@ -508,8 +524,25 @@ def sub_balance_with_reversion( self.constrain_zero(carry) return balance, balance_prev - def account_storage_read(self, account_address: int, storage_key: int) -> Tuple[int, int]: + def account_storage_read(self, account_address: int, storage_key: int) -> Tuple[int, int, int, int]: row = self.rw_lookup(RW.Read, RWTableTag.AccountStorage, [account_address, storage_key, 0]) + return row[-4], row[-3], row[-2], row[-1] + + def account_storage_write_with_reversion( + self, + account_address: int, + storage_key: int, + is_persistent: bool, + rw_counter_end_of_reversion: int, + state_write_counter: Optional[int] = None, + ) -> Tuple[int, int]: + row = self.state_write_with_reversion( + RWTableTag.AccountStorage, + [account_address, storage_key], + is_persistent, + rw_counter_end_of_reversion, + state_write_counter, + ) return row[-4], row[-3] def add_account_to_access_list( diff --git a/tests/evm/test_sload.py b/tests/evm/test_sload.py index 2bd0535a4..c14934ad9 100644 --- a/tests/evm/test_sload.py +++ b/tests/evm/test_sload.py @@ -49,7 +49,6 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): storage_slot = RLC(bytes(reversed(slot_be_bytes)), randomness) - # PUSH32 STORAGE_SLOT SLOAD STOP bytecode = Bytecode().push32(slot_be_bytes).sload().stop() bytecode_hash = RLC(bytecode.hash(), randomness) diff --git a/tests/evm/test_sstore.py b/tests/evm/test_sstore.py index 5c5b13b28..cf95364af 100644 --- a/tests/evm/test_sstore.py +++ b/tests/evm/test_sstore.py @@ -25,18 +25,30 @@ def gen_test_cases(): value_cases = [ - [bytes([i for i in range(0, 32, 1)]), 0, -1], # value_prev == value - # "value_prev != value, original_value == value_prev, original_value == 0" case is skipped because inconvenient to generate for now [ bytes([i for i in range(0, 32, 1)]), - -1, - -1, + bytes([i for i in range(0, 32, 1)]), + bytes([i for i in range(0, 32, 1)]), + ], # value_prev == value + [ + bytes([1]), + bytes([0]), + bytes([0]), + ], # value_prev != value, original_value == value_prev, original_value == 0 + [ + bytes([2]), + bytes([1]), + bytes([1]), ], # value_prev != value, original_value == value_prev, original_value != 0 - [bytes([i for i in range(0, 32, 1)]), -1, -2], # value_prev != value, original_value != value_prev [ - bytes([i for i in range(0, 32, 1)]), - -1, - 0, + bytes([3]), + bytes([2]), + bytes([1]), + ], # value_prev != value, original_value != value_prev + [ + bytes([1]), + bytes([2]), + bytes([1]), ], # value_prev != value, original_value != value_prev, value == original_value ] warm_cases = [False, True] @@ -64,14 +76,14 @@ def gen_test_cases(): @pytest.mark.parametrize( - "tx, slot_be_bytes, value_be_bytes, value_prev_diff, original_value_diff, warm, result", TESTING_DATA + "tx, slot_be_bytes, value_be_bytes, value_prev_be_bytes, original_value_be_bytes, warm, result", TESTING_DATA ) def test_sstore( tx: Transaction, slot_be_bytes: bytes, value_be_bytes: bytes, - value_prev_diff: int, - original_value_diff: int, + value_prev_be_bytes: int, + original_value_be_bytes: int, warm: bool, result: bool, ): @@ -79,13 +91,10 @@ def test_sstore( storage_slot = RLC(bytes(reversed(slot_be_bytes)), randomness) value = RLC(bytes(reversed(value_be_bytes)), randomness) - value_prev = value + value_prev_diff - original_value = value + original_value_diff - - block = Block() + value_prev = RLC(bytes(reversed(value_prev_be_bytes)), randomness) + original_value = RLC(bytes(reversed(original_value_be_bytes)), randomness) - # PUSH32 STORAGE_SLOT PUSH32 VALUE SSTORE STOP - bytecode = Bytecode(f"7f{slot_be_bytes.hex()}7f{value_be_bytes.hex()}5500") + bytecode = Bytecode().push32(slot_be_bytes).push32(value_be_bytes).sstore().stop() bytecode_hash = RLC(bytecode.hash(), randomness) if value_prev == value: @@ -120,9 +129,9 @@ def test_sstore( gas_refund = gas_refund + SSTORE_RESET_GAS - SLOAD_GAS tables = Tables( - block_table=set(block.table_assignments(rlc_store)), - tx_table=set(tx.table_assignments(rlc_store)), - bytecode_table=set(bytecode.table_assignments(rlc_store)), + block_table=set(Block().table_assignments(randomness)), + tx_table=set(tx.table_assignments(randomness)), + bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ (1, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0), From 72e099d6e7a0333607aace471e5e0aee1741fba8 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Tue, 25 Jan 2022 10:38:59 +0800 Subject: [PATCH 4/6] rename --- src/zkevm_specs/evm/execution/storage.py | 18 ++++++++-------- tests/evm/test_sload.py | 18 ++++++++-------- tests/evm/test_sstore.py | 26 ++++++++++++------------ 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/zkevm_specs/evm/execution/storage.py b/src/zkevm_specs/evm/execution/storage.py index 4793fc305..d9bcbec43 100644 --- a/src/zkevm_specs/evm/execution/storage.py +++ b/src/zkevm_specs/evm/execution/storage.py @@ -20,15 +20,15 @@ def sload(instruction: Instruction): is_persistent = instruction.call_context_lookup(CallContextFieldTag.IsPersistent) tx_callee_address = instruction.tx_context_lookup(tx_id, TxContextFieldTag.CalleeAddress) - storage_slot = instruction.stack_pop() - warm, _ = instruction.access_list_account_storage_read(tx_id, tx_callee_address, storage_slot) + storage_key = instruction.stack_pop() + warm, _ = instruction.access_list_account_storage_read(tx_id, tx_callee_address, storage_key) # TODO: Use intrinsic gas (EIP 2028, 2930) dynamic_gas_cost = WARM_STORAGE_READ_COST if warm else COLD_SLOAD_COST - instruction.account_storage_read(tx_callee_address, storage_slot) + instruction.account_storage_read(tx_callee_address, storage_key) instruction.add_account_storage_to_access_list_with_reversion( - tx_id, tx_callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion + tx_id, tx_callee_address, storage_key, is_persistent, rw_counter_end_of_reversion ) instruction.stack_push() @@ -51,10 +51,10 @@ def sstore(instruction: Instruction): is_persistent = instruction.call_context_lookup(CallContextFieldTag.IsPersistent) tx_callee_address = instruction.tx_context_lookup(tx_id, TxContextFieldTag.CalleeAddress) - storage_slot = instruction.stack_pop() + storage_key = instruction.stack_pop() new_value = instruction.stack_pop() - warm, _ = instruction.access_list_account_storage_read(tx_id, tx_callee_address, storage_slot) - current_value, _, txid, original_value = instruction.account_storage_read(tx_callee_address, storage_slot) + warm, _ = instruction.access_list_account_storage_read(tx_id, tx_callee_address, storage_key) + current_value, _, txid, original_value = instruction.account_storage_read(tx_callee_address, storage_key) instruction.constrain_equal(tx_id, txid) # TODO: Use intrinsic gas (EIP 2028, 2930) @@ -89,10 +89,10 @@ def sstore(instruction: Instruction): gas_refund = gas_refund + SSTORE_RESET_GAS - SLOAD_GAS instruction.account_storage_write_with_reversion( - tx_callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion + tx_callee_address, storage_key, is_persistent, rw_counter_end_of_reversion ) instruction.add_account_storage_to_access_list_with_reversion( - tx_id, tx_callee_address, storage_slot, is_persistent, rw_counter_end_of_reversion + tx_id, tx_callee_address, storage_key, is_persistent, rw_counter_end_of_reversion ) new_gas_refund, _ = instruction.tx_refund_write_with_reversion(tx_id, is_persistent, rw_counter_end_of_reversion) instruction.constrain_equal(gas_refund, new_gas_refund) diff --git a/tests/evm/test_sload.py b/tests/evm/test_sload.py index c14934ad9..77bd50c88 100644 --- a/tests/evm/test_sload.py +++ b/tests/evm/test_sload.py @@ -43,13 +43,13 @@ ) -@pytest.mark.parametrize("tx, slot_be_bytes, warm, result", TESTING_DATA) -def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): +@pytest.mark.parametrize("tx, storage_key_be_bytes, warm, result", TESTING_DATA) +def test_sload(tx: Transaction, storage_key_be_bytes: bytes, warm: bool, result: bool): randomness = rand_fp() - storage_slot = RLC(bytes(reversed(slot_be_bytes)), randomness) + storage_key = RLC(bytes(reversed(storage_key_be_bytes)), randomness) - bytecode = Bytecode().push32(slot_be_bytes).sload().stop() + bytecode = Bytecode().push32(storage_key_be_bytes).sload().stop() bytecode_hash = RLC(bytecode.hash(), randomness) value = 2 @@ -76,14 +76,14 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): 0, ), (11, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsPersistent, 0, result, 0, 0, 0), - (12, RW.Read, RWTableTag.Stack, 1, 1023, 0, storage_slot, 0, 0, 0), + (12, RW.Read, RWTableTag.Stack, 1, 1023, 0, storage_key, 0, 0, 0), ( 13, RW.Read, RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, - storage_slot, + storage_key, 1 if warm else 0, 0, 0, @@ -94,7 +94,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): RW.Read, RWTableTag.AccountStorage, tx.callee_address, - storage_slot, + storage_key, 0, value, value_prev, @@ -107,7 +107,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, - storage_slot, + storage_key, 1, 1 if warm else 0, 0, @@ -125,7 +125,7 @@ def test_sload(tx: Transaction, slot_be_bytes: bytes, warm: bool, result: bool): RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, - storage_slot, + storage_key, 1 if warm else 0, 1, 0, diff --git a/tests/evm/test_sstore.py b/tests/evm/test_sstore.py index cf95364af..380cdfe2a 100644 --- a/tests/evm/test_sstore.py +++ b/tests/evm/test_sstore.py @@ -61,11 +61,11 @@ def gen_test_cases(): test_cases.append( ( Transaction(caller_address=rand_address(), callee_address=rand_address()), # tx - bytes([i for i in range(32, 0, -1)]), # storage_slot + bytes([i for i in range(32, 0, -1)]), # storage_key value_case[0], # new_value value_case[1], # value_prev_diff value_case[2], # original_value_diff - warm_case, # is_warm_storage_slot + warm_case, # is_warm_storage_key persist_case, # is_not_reverted ) ) @@ -76,11 +76,11 @@ def gen_test_cases(): @pytest.mark.parametrize( - "tx, slot_be_bytes, value_be_bytes, value_prev_be_bytes, original_value_be_bytes, warm, result", TESTING_DATA + "tx, storage_key_be_bytes, value_be_bytes, value_prev_be_bytes, original_value_be_bytes, warm, result", TESTING_DATA ) def test_sstore( tx: Transaction, - slot_be_bytes: bytes, + storage_key_be_bytes: bytes, value_be_bytes: bytes, value_prev_be_bytes: int, original_value_be_bytes: int, @@ -89,12 +89,12 @@ def test_sstore( ): randomness = rand_fp() - storage_slot = RLC(bytes(reversed(slot_be_bytes)), randomness) + storage_key = RLC(bytes(reversed(storage_key_be_bytes)), randomness) value = RLC(bytes(reversed(value_be_bytes)), randomness) value_prev = RLC(bytes(reversed(value_prev_be_bytes)), randomness) original_value = RLC(bytes(reversed(original_value_be_bytes)), randomness) - bytecode = Bytecode().push32(slot_be_bytes).push32(value_be_bytes).sstore().stop() + bytecode = Bytecode().push32(storage_key_be_bytes).push32(value_be_bytes).sstore().stop() bytecode_hash = RLC(bytecode.hash(), randomness) if value_prev == value: @@ -148,7 +148,7 @@ def test_sstore( 0, ), (3, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsPersistent, 0, result, 0, 0, 0), - (4, RW.Read, RWTableTag.Stack, 1, 1022, 0, storage_slot, 0, 0, 0), + (4, RW.Read, RWTableTag.Stack, 1, 1022, 0, storage_key, 0, 0, 0), (5, RW.Read, RWTableTag.Stack, 1, 1023, 0, value, 0, 0, 0), ( 6, @@ -156,7 +156,7 @@ def test_sstore( RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, - storage_slot, + storage_key, 1 if warm else 0, 0, 0, @@ -167,7 +167,7 @@ def test_sstore( RW.Read, RWTableTag.AccountStorage, tx.callee_address, - storage_slot, + storage_key, 0, value_prev, original_value, @@ -180,7 +180,7 @@ def test_sstore( RW.Write, RWTableTag.AccountStorage, tx.callee_address, - storage_slot, + storage_key, 0, value, value_prev, @@ -193,7 +193,7 @@ def test_sstore( RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, - storage_slot, + storage_key, 1, 1 if warm else 0, 0, @@ -223,7 +223,7 @@ def test_sstore( RWTableTag.TxAccessListAccountStorage, tx.id, tx.callee_address, - storage_slot, + storage_key, 1 if warm else 0, 1, 0, @@ -234,7 +234,7 @@ def test_sstore( RW.Write, RWTableTag.AccountStorage, tx.callee_address, - storage_slot, + storage_key, 0, value_prev, value, From 1e3b596a6dc7327af6171af172b780a938b012e9 Mon Sep 17 00:00:00 2001 From: Scroll Dev Date: Wed, 26 Jan 2022 00:40:01 +0800 Subject: [PATCH 5/6] feat: upgrade RwTable and add aux (#94) * init: increase RwTable size * fix test except begin_tx * fix test_ begin_tx * clean up * lint codes * fix call_context_lookup * fix stack_lookup * update * fix * fix Co-authored-by: HAOYUatHZ Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> --- src/zkevm_specs/evm/instruction.py | 45 +++++++---------- src/zkevm_specs/evm/table.py | 33 ++++++------ src/zkevm_specs/util/typing.py | 1 + tests/evm/test_add.py | 6 +-- tests/evm/test_begin_tx.py | 80 ++++++++++++++++++++++++++---- tests/evm/test_calldatasize.py | 4 +- tests/evm/test_caller.py | 4 +- tests/evm/test_callvalue.py | 4 +- tests/evm/test_coinbase.py | 2 +- tests/evm/test_end_block.py | 2 +- tests/evm/test_end_tx.py | 10 ++-- tests/evm/test_jump.py | 2 +- tests/evm/test_jumpi.py | 8 +-- tests/evm/test_push.py | 2 +- tests/evm/test_slt_sgt.py | 6 +-- 15 files changed, 130 insertions(+), 79 deletions(-) diff --git a/src/zkevm_specs/evm/instruction.py b/src/zkevm_specs/evm/instruction.py index 02527ff10..d9a570682 100644 --- a/src/zkevm_specs/evm/instruction.py +++ b/src/zkevm_specs/evm/instruction.py @@ -4,7 +4,7 @@ from ..util import ( Array4, - Array8, + Array10, RLC, MAX_N_BYTES, N_BYTES_MEMORY_ADDRESS, @@ -349,7 +349,7 @@ def opcode_lookup_at(self, index: int, is_code: bool) -> int: else: return self.bytecode_lookup(self.curr.code_source, index, is_code) - def rw_lookup(self, rw: RW, tag: RWTableTag, inputs: Sequence[int], rw_counter: Optional[int] = None) -> Array8: + def rw_lookup(self, rw: RW, tag: RWTableTag, inputs: Sequence[int], rw_counter: Optional[int] = None) -> Array10: if rw_counter is None: rw_counter = self.curr.rw_counter + self.rw_counter_offset self.rw_counter_offset += 1 @@ -361,13 +361,13 @@ def state_write_only_persistent( tag: RWTableTag, inputs: Sequence[int], is_persistent: bool, - ) -> Array8: + ) -> Array10: assert tag.write_only_persistent() if is_persistent: return self.rw_lookup(RW.Write, tag, inputs) - return 8 * [None] + return 10 * [None] def state_write_with_reversion( self, @@ -376,7 +376,7 @@ def state_write_with_reversion( is_persistent: bool, rw_counter_end_of_reversion: int, state_write_counter: Optional[int] = None, - ) -> Array8: + ) -> Array10: assert tag.write_with_reversion() row = self.rw_lookup(RW.Write, tag, inputs) @@ -390,14 +390,7 @@ def state_write_with_reversion( if not is_persistent: # Swap value and value_prev inputs = list(row[3:]) - if tag == RWTableTag.TxAccessListAccount: - inputs[2], inputs[3] = inputs[3], inputs[2] - elif tag == RWTableTag.TxAccessListAccountStorage: - inputs[3], inputs[4] = inputs[4], inputs[3] - elif tag == RWTableTag.Account: - inputs[2], inputs[3] = inputs[3], inputs[2] - elif tag == RWTableTag.AccountStorage: - inputs[2], inputs[3] = inputs[3], inputs[2] + inputs[-3], inputs[-4] = inputs[-4], inputs[-3] self.rw_lookup(RW.Write, tag, inputs, rw_counter=rw_counter) return row @@ -408,7 +401,7 @@ def call_context_lookup( if call_id is None: call_id = self.curr.call_id - return self.rw_lookup(rw, RWTableTag.CallContext, [call_id, field_tag])[5] + return self.rw_lookup(rw, RWTableTag.CallContext, [call_id, field_tag])[-4] def stack_pop(self) -> Union[int, RLC]: stack_pointer_offset = self.stack_pointer_offset @@ -421,7 +414,7 @@ def stack_push(self) -> Union[int, RLC]: def stack_lookup(self, rw: RW, stack_pointer_offset: int) -> Union[int, RLC]: stack_pointer = self.curr.stack_pointer + stack_pointer_offset - return self.rw_lookup(rw, RWTableTag.Stack, [self.curr.call_id, stack_pointer])[5] + return self.rw_lookup(rw, RWTableTag.Stack, [self.curr.call_id, stack_pointer])[-4] def memory_write(self, memory_address: int, call_id: Optional[int] = None) -> int: return self.memory_lookup(RW.Write, memory_address, call_id) @@ -430,15 +423,15 @@ def memory_lookup(self, rw: RW, memory_address: int, call_id: Optional[int] = No if call_id is None: call_id = self.curr.call_id - return self.rw_lookup(rw, RWTableTag.Memory, [call_id, memory_address])[5] + return self.rw_lookup(rw, RWTableTag.Memory, [call_id, memory_address])[-4] def tx_refund_read(self, tx_id) -> int: row = self.rw_lookup(RW.Read, RWTableTag.TxRefund, [tx_id]) - return row[4] + return row[-4] def account_read(self, account_address: int, account_field_tag: AccountFieldTag) -> int: row = self.rw_lookup(RW.Read, RWTableTag.Account, [account_address, account_field_tag]) - return row[5] + return row[-4] def account_write( self, @@ -450,7 +443,7 @@ def account_write( RWTableTag.Account, [account_address, account_field_tag], ) - return row[5], row[6] + return row[-4], row[-3] def account_write_with_reversion( self, @@ -467,7 +460,7 @@ def account_write_with_reversion( rw_counter_end_of_reversion, state_write_counter, ) - return row[5], row[6] + return row[-4], row[-3] def add_balance(self, account_address: int, values: Sequence[int]) -> Tuple[int, int]: balance, balance_prev = self.account_write(account_address, AccountFieldTag.Balance) @@ -523,9 +516,9 @@ def add_account_to_access_list( row = self.rw_lookup( RW.Write, RWTableTag.TxAccessListAccount, - [tx_id, account_address, 1], + [tx_id, account_address, 0, 1], ) - return row[5] - row[6] + return row[-4] - row[-3] def add_account_to_access_list_with_reversion( self, @@ -537,12 +530,12 @@ def add_account_to_access_list_with_reversion( ) -> bool: row = self.state_write_with_reversion( RWTableTag.TxAccessListAccount, - [tx_id, account_address, 1], + [tx_id, account_address, 0, 1], is_persistent, rw_counter_end_of_reversion, state_write_counter, ) - return row[5] - row[6] + return row[-4] - row[-3] def add_account_storage_to_access_list( self, @@ -555,7 +548,7 @@ def add_account_storage_to_access_list( RWTableTag.TxAccessListAccountStorage, [tx_id, account_address, storage_key, 1], ) - return row[6] - row[7] + return row[-4] - row[-3] def add_account_storage_to_access_list_with_reversion( self, @@ -573,7 +566,7 @@ def add_account_storage_to_access_list_with_reversion( rw_counter_end_of_reversion, state_write_counter, ) - return row[6] - row[7] + return row[-4] - row[-3] def transfer_with_gas_fee( self, diff --git a/src/zkevm_specs/evm/table.py b/src/zkevm_specs/evm/table.py index 116605a2f..993495e15 100644 --- a/src/zkevm_specs/evm/table.py +++ b/src/zkevm_specs/evm/table.py @@ -3,7 +3,7 @@ from enum import IntEnum, auto from itertools import chain, product -from ..util import Array3, Array4, Array8 +from ..util import Array3, Array4, Array10 from .execution_state import ExecutionState from .opcode import ( invalid_opcodes, @@ -166,15 +166,8 @@ def write_with_reversion(self) -> bool: RWTableTag.TxAccessListAccountStorage, RWTableTag.Account, RWTableTag.AccountStorage, - ] - - # For state writes which don't affect future execution before reversion, we - # don't need to write them with reversion, instead we only need to write - # them (enable the lookup) when is_persistent is True. - def write_only_persistent(self) -> bool: - return self in [ - RWTableTag.TxRefund, RWTableTag.AccountDestructed, + RWTableTag.TxRefund, ] @@ -283,20 +276,22 @@ class Tables: # Each row in RWTable contains: # - rw_counter # - is_write - # - tag - # - value1 - # - value2 - # - value3 - # - value4 - # - value5 - rw_table: Set[Array8] + # - key1 (tag) + # - key2 + # - key3 + # - key4 + # - value + # - value_prev + # - aux1 + # - aux2 + rw_table: Set[Array10] def __init__( self, block_table: Set[Array3], tx_table: Set[Array4], bytecode_table: Set[Array4], - rw_table: Set[Array8], + rw_table: Set[Array10], ) -> None: self.block_table = block_table self.tx_table = tx_table @@ -319,8 +314,8 @@ def bytecode_lookup(self, inputs: Sequence[int]) -> Array4: assert len(inputs) <= 4 return _lookup("bytecode_table", self.bytecode_table, inputs) - def rw_lookup(self, inputs: Sequence[int]) -> Array8: - assert len(inputs) <= 8 + def rw_lookup(self, inputs: Sequence[int]) -> Array10: + assert len(inputs) <= 10 return _lookup("rw_table", self.rw_table, inputs) diff --git a/src/zkevm_specs/util/typing.py b/src/zkevm_specs/util/typing.py index 370f562db..0ab5b8234 100644 --- a/src/zkevm_specs/util/typing.py +++ b/src/zkevm_specs/util/typing.py @@ -8,6 +8,7 @@ Array3 = NewType("Array3", Tuple[int, int, int]) Array4 = NewType("Array4", Tuple[int, int, int, int]) Array8 = NewType("Array8", Tuple[int, int, int, int, int, int, int, int]) +Array10 = NewType("Array10", Tuple[int, int, int, int, int, int, int, int, int, int]) Array32 = NewType( "Array32", Tuple[ diff --git a/tests/evm/test_add.py b/tests/evm/test_add.py index d54a97ddf..da3d369c8 100644 --- a/tests/evm/test_add.py +++ b/tests/evm/test_add.py @@ -40,9 +40,9 @@ def test_add(opcode: Opcode, a: int, b: int, c: Optional[int]): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.Stack, 1, 1022, a, 0, 0), - (10, RW.Read, RWTableTag.Stack, 1, 1023, b, 0, 0), - (11, RW.Write, RWTableTag.Stack, 1, 1023, c, 0, 0), + (9, RW.Read, RWTableTag.Stack, 1, 1022, 0, a, 0, 0, 0), + (10, RW.Read, RWTableTag.Stack, 1, 1023, 0, b, 0, 0, 0), + (11, RW.Write, RWTableTag.Stack, 1, 1023, 0, c, 0, 0, 0), ] ), ) diff --git a/tests/evm/test_begin_tx.py b/tests/evm/test_begin_tx.py index ac199750a..6697256a9 100644 --- a/tests/evm/test_begin_tx.py +++ b/tests/evm/test_begin_tx.py @@ -97,30 +97,45 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): bytecode_table=set(callee.code.table_assignments(randomness)), rw_table=set( [ - (1, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, tx.id, 0, 0), + (1, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0), ( 2, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.RwCounterEndOfReversion, + 0, 0 if result else rw_counter_end_of_reversion, 0, 0, + 0, ), - (3, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsPersistent, result, 0, 0), - (4, RW.Write, RWTableTag.Account, tx.caller_address, AccountFieldTag.Nonce, tx.nonce + 1, tx.nonce, 0), - (5, RW.Write, RWTableTag.TxAccessListAccount, 1, tx.caller_address, 1, 0, 0), - (6, RW.Write, RWTableTag.TxAccessListAccount, 1, tx.callee_address, 1, 0, 0), + (3, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsPersistent, 0, result, 0, 0, 0), + ( + 4, + RW.Write, + RWTableTag.Account, + tx.caller_address, + AccountFieldTag.Nonce, + 0, + tx.nonce + 1, + tx.nonce, + 0, + 0, + ), + (5, RW.Write, RWTableTag.TxAccessListAccount, 1, tx.caller_address, 0, 1, 0, 0, 0), + (6, RW.Write, RWTableTag.TxAccessListAccount, 1, tx.callee_address, 0, 1, 0, 0, 0), ( 7, RW.Write, RWTableTag.Account, tx.caller_address, AccountFieldTag.Balance, + 0, RLC(caller_balance, randomness), RLC(caller_balance_prev, randomness), 0, + 0, ), ( 8, @@ -128,9 +143,11 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): RWTableTag.Account, tx.callee_address, AccountFieldTag.Balance, + 0, RLC(callee_balance, randomness), RLC(callee_balance_prev, randomness), 0, + 0, ), ( 9, @@ -138,26 +155,63 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): RWTableTag.Account, tx.callee_address, AccountFieldTag.CodeHash, + 0, bytecode_hash, bytecode_hash, 0, + 0, + ), + (10, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Depth, 0, 1, 0, 0, 0), + ( + 11, + RW.Read, + RWTableTag.CallContext, + 1, + CallContextFieldTag.CallerAddress, + 0, + tx.caller_address, + 0, + 0, + 0, + ), + ( + 12, + RW.Read, + RWTableTag.CallContext, + 1, + CallContextFieldTag.CalleeAddress, + 0, + tx.callee_address, + 0, + 0, + 0, + ), + (13, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataOffset, 0, 0, 0, 0, 0), + ( + 14, + RW.Read, + RWTableTag.CallContext, + 1, + CallContextFieldTag.CallDataLength, + 0, + len(tx.call_data), + 0, + 0, + 0, ), - (10, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Depth, 1, 0, 0), - (11, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallerAddress, tx.caller_address, 0, 0), - (12, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CalleeAddress, tx.callee_address, 0, 0), - (13, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataOffset, 0, 0, 0), - (14, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataLength, len(tx.call_data), 0, 0), ( 15, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Value, + 0, RLC(tx.value, randomness), 0, 0, + 0, ), - (16, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsStatic, 0, 0, 0), + (16, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.IsStatic, 0, 0, 0, 0, 0), ] + ( [] @@ -169,9 +223,11 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): RWTableTag.Account, tx.callee_address, AccountFieldTag.Balance, + 0, RLC(callee_balance_prev, randomness), RLC(callee_balance, randomness), 0, + 0, ), ( rw_counter_end_of_reversion, @@ -179,9 +235,11 @@ def test_begin_tx(tx: Transaction, callee: Account, result: bool): RWTableTag.Account, tx.caller_address, AccountFieldTag.Balance, + 0, RLC(caller_balance_prev, randomness), RLC(caller_balance, randomness), 0, + 0, ), ] ) diff --git a/tests/evm/test_calldatasize.py b/tests/evm/test_calldatasize.py index 84bbeecac..0f652fee3 100644 --- a/tests/evm/test_calldatasize.py +++ b/tests/evm/test_calldatasize.py @@ -35,8 +35,8 @@ def test_calldatasize(calldatasize: U64): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataLength, calldatasize, 0, 0), - (10, RW.Write, RWTableTag.Stack, 1, 1023, RLC(calldatasize, randomness, N_BYTES_U64), 0, 0), + (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataLength, 0, calldatasize, 0, 0, 0), + (10, RW.Write, RWTableTag.Stack, 1, 1023, 0, RLC(calldatasize, randomness, N_BYTES_U64), 0, 0, 0), ] ), ) diff --git a/tests/evm/test_caller.py b/tests/evm/test_caller.py index 5d25a927d..b04988eac 100644 --- a/tests/evm/test_caller.py +++ b/tests/evm/test_caller.py @@ -36,8 +36,8 @@ def test_caller(caller: U160): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallerAddress, caller, 0, 0), - (10, RW.Write, RWTableTag.Stack, 1, 1023, RLC(caller, randomness, N_BYTES_ACCOUNT_ADDRESS), 0, 0), + (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallerAddress, 0, caller, 0, 0, 0), + (10, RW.Write, RWTableTag.Stack, 1, 1023, 0, RLC(caller, randomness, N_BYTES_ACCOUNT_ADDRESS), 0, 0, 0), ] ), ) diff --git a/tests/evm/test_callvalue.py b/tests/evm/test_callvalue.py index 5e4b619bc..48cf38ba1 100644 --- a/tests/evm/test_callvalue.py +++ b/tests/evm/test_callvalue.py @@ -38,8 +38,8 @@ def test_callvalue(callvalue: U256): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Value, callvalue_rlc, 0, 0), - (10, RW.Write, RWTableTag.Stack, 1, 1023, callvalue_rlc, 0, 0), + (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Value, 0, callvalue_rlc, 0, 0, 0), + (10, RW.Write, RWTableTag.Stack, 1, 1023, 0, callvalue_rlc, 0, 0, 0), ] ), ) diff --git a/tests/evm/test_coinbase.py b/tests/evm/test_coinbase.py index c63506fd0..482753baf 100644 --- a/tests/evm/test_coinbase.py +++ b/tests/evm/test_coinbase.py @@ -31,7 +31,7 @@ def test_coinbase(coinbase: U160): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Write, RWTableTag.Stack, 1, 1023, RLC(coinbase, randomness, 20), 0, 0), + (9, RW.Write, RWTableTag.Stack, 1, 1023, 0, RLC(coinbase, randomness, 20), 0, 0, 0), ] ), ) diff --git a/tests/evm/test_end_block.py b/tests/evm/test_end_block.py index 21873f85b..005b1be8f 100644 --- a/tests/evm/test_end_block.py +++ b/tests/evm/test_end_block.py @@ -31,7 +31,7 @@ def test_end_block(is_last_step: bool): chain( # dummy read/write for counting [(i, *7 * [0]) for i in range(22)], - [(22, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, tx.id, 0, 0)] + [(22, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0)] if is_last_step else [], ) diff --git a/tests/evm/test_end_tx.py b/tests/evm/test_end_tx.py index b69725937..284ce88f3 100644 --- a/tests/evm/test_end_tx.py +++ b/tests/evm/test_end_tx.py @@ -58,17 +58,19 @@ def test_end_tx(tx: Transaction, gas_left: int, refund: int, is_last_tx: bool): bytecode_table=set(), rw_table=set( [ - (17, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, tx.id, 0, 0), - (18, RW.Read, RWTableTag.TxRefund, tx.id, refund, refund, 0, 0), + (17, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.TxId, 0, tx.id, 0, 0, 0), + (18, RW.Read, RWTableTag.TxRefund, tx.id, 0, 0, refund, refund, 0, 0), ( 19, RW.Write, RWTableTag.Account, tx.caller_address, AccountFieldTag.Balance, + 0, RLC(caller_balance, randomness), RLC(caller_balance_prev, randomness), 0, + 0, ), ( 20, @@ -76,15 +78,17 @@ def test_end_tx(tx: Transaction, gas_left: int, refund: int, is_last_tx: bool): RWTableTag.Account, block.coinbase, AccountFieldTag.Balance, + 0, RLC(coinbase_balance, randomness), RLC(coinbase_balance_prev, randomness), 0, + 0, ), ] + ( [] if is_last_tx - else [(21, RW.Read, RWTableTag.CallContext, 22, CallContextFieldTag.TxId, tx.id + 1, 0, 0)] + else [(21, RW.Read, RWTableTag.CallContext, 22, CallContextFieldTag.TxId, 0, tx.id + 1, 0, 0, 0)] ) ), ) diff --git a/tests/evm/test_jump.py b/tests/evm/test_jump.py index 06ea99e65..c30adb868 100644 --- a/tests/evm/test_jump.py +++ b/tests/evm/test_jump.py @@ -34,7 +34,7 @@ def test_jump(opcode: Opcode, dest_bytes: bytes): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.Stack, 1, 1021, dest, 0, 0), + (9, RW.Read, RWTableTag.Stack, 1, 1021, 0, dest, 0, 0, 0), ] ), ) diff --git a/tests/evm/test_jumpi.py b/tests/evm/test_jumpi.py index 6c7105c7b..ae49736af 100644 --- a/tests/evm/test_jumpi.py +++ b/tests/evm/test_jumpi.py @@ -35,8 +35,8 @@ def test_jumpi_cond_nonzero(opcode: Opcode, cond_bytes: bytes, dest_bytes: bytes bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.Stack, 1, 1021, dest, 0, 0), - (10, RW.Read, RWTableTag.Stack, 1, 1022, cond, 0, 0), + (9, RW.Read, RWTableTag.Stack, 1, 1021, 0, dest, 0, 0, 0), + (10, RW.Read, RWTableTag.Stack, 1, 1022, 0, cond, 0, 0, 0), ], ), ) @@ -92,8 +92,8 @@ def test_jumpi_cond_zero(opcode: Opcode, cond_bytes: bytes, dest_bytes: bytes): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.Stack, 1, 1021, dest, 0, 0), - (10, RW.Read, RWTableTag.Stack, 1, 1022, cond, 0, 0), + (9, RW.Read, RWTableTag.Stack, 1, 1021, 0, dest, 0, 0, 0), + (10, RW.Read, RWTableTag.Stack, 1, 1022, 0, cond, 0, 0, 0), ], ), ) diff --git a/tests/evm/test_push.py b/tests/evm/test_push.py index 252b11a17..2262db9ed 100644 --- a/tests/evm/test_push.py +++ b/tests/evm/test_push.py @@ -39,7 +39,7 @@ def test_push(value_be_bytes: bytes): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (8, RW.Write, RWTableTag.Stack, 1, 1023, value, 0, 0), + (8, RW.Write, RWTableTag.Stack, 1, 1023, 0, value, 0, 0, 0), ] ), ) diff --git a/tests/evm/test_slt_sgt.py b/tests/evm/test_slt_sgt.py index 7d26ccb5c..f0fcb015d 100644 --- a/tests/evm/test_slt_sgt.py +++ b/tests/evm/test_slt_sgt.py @@ -201,9 +201,9 @@ def test_slt_sgt(opcode: Opcode, a: int, b: int, res: int): bytecode_table=set(bytecode.table_assignments(randomness)), rw_table=set( [ - (9, RW.Read, RWTableTag.Stack, 1, 1022, a, 0, 0), - (10, RW.Read, RWTableTag.Stack, 1, 1023, b, 0, 0), - (11, RW.Write, RWTableTag.Stack, 1, 1023, res, 0, 0), + (9, RW.Read, RWTableTag.Stack, 1, 1022, 0, a, 0, 0, 0), + (10, RW.Read, RWTableTag.Stack, 1, 1023, 0, b, 0, 0, 0), + (11, RW.Write, RWTableTag.Stack, 1, 1023, 0, res, 0, 0, 0), ] ), ) From 807f607bb4898c84154609ea8830e62e29bbe738 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Wed, 26 Jan 2022 11:12:53 +0800 Subject: [PATCH 6/6] format .md --- specs/opcode/54SLOAD_55SSTORE.md | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/specs/opcode/54SLOAD_55SSTORE.md b/specs/opcode/54SLOAD_55SSTORE.md index c498fa63c..6396ce92b 100644 --- a/specs/opcode/54SLOAD_55SSTORE.md +++ b/specs/opcode/54SLOAD_55SSTORE.md @@ -20,46 +20,46 @@ - gc - `SLOAD`: +5 (2 stack operations + 1 storage reads + 2 access_list reads/writes) - `SSTORE`: +8 - + 2 stack operations - + 2 storage reads/writes - + 2 access_list reads/writes - + 2 gas_refund reads/writes + - 2 stack operations + - 2 storage reads/writes + - 2 access_list reads/writes + - 2 gas_refund reads/writes - stack_pointer - `SLOAD`: remains the same - `SSTORE`: -2 - pc + 1 - state_write_counter - - `SLOAD`: +1 (access_list) - - `SSTORE`: +3 (for storage, access_list & gas_refund respectively) + - `SLOAD`: +1 (access_list) + - `SSTORE`: +3 (for storage, access_list & gas_refund respectively) - gas: - `SLOAD`: - + the accessed address is warm: gas + WARM_STORAGE_READ_COST - + the accessed address is cold: gas + COLD_SLOAD_COST + - the accessed address is warm: gas + WARM_STORAGE_READ_COST + - the accessed address is cold: gas + COLD_SLOAD_COST - `SSTORE`: - + the accessed address is warm: - * `current_value == new_value`: gas + SLOAD_GAS - * `current_value != new_value`: + - the accessed address is warm: + - `current_value == new_value`: gas + SLOAD_GAS + - `current_value != new_value`: - `original_value == current_value`: - `original_value == 0`: gas + SSTORE_SET_GAS - `original_value != 0`: gas + SSTORE_RESET_GAS - `original_value != current_value`: gas + SLOAD_GAS - + the accessed address is cold: - * `current_value == new_value`: gas + SLOAD_GAS + COLD_SLOAD_COST - * `current_value != new_value`: + - the accessed address is cold: + - `current_value == new_value`: gas + SLOAD_GAS + COLD_SLOAD_COST + - `current_value != new_value`: - `original_value == current_value`: - `original_value == 0`: gas + SSTORE_SET_GAS + COLD_SLOAD_COST - `original_value != 0`: gas + SSTORE_RESET_GAS + COLD_SLOAD_COST - `original_value != current_value`: gas + SLOAD_GAS + COLD_SLOAD_COST * gas_refund: - `SSTORE`: - + `current_value != new_value`: - * `original_value == current_value`: - * `original_value != 0` && `new_value == 0`: gas_refund + SSTORE_CLEARS_SCHEDULE - * `original_value != current_value`: - * `original_value != 0`: + - `current_value != new_value`: + - `original_value == current_value`: + - `original_value != 0` && `new_value == 0`: gas_refund + SSTORE_CLEARS_SCHEDULE + - `original_value != current_value`: + - `original_value != 0`: - `current_value == 0`: gas_refund - SSTORE_CLEARS_SCHEDULE - `new_value == 0`: gas_refund + SSTORE_CLEARS_SCHEDULE - * `original_value == new_value`: + - `original_value == new_value`: - `original_value == 0`: gas_refund + SSTORE_SET_GAS - SLOAD_GAS - `original_value != 0`: gas_refund + SSTORE_RESET_GAS - SLOAD_GAS 3. lookups: @@ -78,8 +78,8 @@ - The 32 bytes of new `value` are written to storage at `address` - access_list: Whether the address is warm (accessed before), mark as warm afterward - gas_refund: - + Read the accumulated gas_refund for this tx - + Write the new accumulated gas_refund for this tx + - Read the accumulated gas_refund for this tx + - Write the new accumulated gas_refund for this tx ## Exceptions