-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat: L1Block uses average L1 basefee #2583
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.9; | ||
|
|
||
| import { FixedPointMathLib } from "@rari-capital/solmate/src/utils/FixedPointMathLib.sol"; | ||
|
|
||
| /** | ||
| * @title Lib_FeeSmoothing | ||
| */ | ||
| library Lib_FeeSmoothing { | ||
| /** | ||
| * @notice Weight applied to average | ||
| */ | ||
| uint256 constant internal prevWeight = 7e18; | ||
|
|
||
| /** | ||
| * @notice Weight applied to value being added to average | ||
| */ | ||
| uint256 constant internal nextWeight = 3e18; | ||
|
|
||
| /** | ||
| * @notice Computes a rolling average | ||
| */ | ||
| function rollingAverage( | ||
| uint256 prev, | ||
| uint256 next | ||
| ) internal returns (uint256) { | ||
| uint256 a = saturatingMul(prev, FixedPointMathLib.divWadDown(prevWeight, 1e19)); | ||
| uint256 b = saturatingMul(next, FixedPointMathLib.divWadDown(nextWeight, 1e19)); | ||
| return saturatingAdd(a, b) / 1e18; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Saturating multiplication | ||
| */ | ||
| function saturatingMul(uint256 a, uint256 b) internal returns (uint256) { | ||
| unchecked { | ||
| if (a == 0) { | ||
| return 0; | ||
| } | ||
| uint256 c = a * b; | ||
| if (c / a != b) { | ||
| return type(uint256).max; | ||
| } | ||
| return c; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @notice Saturating addition | ||
| */ | ||
| function saturatingAdd(uint256 a, uint256 b) internal returns (uint256) { | ||
| unchecked { | ||
| uint256 c = a + b; | ||
| if (c < a) { | ||
| return type(uint256).max; | ||
| } | ||
| return c; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,15 @@ pragma solidity 0.8.10; | |
|
|
||
| import { CommonTest } from "./CommonTest.t.sol"; | ||
| import { L1Block } from "../L2/L1Block.sol"; | ||
| import { Lib_FeeSmoothing } from "../libraries/Lib_FeeSmoothing.sol"; | ||
|
|
||
| contract L1BlockTest is CommonTest { | ||
| L1Block lb; | ||
| address depositor; | ||
| bytes32 immutable NON_ZERO_HASH = keccak256(abi.encode(1)); | ||
|
|
||
| uint256 averageBasefee; | ||
|
|
||
| function setUp() external { | ||
| lb = new L1Block(); | ||
| depositor = lb.DEPOSITOR_ACCOUNT(); | ||
|
|
@@ -17,11 +20,14 @@ contract L1BlockTest is CommonTest { | |
| } | ||
|
|
||
| function test_updatesValues(uint64 n, uint64 t, uint256 b, bytes32 h, uint64 s) external { | ||
| averageBasefee = Lib_FeeSmoothing.rollingAverage(averageBasefee, b); | ||
|
|
||
| vm.prank(depositor); | ||
| lb.setL1BlockValues(n, t, b, h, s); | ||
| assertEq(lb.number(), n); | ||
| assertEq(lb.timestamp(), t); | ||
| assertEq(lb.basefee(), b); | ||
| assertEq(lb.averageBasefee(), averageBasefee); | ||
| assertEq(lb.hash(), h); | ||
| assertEq(lb.sequenceNumber(), s); | ||
| } | ||
|
|
@@ -38,6 +44,11 @@ contract L1BlockTest is CommonTest { | |
| assertEq(lb.basefee(), 3); | ||
| } | ||
|
|
||
| function test_averageBasefee() external { | ||
| uint256 expect = Lib_FeeSmoothing.rollingAverage(0, 3); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not sufficient as a test, we need unit tests that are realistic |
||
| assertEq(lb.averageBasefee(), expect); | ||
| } | ||
|
|
||
| function test_hash() external { | ||
| assertEq(lb.hash(), NON_ZERO_HASH); | ||
| } | ||
|
|
||
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 value here is
30which I believe is realistic if 70% weighted towards prev value and 30% weighted towards next value. We need unit tests for therollingAveragefunction, not 100% sure of the best way to go about it