Skip to content

Commit 2b9d8eb

Browse files
committed
Edit zkEVM - effective gas docs
1 parent 8cd586e commit 2b9d8eb

File tree

18 files changed

+1408
-1009
lines changed

18 files changed

+1408
-1009
lines changed
153 KB
Loading
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
In this section we provide an elaborate discussion on how Polygon zkEVM network ensures transactions are executed with the best gas price for the user, while incurring minimal or no losses.
2+
3+
You will learn how to sign transations with the gas price that ensures:
4+
5+
- There is minimal likelihood for your transactions to be reverted.
6+
- The sequencer prioritizes your transactions for execution.
7+
8+
## How to avert rejection of transactions
9+
10+
The first step is to ensure that users sign transactions with sufficient gas price, and thus ensure the transactions are included in the L2's pool of transactions.
11+
12+
Polygon zkEVM's strategy is to use pre-execution of transactions so as to estimate each transaction's possible gas costs.
13+
14+
### Suggested gas prices
15+
16+
Due to fluctuations in the L1 gas price, the L2 network polls for L1 gas price every 5 seconds.
17+
18+
The polled L1 gas prices are used to determine the appropriate L2 gas price to suggest to users.
19+
20+
Since the L1 gas price is likely to change between when a user signs a transaction and when the transaction is pre-executed, the following parameters are put in place:
21+
22+
- A $5$-minute interval of several suggested gas prices, called $\texttt{MinAllowedPriceInterval}$.
23+
- During the $\texttt{MinAllowedPriceInterval}$, the user's transactions can be accepted for pre-execution, provided the $\texttt{SignedGasPrice}$ is above the least among the suggested gas prices in the interval.
24+
- The least of the suggested gas prices is called $\texttt{L2MinGasPrice}$.
25+
26+
![Figure: minimum allowed gas interval](../../../img/zkEVM/min-allowed-gas-interval.png)
27+
28+
All transactions such that $\texttt{SignedGasPrice} > \texttt{L2MinGasPrice}$ are accepted for pre-execution.
29+
30+
## How to avoid incurring losses in L2
31+
32+
There are basically three measures put in place to avoid incurring losses in the L2 network:
33+
34+
- Pre-execution of transactions.
35+
- The breakeven gas price: $\texttt{BreakEvenGasPrice}$.
36+
- The L2's net profit.
37+
38+
### Transaction pre-execution
39+
40+
Pre-execution of transactions is used to estimate L2 resources each transaction will spent when processed.
41+
42+
These resources are measured in terms of counters in the zkEVM's ROM, but are converted to gas units for better UX.
43+
44+
This is the stage where transactions are either discarded or stored in the pool database, a pool of transactions waiting to be processed by the sequencer.
45+
46+
The price of posting transaction data to L1 is charged to the zkEVM network at a full L1 price.
47+
48+
Although computational costs in L2 may be accurately estimated, in cases where there is a reduction in such costs due to fewer L2 resources being spent, the user may be justified to sign a transaction with a very low gas price. But by signing such a low gas price, the user runs the risk of exhausting their wei reserves when transaction data is posted to L1.
49+
50+
So then, if the use of $\texttt{L1GasPriceFactor = 0.04}$ is the only precautionary measure the L2 network takes in computing suggested gas prices, the L2 network will most likely incur losses.
51+
52+
A $\texttt{suggestedFactor = 0.15}$ is therefore used to calculate each suggested gas price:
53+
54+
$$
55+
\texttt{suggestedL2GasPrice} = \texttt{L1GasPrice} \cdot \texttt{suggestFactor}
56+
$$
57+
58+
Such a factor is justifiable considering the fact that, the sequencer is obliged to process every transaction stored in the pool database, irrespective of its $\texttt{SignedGasPrice}$ and the prevailing L1 gas price.
59+
60+
### Breakeven gas price
61+
62+
Calculating the breakeven gas price is another measure used in the Polygon zkEVM network to mitigate possible losses.
63+
64+
The breakeven gas price is calculated as a ratio of the total transaction cost and the amount of gas used:
65+
66+
$$
67+
\texttt{BreakEvenGasPrice} = \frac{\texttt{TotalTxPrice}}{\texttt{GasUsed}}
68+
$$
69+
70+
In order to attain some profit for processing transactions, a marginal profit factor called $\texttt{NetProfit}$ is incorporated in the $\texttt{BreakEvenGasPrice}$ formula as follows:
71+
72+
$$
73+
\texttt{BreakEvenGasPrice} = \frac{\texttt{TotalTxPrice}}{\texttt{GasUsed}} \cdot \texttt{NetProfit}
74+
$$
75+
76+
The breakeven gas price factor is set to $1.3$​, resulting in a 30% net profit.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Here's how you can accurately determine the gas price to sign your transactions with when submitting transactions to the Polygon zkEVM network.
2+
3+
Polygon zkEVM implements a mechanism called the Effective Gas Price (EGP), so as to guarantee fair gas fees for the benefit of both the user and the network.
4+
5+
Now that the EGP is in place, you can reduce any chance for a transaction revert, while making sure that accepted transactions receive your preferred prioritization. Meanwhile, the zkEVM network incurs minimal or no loss.
6+
7+
## How gas fees work in Ethereum
8+
9+
In Ethereum, users decide on two parameters:
10+
11+
- The $\texttt{gasLimit}$, which is the maximum amount of gas units the user is willing to buy, in order for their transactions to be completed.
12+
- The $\texttt{gasPrice}$, that is, the amount of wei a user is willing to pay for 1 gas unit.
13+
14+
At the beginning of each transaction, the amount of wei sufficient to cover transaction costs is deducted from the user's account balance.
15+
16+
That amount of wei is calculated as:
17+
18+
$$
19+
\texttt{gasLimit} \cdot \texttt{gasPrice}
20+
$$
21+
22+
The actual amount spent for the transaction is given by:
23+
24+
$$
25+
\texttt{gasUsed} \cdot \texttt{gasPrice}
26+
$$
27+
28+
And thus, any refund to the user is simply:
29+
30+
$$
31+
\texttt{gasLimit} \cdot \texttt{gasPrice} - \texttt{gasUsed} \cdot \texttt{gasPrice}
32+
$$
33+
34+
Transactions get reverted if the $\texttt{gasUsed}$ is greater than the $\texttt{gasLimit}$. Otherwise, transaction succeed.
35+
36+
## Computing L2 gas fees
37+
38+
The L2 gas price cannot be simply set to be the same as L1 gas price (more especially in the particular case of rollups, where the main benefit for using a rollup is to reduce gas fees).
39+
40+
For this reason we henceforth distinguish between the two gas prices, and denote them as: $\texttt{L2GasPrice}$ and $\texttt{L1GasPrice}$.
41+
42+
But how can you calculate the appropriate L2 gas price while ensuring that transactions are successfully executed?
43+
44+
Although the same formula is used, that is,
45+
46+
$$
47+
\texttt{gasLimit} \cdot \texttt{gasPrice}
48+
$$
49+
50+
and success is guaranteed if $\texttt{gasLimit}$ is greater than $\texttt{gasUsed}$, the gas used is determined by the gas cost for data availability plus the gas cost for transaction execution in the L2.
51+
52+
That is,
53+
54+
$$
55+
\texttt{gasUsed} = \texttt{DataCost} + (\texttt{L2 Execution gas cost})
56+
$$
57+
58+
The total fees paid by the user is given by:
59+
60+
$$
61+
\texttt{TotalTxPrice} = \texttt{DataCost} \cdot \texttt{L1GasPrice} + (\texttt{L2 Execution gas cost}) \cdot \texttt{L2GasPrice}
62+
$$
63+
64+
Note that data availability is charged in L1 using the prevailing L1 gas price at the time of posting data.
65+
66+
The main challenge is "How to adjust $\texttt{L2GasPrice}$ in terms of the $\texttt{L1GasPrice}$, so as to account for L2 resources spent when processing the user's transaction?"
67+
68+
The general strategy is to use an $\texttt{L1GasPriceFactor}$ such that
69+
70+
$$
71+
\texttt{L2GasPrice} = \texttt{L1GasPrice} \cdot \texttt{L1GasPriceFactor}
72+
$$
73+
74+
### Example. (L1 gas price factor)
75+
76+
For a transaction with an L1 gas price of 20 Gwei, the L2 gas price in the Polygon zkEVM network is obtained with a 4% factor as follows:
77+
78+
$$
79+
\texttt{L2GasPrice}\ =\ 20\ \text{Gwei}⋅0.04=0.8\ \text{Gwei}
80+
$$
81+
82+
Current L2 fees can be viewed here [https://l2fees.info](https://l2fees.info/).
83+
84+
Although this factor is used in the Polygon zkEVM network, in fact $\texttt{L1GasPriceFactor}$ is set to $0.04$​, there are a few complications that need to be carefully considered.
85+
86+
So the question remains: What gas price should the user sign transactions with?
87+
88+
There are 3 scenarios we aim to avoid when determining the $\texttt{L2GasPrice}$ to sign transactions with:
89+
90+
- Transactions getting rejected due to the $\texttt{SignedGasPrice}$ being less than L2's minimum expected gas price ($\texttt{L2MinGasPrice}$).
91+
- Incurring losses in the L2 network because of high transaction gas costs.
92+
- Transactions receiving the least priority for sequencing.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
Users who want their transactions to be priotized for sequencing can sign their transactions with gas prices that are higher than the suggested gas price. That is,
2+
3+
$$
4+
\texttt{SignedGasPrice} > \texttt{suggestedGasPrice}
5+
$$
6+
7+
The priority ratio is defined as:
8+
9+
$$
10+
\texttt{PriorityRatio} = \frac{\texttt{SignedGasPrice}}{\texttt{suggestedGasPrice}} - 1
11+
$$
12+
13+
Any transaction with $\mathtt{SignedGasPrice \leq GasPriceSuggested}$, implies that the user has chosen not to have the transaction prioritized.
14+
15+
## Computing effective gas price
16+
17+
The computation of the effective gas price (or EGP) involves both the break even gas price and the priority ratio:
18+
19+
$$
20+
\texttt{EffectiveGasPrice} = \texttt{BreakEvenGasPrice} \cdot \big(1 + \texttt{PriorityRatio}\big)
21+
$$
22+
23+
It’s important to observe that, among all the transactions stored in the pool database, those that are prioritized at the time of sequencing are those with higher $\texttt{effectiveGasPrice}$, due to the added $\texttt{PriorityRatio}$.
24+
25+
### Gas consumption deviations
26+
27+
Since the actual gas consumed can deviate from the estimated gas consumption, we can introduce the estimated $\texttt{EGP}$ and the 'new' $\texttt{EGP}$, and denote them as $\texttt{EEGP}$ and $\texttt{NEGP}$, respectively.
28+
29+
The extent of the deviation can be computed as a percentage:
30+
31+
$$
32+
\frac{|{\texttt{NEGP} - \texttt{EEGP}}|}{\texttt{EEGP}} \cdot 100
33+
$$
34+
35+
The deviation percentage is compared to a parameter called $\texttt{FinalDeviationParameter}$, which is set to $10$.
36+
37+
This presents 2 scenarios and their corresponding consequences:
38+
39+
1. If the percentage deviation is higher than the fixed deviation parameter,
40+
41+
$$
42+
\frac{|\texttt{NEGP} − \texttt{EEGP}|}{\texttt{EEGP}} \cdot 100 < \texttt{FinalDeviationParameter} = 10,
43+
$$
44+
45+
it indicates that there is minimal distinction between charging the user with $\texttt{NEGP}$ compared to $\texttt{EEGP}$.
46+
47+
Despite potential losses to the network, the user gets charged the $\texttt{EEGP}$ amount as the gas price.
48+
49+
2. On the contrary, if the percentage deviation equals or exceeds the deviation parameter,
50+
51+
$$
52+
\frac{|\texttt{NEGP} − \texttt{EEGP}|}{\texttt{EEGP}} \cdot 100 ≥ \texttt{FinalDeviationParameter} = 10,
53+
$$
54+
55+
the difference between executions can be so big it warrants adjustment of the gas price to be $\texttt{NEGP}$​, and thus mitigate for potential losses to the network.
56+
57+
### Effective percentage
58+
59+
The last parameter called the $\texttt{EffectivePercentage}$ is for measuring the unused portion of the user's signed gas price.
60+
61+
In order to calculate the $\texttt{EffectivePercentage}$, one option is to consider pricing resources based on the number of consumed counters within the Polygon zkEVM's proving system.
62+
63+
However, understanding this metric can be challenging for users because stating the efficiency through counters is intricate.
64+
65+
For positive UX's sake, a better alternative is taken. A formula involving gas is applied, as it is more user-friendly.
66+
67+
The primary objective is to compute $\texttt{EffectivePercentage}$ exclusively using gas, while allowing users to prioritize their transactions through the use of gas price, without the need for complex considerations such as used ROM counters.
68+
69+
The effective percentage is computed as follows:
70+
71+
$$
72+
\texttt{EffectivePercentage} = \frac{ \texttt{GasPriceFinal}}{ \texttt{SignedGasPrice}}
73+
$$
74+
75+
where $\texttt{GasPriceFinal}$ is the gas price charged at the end of the entire processing by the sequencer.
76+
77+
Observe that, by modifying $\texttt{GasPriceFinal}$, the amount of wei that the user is charged for processing their sent transactions can be adjusted.
78+
79+
This $\texttt{EffectivePercentage}$ is provided by the sequencer as a single byte:
80+
81+
$$
82+
\texttt{EffectivePercentageByte} \in \{ 0, 1, . . . , 255 \}
83+
$$
84+
85+
which is computed from the $\texttt{EffectivePercentage}$ as follows:
86+
87+
$$
88+
\texttt{EffectivePercentageByte} = \lfloor \texttt{EffectivePercentage} · 256 \rfloor − 1
89+
$$
90+
91+
Since having $\texttt{EffectivePercentage}$ implies having $\texttt{EffectivePercentageByte}$ and vice versa, the two terms are used interchangeably.
92+
93+
So, $\texttt{EffectivePercentageByte}$ is often referred to as $\texttt{EffectivePercentage}$.
94+
95+
**Example (Effective percentage)**
96+
97+
Setting an $\texttt{EffectivePercentageByte}$ of $255\ (= \texttt{0xFF})$ means the $\texttt{EffectivePercentage} = 1$.
98+
99+
In which case the user would pay the gas price they signed with, when sending the transaction, in total. That is:
100+
101+
$$
102+
\texttt{GasPriceFinal} = \texttt{SignedGasPrice}
103+
$$
104+
105+
In contrast, setting $\texttt{EffectivePercentageByte}$ to $127$ means:
106+
107+
$$
108+
\texttt{EffectivePercentage} = 0.5
109+
$$
110+
111+
so, only half of the gas price the user signed with gets charged as the transaction cost:
112+
113+
$$
114+
\texttt{GasPriceFinal} = \frac{\texttt{SignedGasPrice}}{2}
115+
$$
116+
117+
The transaction execution takes only half of the signed gas price.
118+
119+
## Concluding remarks
120+
121+
The effective gas price scheme as outlined above, although steeped in details, takes all necessary factors and eventualities into consideration.
122+
123+
Ultimately, the scheme is accurate and fair to both the users and the zkEVM network.
124+
125+
Check out this [repo](https://github.com/0xPolygonHermez/zkevm-rom/issues/316) for a detailed example of how the effective gas price is calculated.
126+
127+
A more elaborate and expanded documentation of the EGP scheme can be found in the specifications section [here](../../spec/user-fees/index.md).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Next, we provide a concise description of the Polygon zkEVM's approach to an effective gas price.
2+
3+
It's an overview of the end-to-end flow of transactions, from when users sign transactions and submit them at the RPC level, to when the sequencer executes them.
4+
5+
It's a quicker way to go through all involved formulas or criteria, but without explanatory examples.
6+
7+
You will find more elaborate discussions, with ample examples, in the Specifications section [here](../../../spec/user-fees/index.md).
8+
9+
The end-to-end flow of transactions has two phases: The RPC flow, and the sequencer flow.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
The RPC flow phase of transactions consists of two stages;
2+
3+
- The gas price suggestion.
4+
- Pre-execution of transactions.
5+
6+
This flow ends with storing transactions in the pool, which is a pool of transactions waiting to be executed by the sequencer.
7+
8+
## Gas price suggestion
9+
10+
The L2 network (the zkEVM) polls for L1 gas price values, and uses them to:
11+
12+
- Suggest L2 gas price to users, at the users' request.
13+
- Sets the minimum acceptable L2 gas price, denoted by $\texttt{L2MinGasPrice}$.
14+
15+
The user then signs transactions with the appropriate gas price, called $\texttt{GasPriceSigned}$, based on the suggested L2 gas price, $\texttt{GasPriceSuggested}$.
16+
17+
User's transactions are accepted for pre-execution only if
18+
19+
$$
20+
\texttt{GasPriceSigned} > \texttt{L2MinGasPrice}
21+
$$
22+
23+
## Pre-execution of transactions
24+
25+
Pre-execution of transactions, which happens at the RPC level, involves estimating the gas required for processing user's transactions.
26+
27+
This is internally measured (internal to the zkEVM) in terms of resources spent to execute the transactions. These resources are the numbers of counters used up in the zkEVM ROM.
28+
29+
A transaction is said to be _out of counters_ (OOC) if the signed gas price is insufficient to pay for all the required gas units.
30+
31+
Transactions with OOC get rejected, while those with no OOC stand a chance to be added to the pool.
32+
33+
At this stage of the flow, the RPC also computes the "break-even-gas-price", denoted by $\texttt{BreakEvenGasPriceRPC}$. That is,
34+
35+
$$
36+
\texttt{BreakEvenGasPrice} = \frac{\texttt{TotalTxPrice}}{\texttt{GasUsedRPC}} \cdot \texttt{NetProfit},
37+
$$
38+
39+
where $\texttt{NetProfit}$ is the L2's marginal profit for transaction processing.
40+
41+
Transactions with no OOC get added to the pool of transactions if,
42+
43+
- Either $\texttt{GasPriceSigned} > \texttt{BreakEvenGasPriceRPC} \cdot \texttt{BreakEvenFactor}$.
44+
45+
where $\texttt{GasUsedRPC}$ is the RPC's estimated gas cost.
46+
47+
- Or $\texttt{GasPriceSigned} \geq \texttt{GasPriceSuggested}$.
48+
49+
The total fees paid by the user is given by:
50+
51+
$$
52+
\texttt{TotalTxPrice} = \texttt{DataCost} \cdot \texttt{L1GasPrice} + (\texttt{L2 Execution gas cost}) \cdot \texttt{L2GasPrice}
53+
$$
54+
55+
The RPC flow is summarized in the figure below.
56+
57+
![Figure: RPC flow](../../../../img/zkEVM/gas-price-flows-i.png)

0 commit comments

Comments
 (0)