|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + "github.com/scroll-tech/da-codec/encoding" |
| 12 | + "github.com/scroll-tech/go-ethereum/ethclient" |
| 13 | + "github.com/scroll-tech/go-ethereum/log" |
| 14 | + "github.com/urfave/cli/v2" |
| 15 | + |
| 16 | + "scroll-tech/common/database" |
| 17 | + "scroll-tech/common/observability" |
| 18 | + "scroll-tech/common/utils" |
| 19 | + "scroll-tech/common/version" |
| 20 | + |
| 21 | + "scroll-tech/rollup/internal/config" |
| 22 | + "scroll-tech/rollup/internal/controller/relayer" |
| 23 | + "scroll-tech/rollup/internal/controller/watcher" |
| 24 | + rutils "scroll-tech/rollup/internal/utils" |
| 25 | +) |
| 26 | + |
| 27 | +var app *cli.App |
| 28 | + |
| 29 | +func init() { |
| 30 | + // Set up blob-uploader app info. |
| 31 | + app = cli.NewApp() |
| 32 | + app.Action = action |
| 33 | + app.Name = "blob-uploader" |
| 34 | + app.Usage = "The Scroll Blob Uploader" |
| 35 | + app.Version = version.Version |
| 36 | + app.Flags = append(app.Flags, utils.CommonFlags...) |
| 37 | + app.Flags = append(app.Flags, utils.RollupRelayerFlags...) |
| 38 | + app.Commands = []*cli.Command{} |
| 39 | + app.Before = func(ctx *cli.Context) error { |
| 40 | + return utils.LogSetup(ctx) |
| 41 | + } |
| 42 | + // Register `rollup-relayer-test` app for integration-test. |
| 43 | + utils.RegisterSimulation(app, utils.RollupRelayerApp) |
| 44 | +} |
| 45 | + |
| 46 | +func action(ctx *cli.Context) error { |
| 47 | + // Load config file. |
| 48 | + cfgFile := ctx.String(utils.ConfigFileFlag.Name) |
| 49 | + cfg, err := config.NewConfig(cfgFile) |
| 50 | + if err != nil { |
| 51 | + log.Crit("failed to load config file", "config file", cfgFile, "error", err) |
| 52 | + } |
| 53 | + |
| 54 | + subCtx, cancel := context.WithCancel(ctx.Context) |
| 55 | + // Init db connection |
| 56 | + db, err := database.InitDB(cfg.DBConfig) |
| 57 | + if err != nil { |
| 58 | + log.Crit("failed to init db connection", "err", err) |
| 59 | + } |
| 60 | + defer func() { |
| 61 | + cancel() |
| 62 | + if err = database.CloseDB(db); err != nil { |
| 63 | + log.Crit("failed to close db connection", "error", err) |
| 64 | + } |
| 65 | + }() |
| 66 | + |
| 67 | + registry := prometheus.DefaultRegisterer |
| 68 | + observability.Server(ctx, db) |
| 69 | + |
| 70 | + // Init l2geth connection |
| 71 | + l2client, err := ethclient.Dial(cfg.L2Config.Endpoint) |
| 72 | + if err != nil { |
| 73 | + log.Crit("failed to connect l2 geth", "config file", cfgFile, "error", err) |
| 74 | + } |
| 75 | + |
| 76 | + genesisPath := ctx.String(utils.Genesis.Name) |
| 77 | + genesis, err := utils.ReadGenesis(genesisPath) |
| 78 | + if err != nil { |
| 79 | + log.Crit("failed to read genesis", "genesis file", genesisPath, "error", err) |
| 80 | + } |
| 81 | + |
| 82 | + // sanity check config |
| 83 | + if cfg.L2Config.RelayerConfig.BatchSubmission == nil { |
| 84 | + log.Crit("cfg.L2Config.RelayerConfig.BatchSubmission must not be nil") |
| 85 | + } |
| 86 | + if cfg.L2Config.RelayerConfig.BatchSubmission.MinBatches < 1 { |
| 87 | + log.Crit("cfg.L2Config.RelayerConfig.SenderConfig.BatchSubmission.MinBatches must be at least 1") |
| 88 | + } |
| 89 | + if cfg.L2Config.RelayerConfig.BatchSubmission.MaxBatches < 1 { |
| 90 | + log.Crit("cfg.L2Config.RelayerConfig.SenderConfig.BatchSubmission.MaxBatches must be at least 1") |
| 91 | + } |
| 92 | + if cfg.L2Config.BatchProposerConfig.MaxChunksPerBatch <= 0 { |
| 93 | + log.Crit("cfg.L2Config.BatchProposerConfig.MaxChunksPerBatch must be greater than 0") |
| 94 | + } |
| 95 | + if cfg.L2Config.ChunkProposerConfig.MaxL2GasPerChunk <= 0 { |
| 96 | + log.Crit("cfg.L2Config.ChunkProposerConfig.MaxL2GasPerChunk must be greater than 0") |
| 97 | + } |
| 98 | + |
| 99 | + l2relayer, err := relayer.NewLayer2Relayer(ctx.Context, l2client, db, cfg.L2Config.RelayerConfig, genesis.Config, relayer.ServiceTypeL2RollupRelayer, registry) |
| 100 | + if err != nil { |
| 101 | + log.Crit("failed to create l2 relayer", "config file", cfgFile, "error", err) |
| 102 | + } |
| 103 | + |
| 104 | + minCodecVersion := encoding.CodecVersion(ctx.Uint(utils.MinCodecVersionFlag.Name)) |
| 105 | + if minCodecVersion < encoding.CodecV7 { |
| 106 | + log.Crit("min codec version must be greater than or equal to CodecV7", "minCodecVersion", minCodecVersion) |
| 107 | + } |
| 108 | + |
| 109 | + chunkProposer := watcher.NewChunkProposer(subCtx, cfg.L2Config.ChunkProposerConfig, minCodecVersion, genesis.Config, db, registry) |
| 110 | + batchProposer := watcher.NewBatchProposer(subCtx, cfg.L2Config.BatchProposerConfig, minCodecVersion, genesis.Config, db, registry) |
| 111 | + bundleProposer := watcher.NewBundleProposer(subCtx, cfg.L2Config.BundleProposerConfig, minCodecVersion, genesis.Config, db, registry) |
| 112 | + |
| 113 | + l2watcher := watcher.NewL2WatcherClient(subCtx, l2client, cfg.L2Config.Confirmations, cfg.L2Config.L2MessageQueueAddress, cfg.L2Config.WithdrawTrieRootSlot, genesis.Config, db, registry) |
| 114 | + |
| 115 | + // Watcher loop to fetch missing blocks |
| 116 | + go utils.LoopWithContext(subCtx, 2*time.Second, func(ctx context.Context) { |
| 117 | + number, loopErr := rutils.GetLatestConfirmedBlockNumber(ctx, l2client, cfg.L2Config.Confirmations) |
| 118 | + if loopErr != nil { |
| 119 | + log.Error("failed to get block number", "err", loopErr) |
| 120 | + return |
| 121 | + } |
| 122 | + l2watcher.TryFetchRunningMissingBlocks(number) |
| 123 | + }) |
| 124 | + |
| 125 | + go utils.Loop(subCtx, time.Duration(cfg.L2Config.ChunkProposerConfig.ProposeIntervalMilliseconds)*time.Millisecond, chunkProposer.TryProposeChunk) |
| 126 | + |
| 127 | + go utils.Loop(subCtx, time.Duration(cfg.L2Config.BatchProposerConfig.ProposeIntervalMilliseconds)*time.Millisecond, batchProposer.TryProposeBatch) |
| 128 | + |
| 129 | + go utils.Loop(subCtx, 10*time.Second, bundleProposer.TryProposeBundle) |
| 130 | + |
| 131 | + go utils.Loop(subCtx, 2*time.Second, l2relayer.ProcessPendingBatches) |
| 132 | + |
| 133 | + go utils.Loop(subCtx, 15*time.Second, l2relayer.ProcessPendingBundles) |
| 134 | + |
| 135 | + // Finish start all blob-uploader functions. |
| 136 | + log.Info("Start blob-uploader successfully", "version", version.Version) |
| 137 | + |
| 138 | + // Catch CTRL-C to ensure a graceful shutdown. |
| 139 | + interrupt := make(chan os.Signal, 1) |
| 140 | + signal.Notify(interrupt, os.Interrupt) |
| 141 | + |
| 142 | + // Wait until the interrupt signal is received from an OS signal. |
| 143 | + <-interrupt |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// Run rollup relayer cmd instance. |
| 149 | +func Run() { |
| 150 | + if err := app.Run(os.Args); err != nil { |
| 151 | + _, _ = fmt.Fprintln(os.Stderr, err) |
| 152 | + os.Exit(1) |
| 153 | + } |
| 154 | +} |
0 commit comments