This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
Spec ExecutionState::{EndTx,EndBlock}
#88
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
099503a
refactor: use RLC directly and refactor all
han0110 94abf34
feat: implement ExecutionState EndTx and EndBlock
han0110 d5a45cd
feat: enforce ExecutionState transition constraint for BeginTx
han0110 f6f2760
feat: add test of EndTx
han0110 0df591d
feat: add test of EndBlock
han0110 7f85c17
fix: fix SignByte to have correct sign byte
han0110 dd06110
refactor: rename fields of Block
han0110 e69963c
refactor: use code and storage in Account instead of hash
han0110 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,28 @@ | ||
| from typing import Callable, Dict | ||
|
|
||
| from ..execution_state import ExecutionState | ||
|
|
||
| from .begin_tx import * | ||
| from .end_tx import * | ||
| from .end_block import * | ||
|
|
||
| # Opcode's successful cases | ||
| from .add import * | ||
| from .push import * | ||
| from .jump import * | ||
| from .jumpi import * | ||
| from .push import * | ||
| from .block_coinbase import * | ||
| from .caller import * | ||
|
|
||
| # Error cases | ||
|
|
||
| EXECUTION_STATE_IMPL: Dict[ExecutionState, Callable] = { | ||
| ExecutionState.BeginTx: begin_tx, | ||
| ExecutionState.EndTx: end_tx, | ||
| ExecutionState.EndBlock: end_block, | ||
| ExecutionState.ADD: add, | ||
| ExecutionState.CALLER: caller, | ||
| ExecutionState.COINBASE: coinbase, | ||
| ExecutionState.JUMP: jump, | ||
| ExecutionState.JUMPI: jumpi, | ||
| ExecutionState.PUSH: push, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from ..instruction import Instruction, Transition | ||
| from ..table import CallContextFieldTag | ||
|
|
||
|
|
||
| # TODO: Introduce constrain_instance to constrain the equality between witness | ||
| # and public input, for total_tx and total_rw | ||
|
|
||
|
|
||
| def end_block(instruction: Instruction): | ||
| if instruction.is_last_step: | ||
| # Verify final step has tx_id identical to the tx amount in tx_table. | ||
| total_tx = instruction.call_context_lookup(CallContextFieldTag.TxId) | ||
| instruction.constrain_equal( | ||
| total_tx, | ||
| max([tx_id for tx_id, *_ in instruction.tables.tx_table]), | ||
| ) | ||
|
|
||
| # Verify rw_counter counts to identical rw amount in rw_table to ensure | ||
| # there is no malicious insertion. | ||
| total_rw = instruction.curr.rw_counter + 1 # extra 1 from the tx_id lookup | ||
| instruction.constrain_equal( | ||
| total_rw, | ||
| len(instruction.tables.rw_table), | ||
| ) | ||
| else: | ||
| # Propagate rw_counter and call_id all the way down | ||
| instruction.constrain_step_state_transition( | ||
| rw_counter=Transition.same(), | ||
| call_id=Transition.same(), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| from ...util import N_BYTES_GAS, MAX_REFUND_QUOTIENT_OF_GAS_USED | ||
| from ..execution_state import ExecutionState | ||
| from ..instruction import Instruction, Transition | ||
| from ..table import BlockContextFieldTag, CallContextFieldTag, TxContextFieldTag | ||
|
|
||
|
|
||
| def end_tx(instruction: Instruction): | ||
| tx_id = instruction.call_context_lookup(CallContextFieldTag.TxId) | ||
|
|
||
| # Handle gas refund (refund is capped to gas_used // MAX_REFUND_QUOTIENT_OF_GAS_USED in EIP 3529) | ||
| tx_gas = instruction.tx_context_lookup(tx_id, TxContextFieldTag.Gas) | ||
| gas_used = tx_gas - instruction.curr.gas_left | ||
| max_refund, _ = instruction.constant_divmod(gas_used, MAX_REFUND_QUOTIENT_OF_GAS_USED, N_BYTES_GAS) | ||
| refund = instruction.tx_refund_read(tx_id) | ||
| effective_refund = instruction.min(max_refund, refund, 8) | ||
|
|
||
| # Add effective_refund * gas_price back to caller's balance | ||
| tx_gas_price = instruction.tx_gas_price(tx_id) | ||
| value, carry = instruction.mul_word_by_u64(tx_gas_price, instruction.curr.gas_left + effective_refund) | ||
| instruction.constrain_zero(carry) | ||
| tx_caller_address = instruction.tx_context_lookup(tx_id, TxContextFieldTag.CallerAddress) | ||
| instruction.add_balance(tx_caller_address, [value]) | ||
|
|
||
| # Add gas_used * effective_tip to coinbase's balance | ||
| base_fee = instruction.block_context_lookup(BlockContextFieldTag.BaseFee) | ||
| effective_tip, _ = instruction.sub_word(tx_gas_price, base_fee) | ||
| reward, carry = instruction.mul_word_by_u64(effective_tip, gas_used) | ||
| instruction.constrain_zero(carry) | ||
| coinbase = instruction.block_context_lookup(BlockContextFieldTag.Coinbase) | ||
| instruction.add_balance(coinbase, [reward]) | ||
|
|
||
| # Go to next transaction | ||
| if instruction.next.execution_state == ExecutionState.BeginTx: | ||
| # Check next tx_id is increased by 1 | ||
| instruction.constrain_equal( | ||
| instruction.call_context_lookup(CallContextFieldTag.TxId, call_id=instruction.next.rw_counter), | ||
| tx_id + 1, | ||
| ) | ||
|
|
||
| # Do step state transition for rw_counter | ||
| instruction.constrain_step_state_transition(rw_counter=Transition.delta(5)) | ||
| # Go to end of block | ||
| elif instruction.next.execution_state == ExecutionState.EndBlock: | ||
| # Do step state transition for rw_counter | ||
| instruction.constrain_step_state_transition(rw_counter=Transition.delta(4)) | ||
| else: | ||
| raise ValueError("Unreacheable") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Which is the purpose of applying the RLC to the keccak inputs? Hasn't it been done previously in
assign_bytecode_circuit?Also, why do we need to
reversethe bytecodet?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a mocking
keccak_table, for lookup thehashbyRLCofbytecodeandlength.Because in bytecode circuit, it accumulates the bytes in big-endian order, so we need to reverse it for
RLC, which takes input as little-endian.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes complete sense!