Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.
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
8 changes: 4 additions & 4 deletions src/zkevm_specs/evm/execution/end_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ 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
is_first_tx = tx_id == 1
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
Expand Down
20 changes: 18 additions & 2 deletions src/zkevm_specs/evm/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,8 @@ def tx_log_lookup(
).value
return value

# look up TxReceipt fields (PostStateOrStatus, CumulativeGasUsed, LogLength)
def tx_receipt_lookup(
# look up TxReceipt read for fields (PostStateOrStatus, CumulativeGasUsed, LogLength)
def tx_receipt_read(
self,
tx_id: Expression,
field_tag: TxReceiptFieldTag,
Expand All @@ -635,6 +635,22 @@ def tx_receipt_lookup(
).value
return value

# look up TxReceipt write for 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:
Expand Down
16 changes: 16 additions & 0 deletions src/zkevm_specs/evm/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions tests/evm/test_end_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,23 @@ 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
)

# check it is first 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,
Expand Down