From 5aaaffeda1dd25f8e2f94ee46efc2feae1f87b4f Mon Sep 17 00:00:00 2001 From: Sebastian Stammler Date: Tue, 11 Jun 2024 20:21:39 +0200 Subject: [PATCH] address review --- packages/contracts-bedrock/scripts/Config.sol | 30 ++++++++++++ .../scripts/DeployConfig.s.sol | 13 +++++ .../contracts-bedrock/scripts/L2Genesis.s.sol | 48 ++++++++----------- 3 files changed, 62 insertions(+), 29 deletions(-) diff --git a/packages/contracts-bedrock/scripts/Config.sol b/packages/contracts-bedrock/scripts/Config.sol index f69838db48a12..0bf567b3dd1c2 100644 --- a/packages/contracts-bedrock/scripts/Config.sol +++ b/packages/contracts-bedrock/scripts/Config.sol @@ -13,6 +13,20 @@ enum OutputMode { ALL } +library OutputModeUtils { + function toString(OutputMode _mode) internal pure returns (string memory) { + if (_mode == OutputMode.NONE) { + return "none"; + } else if (_mode == OutputMode.LATEST) { + return "latest"; + } else if (_mode == OutputMode.ALL) { + return "all"; + } else { + return "unknown"; + } + } +} + /// @notice Enum of forks available for selection when generating genesis allocs. enum Fork { NONE, @@ -23,6 +37,22 @@ enum Fork { Fork constant LATEST_FORK = Fork.FJORD; +library ForkUtils { + function toString(Fork _fork) internal pure returns (string memory) { + if (_fork == Fork.NONE) { + return "none"; + } else if (_fork == Fork.DELTA) { + return "delta"; + } else if (_fork == Fork.ECOTONE) { + return "ecotone"; + } else if (_fork == Fork.FJORD) { + return "fjord"; + } else { + return "unknown"; + } + } +} + /// @title Config /// @notice Contains all env var based config. Add any new env var parsing to this file /// to ensure that all config is in a single place. diff --git a/packages/contracts-bedrock/scripts/DeployConfig.s.sol b/packages/contracts-bedrock/scripts/DeployConfig.s.sol index 7f2f76b1c8fe1..0f9ac8d9af11b 100644 --- a/packages/contracts-bedrock/scripts/DeployConfig.s.sol +++ b/packages/contracts-bedrock/scripts/DeployConfig.s.sol @@ -175,6 +175,19 @@ contract DeployConfig is Script { useInterop = _readOr(_json, "$.useInterop", false); } + + function fork() public view returns (Fork fork_) { + // let env var take precedence + fork_ = Config.fork(); + if (fork_ == Fork.NONE) { + // Will revert if no deploy config can be found either. + fork_ = latestGenesisFork(); + console.log("DeployConfig: using deploy config fork: %s", fork.toString()); + } else { + console.log("DeployConfig: using env var fork: %s", fork.toString()); + } + } + function latestGenesisFork() public view returns (Fork) { if (l2GenesisFjordTimeOffset == 0) { return Fork.FJORD; diff --git a/packages/contracts-bedrock/scripts/L2Genesis.s.sol b/packages/contracts-bedrock/scripts/L2Genesis.s.sol index 98fa580842280..44607c5346508 100644 --- a/packages/contracts-bedrock/scripts/L2Genesis.s.sol +++ b/packages/contracts-bedrock/scripts/L2Genesis.s.sol @@ -5,7 +5,7 @@ import { Script } from "forge-std/Script.sol"; import { console2 as console } from "forge-std/console2.sol"; import { Deployer } from "scripts/Deployer.sol"; -import { Config, OutputMode, Fork, LATEST_FORK } from "scripts/Config.sol"; +import { Config, OutputMode, OutputModeUtils, Fork, ForkUtils, LATEST_FORK } from "scripts/Config.sol"; import { Artifacts } from "scripts/Artifacts.s.sol"; import { DeployConfig } from "scripts/DeployConfig.s.sol"; import { Predeploys } from "src/libraries/Predeploys.sol"; @@ -45,6 +45,9 @@ struct L1Dependencies { /// 2. A contract must be deployed using the `new` syntax if there are immutables in the code. /// Any other side effects from the init code besides setting the immutables must be cleaned up afterwards. contract L2Genesis is Deployer { + using ForkUtils for Fork; + using OutputModeUtils for OutputMode; + uint256 public constant PRECOMPILE_COUNT = 256; uint80 internal constant DEV_ACCOUNT_FUND_AMT = 10_000 ether; @@ -106,21 +109,7 @@ contract L2Genesis is Deployer { /// Sets the precompiles, proxies, and the implementation accounts to be `vm.dumpState` /// to generate a L2 genesis alloc. function runWithStateDump() public { - Fork fork = Config.fork(); - if (fork == Fork.NONE) { - // Will revert if no deploy config can be found either. - fork = latestDeployConfigGenesisFork(); - console.log("L2Genesis: using deploy config fork: %d", uint256(fork)); - } else { - console.log("L2Genesis: using env var fork: %d", uint256(fork)); - } - runWithOptions(Config.outputMode(), fork, artifactDependencies()); - } - - function latestDeployConfigGenesisFork() internal returns (Fork) { - DeployConfig cfg = DeployConfig(address(uint160(uint256(keccak256(abi.encode("optimism.deployconfig")))))); - cfg.read(Config.deployConfigPath()); - return cfg.latestGenesisFork(); + runWithOptions(Config.outputMode(), cfg.fork(), artifactDependencies()); } /// @notice Alias for `runWithStateDump` so that no `--sig` needs to be specified. @@ -141,7 +130,7 @@ contract L2Genesis is Deployer { /// @notice Build the L2 genesis. function runWithOptions(OutputMode _mode, Fork _fork, L1Dependencies memory _l1Dependencies) public { - console.log("L2Genesis: outputMode: %d, fork: %d", uint256(_mode), uint256(_fork)); + console.log("L2Genesis: outputMode: %s, fork: %s", _mode.toString(), _fork.toString()); vm.startPrank(deployer); vm.chainId(cfg.l2ChainID()); @@ -154,32 +143,33 @@ contract L2Genesis is Deployer { } vm.stopPrank(); - if (_mode == OutputMode.ALL || _fork == Fork.DELTA && _mode == OutputMode.LATEST) { - writeGenesisAllocs(Config.stateDumpPath("-delta")); - } - if (_fork == Fork.DELTA) { + if (writeForkGenesisAllocs(_fork, Fork.DELTA, _mode)) { return; } activateEcotone(); - if (_mode == OutputMode.ALL || _fork == Fork.ECOTONE && _mode == OutputMode.LATEST) { - writeGenesisAllocs(Config.stateDumpPath("-ecotone")); - } - if (_fork == Fork.ECOTONE) { + if (writeForkGenesisAllocs(_fork, Fork.ECOTONE, _mode)) { return; } activateFjord(); - if (_mode == OutputMode.ALL || _fork == Fork.FJORD && _mode == OutputMode.LATEST) { - writeGenesisAllocs(Config.stateDumpPath("-fjord")); - } - if (_fork == Fork.FJORD) { + if (writeForkGenesisAllocs(_fork, Fork.FJORD, _mode)) { return; } } + function writeForkGenesisAllocs(Fork _latest, Fork _current, OutputMode _mode) internal returns (bool isLatest_) { + if (_mode == OutputMode.ALL || _latest == _current && _mode == OutputMode.LATEST) { + string memory suffix = string.concat("-", _current.toString()); + writeGenesisAllocs(Config.stateDumpPath(suffix)); + } + if (_latest == _current) { + isLatest_ = true; + } + } + /// @notice Give all of the precompiles 1 wei function dealEthToPrecompiles() internal { console.log("Setting precompile 1 wei balances");