Commit bc6756c5 authored by Maurelian's avatar Maurelian Committed by Kelvin Fichter

chore(contracts): Improve the comment explaining the gas burn calculation

parent 3799b760
...@@ -111,6 +111,7 @@ contract CanonicalTransactionChain is ICanonicalTransactionChain, Lib_AddressRes ...@@ -111,6 +111,7 @@ contract CanonicalTransactionChain is ICanonicalTransactionChain, Lib_AddressRes
onlyBurnAdmin onlyBurnAdmin
{ {
enqueueGasCost = _enqueueGasCost; enqueueGasCost = _enqueueGasCost;
// See the comment in enqueue() for the rationale behind this formula.
enqueueL2GasPrepaid = l2GasDiscountDivisor * _enqueueGasCost; enqueueL2GasPrepaid = l2GasDiscountDivisor * _enqueueGasCost;
emit L2GasParamsUpdated( emit L2GasParamsUpdated(
...@@ -130,6 +131,7 @@ contract CanonicalTransactionChain is ICanonicalTransactionChain, Lib_AddressRes ...@@ -130,6 +131,7 @@ contract CanonicalTransactionChain is ICanonicalTransactionChain, Lib_AddressRes
onlyBurnAdmin onlyBurnAdmin
{ {
l2GasDiscountDivisor = _l2GasDiscountDivisor; l2GasDiscountDivisor = _l2GasDiscountDivisor;
// See the comment in enqueue() for the rationale behind this formula.
enqueueL2GasPrepaid = _l2GasDiscountDivisor * enqueueGasCost; enqueueL2GasPrepaid = _l2GasDiscountDivisor * enqueueGasCost;
emit L2GasParamsUpdated( emit L2GasParamsUpdated(
...@@ -324,11 +326,16 @@ contract CanonicalTransactionChain is ICanonicalTransactionChain, Lib_AddressRes ...@@ -324,11 +326,16 @@ contract CanonicalTransactionChain is ICanonicalTransactionChain, Lib_AddressRes
// Transactions submitted to the queue lack a method for paying gas fees to the Sequencer. // Transactions submitted to the queue lack a method for paying gas fees to the Sequencer.
// So we need to prevent spam attacks by ensuring that the cost of enqueueing a transaction // So we need to prevent spam attacks by ensuring that the cost of enqueueing a transaction
// from L1 to L2 is not underpriced. Therefore, we define 'enqueueL2GasPrepaid' as a // from L1 to L2 is not underpriced. For transaction with a high L2 gas limit, we do this by
// threshold. If the _gasLimit for the enqueued transaction is above this threshold, then we // burning some extra gas on L1. Of course there is also some intrinsic cost to enqueueing a
// 'charge' to user by burning additional L1 gas. Since gas is cheaper on L2 than L1, we // transaction, so we want to make sure not to over-charge (by burning too much L1 gas).
// only need to burn a fraction of the provided L1 gas, which is determined by the // Therefore, we define 'enqueueL2GasPrepaid' as the L2 gas limit above which we must burn
// l2GasDiscountDivisor. // additional gas on L1. This threshold is the product of two inputs:
// 1. enqueueGasCost: the base cost of calling this function.
// 2. l2GasDiscountDivisor: the ratio between the cost of gas on L1 and L2. This is a
// positive integer, meaning we assume L2 gas is always less costly.
// The calculation below for gasToConsume can be seen as converting the difference (between
// the specified L2 gas limit and the prepaid L2 gas limit) to an L1 gas amount.
if(_gasLimit > enqueueL2GasPrepaid) { if(_gasLimit > enqueueL2GasPrepaid) {
uint256 gasToConsume = (_gasLimit - enqueueL2GasPrepaid) / l2GasDiscountDivisor; uint256 gasToConsume = (_gasLimit - enqueueL2GasPrepaid) / l2GasDiscountDivisor;
uint256 startingGas = gasleft(); uint256 startingGas = gasleft();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment