-
Notifications
You must be signed in to change notification settings - Fork 157
Add BaseFeeManager contract #416
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
TucksonDev
wants to merge
2
commits into
develop
Choose a base branch
from
base-fee-manager
base: develop
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.
+325
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright 2022-2026, Offchain Labs, Inc. | ||
| // For license information, see https://github.com/nitro/blob/master/LICENSE | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| pragma solidity ^0.8.0; | ||
|
|
||
| import "../precompiles/ArbOwner.sol"; | ||
| import "../precompiles/ArbGasInfo.sol"; | ||
| import "@openzeppelin/contracts/access/AccessControlEnumerable.sol"; | ||
|
|
||
| contract BaseFeeManager is AccessControlEnumerable { | ||
| ArbOwner internal constant ARB_OWNER = ArbOwner(address(0x70)); | ||
| ArbGasInfo internal constant ARB_GAS_INFO = ArbGasInfo(address(0x6c)); | ||
|
TucksonDev marked this conversation as resolved.
|
||
| bytes32 public constant MANAGER_ROLE = keccak256("MANAGER_ROLE"); | ||
|
|
||
| uint256 public constant MIN_BASE_FEE_WEI = 0.01 gwei; | ||
| uint256 public constant MAX_BASE_FEE_WEI = 0.1 gwei; | ||
|
|
||
| uint256 public immutable expiryTimestamp; | ||
|
|
||
| error InvalidBaseFee(uint256 newL2BaseFee); | ||
| error BaseFeeBelowMinimum(uint256 newL2BaseFee, uint256 minimumBaseFee); | ||
| error NotExpired(uint256 expiryTimestamp); | ||
|
|
||
| constructor(address admin, address manager, uint256 _expiryTimestamp) { | ||
| _setupRole(DEFAULT_ADMIN_ROLE, admin); | ||
| _setupRole(MANAGER_ROLE, manager); | ||
| expiryTimestamp = _expiryTimestamp; | ||
| } | ||
|
|
||
| /// @notice Removes the contract from the list of chain owners after the expiry timestamp | ||
| function revoke() external { | ||
| if (block.timestamp < expiryTimestamp) { | ||
| revert NotExpired(expiryTimestamp); | ||
| } | ||
| ARB_OWNER.removeChainOwner(address(this)); | ||
| } | ||
|
|
||
| /// @notice Sets the L2 base fee within the allowed range, and above the minimum base fee | ||
| /// @param newL2BaseFeeInWei The new L2 base fee to set (in wei) | ||
|
waelsy123 marked this conversation as resolved.
|
||
| function setL2BaseFee( | ||
| uint256 newL2BaseFeeInWei | ||
| ) external onlyRole(MANAGER_ROLE) { | ||
| if (newL2BaseFeeInWei < MIN_BASE_FEE_WEI || newL2BaseFeeInWei > MAX_BASE_FEE_WEI) { | ||
| revert InvalidBaseFee(newL2BaseFeeInWei); | ||
| } | ||
|
|
||
| uint256 minimumL2BaseFee = ARB_GAS_INFO.getMinimumGasPrice(); | ||
| if (newL2BaseFeeInWei < minimumL2BaseFee) { | ||
| revert BaseFeeBelowMinimum(newL2BaseFeeInWei, minimumL2BaseFee); | ||
| } | ||
|
waelsy123 marked this conversation as resolved.
|
||
|
|
||
| ARB_OWNER.setL2BaseFee(newL2BaseFeeInWei); | ||
| } | ||
|
|
||
| /// @notice Sets the minimum L2 base fee within the allowed range | ||
| /// @param newMinimumL2BaseFeeInWei The new minimum L2 base fee to set (in wei) | ||
| function setMinimumL2BaseFee( | ||
| uint256 newMinimumL2BaseFeeInWei | ||
| ) external onlyRole(MANAGER_ROLE) { | ||
| if ( | ||
| newMinimumL2BaseFeeInWei < MIN_BASE_FEE_WEI | ||
| || newMinimumL2BaseFeeInWei > MAX_BASE_FEE_WEI | ||
| ) { | ||
| revert InvalidBaseFee(newMinimumL2BaseFeeInWei); | ||
| } | ||
|
|
||
| ARB_OWNER.setMinimumL2BaseFee(newMinimumL2BaseFeeInWei); | ||
| } | ||
| } | ||
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,255 @@ | ||
| // SPDX-License-Identifier: UNLICENSED | ||
| pragma solidity ^0.8.4; | ||
|
|
||
| import "forge-std/Test.sol"; | ||
| import "../../src/chain/BaseFeeManager.sol"; | ||
|
|
||
| // We need this library to storage the addresses of the mocked contracts, | ||
| // since they have to be available in both BaseFeeManagerTest and the mock contracts | ||
| library AddressStore { | ||
| address constant StorageContractAddress = address(0xabcdef); | ||
| address constant ArbOwnerAddress = address(0x70); | ||
| address constant ArbGasInfoAddress = address(0x6c); | ||
| } | ||
|
|
||
| contract BaseFeeManagerTest is Test { | ||
| ArbOwnerMock internal constant ARB_OWNER = ArbOwnerMock(AddressStore.ArbOwnerAddress); | ||
| ArbGasInfoMock internal constant ARB_GAS_INFO = ArbGasInfoMock(AddressStore.ArbGasInfoAddress); | ||
|
|
||
| address constant admin = address(1337); | ||
| address constant manager = address(7331); | ||
| uint256 constant expiryTimestamp = 12345678; | ||
|
|
||
| BaseFeeManager public baseFeeManager; | ||
|
|
||
| constructor() { | ||
| baseFeeManager = new BaseFeeManager(admin, manager, expiryTimestamp); | ||
| vm.etch(AddressStore.StorageContractAddress, type(StorageContractMock).runtimeCode); | ||
| vm.etch(AddressStore.ArbOwnerAddress, type(ArbOwnerMock).runtimeCode); | ||
| vm.etch(AddressStore.ArbGasInfoAddress, type(ArbGasInfoMock).runtimeCode); | ||
| } | ||
|
|
||
| function test_revoke() external { | ||
| // Test before expiry | ||
| vm.warp(expiryTimestamp - 1); | ||
| vm.expectRevert( | ||
| abi.encodeWithSelector(BaseFeeManager.NotExpired.selector, expiryTimestamp) | ||
| ); | ||
| baseFeeManager.revoke(); | ||
|
|
||
| // Test after expiry | ||
| vm.warp(expiryTimestamp); | ||
| assertFalse(ARB_OWNER.removeChainOwnerCalled()); | ||
| baseFeeManager.revoke(); | ||
| assertTrue(ARB_OWNER.removeChainOwnerCalled()); | ||
| } | ||
|
|
||
| // | ||
| // --- setL2BaseFee tests --- | ||
| // | ||
| function test_setL2BaseFee_success() external { | ||
| // Set the on-chain minimum low so the cross-validation passes | ||
| ARB_OWNER.setMinimumL2BaseFee(0.01 gwei); | ||
|
|
||
| // Test at minimum | ||
| vm.prank(manager); | ||
| baseFeeManager.setL2BaseFee(0.01 gwei); | ||
|
|
||
| // Test at maximum | ||
| vm.prank(manager); | ||
| baseFeeManager.setL2BaseFee(0.1 gwei); | ||
|
|
||
| // Test in the middle | ||
| vm.prank(manager); | ||
| baseFeeManager.setL2BaseFee(0.05 gwei); | ||
| } | ||
|
|
||
| function test_setL2BaseFee_accessControl() external { | ||
| // Test non-manager cannot call | ||
| vm.expectRevert(); | ||
| baseFeeManager.setL2BaseFee(0.05 gwei); | ||
|
|
||
| // Test admin without manager role cannot call | ||
| vm.prank(admin); | ||
| vm.expectRevert(); | ||
| baseFeeManager.setL2BaseFee(0.05 gwei); | ||
|
|
||
| // Test manager can call | ||
| vm.prank(manager); | ||
| baseFeeManager.setL2BaseFee(0.05 gwei); | ||
| } | ||
|
|
||
| function test_setL2BaseFee_invalidBaseFee() external { | ||
| // Test below minimum (0.01 gwei - 1) | ||
| vm.prank(manager); | ||
| vm.expectRevert( | ||
| abi.encodeWithSelector(BaseFeeManager.InvalidBaseFee.selector, 0.01 gwei - 1) | ||
| ); | ||
| baseFeeManager.setL2BaseFee(0.01 gwei - 1); | ||
|
|
||
| // Test above maximum (0.1 gwei + 1) | ||
| vm.prank(manager); | ||
| vm.expectRevert( | ||
| abi.encodeWithSelector(BaseFeeManager.InvalidBaseFee.selector, 0.1 gwei + 1) | ||
| ); | ||
| baseFeeManager.setL2BaseFee(0.1 gwei + 1); | ||
| } | ||
|
|
||
| function test_setL2BaseFee_belowMinimumBaseFee() external { | ||
| // Set the on-chain minimum to 0.05 gwei | ||
| ARB_OWNER.setMinimumL2BaseFee(0.05 gwei); | ||
|
|
||
| // Setting base fee at the minimum should succeed | ||
| vm.prank(manager); | ||
| baseFeeManager.setL2BaseFee(0.05 gwei); | ||
|
|
||
| // Setting base fee above the minimum should succeed | ||
| vm.prank(manager); | ||
| baseFeeManager.setL2BaseFee(0.06 gwei); | ||
|
|
||
| // Setting base fee below the on-chain minimum should revert | ||
| vm.prank(manager); | ||
| vm.expectRevert( | ||
| abi.encodeWithSelector( | ||
| BaseFeeManager.BaseFeeBelowMinimum.selector, 0.04 gwei, 0.05 gwei | ||
| ) | ||
| ); | ||
| baseFeeManager.setL2BaseFee(0.04 gwei); | ||
| } | ||
|
|
||
| // | ||
| // --- setMinimumL2BaseFee tests --- | ||
| // | ||
| function test_setMinimumL2BaseFee_success() external { | ||
| // Test at minimum | ||
| vm.prank(manager); | ||
| baseFeeManager.setMinimumL2BaseFee(0.01 gwei); | ||
|
|
||
| // Test at maximum | ||
| vm.prank(manager); | ||
| baseFeeManager.setMinimumL2BaseFee(0.1 gwei); | ||
|
|
||
| // Test in the middle | ||
| vm.prank(manager); | ||
| baseFeeManager.setMinimumL2BaseFee(0.05 gwei); | ||
| } | ||
|
|
||
| function test_setMinimumL2BaseFee_accessControl() external { | ||
| // Test non-manager cannot call | ||
| vm.expectRevert(); | ||
| baseFeeManager.setMinimumL2BaseFee(0.05 gwei); | ||
|
|
||
| // Test admin without manager role cannot call | ||
| vm.prank(admin); | ||
| vm.expectRevert(); | ||
| baseFeeManager.setMinimumL2BaseFee(0.05 gwei); | ||
|
|
||
| // Test manager can call | ||
| vm.prank(manager); | ||
| baseFeeManager.setMinimumL2BaseFee(0.05 gwei); | ||
| } | ||
|
|
||
| function test_setMinimumL2BaseFee_invalidBaseFee() external { | ||
| // Test below minimum (0.01 gwei - 1) | ||
| vm.prank(manager); | ||
| vm.expectRevert( | ||
| abi.encodeWithSelector(BaseFeeManager.InvalidBaseFee.selector, 0.01 gwei - 1) | ||
| ); | ||
| baseFeeManager.setMinimumL2BaseFee(0.01 gwei - 1); | ||
|
|
||
| // Test above maximum (0.1 gwei + 1) | ||
| vm.prank(manager); | ||
| vm.expectRevert( | ||
| abi.encodeWithSelector(BaseFeeManager.InvalidBaseFee.selector, 0.1 gwei + 1) | ||
| ); | ||
| baseFeeManager.setMinimumL2BaseFee(0.1 gwei + 1); | ||
| } | ||
|
|
||
| function test_setMinimumL2BaseFee_updatesMinimumForBaseFeeCheck() external { | ||
| // Set a new minimum via the manager contract | ||
| vm.prank(manager); | ||
| baseFeeManager.setMinimumL2BaseFee(0.08 gwei); | ||
|
|
||
| // Confirm the minimum was updated in the shared storage | ||
| assertEq(ARB_GAS_INFO.getMinimumGasPrice(), 0.08 gwei); | ||
|
|
||
| // Setting base fee below the new minimum should revert | ||
| vm.prank(manager); | ||
| vm.expectRevert( | ||
| abi.encodeWithSelector( | ||
| BaseFeeManager.BaseFeeBelowMinimum.selector, 0.07 gwei, 0.08 gwei | ||
| ) | ||
| ); | ||
| baseFeeManager.setL2BaseFee(0.07 gwei); | ||
|
|
||
| // Setting base fee at the new minimum should succeed | ||
| vm.prank(manager); | ||
| baseFeeManager.setL2BaseFee(0.08 gwei); | ||
| } | ||
| } | ||
|
|
||
| // Since ArbOwner and ArbGasInfo access the same shared database, we create a mock storage contract to hold that storage and access it | ||
| // from forwarded calls in ArbOwnerMock and ArbGasInfoMock. | ||
| contract StorageContractMock { | ||
| uint256 public l2BaseFee; | ||
| uint256 public minimumL2BaseFee; | ||
|
|
||
| constructor() { | ||
| l2BaseFee = 0.02 gwei; | ||
| minimumL2BaseFee = 0.02 gwei; | ||
| } | ||
|
|
||
| function setL2BaseFee( | ||
| uint256 priceInWei | ||
| ) external { | ||
| l2BaseFee = priceInWei; | ||
| } | ||
|
|
||
| function setMinimumL2BaseFee( | ||
| uint256 priceInWei | ||
| ) external { | ||
| minimumL2BaseFee = priceInWei; | ||
| } | ||
|
|
||
| function getMinimumGasPrice() external view returns (uint256) { | ||
| return minimumL2BaseFee; | ||
| } | ||
| } | ||
|
|
||
| contract ArbGasInfoMock { | ||
| StorageContractMock internal constant STORAGE = | ||
| StorageContractMock(AddressStore.StorageContractAddress); | ||
|
|
||
| function getMinimumGasPrice() external view returns (uint256) { | ||
| return STORAGE.getMinimumGasPrice(); | ||
| } | ||
| } | ||
|
|
||
| contract ArbOwnerMock { | ||
| StorageContractMock internal constant STORAGE = | ||
| StorageContractMock(AddressStore.StorageContractAddress); | ||
|
|
||
| bool public removeChainOwnerCalled; | ||
|
|
||
| function removeChainOwner( | ||
| address | ||
| ) external { | ||
| removeChainOwnerCalled = true; | ||
| } | ||
|
|
||
| function setL2BaseFee( | ||
| uint256 priceInWei | ||
| ) external { | ||
| STORAGE.setL2BaseFee(priceInWei); | ||
| } | ||
|
|
||
| function setMinimumL2BaseFee( | ||
| uint256 priceInWei | ||
| ) external { | ||
| STORAGE.setMinimumL2BaseFee(priceInWei); | ||
| } | ||
|
|
||
| function getMinimumGasPrice() external view returns (uint256) { | ||
| return STORAGE.getMinimumGasPrice(); | ||
| } | ||
| } |
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.