-
Notifications
You must be signed in to change notification settings - Fork 198
fjord: Add FastLZ L1-Cost function specs #25
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c5f087e
Add specs for the FastLZ change
mdehoog 275d0f8
Specify fjord must activate one block after ecotone
danyalprout 26490d3
Update code hash / bytecode
danyalprout 2dd412c
Update bytecode / code hash / commit
danyalprout 44edb4f
Specify removal
danyalprout 90e521d
Merge branch 'main' into fastlz
danyalprout 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
There are no files selected for viewing
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,80 @@ | ||
| # L2 Execution Engine | ||
|
|
||
| <!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
| <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
| **Table of Contents** | ||
|
|
||
| - [Fees](#fees) | ||
| - [L1-Cost fees (L1 Fee Vault)](#l1-cost-fees-l1-fee-vault) | ||
| - [Fjord L1-Cost fee changes (FastLZ estimator)](#fjord-l1-cost-fee-changes-fastlz-estimator) | ||
| - [FastLZ Implementation](#fastlz-implementation) | ||
| - [L1-Cost linear regression details](#l1-cost-linear-regression-details) | ||
| - [L1 Gas Usage Estimation](#l1-gas-usage-estimation) | ||
|
|
||
| <!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
|
||
| ## Fees | ||
|
|
||
| ### L1-Cost fees (L1 Fee Vault) | ||
|
|
||
| #### Fjord L1-Cost fee changes (FastLZ estimator) | ||
|
|
||
| Fjord updates the L1 cost calculation function to use a FastLZ-based compression estimator. | ||
| The L1 cost is computed as: | ||
|
|
||
| ```pseudocode | ||
| l1FeeScaled = baseFeeScalar*l1BaseFee*16 + blobFeeScalar*l1BlobBaseFee | ||
| estimatedSize = max(minTransactionSize, intercept + fastlzCoef*fastlzSize) | ||
| l1Cost = estimatedSize * l1FeeScaled / 1e12 | ||
| ``` | ||
|
|
||
| The final `l1Cost` computation is an unlimited precision unsigned integer computation, with the result in Wei and | ||
| having `uint256` range. The values in this computation, are as follows: | ||
|
|
||
mdehoog marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| | Input arg | Type | Description | Value | | ||
| |----------------------|-----------|-------------------------------------------------------------------|--------------------------| | ||
| | `l1BaseFee` | `uint256` | L1 base fee of the latest L1 origin registered in the L2 chain | varies, L1 fee | | ||
| | `l1BlobBaseFee` | `uint256` | Blob gas price of the latest L1 origin registered in the L2 chain | varies, L1 fee | | ||
| | `fastlzSize` | `uint256` | Size of the FastLZ-compressed RLP-encoded signed tx | varies, per transaction | | ||
| | `baseFeeScalar` | `uint32` | L1 base fee scalar, scaled by `1e6` | varies, L2 configuration | | ||
| | `blobFeeScalar` | `uint32` | L1 blob fee scalar, scaled by `1e6` | varies, L2 configuration | | ||
| | `intercept` | `int32` | Intercept constant, scaled by `1e6` (can be negative) | -42_585_600 | | ||
| | `fastlzCoef` | `uint32` | FastLZ coefficient, scaled by `1e6` | 836_500 | | ||
| | `minTransactionSize` | `uint32` | A lower bound on transaction size, in bytes | 100 | | ||
|
|
||
| Previously, `baseFeeScalar` and `blobFeeScalar` were used to encode the compression ratio, due to the inaccuracy of | ||
| the L1 cost function. However, the new cost function takes into account the compression ratio, so these scalars should | ||
| be adjusted to account for any previous compression ratio they encoded. | ||
|
|
||
| ##### FastLZ Implementation | ||
|
|
||
| All compression algorithms must be implemented equivalently to the `fastlz_compress` function in `fastlz.c` at the | ||
| following [commit](https://github.com/ariya/FastLZ/blob/344eb4025f9ae866ebf7a2ec48850f7113a97a42/fastlz.c#L482-L506). | ||
|
|
||
| ##### L1-Cost linear regression details | ||
|
|
||
| The `intercept` and `fastlzCoef` constants are calculated by linear regression using a dataset | ||
| of previous L2 transactions. The dataset is generated by iterating over all transactions in a given time range, and | ||
| performing the following actions. For each transaction: | ||
|
|
||
| 1. Compress the payload using FastLZ. Record the size of the compressed payload as `fastlzSize`. | ||
| 2. Emulate the change in batch size adding the transaction to a batch, compressed with Brotli 10. Record the change in | ||
| batch size as `bestEstimateSize`. | ||
|
|
||
| Once this dataset is generated, a linear regression can be calculated using the `bestEstimateSize` as | ||
| the dependent variable and `fastlzSize` as the independent variable. | ||
|
|
||
| We generated a dataset from two weeks of post-Ecotone transactions on Optimism Mainnet, as we found that was | ||
| the most representative of performance across multiple chains and time periods. More details on the linear regression | ||
| and datasets used can be found in this [repository](https://github.com/roberto-bayardo/compression-analysis/tree/main). | ||
|
|
||
| ### L1 Gas Usage Estimation | ||
|
|
||
| The `L1GasUsed` property on the transaction receipt is updated to take into account the improvement in | ||
| [compression estimation](./exec-engine.md#fees) accuracy. The value will be calculated by | ||
| multiplying the `estimatedSize` of the transaction from the above L1 cost formula by 16. The value of 16 assumes most | ||
| of the bytes in the compressed data are non-zero. | ||
|
|
||
| The `L1GasUsed` property will be deprecated due to it not accurately calculating the L1 gas used | ||
| by a transaction. Users can continue to use the `L1Fee` field to retrieve the L1 fee for a given transaction. This field | ||
| will be removed in a future network upgrade. | ||
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,77 @@ | ||
| # Predeploys | ||
|
|
||
| <!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
| <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
| **Table of Contents** | ||
|
|
||
| - [GasPriceOracle](#gaspriceoracle) | ||
| - [L1 Gas Usage Estimation](#l1-gas-usage-estimation) | ||
|
|
||
| <!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
|
||
| ## GasPriceOracle | ||
|
|
||
| Following the Fjord upgrade, three additional values used for L1 fee computation are: | ||
|
|
||
| - costIntercept | ||
| - costFastlzCoef | ||
| - minTransactionSize | ||
|
|
||
| These values are hard-coded constants in the `GasPriceOracle` contract. The | ||
| calculation follows the same formula outlined in the | ||
| [Fjord L1-Cost fee changes (FastLZ estimator)](./exec-engine.md#fjord-l1-cost-fee-changes-fastlz-estimator) | ||
| section. | ||
|
|
||
| A new method is introduced: `getL1FeeUpperBound(uint256)`. This method returns an upper bound for the L1 fee | ||
| for a given transaction size. It is provided for callers who wish to estimate L1 transaction costs in the | ||
mdehoog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| write path, and is much more gas efficient than `getL1Fee`. | ||
|
|
||
| The upper limit overhead is assumed to be `original/255+16`, borrowed from LZ4. According to historical data, this | ||
| approach can encompass more than 99.99% of transactions. | ||
|
|
||
| This is implemented as follows: | ||
|
|
||
| ```solidity | ||
| function getL1FeeUpperBound(uint256 unsignedTxSize) external view returns (uint256) { | ||
sebastianst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Add 68 to account for unsigned tx | ||
| uint256 txSize = unsignedTxSize + 68; | ||
| // txSize / 255 + 16 is the pratical fastlz upper-bound covers 99.99% txs. | ||
| uint256 flzUpperBound = txSize + txSize / 255 + 16; | ||
|
|
||
| int256 estimatedSize = costIntercept + costFastlzCoef * flzUpperBound; | ||
| if (estimatedSize < minTransactionSize) { | ||
| estimatedSize = minTransactionSize; | ||
| } | ||
|
|
||
| uint256 l1FeeScaled = baseFeeScalar() * l1BaseFee() * 16 + blobBaseFeeScalar() * blobBaseFee(); | ||
| return uint256(estimatedSize) * feeScaled / (10 ** (DECIMALS * 2)); | ||
| } | ||
| ``` | ||
|
|
||
| ### L1 Gas Usage Estimation | ||
|
|
||
| The `getL1GasUsed` method is updated to take into account the improved [compression estimation](./exec-engine.md#fees) | ||
| accuracy as part of the Fjord upgrade. | ||
|
|
||
| ```solidity | ||
| function getL1GasUsed(bytes memory _data) public view returns (uint256) { | ||
| if (isFjord) { | ||
| // Add 68 to the size to account for the unsigned tx | ||
| int256 flzSize = LibZip.flzCompress(_data).length + 68; | ||
|
|
||
| int256 estimatedSize = costIntercept + costFastlzCoef * flzSize; | ||
| if (estimatedSize < minTransactionSize) { | ||
| estimatedSize = minTransactionSize; | ||
| } | ||
|
|
||
| // Assume the compressed data is mostly non-zero, and would pay 16 gas per calldata byte | ||
| return estimatedSize * 16; | ||
| } | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| The `getL1GasUsed` method will be deprecated. This is due to it not accurately estimating the | ||
sebastianst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| L1 gas used, for a transaction. In a future network upgrade this function will revert when called. | ||
|
|
||
| Users can continue to use the `getL1FeeUpperBound` or `getL1Fee` method to estimate the L1 fee for a given transaction. | ||
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.
Uh oh!
There was an error while loading. Please reload this page.