|
| 1 | +package bootstrap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/ecdsa" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer" |
| 12 | + "github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer/artifacts" |
| 13 | + "github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer/broadcaster" |
| 14 | + "github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer/opcm" |
| 15 | + "github.com/ethereum-optimism/optimism/op-deployer/pkg/env" |
| 16 | + opcrypto "github.com/ethereum-optimism/optimism/op-service/crypto" |
| 17 | + "github.com/ethereum-optimism/optimism/op-service/ctxinterrupt" |
| 18 | + "github.com/ethereum-optimism/optimism/op-service/ioutil" |
| 19 | + "github.com/ethereum-optimism/optimism/op-service/jsonutil" |
| 20 | + oplog "github.com/ethereum-optimism/optimism/op-service/log" |
| 21 | + "github.com/ethereum/go-ethereum/common" |
| 22 | + "github.com/ethereum/go-ethereum/crypto" |
| 23 | + "github.com/ethereum/go-ethereum/ethclient" |
| 24 | + "github.com/ethereum/go-ethereum/log" |
| 25 | + "github.com/ethereum/go-ethereum/rpc" |
| 26 | + "github.com/urfave/cli/v2" |
| 27 | +) |
| 28 | + |
| 29 | +type ValidatorConfig struct { |
| 30 | + L1RPCUrl string |
| 31 | + PrivateKey string |
| 32 | + Logger log.Logger |
| 33 | + ArtifactsLocator *artifacts.Locator |
| 34 | + Input ValidatorInput |
| 35 | + |
| 36 | + privateKeyECDSA *ecdsa.PrivateKey |
| 37 | +} |
| 38 | + |
| 39 | +type ValidatorInput struct { |
| 40 | + Release string `json:"release"` |
| 41 | + SuperchainConfig common.Address `json:"superchainConfig"` |
| 42 | + ProtocolVersions common.Address `json:"protocolVersions"` |
| 43 | + L1PAOMultisig common.Address `json:"l1PAOMultisig"` |
| 44 | + MIPS common.Address `json:"mips" evm:"mips"` |
| 45 | + Challenger common.Address `json:"challenger"` |
| 46 | + SuperchainConfigImpl common.Address `json:"superchainConfigImpl"` |
| 47 | + ProtocolVersionsImpl common.Address `json:"protocolVersionsImpl"` |
| 48 | + L1ERC721BridgeImpl common.Address `json:"l1ERC721BridgeImpl"` |
| 49 | + OptimismPortalImpl common.Address `json:"optimismPortalImpl"` |
| 50 | + SystemConfigImpl common.Address `json:"systemConfigImpl"` |
| 51 | + OptimismMintableERC20FactoryImpl common.Address `json:"optimismMintableERC20FactoryImpl"` |
| 52 | + L1CrossDomainMessengerImpl common.Address `json:"l1CrossDomainMessengerImpl"` |
| 53 | + L1StandardBridgeImpl common.Address `json:"l1StandardBridgeImpl"` |
| 54 | + DisputeGameFactoryImpl common.Address `json:"disputeGameFactoryImpl"` |
| 55 | + AnchorStateRegistryImpl common.Address `json:"anchorStateRegistryImpl"` |
| 56 | + DelayedWETHImpl common.Address `json:"delayedWETHImpl"` |
| 57 | + MIPSImpl common.Address `json:"mipsImpl" evm:"mipsImpl"` |
| 58 | +} |
| 59 | + |
| 60 | +type ValidatorOutput struct { |
| 61 | + Validator common.Address `json:"validator"` |
| 62 | +} |
| 63 | + |
| 64 | +func (c *ValidatorConfig) Check() error { |
| 65 | + if c.L1RPCUrl == "" { |
| 66 | + return fmt.Errorf("l1RPCUrl must be specified") |
| 67 | + } |
| 68 | + |
| 69 | + if c.PrivateKey == "" { |
| 70 | + return fmt.Errorf("private key must be specified") |
| 71 | + } |
| 72 | + |
| 73 | + privECDSA, err := crypto.HexToECDSA(strings.TrimPrefix(c.PrivateKey, "0x")) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("failed to parse private key: %w", err) |
| 76 | + } |
| 77 | + c.privateKeyECDSA = privECDSA |
| 78 | + |
| 79 | + if c.Logger == nil { |
| 80 | + return fmt.Errorf("logger must be specified") |
| 81 | + } |
| 82 | + |
| 83 | + if c.ArtifactsLocator == nil { |
| 84 | + return fmt.Errorf("artifacts locator must be specified") |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func ValidatorCLI(cliCtx *cli.Context) error { |
| 91 | + logCfg := oplog.ReadCLIConfig(cliCtx) |
| 92 | + l := oplog.NewLogger(oplog.AppOut(cliCtx), logCfg) |
| 93 | + oplog.SetGlobalLogHandler(l.Handler()) |
| 94 | + |
| 95 | + l1RPCUrl := cliCtx.String(deployer.L1RPCURLFlagName) |
| 96 | + privateKey := cliCtx.String(deployer.PrivateKeyFlagName) |
| 97 | + outfile := cliCtx.String(OutfileFlagName) |
| 98 | + artifactsURLStr := cliCtx.String(ArtifactsLocatorFlagName) |
| 99 | + configFile := cliCtx.String(ConfigFileFlag.Name) |
| 100 | + |
| 101 | + artifactsLocator := new(artifacts.Locator) |
| 102 | + if err := artifactsLocator.UnmarshalText([]byte(artifactsURLStr)); err != nil { |
| 103 | + return fmt.Errorf("failed to parse artifacts URL: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + // Read the input configuration from file |
| 107 | + configData, err := os.ReadFile(configFile) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("failed to read config file: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + var input ValidatorInput |
| 113 | + if err := json.Unmarshal(configData, &input); err != nil { |
| 114 | + return fmt.Errorf("failed to parse config file: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + ctx := ctxinterrupt.WithCancelOnInterrupt(cliCtx.Context) |
| 118 | + |
| 119 | + dvo, err := Validator(ctx, ValidatorConfig{ |
| 120 | + L1RPCUrl: l1RPCUrl, |
| 121 | + PrivateKey: privateKey, |
| 122 | + Logger: l, |
| 123 | + ArtifactsLocator: artifactsLocator, |
| 124 | + Input: input, |
| 125 | + }) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("failed to deploy Validator: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + if err := jsonutil.WriteJSON(dvo, ioutil.ToStdOutOrFileOrNoop(outfile, 0o755)); err != nil { |
| 131 | + return fmt.Errorf("failed to write output: %w", err) |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func Validator(ctx context.Context, cfg ValidatorConfig) (ValidatorOutput, error) { |
| 137 | + var output ValidatorOutput |
| 138 | + if err := cfg.Check(); err != nil { |
| 139 | + return output, fmt.Errorf("invalid config for Validator: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + lgr := cfg.Logger |
| 143 | + |
| 144 | + artifactsFS, err := artifacts.Download(ctx, cfg.ArtifactsLocator, artifacts.BarProgressor()) |
| 145 | + if err != nil { |
| 146 | + return output, fmt.Errorf("failed to download artifacts: %w", err) |
| 147 | + } |
| 148 | + |
| 149 | + l1Client, err := ethclient.Dial(cfg.L1RPCUrl) |
| 150 | + if err != nil { |
| 151 | + return output, fmt.Errorf("failed to connect to L1 RPC: %w", err) |
| 152 | + } |
| 153 | + |
| 154 | + chainID, err := l1Client.ChainID(ctx) |
| 155 | + if err != nil { |
| 156 | + return output, fmt.Errorf("failed to get chain ID: %w", err) |
| 157 | + } |
| 158 | + |
| 159 | + signer := opcrypto.SignerFnFromBind(opcrypto.PrivateKeySignerFn(cfg.privateKeyECDSA, chainID)) |
| 160 | + chainDeployer := crypto.PubkeyToAddress(cfg.privateKeyECDSA.PublicKey) |
| 161 | + |
| 162 | + bcaster, err := broadcaster.NewKeyedBroadcaster(broadcaster.KeyedBroadcasterOpts{ |
| 163 | + Logger: lgr, |
| 164 | + ChainID: chainID, |
| 165 | + Client: l1Client, |
| 166 | + Signer: signer, |
| 167 | + From: chainDeployer, |
| 168 | + }) |
| 169 | + if err != nil { |
| 170 | + return output, fmt.Errorf("failed to create broadcaster: %w", err) |
| 171 | + } |
| 172 | + |
| 173 | + l1RPC, err := rpc.Dial(cfg.L1RPCUrl) |
| 174 | + if err != nil { |
| 175 | + return output, fmt.Errorf("failed to connect to L1 RPC: %w", err) |
| 176 | + } |
| 177 | + |
| 178 | + l1Host, err := env.DefaultForkedScriptHost( |
| 179 | + ctx, |
| 180 | + bcaster, |
| 181 | + lgr, |
| 182 | + chainDeployer, |
| 183 | + artifactsFS, |
| 184 | + l1RPC, |
| 185 | + ) |
| 186 | + if err != nil { |
| 187 | + return output, fmt.Errorf("failed to create script host: %w", err) |
| 188 | + } |
| 189 | + |
| 190 | + // Execute the deployment script |
| 191 | + output, err = opcm.RunScriptSingle[ValidatorInput, ValidatorOutput]( |
| 192 | + l1Host, |
| 193 | + cfg.Input, |
| 194 | + "DeployStandardValidator.s.sol", |
| 195 | + "DeployStandardValidator", |
| 196 | + ) |
| 197 | + if err != nil { |
| 198 | + return output, fmt.Errorf("error deploying validator: %w", err) |
| 199 | + } |
| 200 | + |
| 201 | + // Broadcast the transactions |
| 202 | + if _, err := bcaster.Broadcast(ctx); err != nil { |
| 203 | + return output, fmt.Errorf("failed to broadcast: %w", err) |
| 204 | + } |
| 205 | + |
| 206 | + lgr.Info("deployed validator contracts") |
| 207 | + |
| 208 | + return output, nil |
| 209 | +} |
0 commit comments