|
| 1 | +package deployer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/ecdsa" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/ethereum-optimism/optimism/op-chain-ops/deployer/pipeline" |
| 10 | + opcrypto "github.com/ethereum-optimism/optimism/op-service/crypto" |
| 11 | + "github.com/ethereum-optimism/optimism/op-service/ctxinterrupt" |
| 12 | + oplog "github.com/ethereum-optimism/optimism/op-service/log" |
| 13 | + "github.com/ethereum/go-ethereum/crypto" |
| 14 | + "github.com/ethereum/go-ethereum/ethclient" |
| 15 | + "github.com/ethereum/go-ethereum/log" |
| 16 | + "github.com/urfave/cli/v2" |
| 17 | +) |
| 18 | + |
| 19 | +type ApplyConfig struct { |
| 20 | + L1RPCUrl string |
| 21 | + Workdir string |
| 22 | + PrivateKey string |
| 23 | + Logger log.Logger |
| 24 | + |
| 25 | + privateKeyECDSA *ecdsa.PrivateKey |
| 26 | +} |
| 27 | + |
| 28 | +func (a *ApplyConfig) Check() error { |
| 29 | + if a.L1RPCUrl == "" { |
| 30 | + return fmt.Errorf("l1RPCUrl must be specified") |
| 31 | + } |
| 32 | + |
| 33 | + if a.Workdir == "" { |
| 34 | + return fmt.Errorf("workdir must be specified") |
| 35 | + } |
| 36 | + |
| 37 | + if a.PrivateKey == "" { |
| 38 | + return fmt.Errorf("private key must be specified") |
| 39 | + } |
| 40 | + |
| 41 | + privECDSA, err := crypto.HexToECDSA(strings.TrimPrefix(a.PrivateKey, "0x")) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to parse private key: %w", err) |
| 44 | + } |
| 45 | + a.privateKeyECDSA = privECDSA |
| 46 | + |
| 47 | + if a.Logger == nil { |
| 48 | + return fmt.Errorf("logger must be specified") |
| 49 | + } |
| 50 | + |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func ApplyCLI() func(cliCtx *cli.Context) error { |
| 55 | + return func(cliCtx *cli.Context) error { |
| 56 | + logCfg := oplog.ReadCLIConfig(cliCtx) |
| 57 | + l := oplog.NewLogger(oplog.AppOut(cliCtx), logCfg) |
| 58 | + oplog.SetGlobalLogHandler(l.Handler()) |
| 59 | + |
| 60 | + l1RPCUrl := cliCtx.String(L1RPCURLFlagName) |
| 61 | + workdir := cliCtx.String(WorkdirFlagName) |
| 62 | + privateKey := cliCtx.String(PrivateKeyFlagName) |
| 63 | + |
| 64 | + ctx := ctxinterrupt.WithCancelOnInterrupt(cliCtx.Context) |
| 65 | + |
| 66 | + return Apply(ctx, ApplyConfig{ |
| 67 | + L1RPCUrl: l1RPCUrl, |
| 68 | + Workdir: workdir, |
| 69 | + PrivateKey: privateKey, |
| 70 | + Logger: l, |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func Apply(ctx context.Context, cfg ApplyConfig) error { |
| 76 | + if err := cfg.Check(); err != nil { |
| 77 | + return fmt.Errorf("invalid config for apply: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + l1Client, err := ethclient.Dial(cfg.L1RPCUrl) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("failed to connect to L1 RPC: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + chainID, err := l1Client.ChainID(ctx) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to get chain ID: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + signer := opcrypto.SignerFnFromBind(opcrypto.PrivateKeySignerFn(cfg.privateKeyECDSA, chainID)) |
| 91 | + deployer := crypto.PubkeyToAddress(cfg.privateKeyECDSA.PublicKey) |
| 92 | + |
| 93 | + env := &pipeline.Env{ |
| 94 | + Workdir: cfg.Workdir, |
| 95 | + L1RPCUrl: cfg.L1RPCUrl, |
| 96 | + L1Client: l1Client, |
| 97 | + Logger: cfg.Logger, |
| 98 | + Signer: signer, |
| 99 | + Deployer: deployer, |
| 100 | + } |
| 101 | + |
| 102 | + intent, err := env.ReadIntent() |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + if err := intent.Check(); err != nil { |
| 108 | + return fmt.Errorf("invalid intent: %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + st, err := env.ReadState() |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + pline := []struct { |
| 117 | + name string |
| 118 | + stage pipeline.Stage |
| 119 | + }{ |
| 120 | + {"init", pipeline.Init}, |
| 121 | + {"deploy-superchain", pipeline.DeploySuperchain}, |
| 122 | + } |
| 123 | + for _, stage := range pline { |
| 124 | + if err := stage.stage(ctx, env, intent, st); err != nil { |
| 125 | + return fmt.Errorf("error in pipeline stage: %w", err) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + st.AppliedIntent = intent |
| 130 | + if err := env.WriteState(st); err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
0 commit comments