From d54076cf1cfbf25fc3091b6f29ffca3e7a4fbbec Mon Sep 17 00:00:00 2001 From: z2trillion Date: Mon, 18 Jul 2022 13:28:38 -0400 Subject: [PATCH 1/2] Change initial tx_receipt accesses to be writes --- src/zkevm_specs/evm/execution/end_tx.py | 8 ++++---- src/zkevm_specs/evm/instruction.py | 18 +++++++++++++++++- src/zkevm_specs/evm/typing.py | 16 ++++++++++++++++ tests/evm/test_end_tx.py | 10 ++++++---- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/zkevm_specs/evm/execution/end_tx.py b/src/zkevm_specs/evm/execution/end_tx.py index 823996403..5bd620cc1 100644 --- a/src/zkevm_specs/evm/execution/end_tx.py +++ b/src/zkevm_specs/evm/execution/end_tx.py @@ -36,11 +36,11 @@ def end_tx(instruction: Instruction): # constrain tx status matches with `PostStateOrStatus` of TxReceipt tag in RW instruction.constrain_equal( - is_persistent, instruction.tx_receipt_lookup(tx_id, TxReceiptFieldTag.PostStateOrStatus) + is_persistent, instruction.tx_receipt_write(tx_id, TxReceiptFieldTag.PostStateOrStatus) ) # constrain log id matches with `LogLength` of TxReceipt tag in RW - log_id = instruction.tx_receipt_lookup(tx_id, TxReceiptFieldTag.LogLength) + log_id = instruction.tx_receipt_write(tx_id, TxReceiptFieldTag.LogLength) instruction.constrain_equal(log_id, instruction.curr.log_id) # constrain `CumulativeGasUsed` of TxReceipt tag in RW @@ -48,13 +48,13 @@ def end_tx(instruction: Instruction): if is_first_tx: # check if it is the first tx current_cumulative_gas_used = FQ(0) else: - current_cumulative_gas_used = instruction.tx_receipt_lookup( + current_cumulative_gas_used = instruction.tx_receipt_read( tx_id - FQ(1), TxReceiptFieldTag.CumulativeGasUsed ).expr() instruction.constrain_equal( current_cumulative_gas_used + gas_used, - instruction.tx_receipt_lookup(tx_id, TxReceiptFieldTag.CumulativeGasUsed), + instruction.tx_receipt_write(tx_id, TxReceiptFieldTag.CumulativeGasUsed), ) # When to next transaction diff --git a/src/zkevm_specs/evm/instruction.py b/src/zkevm_specs/evm/instruction.py index 01b45aab5..002a705ee 100644 --- a/src/zkevm_specs/evm/instruction.py +++ b/src/zkevm_specs/evm/instruction.py @@ -620,7 +620,7 @@ def tx_log_lookup( return value # look up TxReceipt fields (PostStateOrStatus, CumulativeGasUsed, LogLength) - def tx_receipt_lookup( + def tx_receipt_read( self, tx_id: Expression, field_tag: TxReceiptFieldTag, @@ -635,6 +635,22 @@ def tx_receipt_lookup( ).value return value + # look up TxReceipt fields (PostStateOrStatus, CumulativeGasUsed, LogLength) + def tx_receipt_write( + self, + tx_id: Expression, + field_tag: TxReceiptFieldTag, + ) -> Expression: + value = self.rw_lookup( + RW.Write, + RWTableTag.TxReceipt, + key1=tx_id, + key2=FQ(0), + key3=FQ(field_tag), + key4=FQ(0), + ).value + return value + def bytecode_lookup( self, bytecode_hash: Expression, index: Expression, is_code: Expression = None ) -> Expression: diff --git a/src/zkevm_specs/evm/typing.py b/src/zkevm_specs/evm/typing.py index 6da1cce26..a1bff3d41 100644 --- a/src/zkevm_specs/evm/typing.py +++ b/src/zkevm_specs/evm/typing.py @@ -440,6 +440,22 @@ def tx_receipt_read( value=value, ) + def tx_receipt_write( + self, + tx_id: IntOrFQ, + field_tag: TxReceiptFieldTag, + value: Union[int, FQ, RLC], + ) -> RWDictionary: + if isinstance(value, int): + value = FQ(value) + return self._append( + RW.Write, + RWTableTag.TxReceipt, + key1=FQ(tx_id), + key3=FQ(field_tag), + value=value, + ) + def tx_refund_read(self, tx_id: IntOrFQ, refund: IntOrFQ) -> RWDictionary: return self._append( RW.Read, RWTableTag.TxRefund, key1=FQ(tx_id), value=FQ(refund), value_prev=FQ(refund) diff --git a/tests/evm/test_end_tx.py b/tests/evm/test_end_tx.py index 0a39735e3..bdb9536a7 100644 --- a/tests/evm/test_end_tx.py +++ b/tests/evm/test_end_tx.py @@ -73,8 +73,8 @@ def test_end_tx( .tx_refund_read(tx.id, refund) .account_write(tx.caller_address, AccountFieldTag.Balance, RLC(caller_balance, randomness), RLC(caller_balance_prev, randomness)) .account_write(block.coinbase, AccountFieldTag.Balance, RLC(coinbase_balance, randomness), RLC(coinbase_balance_prev, randomness)) - .tx_receipt_read(tx.id, TxReceiptFieldTag.PostStateOrStatus, 1) - .tx_receipt_read(tx.id, TxReceiptFieldTag.LogLength, 0) + .tx_receipt_write(tx.id, TxReceiptFieldTag.PostStateOrStatus, 1) + .tx_receipt_write(tx.id, TxReceiptFieldTag.LogLength, 0) # fmt: on ) @@ -82,12 +82,14 @@ def test_end_tx( is_first_tx = tx.id == 1 if is_first_tx: assert current_cumulative_gas_used == 0 - rw_dictionary.tx_receipt_read(tx.id, TxReceiptFieldTag.CumulativeGasUsed, tx.gas - gas_left) + rw_dictionary.tx_receipt_write( + tx.id, TxReceiptFieldTag.CumulativeGasUsed, tx.gas - gas_left + ) else: rw_dictionary.tx_receipt_read( tx.id - 1, TxReceiptFieldTag.CumulativeGasUsed, current_cumulative_gas_used ) - rw_dictionary.tx_receipt_read( + rw_dictionary.tx_receipt_write( tx.id, TxReceiptFieldTag.CumulativeGasUsed, tx.gas - gas_left + current_cumulative_gas_used, From 8fc9823689d03b9c69d655bdf3fd1be63b987e79 Mon Sep 17 00:00:00 2001 From: z2trillion Date: Mon, 18 Jul 2022 13:29:40 -0400 Subject: [PATCH 2/2] Update comments --- src/zkevm_specs/evm/instruction.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zkevm_specs/evm/instruction.py b/src/zkevm_specs/evm/instruction.py index 002a705ee..371578e39 100644 --- a/src/zkevm_specs/evm/instruction.py +++ b/src/zkevm_specs/evm/instruction.py @@ -619,7 +619,7 @@ def tx_log_lookup( ).value return value - # look up TxReceipt fields (PostStateOrStatus, CumulativeGasUsed, LogLength) + # look up TxReceipt read for fields (PostStateOrStatus, CumulativeGasUsed, LogLength) def tx_receipt_read( self, tx_id: Expression, @@ -635,7 +635,7 @@ def tx_receipt_read( ).value return value - # look up TxReceipt fields (PostStateOrStatus, CumulativeGasUsed, LogLength) + # look up TxReceipt write for fields (PostStateOrStatus, CumulativeGasUsed, LogLength) def tx_receipt_write( self, tx_id: Expression,