Skip to content

Commit 499df79

Browse files
committed
Add zkEVM - sequencing batches
1 parent daa9146 commit 499df79

File tree

9 files changed

+120
-0
lines changed

9 files changed

+120
-0
lines changed
30 KB
Loading
17.5 KB
Loading
89.4 KB
Loading
31.1 KB
Loading
45.7 KB
Loading
52.9 KB
Loading
56.6 KB
Loading
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
The central entity responsible for assembling batches for sequencing is the trusted sequencer, which is built and managed by Polygon.
2+
3+
This sequencer may omit Layer 2 transactions therefore we have implemented [anti-censorship mechanism](https://docs.polygon.technology/zkEVM/architecture/protocol/malfunction-resistance/sequencer-resistance/).
4+
5+
The diagram below shows the sequencing workflow.
6+
7+
![Figure: ](../../../img/zkEVM/sqb-l2-txs-seq-batches.png)
8+
9+
1. L2 transactions via JSON RPC.
10+
2. Transactions stored in the pool DB.
11+
3. Trusted sequencer selects transactions from pool DB.
12+
4. Trusted sequencer batches and sequences transactions.
13+
14+
## Batch pre-execution
15+
16+
The initial step in creating a batch involves verifying whether the chosen transactions align with execution parameters and do not surpass the gas limit. This step is known as _batch pre-execution_.
17+
18+
It is carried out by the sequencer through an executor, as depicted in the figure below.
19+
20+
![Figure: Pre-execution](../../../img/zkEVM/sqb-batch-preexecution.png)
21+
22+
While no proof is generated during the pre-execution stage, it ensures that the subsequent proof generation process by the prover can be successfully accomplished. This expedites the overall sequencing of batches.
23+
24+
A fast executor is employed, which is a _single-computation_ executor, making it capable of executing within blocktime.
25+
26+
The sequencer communicates with this executor to perform the pre-execution process swiftly.
27+
28+
Upon determining the transactions that correctly fill a specific batch through successful batch pre-execution, the sequencer records the batch in the node’s StateDB as a _closed batch_.
29+
30+
Closure may occur when either one of the following conditions is fulfilled:
31+
32+
- The maximum number of execution trace rows is reached.
33+
- The maximum gas limit is attained.
34+
- The allocated time expires.
35+
36+
During batch pre-execution, and for batch closure, the sequencer and the executor update the Merkle tree of the zkEVM with L2 state changes, which is stored in the Prover HashDB. This is illustrated the figure below.
37+
38+
![Figure: Update L2 state](../../../img/zkEVM/sqb-l2-state-update-01.png)
39+
40+
The zkEVM's throughput depends highly on the speed at which we are able to close batches, which is directly impacted by the batch pre-execution process.
41+
42+
In fact, most of the performance problems occur here because excessive interaction with the HashDB is inefficient.
43+
44+
Efforts are currently underway to optimize this process:
45+
46+
- Reducing the time spent on frequent updates during transaction processing, by accumulating all state changes caused by a transaction, and only update the HashDB at the end of a transaction's pre-execution.
47+
48+
## Sending batches to L1
49+
50+
The next step is to send a call to the smart contract to sequence batches.
51+
52+
Once a batch is closed, the sequencer stores the data of the batch in the node’s StateDB.
53+
54+
Then, the $\texttt{sequenceSender}$ looks for closed batches and sends them to the [L1 smart contract](https://github.com/0xPolygonHermez/zkevm-contracts/blob/main/contracts/PolygonZkEVM.sol) via the $\texttt{EthTxManager}$, who makes sure that the transaction is included in a batch.
55+
56+
This process is depicted in the figure below.
57+
58+
![Figure: Sequence sender and ETH Tx Manager](../../../img/zkEVM/sqb-seq-sender-tx-manager.png)
59+
60+
In order to sequence a batch, the sequencer calls the $\texttt{sequenceBatches()}$ function in the L1 Smart Contract.
61+
62+
The name of the function is in plural because it is capable of sequencing several batches at once.
63+
64+
This step provides L2 data availability in the L1 execution layer, because we are registering in L1 all the bytes of the L2 transactions.
65+
66+
The calldata for the L1 $\texttt{sequenceBatches()}$​ function needs to include the following information:
67+
68+
- The L2 transactions’ data, which is an array containing data for each batch. It includes all transactions within the batch along with a timestamp indicating its closure time.
69+
- Additionally, the L2 coinbase address, representing the Ethereum address for receiving user fees.
70+
- Lastly, a timestamp indicating when the L2 transactions were sequenced.
71+
72+
The L2 coinbase address serves as a critical destination for rewards earned by the sequencer in the Layer 2 environment.
73+
74+
The sequencer undertakes the responsibility of paying for data availability in Layer 1 using L1 Ether.
75+
76+
When the sequencer successfully closes a batch and executes transactions, they receive a reward for their services. This reward, denominated in L2 Ether, is routed to the L2 coinbase address.
77+
78+
Crucially, the L2 coinbase address is situated within Layer 2 because users compensate the sequencer with L2 Ether.
79+
80+
This L2 Ether, representing the reward, is a reflection of the Ether in L1 that users have previously transferred to L2 through transactions via the Bridge.
81+
82+
Importantly, there exists a direct and fixed one-to-one correspondence between L1 ETH and L2 ETH, as we can observe in the figure below.
83+
84+
![Figure: L1 ETH and L2 ETH equivalence](../../../img/zkEVM/sqb-l1-and-l2-eth-equiv.png)
85+
86+
## Accumulated input hash pointers
87+
88+
Let's briefly explain how the sequencing operation and the proving process link up.
89+
90+
In particularly, we look at how the prover can match each sequenced batch with its data.
91+
92+
When the smart contract receives a call to sequence batches, it initiates creation of cryptographic pointers for each batch.
93+
94+
These pointers play a crucial role in identifying a batch uniquely, specifying its position, and encapsulating its data.
95+
96+
Subsequently, provers utilize these pointers as references during the proving process, allowing them to precisely identify the batch being proved and retrieve its associated data.
97+
98+
The use of cryptographic pointers ensures a robust and unambiguous link between the sequencing operation and the corresponding batch data for subsequent verification.
99+
100+
![Figure: Sequence of batches - ... timeline](../../../img/zkEVM/sqb-batches-timeline.png)
101+
102+
These pointers are constructed using a hash that accumulates data, incorporating information from all preceding blocks.
103+
104+
The procedural steps for this process are illustrated in the figure below:
105+
106+
![Figure: Stringing together batch hash data](../../../img/zkEVM/sqb-stringing-batches-together.png)
107+
108+
Pointers are generated by executing the KECCAK hash function on:
109+
110+
- The preceding pointer.
111+
- The transactions encompassed within the L2 batch.
112+
- The batch timestamp.
113+
- The L2 coinbase.
114+
115+
Due to the inter-connected nature of their creation, with each pointer encapsulating the previous one in the input of the hash, they are aptly referred to as *accumulated input hash* or $\texttt{accInputHash}$.
116+
117+
Such a construction, where previous pointers are linked to the current pointer, guarantees that batch data is proved in the correct and sequential order.
118+
119+
Upon completion of the sequencing process, the batch enters a state of being *virtualized*, residing within a _virtual state_.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ nav:
119119
- Transaction batching: zkEVM/architecture/protocol/transaction-life-cycle/transaction-batching.md
120120
- Batch sequencing: zkEVM/architecture/protocol/transaction-life-cycle/batch-sequencing.md
121121
- Batch aggregation: zkEVM/architecture/protocol/transaction-life-cycle/batch-aggregation.md
122+
- Sequencing batches: zkEVM/architecture/protocol/sequencing-batches.md
122123
- Incentive mechanism: zkEVM/architecture/protocol/incentive-mechanism.md
123124
- Protocol upgradability: zkEVM/architecture/protocol/upgradability.md
124125
- Admin role and governance: zkEVM/architecture/protocol/admin-role.md

0 commit comments

Comments
 (0)