flags.go 8.9 KB
Newer Older
1 2 3
package flags

import (
4
	"fmt"
5
	"slices"
6
	"strings"
7
	"time"
8

9
	"github.com/urfave/cli/v2"
10

11
	altda "github.com/ethereum-optimism/optimism/op-alt-da"
12
	"github.com/ethereum-optimism/optimism/op-batcher/compressor"
13
	"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
14
	opservice "github.com/ethereum-optimism/optimism/op-service"
15
	openum "github.com/ethereum-optimism/optimism/op-service/enum"
16 17
	oplog "github.com/ethereum-optimism/optimism/op-service/log"
	opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
18
	"github.com/ethereum-optimism/optimism/op-service/oppprof"
19
	oprpc "github.com/ethereum-optimism/optimism/op-service/rpc"
20
	"github.com/ethereum-optimism/optimism/op-service/txmgr"
21 22
)

23
const EnvVarPrefix = "OP_BATCHER"
24

25
func prefixEnvVars(name string) []string {
26
	return opservice.PrefixEnvVar(EnvVarPrefix, name)
27 28
}

29
var (
30
	// Required flags
31 32 33 34
	L1EthRpcFlag = &cli.StringFlag{
		Name:    "l1-eth-rpc",
		Usage:   "HTTP provider URL for L1",
		EnvVars: prefixEnvVars("L1_ETH_RPC"),
35
	}
36 37
	L2EthRpcFlag = &cli.StringFlag{
		Name:    "l2-eth-rpc",
38
		Usage:   "HTTP provider URL for L2 execution engine. A comma-separated list enables the active L2 endpoint provider. Such a list needs to match the number of rollup-rpcs provided.",
39
		EnvVars: prefixEnvVars("L2_ETH_RPC"),
40
	}
41 42
	RollupRpcFlag = &cli.StringFlag{
		Name:    "rollup-rpc",
43
		Usage:   "HTTP provider URL for Rollup node. A comma-separated list enables the active L2 endpoint provider. Such a list needs to match the number of l2-eth-rpcs provided.",
44
		EnvVars: prefixEnvVars("ROLLUP_RPC"),
45
	}
46
	// Optional flags
47
	SubSafetyMarginFlag = &cli.Uint64Flag{
48 49 50 51
		Name: "sub-safety-margin",
		Usage: "The batcher tx submission safety margin (in #L1-blocks) to subtract " +
			"from a channel's timeout and sequencing window, to guarantee safe inclusion " +
			"of a channel on L1.",
52 53
		Value:   10,
		EnvVars: prefixEnvVars("SUB_SAFETY_MARGIN"),
54
	}
55 56 57 58 59
	PollIntervalFlag = &cli.DurationFlag{
		Name:    "poll-interval",
		Usage:   "How frequently to poll L2 for new blocks",
		Value:   6 * time.Second,
		EnvVars: prefixEnvVars("POLL_INTERVAL"),
60
	}
61 62 63 64 65
	MaxPendingTransactionsFlag = &cli.Uint64Flag{
		Name:    "max-pending-tx",
		Usage:   "The maximum number of pending transactions. 0 for no limit.",
		Value:   1,
		EnvVars: prefixEnvVars("MAX_PENDING_TX"),
66
	}
67 68 69 70 71
	MaxChannelDurationFlag = &cli.Uint64Flag{
		Name:    "max-channel-duration",
		Usage:   "The maximum duration of L1-blocks to keep a channel open. 0 to disable.",
		Value:   0,
		EnvVars: prefixEnvVars("MAX_CHANNEL_DURATION"),
72
	}
73 74
	MaxL1TxSizeBytesFlag = &cli.Uint64Flag{
		Name:    "max-l1-tx-size-bytes",
75 76
		Usage:   "The maximum size of a batch tx submitted to L1. Ignored for blobs, where max blob size will be used.",
		Value:   120_000, // will be overwritten to max for blob da-type
77
		EnvVars: prefixEnvVars("MAX_L1_TX_SIZE_BYTES"),
78
	}
79 80 81 82 83
	MaxBlocksPerSpanBatch = &cli.IntFlag{
		Name:    "max-blocks-per-span-batch",
		Usage:   "Maximum number of blocks to add to a span batch. Default is 0 - no maximum.",
		EnvVars: prefixEnvVars("MAX_BLOCKS_PER_SPAN_BATCH"),
	}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	TargetNumFramesFlag = &cli.IntFlag{
		Name:    "target-num-frames",
		Usage:   "The target number of frames to create per channel. Controls number of blobs per blob tx, if using Blob DA.",
		Value:   1,
		EnvVars: prefixEnvVars("TARGET_NUM_FRAMES"),
	}
	ApproxComprRatioFlag = &cli.Float64Flag{
		Name:    "approx-compr-ratio",
		Usage:   "The approximate compression ratio (<= 1.0). Only relevant for ratio compressor.",
		Value:   0.6,
		EnvVars: prefixEnvVars("APPROX_COMPR_RATIO"),
	}
	CompressorFlag = &cli.StringFlag{
		Name:    "compressor",
		Usage:   "The type of compressor. Valid options: " + strings.Join(compressor.KindKeys, ", "),
		EnvVars: prefixEnvVars("COMPRESSOR"),
		Value:   compressor.ShadowKind,
		Action: func(_ *cli.Context, s string) error {
			if !slices.Contains(compressor.KindKeys, s) {
				return fmt.Errorf("unsupported compressor: %s", s)
			}
			return nil
		},
	}
108 109
	CompressionAlgoFlag = &cli.GenericFlag{
		Name:    "compression-algo",
110
		Usage:   "The compression algorithm to use. Valid options: " + openum.EnumString(derive.CompressionAlgos),
111 112 113 114 115 116
		EnvVars: prefixEnvVars("COMPRESSION_ALGO"),
		Value: func() *derive.CompressionAlgo {
			out := derive.Zlib
			return &out
		}(),
	}
117 118 119 120
	StoppedFlag = &cli.BoolFlag{
		Name:    "stopped",
		Usage:   "Initialize the batcher in a stopped state. The batcher can be started using the admin_startBatcher RPC",
		EnvVars: prefixEnvVars("STOPPED"),
121
	}
122
	BatchTypeFlag = &cli.UintFlag{
123 124 125 126 127
		Name:        "batch-type",
		Usage:       "The batch type. 0 for SingularBatch and 1 for SpanBatch.",
		Value:       0,
		EnvVars:     prefixEnvVars("BATCH_TYPE"),
		DefaultText: "singular",
128
	}
129 130 131 132 133 134 135 136
	DataAvailabilityTypeFlag = &cli.GenericFlag{
		Name: "data-availability-type",
		Usage: "The data availability type to use for submitting batches to the L1. Valid options: " +
			openum.EnumString(DataAvailabilityTypes),
		Value: func() *DataAvailabilityType {
			out := CalldataType
			return &out
		}(),
137 138
		EnvVars: prefixEnvVars("DATA_AVAILABILITY_TYPE"),
	}
139 140 141 142 143 144
	ActiveSequencerCheckDurationFlag = &cli.DurationFlag{
		Name:    "active-sequencer-check-duration",
		Usage:   "The duration between checks to determine the active sequencer endpoint. ",
		Value:   2 * time.Minute,
		EnvVars: prefixEnvVars("ACTIVE_SEQUENCER_CHECK_DURATION"),
	}
145 146 147 148 149 150 151 152 153 154 155 156 157 158
	CheckRecentTxsDepthFlag = &cli.IntFlag{
		Name: "check-recent-txs-depth",
		Usage: "Indicates how many blocks back the batcher should look during startup for a recent batch tx on L1. This can " +
			"speed up waiting for node sync. It should be set to the verifier confirmation depth of the sequencer (e.g. 4).",
		Value:   0,
		EnvVars: prefixEnvVars("CHECK_RECENT_TXS_DEPTH"),
	}
	WaitNodeSyncFlag = &cli.BoolFlag{
		Name: "wait-node-sync",
		Usage: "Indicates if, during startup, the batcher should wait for a recent batcher tx on L1 to " +
			"finalize (via more block confirmations). This should help avoid duplicate batcher txs.",
		Value:   false,
		EnvVars: prefixEnvVars("WAIT_NODE_SYNC"),
	}
159
	ThrottleIntervalFlag = &cli.DurationFlag{
160 161 162
		Name:    "throttle-interval",
		Usage:   "Interval between potential DA throttling actions. Zero disables throttling.",
		Value:   2 * time.Second,
163 164 165 166 167 168 169 170 171 172 173
		EnvVars: prefixEnvVars("THROTTLE_INTERVAL"),
	}
	ThrottleThresholdFlag = &cli.IntFlag{
		Name:    "throttle-threshold",
		Usage:   "The threshold on pending-blocks-bytes-current beyond which the batcher will instruct the block builder to start throttling transactions with larger DA demands",
		Value:   1_000_000,
		EnvVars: prefixEnvVars("THROTTLE_THRESHOLD"),
	}
	ThrottleTxSizeFlag = &cli.IntFlag{
		Name:    "throttle-tx-size",
		Usage:   "The DA size of transactions to start throttling when we are over the throttle threshold",
174
		Value:   5000, // less than 1% of all transactions should be affected by this limit
175 176 177 178 179 180 181 182 183 184 185 186 187 188
		EnvVars: prefixEnvVars("THROTTLE_TX_SIZE"),
	}
	ThrottleBlockSizeFlag = &cli.IntFlag{
		Name:    "throttle-block-size",
		Usage:   "The total DA limit to start imposing on block building when we are over the throttle threshold",
		Value:   21_000, // at least 70 transactions per block of up to 300 compressed bytes each.
		EnvVars: prefixEnvVars("THROTTLE_BLOCK_SIZE"),
	}
	ThrottleAlwaysBlockSizeFlag = &cli.IntFlag{
		Name:    "throttle-always-block-size",
		Usage:   "The total DA limit to start imposing on block building at all times",
		Value:   130_000, // should be larger than the builder's max-l2-tx-size to prevent endlessly throttling some txs
		EnvVars: prefixEnvVars("THROTTLE_ALWAYS_BLOCK_SIZE"),
	}
189 190
	// Legacy Flags
	SequencerHDPathFlag = txmgr.SequencerHDPathFlag
191 192 193 194 195
)

var requiredFlags = []cli.Flag{
	L1EthRpcFlag,
	L2EthRpcFlag,
196
	RollupRpcFlag,
197 198 199
}

var optionalFlags = []cli.Flag{
200 201
	WaitNodeSyncFlag,
	CheckRecentTxsDepthFlag,
202 203
	SubSafetyMarginFlag,
	PollIntervalFlag,
204
	MaxPendingTransactionsFlag,
205
	MaxChannelDurationFlag,
206
	MaxL1TxSizeBytesFlag,
207
	MaxBlocksPerSpanBatch,
208 209 210
	TargetNumFramesFlag,
	ApproxComprRatioFlag,
	CompressorFlag,
211
	StoppedFlag,
212
	SequencerHDPathFlag,
213
	BatchTypeFlag,
214
	DataAvailabilityTypeFlag,
215
	ActiveSequencerCheckDurationFlag,
216
	CompressionAlgoFlag,
217 218 219 220 221
	ThrottleThresholdFlag,
	ThrottleIntervalFlag,
	ThrottleTxSizeFlag,
	ThrottleBlockSizeFlag,
	ThrottleAlwaysBlockSizeFlag,
222 223 224
}

func init() {
225 226 227 228 229
	optionalFlags = append(optionalFlags, oprpc.CLIFlags(EnvVarPrefix)...)
	optionalFlags = append(optionalFlags, oplog.CLIFlags(EnvVarPrefix)...)
	optionalFlags = append(optionalFlags, opmetrics.CLIFlags(EnvVarPrefix)...)
	optionalFlags = append(optionalFlags, oppprof.CLIFlags(EnvVarPrefix)...)
	optionalFlags = append(optionalFlags, txmgr.CLIFlags(EnvVarPrefix)...)
230
	optionalFlags = append(optionalFlags, altda.CLIFlags(EnvVarPrefix, "")...)
231 232

	Flags = append(requiredFlags, optionalFlags...)
233 234 235
}

// Flags contains the list of configuration options available to the binary.
236
var Flags []cli.Flag
237 238 239

func CheckRequired(ctx *cli.Context) error {
	for _, f := range requiredFlags {
240 241
		if !ctx.IsSet(f.Names()[0]) {
			return fmt.Errorf("flag %s is required", f.Names()[0])
242 243 244 245
		}
	}
	return nil
}