-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] adjust additionalGasUsed calculation + gas measurement docs #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prin-r
wants to merge
8
commits into
main
Choose a base branch
from
gas-measure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b25213b
gas-measure-tests-docs
prin-r cf57531
fix docs
prin-r 85722de
fix Constants.sol
prin-r 6251d07
update docs
prin-r 9cd4088
fix as comments
prin-r 3c4b0b9
add work-in-progress
prin-r cb56627
resolve conflicts
prin-r 16636ab
fix definition
prin-r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; | ||
import "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol"; | ||
|
||
/** | ||
* @title L1RouterGasCalculator | ||
* @notice Owns and evaluates a quadratic model for base gas as a function of calldata size. | ||
* | ||
* packedAdditionalGasFuncCoeffs layout (fixed-point 1e18): | ||
* packed = [ c2 | c1 | c0 ], each ci is uint80 (0..2^80-1) representing ci/1e18 | ||
* | ||
* f(x) = (c2*x^2 + c1*x + c0) / 1e18, where x = calldata size in bytes. | ||
* | ||
*/ | ||
abstract contract L1RouterGasCalculator is | ||
Initializable, | ||
Ownable2StepUpgradeable | ||
{ | ||
// ----- packing constants ----- | ||
uint256 internal constant COEFF_BITS = 80; | ||
uint256 internal constant COEFF_MASK = (uint256(1) << COEFF_BITS) - 1; | ||
uint256 internal constant FP_SCALE = 1e18; | ||
uint256 internal constant SHIFT_C1 = COEFF_BITS; | ||
uint256 internal constant SHIFT_C2 = COEFF_BITS * 2; | ||
|
||
/// @dev packed coefficients: [c2 (80b) | c1 (80b) | c0 (80b)] | ||
uint256 public packedAdditionalGasFuncCoeffs; | ||
|
||
/// @dev maximum calldata length (bytes) the model will accept. | ||
uint256 public maxCalldataBytes; | ||
|
||
/** | ||
* @notice Emitted when the packed coefficients are updated. | ||
* @param packedCoeffs The new packed value [c2|c1|c0] (fixed-point 1e18 lanes). | ||
*/ | ||
event PackedAdditionalGasFuncCoeffsSet(uint256 packedCoeffs); | ||
|
||
/** | ||
* @notice Emitted when the maximum supported calldata length is updated. | ||
* @param maxBytes New maximum calldata bytes accepted by the model. | ||
*/ | ||
event MaxCalldataBytesSet(uint256 maxBytes); | ||
|
||
error CoefficientOutOfRange(); // any ci > 2^80-1 | ||
error CalldataSizeTooLarge(uint256 got, uint256 maxAllowed); | ||
|
||
function __L1RouterGasCalculator_init( | ||
uint256 packedCoeffs, | ||
uint256 maxBytes | ||
) internal onlyInitializing { | ||
_setPackedAdditionalGasFuncCoeffs(packedCoeffs); | ||
_setMaxCalldataBytes(maxBytes); | ||
} | ||
|
||
/// @notice Pack 3×80-bit fixed-point (1e18) coefficients into one uint256. | ||
function packCoeffs( | ||
uint256 c2, | ||
uint256 c1, | ||
uint256 c0 | ||
) public pure returns (uint256 packedCoeffs) { | ||
if (c2 > COEFF_MASK || c1 > COEFF_MASK || c0 > COEFF_MASK) { | ||
revert CoefficientOutOfRange(); | ||
} | ||
unchecked { | ||
packedCoeffs = (c2 << SHIFT_C2) | (c1 << SHIFT_C1) | c0; | ||
} | ||
} | ||
|
||
/// @notice Unpack a provided packed value into its (c2, c1, c0) lanes. | ||
function unpackCoeffs( | ||
uint256 packedCoeffs | ||
) public pure returns (uint256 c2, uint256 c1, uint256 c0) { | ||
unchecked { | ||
c2 = (packedCoeffs >> SHIFT_C2) & COEFF_MASK; | ||
c1 = (packedCoeffs >> SHIFT_C1) & COEFF_MASK; | ||
c0 = packedCoeffs & COEFF_MASK; | ||
} | ||
} | ||
|
||
/// @notice View the currently stored coefficients. | ||
function currentCoeffs() | ||
public | ||
view | ||
returns (uint256 c2, uint256 c1, uint256 c0) | ||
{ | ||
return unpackCoeffs(packedAdditionalGasFuncCoeffs); | ||
} | ||
|
||
/// @dev Store a new packed coefficient triple and emit. | ||
function _setPackedAdditionalGasFuncCoeffs(uint256 packedCoeffs) internal { | ||
packedAdditionalGasFuncCoeffs = packedCoeffs; | ||
emit PackedAdditionalGasFuncCoeffsSet(packedCoeffs); | ||
} | ||
|
||
/// @notice Owner: set the maximum accepted calldata bytes. | ||
function setMaxCalldataBytes(uint256 maxBytes) external onlyOwner { | ||
_setMaxCalldataBytes(maxBytes); | ||
} | ||
|
||
/// @dev Internal setter with event. | ||
function _setMaxCalldataBytes(uint256 maxBytes) internal { | ||
maxCalldataBytes = maxBytes; | ||
emit MaxCalldataBytesSet(maxBytes); | ||
} | ||
|
||
/** | ||
* @dev Evaluate baseGas(x) in *gas units* for a calldata length `x` (bytes). | ||
* Reverts if `x` exceeds `maxCalldataBytes`. | ||
* Returns the quadratic (c2*x^2 + c1*x + c0)/1e18 using the stored coefficients. | ||
* | ||
* @param x Calldata size in bytes (i.e., `calldatasize()` when called from a router). | ||
*/ | ||
function _additionalGasForCalldata( | ||
uint256 x | ||
) internal view returns (uint256 y) { | ||
if (x > maxCalldataBytes) | ||
revert CalldataSizeTooLarge(x, maxCalldataBytes); | ||
(uint256 c2, uint256 c1, uint256 c0) = unpackCoeffs( | ||
packedAdditionalGasFuncCoeffs | ||
); | ||
unchecked { | ||
y = (c2 * x * x + c1 * x + c0) / FP_SCALE; | ||
} | ||
} | ||
|
||
/// @notice Public preview of baseGas(x) using the stored coefficients. | ||
function additionalGasForCalldata( | ||
uint256 x | ||
) external view returns (uint256 y) { | ||
return _additionalGasForCalldata(x); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it linear though? (also L1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The total gas on L2 is made up of L1 and L2 gas. In the experiment, the gas on L2 is strongly linear, despite the fact that memory expansion should still occur, but its effect on L2 is insignificant.
However, the behavior of L1 gas on L2 is linear, but the maximum error can be large due to the unknown behavior of the compression algorithm and the calldata itself.