-
Notifications
You must be signed in to change notification settings - Fork 234
feat(L2): add BaseTime predeploy #364
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
4 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,24 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| // Interfaces | ||
| import { ISemver } from "interfaces/universal/ISemver.sol"; | ||
|
|
||
| /// @title IBaseTime | ||
| /// @notice Interface for the BaseTime predeploy. | ||
| interface IBaseTime is ISemver { | ||
| /// @notice Thrown when a caller other than the protocol depositor attempts to update BaseTime. | ||
| error BaseTime_NotDepositor(); | ||
|
|
||
| /// @notice Thrown when the millisecond component is not aligned to a 200 millisecond interval. | ||
| error BaseTime_InvalidTimestampMillisPart(); | ||
|
|
||
| /// @notice Returns the millisecond component of the current L2 block timestamp. | ||
| function timestampMillisPart() external view returns (uint16); | ||
|
|
||
| /// @notice Returns the current L2 block timestamp in milliseconds. | ||
| function timestampMs() external view returns (uint64 timestampMs_); | ||
|
|
||
| /// @notice Updates the millisecond component of the current L2 block timestamp. | ||
| function setTimestampMillisPart(uint16 _timestampMillisPart) external; | ||
| } |
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,64 @@ | ||
| [ | ||
| { | ||
| "inputs": [ | ||
| { | ||
| "internalType": "uint16", | ||
| "name": "_timestampMillisPart", | ||
| "type": "uint16" | ||
| } | ||
| ], | ||
| "name": "setTimestampMillisPart", | ||
| "outputs": [], | ||
| "stateMutability": "nonpayable", | ||
| "type": "function" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "timestampMillisPart", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "uint16", | ||
| "name": "", | ||
| "type": "uint16" | ||
| } | ||
| ], | ||
| "stateMutability": "view", | ||
| "type": "function" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "timestampMs", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "uint64", | ||
| "name": "timestampMs_", | ||
| "type": "uint64" | ||
| } | ||
| ], | ||
| "stateMutability": "view", | ||
| "type": "function" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "version", | ||
| "outputs": [ | ||
| { | ||
| "internalType": "string", | ||
| "name": "", | ||
| "type": "string" | ||
| } | ||
| ], | ||
| "stateMutability": "view", | ||
| "type": "function" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "BaseTime_InvalidTimestampMillisPart", | ||
| "type": "error" | ||
| }, | ||
| { | ||
| "inputs": [], | ||
| "name": "BaseTime_NotDepositor", | ||
| "type": "error" | ||
| } | ||
| ] |
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,9 @@ | ||
| [ | ||
| { | ||
| "bytes": "2", | ||
| "label": "timestampMillisPart", | ||
| "offset": 0, | ||
| "slot": "0", | ||
| "type": "uint16" | ||
| } | ||
| ] |
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,36 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.15; | ||
|
|
||
| // Libraries | ||
| import { Constants } from "src/libraries/Constants.sol"; | ||
|
|
||
| // Interfaces | ||
| import { IBaseTime } from "interfaces/L2/IBaseTime.sol"; | ||
|
|
||
| /// @custom:proxied true | ||
| /// @custom:predeploy 0x4200000000000000000000000000000000000030 | ||
| /// @title BaseTime | ||
| /// @notice Exposes the millisecond component of the current L2 block timestamp. | ||
| contract BaseTime is IBaseTime { | ||
| /// @notice Semantic version. | ||
| /// @custom:semver 1.0.0 | ||
| string public constant version = "1.0.0"; | ||
|
|
||
| /// @inheritdoc IBaseTime | ||
| uint16 public timestampMillisPart; | ||
|
|
||
| /// @inheritdoc IBaseTime | ||
| function timestampMs() external view returns (uint64 timestampMs_) { | ||
| timestampMs_ = uint64(block.timestamp * 1000 + timestampMillisPart); | ||
| } | ||
|
|
||
| /// @inheritdoc IBaseTime | ||
| function setTimestampMillisPart(uint16 _timestampMillisPart) external { | ||
| if (msg.sender != Constants.DEPOSITOR_ACCOUNT) revert BaseTime_NotDepositor(); | ||
| if (_timestampMillisPart > 800 || _timestampMillisPart % 200 != 0) { | ||
| revert BaseTime_InvalidTimestampMillisPart(); | ||
| } | ||
|
|
||
| timestampMillisPart = _timestampMillisPart; | ||
| } | ||
| } |
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,87 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.15; | ||
|
|
||
| // Testing | ||
| import { Test } from "forge-std/Test.sol"; | ||
|
|
||
| // Libraries | ||
| import { Constants } from "src/libraries/Constants.sol"; | ||
|
|
||
| // Target contract | ||
| import { BaseTime } from "src/L2/BaseTime.sol"; | ||
| import { IBaseTime } from "interfaces/L2/IBaseTime.sol"; | ||
|
|
||
| /// @title BaseTime_TestInit | ||
| /// @notice Reusable test initialization for BaseTime tests. | ||
| abstract contract BaseTime_TestInit is Test { | ||
| BaseTime internal baseTime; | ||
|
|
||
| /// @notice Sets up the test suite. | ||
| function setUp() public { | ||
| baseTime = new BaseTime(); | ||
| } | ||
|
|
||
| /// @notice Sets the millisecond component as the protocol depositor. | ||
| function setTimestampMillisPart(uint16 _timestampMillisPart) internal { | ||
| vm.prank(Constants.DEPOSITOR_ACCOUNT); | ||
| baseTime.setTimestampMillisPart(_timestampMillisPart); | ||
| } | ||
| } | ||
|
|
||
| /// @title BaseTime_TimestampMillisPart_Test | ||
| /// @notice Tests BaseTime's initial value. | ||
| contract BaseTime_TimestampMillisPart_Test is BaseTime_TestInit { | ||
| /// @notice Tests that the millisecond component initially equals zero. | ||
| function test_initialValue_succeeds() external view { | ||
| assertEq(baseTime.timestampMillisPart(), 0); | ||
| } | ||
| } | ||
|
|
||
| /// @title BaseTime_SetTimestampMillisPart_Test | ||
| /// @notice Tests updating BaseTime's millisecond component. | ||
| contract BaseTime_SetTimestampMillisPart_Test is BaseTime_TestInit { | ||
| /// @notice Tests every valid millisecond component. | ||
| function test_setTimestampMillisPart_succeeds() external { | ||
| uint16[5] memory validValues = [uint16(0), 200, 400, 600, 800]; | ||
|
|
||
| for (uint256 i; i < validValues.length; i++) { | ||
| setTimestampMillisPart(validValues[i]); | ||
| assertEq(baseTime.timestampMillisPart(), validValues[i]); | ||
| } | ||
| } | ||
|
|
||
| /// @notice Tests that an invalid millisecond component is rejected. | ||
| function testFuzz_setTimestampMillisPart_invalidValue_reverts(uint16 _timestampMillisPart) external { | ||
| vm.assume(_timestampMillisPart > 800 || _timestampMillisPart % 200 != 0); | ||
| vm.expectRevert(IBaseTime.BaseTime_InvalidTimestampMillisPart.selector); | ||
| vm.prank(Constants.DEPOSITOR_ACCOUNT); | ||
| baseTime.setTimestampMillisPart(_timestampMillisPart); | ||
| } | ||
|
|
||
| /// @notice Tests that callers other than the protocol depositor are rejected. | ||
| function testFuzz_setTimestampMillisPart_notDepositor_reverts(address _caller) external { | ||
| vm.assume(_caller != Constants.DEPOSITOR_ACCOUNT); | ||
| vm.expectRevert(IBaseTime.BaseTime_NotDepositor.selector); | ||
| vm.prank(_caller); | ||
| baseTime.setTimestampMillisPart(200); | ||
| } | ||
|
|
||
| /// @notice Tests that the millisecond component occupies slot zero as a uint16. | ||
| function test_setTimestampMillisPart_usesSlotZero_succeeds() external { | ||
| setTimestampMillisPart(600); | ||
|
|
||
| assertEq(vm.load(address(baseTime), bytes32(0)), bytes32(uint256(600))); | ||
| } | ||
| } | ||
|
|
||
| /// @title BaseTime_TimestampMs_Test | ||
| /// @notice Tests BaseTime's full millisecond timestamp getter. | ||
| contract BaseTime_TimestampMs_Test is BaseTime_TestInit { | ||
| /// @notice Tests that timestampMs combines block.timestamp with the millisecond component. | ||
| function test_timestampMs_succeeds() external { | ||
| vm.warp(1725); | ||
| setTimestampMillisPart(600); | ||
|
|
||
| assertEq(baseTime.timestampMs(), 1_725_600); | ||
| } | ||
| } |
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.