|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.21; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | +import {SnapchainConfigRegistry} from "../src/SnapchainConfigRegistry.sol"; |
| 6 | +import {ISnapchainConfigRegistry} from "../src/interfaces/ISnapchainConfigRegistry.sol"; |
| 7 | +import {SnapchainConfigRegistrySeed} from "./abstract/SnapchainConfigRegistrySeed.sol"; |
| 8 | +import {console, CanonicalCreate2Deployer} from "./abstract/CanonicalCreate2Deployer.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @title DeploySnapchainConfigRegistry |
| 12 | + * |
| 13 | + * @notice Deploys a SnapchainConfigRegistry, seeds it with the config for the chain it lands on, |
| 14 | + * and hands ownership to the address that will author changes from then on. |
| 15 | + * |
| 16 | + * @dev Two instances are expected, one per Snapchain network: |
| 17 | + * |
| 18 | + * - **Mainnet** on Ethereum L1 (chain 1) |
| 19 | + * - **Testnet** on Sepolia (chain 11155111) |
| 20 | + * |
| 21 | + * Different chains rather than two addresses on one chain, so a testnet mistake cannot touch |
| 22 | + * mainnet state and the rehearsal costs nothing real. With identical creation code, constructor |
| 23 | + * args, and salt, the two land at the *same address on both chains* -- a convenience, not |
| 24 | + * something anything should depend on, since a redeploy on either chain breaks it. |
| 25 | + * |
| 26 | + * Deployed through the canonical CREATE2 proxy rather than the ImmutableCreate2Factory the rest |
| 27 | + * of this repo uses, so that the mined salt is a property of the contract alone and not of the |
| 28 | + * key that broadcasts it. CanonicalCreate2Deployer explains the trade. |
| 29 | + */ |
| 30 | +contract DeploySnapchainConfigRegistry is SnapchainConfigRegistrySeed, CanonicalCreate2Deployer, Test { |
| 31 | + struct Salts { |
| 32 | + bytes32 snapchainConfigRegistry; |
| 33 | + } |
| 34 | + |
| 35 | + struct DeploymentParams { |
| 36 | + address deployer; |
| 37 | + address owner; |
| 38 | + Salts salts; |
| 39 | + } |
| 40 | + |
| 41 | + struct Addresses { |
| 42 | + address snapchainConfigRegistry; |
| 43 | + } |
| 44 | + |
| 45 | + struct Contracts { |
| 46 | + SnapchainConfigRegistry snapchainConfigRegistry; |
| 47 | + } |
| 48 | + |
| 49 | + function run() public { |
| 50 | + runSetup(runDeploy(loadDeploymentParams())); |
| 51 | + } |
| 52 | + |
| 53 | + function runDeploy( |
| 54 | + DeploymentParams memory params |
| 55 | + ) public returns (Contracts memory) { |
| 56 | + return runDeploy(params, true); |
| 57 | + } |
| 58 | + |
| 59 | + function runDeploy(DeploymentParams memory params, bool broadcast) public returns (Contracts memory) { |
| 60 | + Addresses memory addrs; |
| 61 | + addrs.snapchainConfigRegistry = register( |
| 62 | + "SnapchainConfigRegistry", |
| 63 | + params.salts.snapchainConfigRegistry, |
| 64 | + type(SnapchainConfigRegistry).creationCode, |
| 65 | + // The deployer owns the registry through setup, since seeding is owner-gated. Handed off |
| 66 | + // at the end of runSetup. |
| 67 | + abi.encode(params.deployer) |
| 68 | + ); |
| 69 | + deploy(broadcast); |
| 70 | + |
| 71 | + return Contracts({snapchainConfigRegistry: SnapchainConfigRegistry(addrs.snapchainConfigRegistry)}); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @dev Seed the full validator-set history and peer lists, then transfer ownership. |
| 76 | + * |
| 77 | + * Ownable2Step leaves `params.owner` as `pendingOwner`; it accepts in a separate |
| 78 | + * transaction. Until it does, the deployer still owns the registry -- which is the point of |
| 79 | + * the two-step, but does mean the handoff is not complete when this script exits. |
| 80 | + * |
| 81 | + * Gated on the registry being unseeded rather than on `deploymentChanged()`, which reports |
| 82 | + * whether *this run* deployed it. The canonical CREATE2 proxy imposes no caller lock, so a |
| 83 | + * stranger can submit the same salt and init code first; a run that then found the registry |
| 84 | + * already deployed would log "no changes" and leave it permanently unseeded. Asking the |
| 85 | + * registry what it holds answers the question that actually matters, and closes the same |
| 86 | + * hole for a run interrupted between deploy and setup. |
| 87 | + * |
| 88 | + * Detects unseeded, not partially seeded. A broadcast that dies midway through the loop |
| 89 | + * leaves a nonzero `configVersion` and will be skipped here; finishing it is a manual job. |
| 90 | + */ |
| 91 | + function runSetup(Contracts memory contracts, DeploymentParams memory params, bool broadcast) public { |
| 92 | + if (contracts.snapchainConfigRegistry.configVersion() == 0) { |
| 93 | + console.log("Running setup"); |
| 94 | + |
| 95 | + // Read before broadcasting: on an unrecognized chain this reverts, and it should do so |
| 96 | + // before any transaction is sent rather than halfway through seeding. |
| 97 | + Seed memory seed = _seedFor(block.chainid); |
| 98 | + uint256 setCount = seed.validatorSets.length; |
| 99 | + |
| 100 | + if (broadcast) vm.startBroadcast(); |
| 101 | + for (uint256 i; i < setCount; ++i) { |
| 102 | + ISnapchainConfigRegistry.ValidatorSet memory validatorSet = seed.validatorSets[i]; |
| 103 | + contracts.snapchainConfigRegistry.appendValidatorSet( |
| 104 | + validatorSet.effectiveAt, validatorSet.shardIds, validatorSet.validatorPublicKeys |
| 105 | + ); |
| 106 | + } |
| 107 | + contracts.snapchainConfigRegistry.setBootstrapPeers(seed.bootstrapPeers); |
| 108 | + contracts.snapchainConfigRegistry.setDirectPeers(seed.directPeers); |
| 109 | + contracts.snapchainConfigRegistry.transferOwnership(params.owner); |
| 110 | + if (broadcast) vm.stopBroadcast(); |
| 111 | + } else { |
| 112 | + console.log("Already seeded, skipping setup"); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + function runSetup( |
| 117 | + Contracts memory contracts |
| 118 | + ) public { |
| 119 | + DeploymentParams memory params = loadDeploymentParams(); |
| 120 | + runSetup(contracts, params, true); |
| 121 | + } |
| 122 | + |
| 123 | + function loadDeploymentParams() internal returns (DeploymentParams memory) { |
| 124 | + return DeploymentParams({ |
| 125 | + deployer: vm.envAddress("DEPLOYER"), |
| 126 | + owner: vm.envAddress("SNAPCHAIN_CONFIG_REGISTRY_OWNER_ADDRESS"), |
| 127 | + salts: Salts({snapchainConfigRegistry: loadSalt()}) |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @dev One salt serves both chains by default, which puts the two registries at the same |
| 133 | + * address. `SNAPCHAIN_CONFIG_REGISTRY_TESTNET_CREATE2_SALT` overrides on Sepolia if that |
| 134 | + * address turns out to be taken there. |
| 135 | + */ |
| 136 | + function loadSalt() internal view returns (bytes32) { |
| 137 | + bytes32 salt = vm.envOr("SNAPCHAIN_CONFIG_REGISTRY_CREATE2_SALT", bytes32(0)); |
| 138 | + if (block.chainid == ETH_SEPOLIA_CHAIN_ID) { |
| 139 | + return vm.envOr("SNAPCHAIN_CONFIG_REGISTRY_TESTNET_CREATE2_SALT", salt); |
| 140 | + } |
| 141 | + return salt; |
| 142 | + } |
| 143 | +} |
0 commit comments