Skip to content

Commit c18a4ba

Browse files
committed
address review
1 parent 27c8e06 commit c18a4ba

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

packages/contracts-bedrock/scripts/Config.sol

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ enum OutputMode {
1313
ALL
1414
}
1515

16+
library OutputModeUtils {
17+
function toString(OutputMode _mode) internal pure returns (string memory) {
18+
if (_mode == OutputMode.NONE) {
19+
return "none";
20+
} else if (_mode == OutputMode.LATEST) {
21+
return "latest";
22+
} else if (_mode == OutputMode.ALL) {
23+
return "all";
24+
} else {
25+
return "unknown";
26+
}
27+
}
28+
}
29+
1630
/// @notice Enum of forks available for selection when generating genesis allocs.
1731
enum Fork {
1832
NONE,
@@ -23,6 +37,22 @@ enum Fork {
2337

2438
Fork constant LATEST_FORK = Fork.FJORD;
2539

40+
library ForkUtils {
41+
function toString(Fork _fork) internal pure returns (string memory) {
42+
if (_fork == Fork.NONE) {
43+
return "none";
44+
} else if (_fork == Fork.DELTA) {
45+
return "delta";
46+
} else if (_fork == Fork.ECOTONE) {
47+
return "ecotone";
48+
} else if (_fork == Fork.FJORD) {
49+
return "fjord";
50+
} else {
51+
return "unknown";
52+
}
53+
}
54+
}
55+
2656
/// @title Config
2757
/// @notice Contains all env var based config. Add any new env var parsing to this file
2858
/// to ensure that all config is in a single place.

packages/contracts-bedrock/scripts/DeployConfig.s.sol

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import { stdJson } from "forge-std/StdJson.sol";
77
import { Executables } from "scripts/Executables.sol";
88
import { Process } from "scripts/libraries/Process.sol";
99
import { Chains } from "scripts/Chains.sol";
10-
import { Fork } from "scripts/Config.sol";
10+
import { Config, Fork, ForkUtils } from "scripts/Config.sol";
1111

1212
/// @title DeployConfig
1313
/// @notice Represents the configuration required to deploy the system. It is expected
1414
/// to read the file from JSON. A future improvement would be to have fallback
1515
/// values if they are not defined in the JSON themselves.
1616
contract DeployConfig is Script {
1717
using stdJson for string;
18+
using ForkUtils for Fork;
1819

1920
/// @notice Represents an unset offset value, as opposed to 0, which denotes no-offset.
2021
uint256 constant NULL_OFFSET = type(uint256).max;
@@ -175,15 +176,16 @@ contract DeployConfig is Script {
175176
useInterop = _readOr(_json, "$.useInterop", false);
176177
}
177178

178-
function latestGenesisFork() public view returns (Fork) {
179-
if (l2GenesisFjordTimeOffset == 0) {
180-
return Fork.FJORD;
181-
} else if (l2GenesisEcotoneTimeOffset == 0) {
182-
return Fork.ECOTONE;
183-
} else if (l2GenesisDeltaTimeOffset == 0) {
184-
return Fork.DELTA;
179+
function fork() public view returns (Fork fork_) {
180+
// let env var take precedence
181+
fork_ = Config.fork();
182+
if (fork_ == Fork.NONE) {
183+
// Will revert if no deploy config can be found either.
184+
fork_ = latestGenesisFork();
185+
console.log("DeployConfig: using deploy config fork: %s", fork_.toString());
186+
} else {
187+
console.log("DeployConfig: using env var fork: %s", fork_.toString());
185188
}
186-
revert("DeployConfig: no supported fork active at genesis");
187189
}
188190

189191
function l1StartingBlockTag() public returns (bytes32) {
@@ -240,6 +242,17 @@ contract DeployConfig is Script {
240242
customGasTokenAddress = _token;
241243
}
242244

245+
function latestGenesisFork() internal view returns (Fork) {
246+
if (l2GenesisFjordTimeOffset == 0) {
247+
return Fork.FJORD;
248+
} else if (l2GenesisEcotoneTimeOffset == 0) {
249+
return Fork.ECOTONE;
250+
} else if (l2GenesisDeltaTimeOffset == 0) {
251+
return Fork.DELTA;
252+
}
253+
revert("DeployConfig: no supported fork active at genesis");
254+
}
255+
243256
function _getBlockByTag(string memory _tag) internal returns (bytes32) {
244257
string[] memory cmd = new string[](3);
245258
cmd[0] = Executables.bash;

packages/contracts-bedrock/scripts/L2Genesis.s.sol

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Script } from "forge-std/Script.sol";
55
import { console2 as console } from "forge-std/console2.sol";
66
import { Deployer } from "scripts/Deployer.sol";
77

8-
import { Config, OutputMode, Fork, LATEST_FORK } from "scripts/Config.sol";
8+
import { Config, OutputMode, OutputModeUtils, Fork, ForkUtils, LATEST_FORK } from "scripts/Config.sol";
99
import { Artifacts } from "scripts/Artifacts.s.sol";
1010
import { DeployConfig } from "scripts/DeployConfig.s.sol";
1111
import { Predeploys } from "src/libraries/Predeploys.sol";
@@ -45,6 +45,9 @@ struct L1Dependencies {
4545
/// 2. A contract must be deployed using the `new` syntax if there are immutables in the code.
4646
/// Any other side effects from the init code besides setting the immutables must be cleaned up afterwards.
4747
contract L2Genesis is Deployer {
48+
using ForkUtils for Fork;
49+
using OutputModeUtils for OutputMode;
50+
4851
uint256 public constant PRECOMPILE_COUNT = 256;
4952

5053
uint80 internal constant DEV_ACCOUNT_FUND_AMT = 10_000 ether;
@@ -106,21 +109,7 @@ contract L2Genesis is Deployer {
106109
/// Sets the precompiles, proxies, and the implementation accounts to be `vm.dumpState`
107110
/// to generate a L2 genesis alloc.
108111
function runWithStateDump() public {
109-
Fork fork = Config.fork();
110-
if (fork == Fork.NONE) {
111-
// Will revert if no deploy config can be found either.
112-
fork = latestDeployConfigGenesisFork();
113-
console.log("L2Genesis: using deploy config fork: %d", uint256(fork));
114-
} else {
115-
console.log("L2Genesis: using env var fork: %d", uint256(fork));
116-
}
117-
runWithOptions(Config.outputMode(), fork, artifactDependencies());
118-
}
119-
120-
function latestDeployConfigGenesisFork() internal returns (Fork) {
121-
DeployConfig cfg = DeployConfig(address(uint160(uint256(keccak256(abi.encode("optimism.deployconfig"))))));
122-
cfg.read(Config.deployConfigPath());
123-
return cfg.latestGenesisFork();
112+
runWithOptions(Config.outputMode(), cfg.fork(), artifactDependencies());
124113
}
125114

126115
/// @notice Alias for `runWithStateDump` so that no `--sig` needs to be specified.
@@ -141,7 +130,7 @@ contract L2Genesis is Deployer {
141130

142131
/// @notice Build the L2 genesis.
143132
function runWithOptions(OutputMode _mode, Fork _fork, L1Dependencies memory _l1Dependencies) public {
144-
console.log("L2Genesis: outputMode: %d, fork: %d", uint256(_mode), uint256(_fork));
133+
console.log("L2Genesis: outputMode: %s, fork: %s", _mode.toString(), _fork.toString());
145134
vm.startPrank(deployer);
146135
vm.chainId(cfg.l2ChainID());
147136

@@ -154,32 +143,33 @@ contract L2Genesis is Deployer {
154143
}
155144
vm.stopPrank();
156145

157-
if (_mode == OutputMode.ALL || _fork == Fork.DELTA && _mode == OutputMode.LATEST) {
158-
writeGenesisAllocs(Config.stateDumpPath("-delta"));
159-
}
160-
if (_fork == Fork.DELTA) {
146+
if (writeForkGenesisAllocs(_fork, Fork.DELTA, _mode)) {
161147
return;
162148
}
163149

164150
activateEcotone();
165151

166-
if (_mode == OutputMode.ALL || _fork == Fork.ECOTONE && _mode == OutputMode.LATEST) {
167-
writeGenesisAllocs(Config.stateDumpPath("-ecotone"));
168-
}
169-
if (_fork == Fork.ECOTONE) {
152+
if (writeForkGenesisAllocs(_fork, Fork.ECOTONE, _mode)) {
170153
return;
171154
}
172155

173156
activateFjord();
174157

175-
if (_mode == OutputMode.ALL || _fork == Fork.FJORD && _mode == OutputMode.LATEST) {
176-
writeGenesisAllocs(Config.stateDumpPath("-fjord"));
177-
}
178-
if (_fork == Fork.FJORD) {
158+
if (writeForkGenesisAllocs(_fork, Fork.FJORD, _mode)) {
179159
return;
180160
}
181161
}
182162

163+
function writeForkGenesisAllocs(Fork _latest, Fork _current, OutputMode _mode) internal returns (bool isLatest_) {
164+
if (_mode == OutputMode.ALL || _latest == _current && _mode == OutputMode.LATEST) {
165+
string memory suffix = string.concat("-", _current.toString());
166+
writeGenesisAllocs(Config.stateDumpPath(suffix));
167+
}
168+
if (_latest == _current) {
169+
isLatest_ = true;
170+
}
171+
}
172+
183173
/// @notice Give all of the precompiles 1 wei
184174
function dealEthToPrecompiles() internal {
185175
console.log("Setting precompile 1 wei balances");

0 commit comments

Comments
 (0)