Skip to content

Commit 6c25be6

Browse files
authored
feat: Galileo gas price oracle (#163)
1 parent 39619d8 commit 6c25be6

File tree

3 files changed

+75
-36
lines changed

3 files changed

+75
-36
lines changed

src/L2/predeploys/IL1GasPriceOracle.sol

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ interface IL1GasPriceOracle {
2323
/// @param scalar The current blob fee scalar updated.
2424
event BlobScalarUpdated(uint256 scalar);
2525

26-
/// @notice Emitted when current compression penalty threshold is updated.
27-
/// @param threshold The new compression penalty threshold.
28-
event PenaltyThresholdUpdated(uint256 threshold);
29-
3026
/// @notice Emitted when current compression penalty factor is updated.
3127
/// @param factor The new compression penalty factor.
3228
event PenaltyFactorUpdated(uint256 factor);
@@ -56,6 +52,7 @@ interface IL1GasPriceOracle {
5652
function blobScalar() external view returns (uint256);
5753

5854
/// @notice Return the current compression penalty threshold.
55+
/// @custom:deprecated The penalty threshold parameter is deprecated after the Galileo fork.
5956
function penaltyThreshold() external view returns (uint256);
6057

6158
/// @notice Return the current compression penalty factor.

src/L2/predeploys/L1GasPriceOracle.sol

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
3939
/// @dev Thrown when we enable Curie fork after Curie fork.
4040
error ErrAlreadyInCurieFork();
4141

42-
/// @dev Thrown when the compression penalty threshold exceeds `MAX_PENALTY_THRESHOLD`,
43-
/// or is less than 1 * PRECISION.
44-
error ErrInvalidPenaltyThreshold();
45-
46-
/// @dev Thrown when the compression penalty factor exceeds `MAX_PENALTY_FACTOR`,
47-
/// or is less than 1 * PRECISION.
42+
/// @dev Thrown when the penalty factor is zero.
4843
error ErrInvalidPenaltyFactor();
4944

5045
/// @dev Thrown when we enable Feynman fork after Feynman fork.
5146
error ErrAlreadyInFeynmanFork();
5247

48+
/// @dev Thrown when we enable Galileo fork after Galileo fork.
49+
error ErrAlreadyInGalileoFork();
50+
5351
/*************
5452
* Constants *
5553
*************/
@@ -81,14 +79,6 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
8179
/// So, the value should not exceed 10^9 * 1e9 normally.
8280
uint256 private constant MAX_BLOB_SCALAR = 10**9 * PRECISION;
8381

84-
/// @dev The maximum possible compression penalty threshold after Feynman.
85-
/// The value should not exceed 10^9 * 1e9 normally.
86-
uint256 private constant MAX_PENALTY_THRESHOLD = 10**9 * PRECISION;
87-
88-
/// @dev The maximum possible compression penalty factor after Feynman.
89-
/// The value should not exceed 10^9 * 1e9 normally.
90-
uint256 private constant MAX_PENALTY_FACTOR = 10**9 * PRECISION;
91-
9282
/*************
9383
* Variables *
9484
*************/
@@ -117,15 +107,28 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
117107
/// @notice Indicates whether the network has gone through the Curie upgrade.
118108
bool public isCurie;
119109

120-
/// @inheritdoc IL1GasPriceOracle
121-
uint256 public override penaltyThreshold;
110+
/// @custom:deprecated The penalty threshold parameter is deprecated after the Galileo fork.
111+
// slither-disable-next-line uninitialized-state
112+
uint256 public __penaltyThreshold;
122113

123114
/// @inheritdoc IL1GasPriceOracle
124115
uint256 public override penaltyFactor;
125116

126117
/// @notice Indicates whether the network has gone through the Feynman upgrade.
127118
bool public isFeynman;
128119

120+
/// @notice Indicates whether the network has gone through the Galileo upgrade.
121+
bool public isGalileo;
122+
123+
/******************
124+
* View functions *
125+
******************/
126+
127+
/// @inheritdoc IL1GasPriceOracle
128+
function penaltyThreshold() external view override returns (uint256) {
129+
return __penaltyThreshold;
130+
}
131+
129132
/*************
130133
* Modifiers *
131134
*************/
@@ -149,7 +152,9 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
149152

150153
/// @inheritdoc IL1GasPriceOracle
151154
function getL1Fee(bytes memory _data) external view override returns (uint256) {
152-
if (isFeynman) {
155+
if (isGalileo) {
156+
return _getL1FeeGalileo(_data);
157+
} else if (isFeynman) {
153158
return _getL1FeeFeynman(_data);
154159
} else if (isCurie) {
155160
return _getL1FeeCurie(_data);
@@ -160,7 +165,7 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
160165

161166
/// @inheritdoc IL1GasPriceOracle
162167
function getL1GasUsed(bytes memory _data) public view override returns (uint256) {
163-
if (isFeynman || isCurie) {
168+
if (isGalileo || isFeynman || isCurie) {
164169
// It is near zero since we put all transactions to blob.
165170
return 0;
166171
} else {
@@ -232,20 +237,10 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
232237
emit BlobScalarUpdated(_scalar);
233238
}
234239

235-
/// Allows the owner to modify the penaltyThreshold.
236-
/// @param _threshold New threshold
237-
function setPenaltyThreshold(uint256 _threshold) external onlyOwner {
238-
if (_threshold < PRECISION || _threshold > MAX_PENALTY_THRESHOLD) revert ErrInvalidPenaltyThreshold();
239-
240-
penaltyThreshold = _threshold;
241-
emit PenaltyThresholdUpdated(_threshold);
242-
}
243-
244240
/// Allows the owner to modify the penaltyFactor.
245241
/// @param _factor New factor
246242
function setPenaltyFactor(uint256 _factor) external onlyOwner {
247-
if (_factor < PRECISION || _factor > MAX_PENALTY_FACTOR) revert ErrInvalidPenaltyFactor();
248-
243+
if (_factor == 0) revert ErrInvalidPenaltyFactor();
249244
penaltyFactor = _factor;
250245
emit PenaltyFactorUpdated(_factor);
251246
}
@@ -280,6 +275,16 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
280275
isFeynman = true;
281276
}
282277

278+
/// @notice Enable the Galileo fork (callable by contract owner).
279+
///
280+
/// @dev Since this is a predeploy contract, we will directly set the slot while hard fork
281+
/// to avoid external owner operations.
282+
/// The reason that we keep this function is for easy unit testing.
283+
function enableGalileo() external onlyOwner {
284+
if (isGalileo) revert ErrAlreadyInGalileoFork();
285+
isGalileo = true;
286+
}
287+
283288
/**********************
284289
* Internal Functions *
285290
**********************/
@@ -334,4 +339,14 @@ contract L1GasPriceOracle is OwnableBase, IL1GasPriceOracle {
334339
PRECISION /
335340
PRECISION;
336341
}
342+
343+
/// @dev Internal function to compute the L1 portion of the fee based on the size of the compressed rlp-
344+
/// encoded input transaction, the current L1 base fee, and the various dynamic parameters, after the Galileo fork.
345+
/// @param _data Signed fully RLP-encoded transaction to get the L1 fee for, compressed using zstd.
346+
/// @return L1 fee that should be paid for the tx
347+
function _getL1FeeGalileo(bytes memory _data) private view returns (uint256) {
348+
uint256 baseTerm = (commitScalar * l1BaseFee + blobScalar * l1BlobBaseFee) * _data.length;
349+
uint256 penaltyTerm = (baseTerm * _data.length) / penaltyFactor;
350+
return (baseTerm + penaltyTerm) / PRECISION;
351+
}
337352
}

src/test/L1GasPriceOracle.t.sol

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,22 +260,23 @@ contract L1GasPriceOracleTest is DSTestPlus {
260260
uint256 _blobBaseFee,
261261
uint256 _commitScalar,
262262
uint256 _blobScalar,
263-
uint256 _penaltyThreshold,
264263
uint256 _penaltyFactor,
265264
bytes memory _data
266265
) external {
267266
_baseFee = bound(_baseFee, 0, 1e9 * 20000); // max 20k gwei
268267
_blobBaseFee = bound(_blobBaseFee, 0, 1e9 * 20000); // max 20k gwei
269268
_commitScalar = bound(_commitScalar, 0, MAX_COMMIT_SCALAR);
270269
_blobScalar = bound(_blobScalar, 0, MAX_BLOB_SCALAR);
271-
_penaltyThreshold = bound(_penaltyThreshold, 1e9, 1e9 * 5);
270+
// Note: setPenaltyThreshold is deprecated
271+
// _penaltyThreshold = bound(_penaltyThreshold, 1e9, 1e9 * 5);
272272
_penaltyFactor = bound(_penaltyFactor, 1e9, 1e9 * 10); // min 1x, max 10x penalty
273273

274274
oracle.enableFeynman();
275275
oracle.setCommitScalar(_commitScalar);
276276
oracle.setBlobScalar(_blobScalar);
277277
oracle.setL1BaseFeeAndBlobBaseFee(_baseFee, _blobBaseFee);
278-
oracle.setPenaltyThreshold(_penaltyThreshold);
278+
// Note: setPenaltyThreshold is deprecated
279+
// oracle.setPenaltyThreshold(_penaltyThreshold);
279280
oracle.setPenaltyFactor(_penaltyFactor);
280281

281282
assertEq(
@@ -285,4 +286,30 @@ contract L1GasPriceOracleTest is DSTestPlus {
285286
PRECISION
286287
);
287288
}
289+
290+
function testGetL1FeeGalileo(
291+
uint256 _baseFee,
292+
uint256 _blobBaseFee,
293+
uint256 _commitScalar,
294+
uint256 _blobScalar,
295+
uint256 _penaltyFactor,
296+
bytes memory _data
297+
) external {
298+
_baseFee = bound(_baseFee, 0, 1e9 * 20000); // max 20k gwei
299+
_blobBaseFee = bound(_blobBaseFee, 0, 1e9 * 20000); // max 20k gwei
300+
_commitScalar = bound(_commitScalar, 0, MAX_COMMIT_SCALAR);
301+
_blobScalar = bound(_blobScalar, 0, MAX_BLOB_SCALAR);
302+
_penaltyFactor = bound(_penaltyFactor, 1, 1e9 * 100);
303+
304+
oracle.enableGalileo();
305+
oracle.setCommitScalar(_commitScalar);
306+
oracle.setBlobScalar(_blobScalar);
307+
oracle.setL1BaseFeeAndBlobBaseFee(_baseFee, _blobBaseFee);
308+
oracle.setPenaltyFactor(_penaltyFactor);
309+
310+
uint256 _baseTerm = (_commitScalar * _baseFee + _blobScalar * _blobBaseFee) * _data.length;
311+
uint256 _penaltyTerm = (_baseTerm * _data.length) / _penaltyFactor;
312+
313+
assertEq(oracle.getL1Fee(_data), (_baseTerm + _penaltyTerm) / PRECISION);
314+
}
288315
}

0 commit comments

Comments
 (0)