|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | + "github.com/scroll-tech/da-codec/encoding" |
| 13 | + "github.com/scroll-tech/go-ethereum/common" |
| 14 | + gethTypes "github.com/scroll-tech/go-ethereum/core/types" |
| 15 | + "github.com/scroll-tech/go-ethereum/ethclient" |
| 16 | + "github.com/scroll-tech/go-ethereum/log" |
| 17 | + "github.com/urfave/cli/v2" |
| 18 | + |
| 19 | + "scroll-tech/common/database" |
| 20 | + "scroll-tech/common/types" |
| 21 | + "scroll-tech/common/utils" |
| 22 | + "scroll-tech/common/version" |
| 23 | + |
| 24 | + "scroll-tech/rollup/internal/config" |
| 25 | + "scroll-tech/rollup/internal/controller/watcher" |
| 26 | + "scroll-tech/rollup/internal/orm" |
| 27 | + rutils "scroll-tech/rollup/internal/utils" |
| 28 | +) |
| 29 | + |
| 30 | +var app *cli.App |
| 31 | + |
| 32 | +func init() { |
| 33 | + // Set up proposer-tool app info. |
| 34 | + app = cli.NewApp() |
| 35 | + app.Action = action |
| 36 | + app.Name = "proposer-tool" |
| 37 | + app.Usage = "The Scroll Proposer Tool" |
| 38 | + app.Version = version.Version |
| 39 | + app.Flags = append(app.Flags, utils.CommonFlags...) |
| 40 | + app.Flags = append(app.Flags, utils.RollupRelayerFlags...) |
| 41 | + app.Commands = []*cli.Command{} |
| 42 | + app.Before = func(ctx *cli.Context) error { |
| 43 | + return utils.LogSetup(ctx) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func action(ctx *cli.Context) error { |
| 48 | + // Load config file. |
| 49 | + cfgFile := ctx.String(utils.ConfigFileFlag.Name) |
| 50 | + cfg, err := config.NewConfig(cfgFile) |
| 51 | + if err != nil { |
| 52 | + log.Crit("failed to load config file", "config file", cfgFile, "error", err) |
| 53 | + } |
| 54 | + |
| 55 | + subCtx, cancel := context.WithCancel(ctx.Context) |
| 56 | + // Init db connection |
| 57 | + db, err := database.InitDB(cfg.DBConfig) |
| 58 | + if err != nil { |
| 59 | + log.Crit("failed to init db connection", "err", err) |
| 60 | + } |
| 61 | + defer func() { |
| 62 | + cancel() |
| 63 | + if err = database.CloseDB(db); err != nil { |
| 64 | + log.Crit("failed to close db connection", "error", err) |
| 65 | + } |
| 66 | + }() |
| 67 | + |
| 68 | + // Init l2BlockOrm connection |
| 69 | + dbForReplay, err := database.InitDB(cfg.DBConfigForReplay) |
| 70 | + if err != nil { |
| 71 | + log.Crit("failed to init l2BlockOrm connection", "err", err) |
| 72 | + } |
| 73 | + defer func() { |
| 74 | + cancel() |
| 75 | + if err = database.CloseDB(dbForReplay); err != nil { |
| 76 | + log.Crit("failed to close l2BlockOrm connection", "error", err) |
| 77 | + } |
| 78 | + }() |
| 79 | + |
| 80 | + // Init l2geth connection |
| 81 | + l2Client, err := ethclient.Dial(cfg.L2Config.Endpoint) |
| 82 | + if err != nil { |
| 83 | + log.Crit("failed to connect l2 geth", "config file", cfgFile, "error", err) |
| 84 | + } |
| 85 | + |
| 86 | + genesisHeader, err := l2Client.HeaderByNumber(subCtx, big.NewInt(0)) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to retrieve L2 genesis header: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + genesisTime := genesisHeader.Time |
| 92 | + currentTime := uint64(time.Now().Unix()) |
| 93 | + timeDrift := currentTime - genesisTime |
| 94 | + |
| 95 | + cfg.L2Config.ChunkProposerConfig.ChunkTimeoutSec += timeDrift |
| 96 | + cfg.L2Config.BatchProposerConfig.BatchTimeoutSec += timeDrift |
| 97 | + cfg.L2Config.BundleProposerConfig.BundleTimeoutSec += timeDrift |
| 98 | + |
| 99 | + chunk := &encoding.Chunk{ |
| 100 | + Blocks: []*encoding.Block{{ |
| 101 | + Header: genesisHeader, |
| 102 | + Transactions: nil, |
| 103 | + WithdrawRoot: common.Hash{}, |
| 104 | + RowConsumption: &gethTypes.RowConsumption{}, |
| 105 | + }}, |
| 106 | + } |
| 107 | + |
| 108 | + var dbChunk *orm.Chunk |
| 109 | + dbChunk, err = orm.NewChunk(db).InsertChunk(subCtx, chunk, encoding.CodecV0, rutils.ChunkMetrics{}) |
| 110 | + if err != nil { |
| 111 | + log.Crit("failed to insert chunk", "error", err) |
| 112 | + } |
| 113 | + |
| 114 | + if err = orm.NewChunk(db).UpdateProvingStatus(subCtx, dbChunk.Hash, types.ProvingTaskVerified); err != nil { |
| 115 | + log.Crit("failed to update genesis chunk proving status", "error", err) |
| 116 | + } |
| 117 | + |
| 118 | + batch := &encoding.Batch{ |
| 119 | + Index: 0, |
| 120 | + TotalL1MessagePoppedBefore: 0, |
| 121 | + ParentBatchHash: common.Hash{}, |
| 122 | + Chunks: []*encoding.Chunk{chunk}, |
| 123 | + } |
| 124 | + |
| 125 | + var dbBatch *orm.Batch |
| 126 | + dbBatch, err = orm.NewBatch(db).InsertBatch(subCtx, batch, encoding.CodecV0, rutils.BatchMetrics{}) |
| 127 | + if err != nil { |
| 128 | + log.Crit("failed to insert batch", "error", err) |
| 129 | + } |
| 130 | + |
| 131 | + if err = orm.NewChunk(db).UpdateBatchHashInRange(subCtx, 0, 0, dbBatch.Hash); err != nil { |
| 132 | + log.Crit("failed to update batch hash for chunks", "error", err) |
| 133 | + } |
| 134 | + |
| 135 | + registry := prometheus.DefaultRegisterer |
| 136 | + |
| 137 | + genesisPath := ctx.String(utils.Genesis.Name) |
| 138 | + genesis, err := utils.ReadGenesis(genesisPath) |
| 139 | + if err != nil { |
| 140 | + log.Crit("failed to read genesis", "genesis file", genesisPath, "error", err) |
| 141 | + } |
| 142 | + |
| 143 | + // sanity check config |
| 144 | + if cfg.L2Config.BatchProposerConfig.MaxChunksPerBatch <= 0 { |
| 145 | + log.Crit("cfg.L2Config.BatchProposerConfig.MaxChunksPerBatch must be greater than 0") |
| 146 | + } |
| 147 | + if cfg.L2Config.ChunkProposerConfig.MaxL2GasPerChunk <= 0 { |
| 148 | + log.Crit("cfg.L2Config.ChunkProposerConfig.MaxL2GasPerChunk must be greater than 0") |
| 149 | + } |
| 150 | + |
| 151 | + minCodecVersion := encoding.CodecVersion(ctx.Uint(utils.MinCodecVersionFlag.Name)) |
| 152 | + chunkProposer := watcher.NewChunkProposer(subCtx, cfg.L2Config.ChunkProposerConfig, minCodecVersion, genesis.Config, dbForReplay, db, registry, true /* used by tool */) |
| 153 | + batchProposer := watcher.NewBatchProposer(subCtx, cfg.L2Config.BatchProposerConfig, minCodecVersion, genesis.Config, dbForReplay, db, registry) |
| 154 | + bundleProposer := watcher.NewBundleProposer(subCtx, cfg.L2Config.BundleProposerConfig, minCodecVersion, genesis.Config, db, registry) |
| 155 | + |
| 156 | + go utils.Loop(subCtx, 100*time.Millisecond, chunkProposer.TryProposeChunk) |
| 157 | + go utils.Loop(subCtx, 100*time.Millisecond, batchProposer.TryProposeBatch) |
| 158 | + go utils.Loop(subCtx, 100*time.Millisecond, bundleProposer.TryProposeBundle) |
| 159 | + |
| 160 | + // Finish start all proposer tool functions. |
| 161 | + log.Info("Start proposer-tool successfully", "version", version.Version) |
| 162 | + |
| 163 | + // Catch CTRL-C to ensure a graceful shutdown. |
| 164 | + interrupt := make(chan os.Signal, 1) |
| 165 | + signal.Notify(interrupt, os.Interrupt) |
| 166 | + |
| 167 | + // Wait until the interrupt signal is received from an OS signal. |
| 168 | + <-interrupt |
| 169 | + |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +// Run proposer tool cmd instance. |
| 174 | +func Run() { |
| 175 | + if err := app.Run(os.Args); err != nil { |
| 176 | + _, _ = fmt.Fprintln(os.Stderr, err) |
| 177 | + os.Exit(1) |
| 178 | + } |
| 179 | +} |
0 commit comments