-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(opsm): scaffold OP Stack Manager and tests (#11274)
* init scaffolding of OPSM contract and tests * add chainIdToBatchInboxAddress * chore: address semgrep findings * test: add auth spec * update functions sig, semver lock, snapshots * semver lock update after installing correct foundry version * udpates based on style guide * snapshots
- Loading branch information
Showing
6 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
121 changes: 121 additions & 0 deletions
121
packages/contracts-bedrock/snapshots/abi/OPStackManager.json
This file contains 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,121 @@ | ||
[ | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "uint256", | ||
"name": "_l2ChainId", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "uint32", | ||
"name": "_basefeeScalar", | ||
"type": "uint32" | ||
}, | ||
{ | ||
"internalType": "uint32", | ||
"name": "_blobBasefeeScalar", | ||
"type": "uint32" | ||
}, | ||
{ | ||
"components": [ | ||
{ | ||
"internalType": "address", | ||
"name": "proxyAdminOwner", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "systemConfigOwner", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "batcher", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "unsafeBlockSigner", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "proposer", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "challenger", | ||
"type": "address" | ||
} | ||
], | ||
"internalType": "struct OPStackManager.Roles", | ||
"name": "_roles", | ||
"type": "tuple" | ||
} | ||
], | ||
"name": "deploy", | ||
"outputs": [ | ||
{ | ||
"internalType": "contract SystemConfig", | ||
"name": "systemConfig_", | ||
"type": "address" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "version", | ||
"outputs": [ | ||
{ | ||
"internalType": "string", | ||
"name": "", | ||
"type": "string" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": true, | ||
"internalType": "uint256", | ||
"name": "l2ChainId", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"indexed": true, | ||
"internalType": "contract SystemConfig", | ||
"name": "systemConfig", | ||
"type": "address" | ||
} | ||
], | ||
"name": "Deployed", | ||
"type": "event" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "string", | ||
"name": "reason", | ||
"type": "string" | ||
} | ||
], | ||
"name": "DeploymentFailed", | ||
"type": "error" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "InvalidChainId", | ||
"type": "error" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "NotImplemented", | ||
"type": "error" | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
packages/contracts-bedrock/snapshots/storageLayout/OPStackManager.json
This file contains 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 @@ | ||
[] |
This file contains 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,67 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.15; | ||
|
||
import { ISemver } from "src/universal/ISemver.sol"; | ||
import { SystemConfig } from "src/L1/SystemConfig.sol"; | ||
|
||
/// @custom:proxied | ||
contract OPStackManager is ISemver { | ||
/// @custom:semver 1.0.0-beta.1 | ||
string public constant version = "1.0.0-beta.1"; | ||
|
||
/// @notice Represents the roles that can be set when deploying a standard OP Stack chain. | ||
struct Roles { | ||
address proxyAdminOwner; | ||
address systemConfigOwner; | ||
address batcher; | ||
address unsafeBlockSigner; | ||
address proposer; | ||
address challenger; | ||
} | ||
|
||
/// @notice Emitted when a new OP Stack chain is deployed. | ||
/// @param l2ChainId The chain ID of the new chain. | ||
/// @param systemConfig The address of the new chain's SystemConfig contract. | ||
event Deployed(uint256 indexed l2ChainId, SystemConfig indexed systemConfig); | ||
|
||
/// @notice Thrown when an invalid `l2ChainId` is provided to `deploy`. | ||
error InvalidChainId(); | ||
|
||
/// @notice Thrown when a deployment fails. | ||
error DeploymentFailed(string reason); | ||
|
||
/// @notice Temporary error since the deploy method is not yet implemented. | ||
error NotImplemented(); | ||
|
||
function deploy( | ||
uint256 _l2ChainId, | ||
uint32 _basefeeScalar, | ||
uint32 _blobBasefeeScalar, | ||
Roles calldata _roles | ||
) | ||
external | ||
view // This is only here to silence the compiler warning until the function is fully implemented. | ||
returns (SystemConfig systemConfig_) | ||
{ | ||
if (_l2ChainId == 0 || _l2ChainId == block.chainid) revert InvalidChainId(); | ||
|
||
// Silence compiler warnings. | ||
_roles; | ||
_basefeeScalar; | ||
_blobBasefeeScalar; | ||
systemConfig_; | ||
|
||
revert NotImplemented(); | ||
} | ||
|
||
/// @notice Maps an L2 chain ID to an L1 batch inbox address as defined by the standard | ||
/// configuration's convention. This convention is `versionByte || keccak256(bytes32(chainId))[:19]`, | ||
/// where || denotes concatenation`, versionByte is 0x00, and chainId is a uint256. | ||
/// https://specs.optimism.io/protocol/configurability.html#consensus-parameters | ||
function chainIdToBatchInboxAddress(uint256 _l2ChainId) internal pure returns (address) { | ||
bytes1 versionByte = 0x00; | ||
bytes32 hashedChainId = keccak256(bytes.concat(bytes32(_l2ChainId))); | ||
bytes19 first19Bytes = bytes19(hashedChainId); | ||
return address(uint160(bytes20(bytes.concat(versionByte, first19Bytes)))); | ||
} | ||
} |
This file contains 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,65 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.15; | ||
|
||
// Testing utilities | ||
import { Test } from "forge-std/Test.sol"; | ||
|
||
// Target contract | ||
import { OPStackManager } from "src/L1/OPStackManager.sol"; | ||
|
||
// Exposes internal functions for testing. | ||
contract OPStackManager_Harness is OPStackManager { | ||
function chainIdToBatchInboxAddress_exposed(uint256 l2ChainId) public pure returns (address) { | ||
return super.chainIdToBatchInboxAddress(l2ChainId); | ||
} | ||
} | ||
|
||
// Unlike other test suites, we intentionally do not inherit from CommonTest or Setup. This is | ||
// because OPStackManager acts as a deploy script, so we start from a clean slate here and | ||
// work OPStackManager's deployment into the existing test setup, instead of using the existing | ||
// test setup to deploy OPStackManager. | ||
contract OPStackManager_Init is Test { | ||
OPStackManager opsm; | ||
|
||
// Default dummy parameters for the deploy function. | ||
OPStackManager.Roles roles; | ||
uint256 l2ChainId = 1234; | ||
uint32 basefeeScalar = 1; | ||
uint32 blobBasefeeScalar = 1; | ||
|
||
function setUp() public { | ||
opsm = new OPStackManager(); | ||
} | ||
} | ||
|
||
contract OPStackManager_Deploy_Test is OPStackManager_Init { | ||
function test_deploy_l2ChainIdEqualsZero_reverts() public { | ||
vm.expectRevert(OPStackManager.InvalidChainId.selector); | ||
opsm.deploy(0, basefeeScalar, blobBasefeeScalar, roles); | ||
} | ||
|
||
function test_deploy_l2ChainIdEqualsCurrentChainId_reverts() public { | ||
vm.expectRevert(OPStackManager.InvalidChainId.selector); | ||
opsm.deploy(block.chainid, basefeeScalar, blobBasefeeScalar, roles); | ||
} | ||
} | ||
|
||
// These tests use the harness which exposes internal functions for testing. | ||
contract OPStackManager_InternalMethods_Test is Test { | ||
function test_calculatesBatchInboxAddress_succeeds() public { | ||
OPStackManager_Harness opsmHarness = new OPStackManager_Harness(); | ||
|
||
// These test vectors were calculated manually: | ||
// 1. Compute the bytes32 encoding of the chainId: bytes32(uint256(chainId)); | ||
// 2. Hash it and manually take the first 19 bytes, and prefixed it with 0x00. | ||
uint256 chainId = 1234; | ||
address expected = 0x0017FA14b0d73Aa6A26D6b8720c1c84b50984f5C; | ||
address actual = opsmHarness.chainIdToBatchInboxAddress_exposed(chainId); | ||
vm.assertEq(expected, actual); | ||
|
||
chainId = type(uint256).max; | ||
expected = 0x00a9C584056064687E149968cBaB758a3376D22A; | ||
actual = opsmHarness.chainIdToBatchInboxAddress_exposed(chainId); | ||
vm.assertEq(expected, actual); | ||
} | ||
} |
This file contains 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