You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/zkEVM/architecture/effective-gas/implement-egp-strat.md
+179-3Lines changed: 179 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,16 +61,192 @@ The need for such a factor originates from the fact that the sequencer is oblige
61
61
62
62
Calculating the breakeven gas price is another measure used in the Polygon zkEVM network to mitigate possible losses.
63
63
64
-
The breakeven gas price is calculated as a ratio of the total transaction cost and the amount of gas used:
64
+
65
+
As explained before, the computation is split in two; costs associated with data availability, and costs associated with the use of resources when transactions are processed.
66
+
67
+
#### Costs associated with data availability
68
+
69
+
The cost associated with data availability is computed as,
70
+
71
+
$$
72
+
\texttt{DataCost} \cdot \texttt{L1GasPrice}
73
+
$$
74
+
75
+
where $\texttt{DataCost}$ is the cost in gas for data stored in L1.
76
+
77
+
The cost of data in Ethereum varies according to whether it involves zero bytes or non-zero bytes. In particular, non-zero bytes cost $16$ gas units, while zero bytes cost $4$ gas units.
78
+
79
+
Also, recall that the computation of the cost for _non-zero bytes_ must take into account constants that appear in transactions but are not included in the RLP, which includes:
80
+
81
+
- The signature, which consists of $65$ bytes.
82
+
- The previously defined $\texttt{EffectivePercentageByte}$, which consists of a single byte.
83
+
84
+
This results in a total of $66$ constantly present bytes.
85
+
86
+
Taking everything into consideration, $\texttt{DataCost}$ can be computed as:
where $\texttt{TxNonZeroBytes}$ represents the count of non-zero bytes in a raw transaction, and similarly $\texttt{TxZeroBytes}$ represents the count of zero bytes in a raw transaction sent by the user.
94
+
95
+
#### Computational costs
96
+
97
+
Costs associated with transaction execution is denoted by $\texttt{ExecutionCost}$, and is measured in gas.
98
+
99
+
In contrast to costs for data availability, calculating computational costs requires executing transactions.
In order to establish the gas price at which the total transaction cost is covered, we can compute $\texttt{BreakEvenGasPrice}$ as the following ratio:
A marginal profit factor called $\texttt{NetProfit}$ is incorporated in the $\texttt{BreakEvenGasPrice}$ formula as a means of generating a fixed proportion of profit from the total gas fees. It is calculated as follows:
135
+
Additionally, we incorporate a factor $\texttt{NetProfit ≥ 1}$ that allows us to achieve a slight profit margin:
In the RPC component, we’re only pre-executing the transaction, meaning we’re using an incorrect state root. Consequently, the $\texttt{GasUsed}$ is only an approximation.
150
+
151
+
This implies that we need to multiply the result by a chosen factor before comparing it to the signed price to hedge against unforeseen costs.
152
+
153
+
This ensures that the costs are covered in case more gas is ultimately required to execute the transaction. This factor is named $\texttt{BreakEvenFactor}$.
Observe that we still need to introduce gas price prioritization, which we explain later.
164
+
165
+
### Example (Breakeven gas price)
166
+
167
+
Recall the example proposed before, where the $\texttt{GasPriceSuggested}$ provided by RPC was $2.85$ gwei per gas, but the user ended up setting $\texttt{SignedGasPrice}$ to $3.3$.
168
+
169
+
The figure below depicts the current situation.
170
+
171
+

172
+
173
+
Suppose the user sends a transaction that has $200$ non-zero bytes, including the constants and $100$ zero bytes.
174
+
175
+
Moreover, on pre-executing the transaction without an out-of-counters (OOC) error, $60,000$ gas units are consumed.
176
+
177
+
Recall that, since we are using a "wrong" state root, this amount is only an estimation.
178
+
179
+
Hence, using the previously explained formulas, the total transaction cost is:
Suppose we disable the $\texttt{BreakEvenFactor}$ by setting it to $1$.
217
+
218
+
Our original transaction’s pre-execution consumed $60,000$ gas:
219
+
220
+
$$
221
+
\texttt{GasUsedRPC} = 60, 000
222
+
$$
223
+
224
+
However, let's assume the correct execution at the time of sequencing consumes $35,000$ gas.
225
+
226
+
If we recompute $\texttt{BreakEvenGasPrice}$ using this updated used gas, we get $3.6\ \texttt{GWei/Gas}$, which is way higher than the original estimation.
227
+
228
+
That means we should have charged the user a higher gas price in order to cover the whole transaction cost, standing at $105,000\ \texttt{GWei}$.
229
+
230
+
But, since we are accepting all the transactions that sign more than $2.85$ of gas price, we do not have any margin to increase it.
231
+
232
+
In the worst case we are losing:
233
+
234
+
$$
235
+
105, 000 − 35, 000 · 2.85 = 5,250\ \texttt{GWei}
236
+
$$
237
+
238
+
By introducing $\texttt{BreakEvenFactor}$, we are limiting the accepted transactions to the ones with,
239
+
240
+
$$
241
+
\texttt{SignedGasPrice} ≥ 3.27
242
+
$$
243
+
244
+
in order to compensate for such losses.
245
+
246
+
In this case, we have the flexibility to avoid losses and adjust both the user's and Polygon zkEVM network's benefits since:
247
+
248
+
$$
249
+
105, 000 − 35, 000 · 3.27 < 0
250
+
$$
251
+
252
+
**Final Note**: In the above example, even though we assume that a decrease in the $\texttt{BreakEvenGasPrice}$ is a result of executing with a correct state root, it can also decrease significantly due to a substantial reduction in $\texttt{L1GasPrice}$.
Copy file name to clipboardExpand all lines: docs/zkEVM/architecture/effective-gas/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Now that the EGP is in place, you can reduce any chance for a transaction revert
6
6
7
7
## How gas fees work in Ethereum
8
8
9
-
In Ethereum, gas fees for a transaction are decided using two adjustable parameters:
9
+
In Ethereum, there are two adjustable parameters that have a direct impact on transaction gas fees:
10
10
11
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 included in a block and processed on chain.
12
12
- The $\texttt{gasPrice}$, that is, the amount of ETH a user is willing to pay for 1 gas unit. For instance, a gas price of 10 Gwei means the user is willing to pay 0.00000001 ETH for each unit of gas.
Consider a scenario where a user sends a query for a suggested gas price during a 5-minute interval, as shown in the figure below.
63
+
64
+
Values of L1 gas prices, polled every 5 seconds, are displayed above the timeline, while the corresponding L2 gas prices are depicted below the timeline. See the figure below.
65
+
66
+

67
+
68
+
1. Observe that, in the above timeline, the user sends a query at the time indicated by the dotted-arrow on the left. And that's when $\texttt{L1GasPrice}$ is $19$.
69
+
70
+
The RPC node responds with a $2.85 \texttt{ GWei/Gas}$, as the value of the suggested L2 gas price.
2. Let's suppose the user sends a transaction signed with a gas price of $3$. That is, $\texttt{SignedGasPrice} = 3$.
81
+
82
+
However, by the time the user sends the signed transaction, the L1 gas price is no longer $19$ but $21$. And its correponding suggested gas price is $\mathtt{3.15 = 21 \cdot 0.15}$.
83
+
84
+
Note that the minimum suggested L2 gas price, in the 5-min time interval, is $2.85$. And since
3. At this point, the RPC makes a request for pre-execution. That is, getting an estimation for the gas used, computed with a state root that differs from the one used when the transaction is sequenced.
93
+
94
+
In this case, suppose an estimation of gas used is $\texttt{GasUsedRPC} = 60,000$, without an out of counters (OOC) error.
95
+
96
+
4. Since there's no out of counters (OOC) error, the next step is to compute the $\texttt{BreakEvenGasPriceRPC}$.
0 commit comments