forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
725 lines (666 loc) · 27.6 KB
/
cli.go
File metadata and controls
725 lines (666 loc) · 27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
package txmgr
import (
"context"
"errors"
"fmt"
"math"
"math/big"
"sync/atomic"
"time"
opservice "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/bgpo"
"github.com/ethereum-optimism/optimism/op-service/cliiface"
opcrypto "github.com/ethereum-optimism/optimism/op-service/crypto"
"github.com/ethereum-optimism/optimism/op-service/eth"
opsigner "github.com/ethereum-optimism/optimism/op-service/signer"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/urfave/cli/v2"
)
const (
// Duplicated L1 RPC flag
L1RPCFlagName = "l1-eth-rpc"
// Key Management Flags (also have signer client flags)
MnemonicFlagName = "mnemonic"
HDPathFlagName = "hd-path"
PrivateKeyFlagName = "private-key"
// TxMgr Flags (new + legacy + some shared flags)
NumConfirmationsFlagName = "num-confirmations"
SafeAbortNonceTooLowCountFlagName = "safe-abort-nonce-too-low-count"
FeeLimitMultiplierFlagName = "fee-limit-multiplier"
FeeLimitThresholdFlagName = "txmgr.fee-limit-threshold"
MinBaseFeeFlagName = "txmgr.min-basefee"
MaxBaseFeeFlagName = "txmgr.max-basefee"
MinTipCapFlagName = "txmgr.min-tip-cap"
MaxTipCapFlagName = "txmgr.max-tip-cap"
RebroadcastIntervalFlagName = "txmgr.rebroadcast-interval"
ResubmissionTimeoutFlagName = "resubmission-timeout"
NetworkTimeoutFlagName = "network-timeout"
RetryIntervalFlagName = "txmgr.retry-interval"
MaxRetriesFlagName = "txmgr.max-retries"
TxSendTimeoutFlagName = "txmgr.send-timeout"
TxNotInMempoolTimeoutFlagName = "txmgr.not-in-mempool-timeout"
ReceiptQueryIntervalFlagName = "txmgr.receipt-query-interval"
AlreadyPublishedCustomErrsFlagName = "txmgr.already-published-custom-errs"
CellProofTimeFlagName = "txmgr.cell-proof-time"
BlobTipCapDynamicFlagName = "txmgr.blob-tip-cap-dynamic"
BlobTipCapPercentileFlagName = "txmgr.blob-tip-cap-percentile"
BlobTipCapRangeFlagName = "txmgr.blob-tip-cap-range"
)
var (
SequencerHDPathFlag = &cli.StringFlag{
Name: "sequencer-hd-path",
Usage: "DEPRECATED: The HD path used to derive the sequencer wallet from the " +
"mnemonic. The mnemonic flag must also be set.",
EnvVars: []string{"OP_BATCHER_SEQUENCER_HD_PATH"},
}
L2OutputHDPathFlag = &cli.StringFlag{
Name: "l2-output-hd-path",
Usage: "DEPRECATED:The HD path used to derive the l2output wallet from the " +
"mnemonic. The mnemonic flag must also be set.",
EnvVars: []string{"OP_PROPOSER_L2_OUTPUT_HD_PATH"},
}
)
type DefaultFlagValues struct {
NumConfirmations uint64
SafeAbortNonceTooLowCount uint64
FeeLimitMultiplier uint64
FeeLimitThresholdGwei float64
MinTipCapGwei float64
MinBaseFeeGwei float64
RebroadcastInterval time.Duration
ResubmissionTimeout time.Duration
NetworkTimeout time.Duration
RetryInterval time.Duration
MaxRetries uint64
TxSendTimeout time.Duration
TxNotInMempoolTimeout time.Duration
ReceiptQueryInterval time.Duration
CellProofTime uint64
BlobTipCapDynamic bool
BlobTipCapPercentile int
BlobTipCapRange int
}
var (
defaultCellProofTime uint64 = math.MaxUint64
DefaultBatcherFlagValues = DefaultFlagValues{
NumConfirmations: uint64(10),
SafeAbortNonceTooLowCount: uint64(3),
FeeLimitMultiplier: uint64(5),
FeeLimitThresholdGwei: 100.0,
MinTipCapGwei: 1.0,
MinBaseFeeGwei: 1.0,
RebroadcastInterval: 12 * time.Second,
ResubmissionTimeout: 48 * time.Second,
NetworkTimeout: 10 * time.Second,
RetryInterval: 1 * time.Second,
MaxRetries: uint64(10),
TxSendTimeout: 0, // Try sending txs indefinitely, to preserve tx ordering for Holocene
TxNotInMempoolTimeout: 2 * time.Minute,
ReceiptQueryInterval: 12 * time.Second,
CellProofTime: defaultCellProofTime,
BlobTipCapDynamic: false,
BlobTipCapPercentile: 60,
BlobTipCapRange: 20,
}
DefaultChallengerFlagValues = DefaultFlagValues{
NumConfirmations: uint64(3),
SafeAbortNonceTooLowCount: uint64(3),
FeeLimitMultiplier: uint64(5),
FeeLimitThresholdGwei: 100.0,
MinTipCapGwei: 1.0,
MinBaseFeeGwei: 1.0,
ResubmissionTimeout: 24 * time.Second,
NetworkTimeout: 10 * time.Second,
RetryInterval: 1 * time.Second,
MaxRetries: uint64(10),
TxSendTimeout: 2 * time.Minute,
TxNotInMempoolTimeout: 1 * time.Minute,
ReceiptQueryInterval: 12 * time.Second,
CellProofTime: defaultCellProofTime,
BlobTipCapDynamic: false,
BlobTipCapPercentile: 0,
BlobTipCapRange: 0,
}
// geth enforces a 1 gwei minimum for blob tx fee
defaultMinBlobTxFee = big.NewInt(params.GWei)
)
// BTOConfig configures the BlobTipOracle for dynamic blob tip cap estimation.
// If provided to txmgr.Config, the txmgr will create and manage a BlobTipOracle.
type BTOConfig struct {
// Backend is the ethclient that implements bgpo.BTOBackend
Backend bgpo.BTOBackend
// ChainConfig is the L1 chain config for blob fee calculation
ChainConfig *params.ChainConfig
// BlobTipCapRange is the number of blocks to analyze (default: 20)
BlobTipCapRange int
// BlobTipCapPercentile is the percentile for tip suggestion (default: 60)
BlobTipCapPercentile int
}
func CLIFlags(envPrefix string) []cli.Flag {
return CLIFlagsWithDefaults(envPrefix, DefaultBatcherFlagValues)
}
// CLIFlagsWithBTO returns CLIFlags with the BlobTipOracle flags included.
// Only the batcher should use this, as it's the only service that uses blob transactions.
func CLIFlagsWithBTO(envPrefix string) []cli.Flag {
return CLIFlagsWithDefaultsAndBTO(envPrefix, DefaultBatcherFlagValues)
}
// CLIFlagsWithDefaultsAndBTO returns CLIFlagsWithDefaults with the BlobTipOracle flags included.
// Only the batcher should use this, as it's the only service that uses blob transactions.
func CLIFlagsWithDefaultsAndBTO(envPrefix string, defaults DefaultFlagValues) []cli.Flag {
prefixEnvVars := func(name string) []string {
return opservice.PrefixEnvVar(envPrefix, name)
}
return append(CLIFlagsWithDefaults(envPrefix, defaults),
&cli.BoolFlag{
Name: BlobTipCapDynamicFlagName,
Usage: "Use dynamic blob tip cap from the blob tip oracle instead of static tip cap for blob transactions. Regular transactions still use min-tip-cap/max-tip-cap.",
EnvVars: prefixEnvVars("TXMGR_BLOB_TIP_CAP_DYNAMIC"),
Value: defaults.BlobTipCapDynamic,
},
&cli.IntFlag{
Name: BlobTipCapPercentileFlagName,
Usage: "Percentile of recent blob tx tips to use for suggestion (1-100). Only used when blob-tip-cap-dynamic is enabled.",
EnvVars: prefixEnvVars("TXMGR_BLOB_TIP_CAP_PERCENTILE"),
Value: defaults.BlobTipCapPercentile,
},
&cli.IntFlag{
Name: BlobTipCapRangeFlagName,
Usage: "Number of recent blocks to analyze for blob tip cap distribution. Only used when blob-tip-cap-dynamic is enabled.",
EnvVars: prefixEnvVars("TXMGR_BLOB_TIP_CAP_RANGE"),
Value: defaults.BlobTipCapRange,
},
)
}
func CLIFlagsWithDefaults(envPrefix string, defaults DefaultFlagValues) []cli.Flag {
prefixEnvVars := func(name string) []string {
return opservice.PrefixEnvVar(envPrefix, name)
}
return append([]cli.Flag{
&cli.StringFlag{
Name: MnemonicFlagName,
Usage: "The mnemonic used to derive the wallets for either the service",
EnvVars: prefixEnvVars("MNEMONIC"),
},
&cli.StringFlag{
Name: HDPathFlagName,
Usage: "The HD path used to derive the sequencer wallet from the mnemonic. The mnemonic flag must also be set.",
EnvVars: prefixEnvVars("HD_PATH"),
},
&cli.StringFlag{
Name: PrivateKeyFlagName,
Usage: "The private key to use with the service. Must not be used with mnemonic.",
EnvVars: prefixEnvVars("PRIVATE_KEY"),
},
&cli.Uint64Flag{
Name: NumConfirmationsFlagName,
Usage: "Number of confirmations which we will wait after sending a transaction",
Value: defaults.NumConfirmations,
EnvVars: prefixEnvVars("NUM_CONFIRMATIONS"),
},
&cli.Uint64Flag{
Name: SafeAbortNonceTooLowCountFlagName,
Usage: "Number of ErrNonceTooLow observations required to give up on a tx at a particular nonce without receiving confirmation",
Value: defaults.SafeAbortNonceTooLowCount,
EnvVars: prefixEnvVars("SAFE_ABORT_NONCE_TOO_LOW_COUNT"),
},
&cli.Uint64Flag{
Name: FeeLimitMultiplierFlagName,
Usage: "The multiplier applied to fee suggestions to put a hard limit on fee increases",
Value: defaults.FeeLimitMultiplier,
EnvVars: prefixEnvVars("TXMGR_FEE_LIMIT_MULTIPLIER"),
},
&cli.Float64Flag{
Name: FeeLimitThresholdFlagName,
Usage: "The minimum threshold (in GWei) at which fee bumping starts to be capped. Allows arbitrary fee bumps below this threshold.",
Value: defaults.FeeLimitThresholdGwei,
EnvVars: prefixEnvVars("TXMGR_FEE_LIMIT_THRESHOLD"),
},
&cli.Float64Flag{
Name: MinTipCapFlagName,
Usage: "Enforces a minimum tip cap (in GWei) to use when determining tx fees. 1 GWei by default.",
Value: defaults.MinTipCapGwei,
EnvVars: prefixEnvVars("TXMGR_MIN_TIP_CAP"),
},
&cli.Float64Flag{
Name: MaxTipCapFlagName,
Usage: "Enforces a maximum tip cap (in GWei) to use when determining tx fees, `TxMgr` returns an error when exceeded. Disabled by default.",
EnvVars: prefixEnvVars("TXMGR_MAX_TIP_CAP"),
},
&cli.Float64Flag{
Name: MinBaseFeeFlagName,
Usage: "Enforces a minimum base fee (in GWei) to assume when determining tx fees. 1 GWei by default.",
Value: defaults.MinBaseFeeGwei,
EnvVars: prefixEnvVars("TXMGR_MIN_BASEFEE"),
},
&cli.Float64Flag{
Name: MaxBaseFeeFlagName,
Usage: "Enforces a maximum base fee (in GWei) to assume when determining tx fees, `TxMgr` returns an error when exceeded. Disabled by default.",
EnvVars: prefixEnvVars("TXMGR_MAX_BASEFEE"),
},
&cli.DurationFlag{
Name: RebroadcastIntervalFlagName,
Usage: "Interval at which a published transaction will be rebroadcasted if it has not yet been mined. Should be less than ResubmissionTimeout to have an effect.",
Value: defaults.RebroadcastInterval,
EnvVars: prefixEnvVars("TXMGR_REBROADCAST_INTERVAL"),
},
&cli.DurationFlag{
Name: ResubmissionTimeoutFlagName,
Usage: "Duration we will wait before resubmitting a transaction to L1",
Value: defaults.ResubmissionTimeout,
EnvVars: prefixEnvVars("RESUBMISSION_TIMEOUT"),
},
&cli.DurationFlag{
Name: NetworkTimeoutFlagName,
Usage: "Timeout for all network operations",
Value: defaults.NetworkTimeout,
EnvVars: prefixEnvVars("NETWORK_TIMEOUT"),
},
&cli.DurationFlag{
Name: RetryIntervalFlagName,
Usage: "Duration we will wait before resubmitting a transaction to L1 on a transient error. Values <= 0 will result in retrying immediately. Should be less than ResubmissionTimeout to have an effect.",
Value: defaults.RetryInterval,
EnvVars: prefixEnvVars("TXMGR_RETRY_INTERVAL"),
},
&cli.Uint64Flag{
Name: MaxRetriesFlagName,
Usage: "Maximum number of times to resubmit a transaction to L1 on a transient error. Set to 0 to disable retries.",
Value: defaults.MaxRetries,
EnvVars: prefixEnvVars("TXMGR_MAX_RETRIES"),
},
&cli.DurationFlag{
Name: TxSendTimeoutFlagName,
Usage: "Timeout for sending transactions. If 0 it is disabled.",
Value: defaults.TxSendTimeout,
EnvVars: prefixEnvVars("TXMGR_TX_SEND_TIMEOUT"),
},
&cli.DurationFlag{
Name: TxNotInMempoolTimeoutFlagName,
Usage: "Timeout for aborting a tx send if the tx does not make it to the mempool.",
Value: defaults.TxNotInMempoolTimeout,
EnvVars: prefixEnvVars("TXMGR_TX_NOT_IN_MEMPOOL_TIMEOUT"),
},
&cli.DurationFlag{
Name: ReceiptQueryIntervalFlagName,
Usage: "Frequency to poll for receipts",
Value: defaults.ReceiptQueryInterval,
EnvVars: prefixEnvVars("TXMGR_RECEIPT_QUERY_INTERVAL"),
},
&cli.StringSliceFlag{
Name: AlreadyPublishedCustomErrsFlagName,
Usage: "List of custom RPC error messages that indicate that a transaction has already been published.",
EnvVars: prefixEnvVars("TXMGR_ALREADY_PUBLISHED_CUSTOM_ERRS"),
},
&cli.Uint64Flag{
Name: CellProofTimeFlagName,
Usage: "Enables cell proofs in blob transactions for Fusaka (EIP-7742) compatibility from the provided unix timestamp. Should be set to the L1 Fusaka time. May be left blank for Ethereum Mainnet, Sepolia, Holesky, or Hoodi L1s.",
EnvVars: prefixEnvVars("TXMGR_CELL_PROOF_TIME"),
Value: defaults.CellProofTime,
},
}, opsigner.CLIFlags(envPrefix, "")...)
}
type CLIConfig struct {
L1RPCURL string
Mnemonic string
HDPath string
SequencerHDPath string
L2OutputHDPath string
PrivateKey string
SignerCLIConfig opsigner.CLIConfig
NumConfirmations uint64
SafeAbortNonceTooLowCount uint64
FeeLimitMultiplier uint64
FeeLimitThresholdGwei float64
MinBaseFeeGwei float64
MinTipCapGwei float64
MaxBaseFeeGwei float64
MaxTipCapGwei float64
RebroadcastInterval time.Duration
ResubmissionTimeout time.Duration
ReceiptQueryInterval time.Duration
NetworkTimeout time.Duration
RetryInterval time.Duration
MaxRetries uint64
TxSendTimeout time.Duration
TxNotInMempoolTimeout time.Duration
AlreadyPublishedCustomErrs []string
CellProofTime uint64
BlobTipCapDynamic bool
BlobTipCapPercentile int
BlobTipCapRange int
}
func NewCLIConfig(l1RPCURL string, defaults DefaultFlagValues) CLIConfig {
return CLIConfig{
L1RPCURL: l1RPCURL,
NumConfirmations: defaults.NumConfirmations,
SafeAbortNonceTooLowCount: defaults.SafeAbortNonceTooLowCount,
FeeLimitMultiplier: defaults.FeeLimitMultiplier,
FeeLimitThresholdGwei: defaults.FeeLimitThresholdGwei,
MinTipCapGwei: defaults.MinTipCapGwei,
MinBaseFeeGwei: defaults.MinBaseFeeGwei,
RebroadcastInterval: defaults.RebroadcastInterval,
ResubmissionTimeout: defaults.ResubmissionTimeout,
NetworkTimeout: defaults.NetworkTimeout,
RetryInterval: defaults.RetryInterval,
MaxRetries: defaults.MaxRetries,
TxSendTimeout: defaults.TxSendTimeout,
TxNotInMempoolTimeout: defaults.TxNotInMempoolTimeout,
ReceiptQueryInterval: defaults.ReceiptQueryInterval,
SignerCLIConfig: opsigner.NewCLIConfig(),
CellProofTime: defaults.CellProofTime,
BlobTipCapDynamic: defaults.BlobTipCapDynamic,
BlobTipCapPercentile: defaults.BlobTipCapPercentile,
BlobTipCapRange: defaults.BlobTipCapRange,
}
}
func (m CLIConfig) Check() error {
if m.L1RPCURL == "" {
return errors.New("must provide a L1 RPC url")
}
if m.NumConfirmations == 0 {
return errors.New("NumConfirmations must not be 0")
}
if m.NetworkTimeout == 0 {
return errors.New("must provide NetworkTimeout")
}
if m.FeeLimitMultiplier == 0 {
return errors.New("must provide FeeLimitMultiplier")
}
if m.MinBaseFeeGwei < m.MinTipCapGwei {
return fmt.Errorf("minBaseFee smaller than minTipCap, have %f < %f",
m.MinBaseFeeGwei, m.MinTipCapGwei)
}
if m.ResubmissionTimeout == 0 {
return errors.New("must provide ResubmissionTimeout")
}
if m.ReceiptQueryInterval == 0 {
return errors.New("must provide ReceiptQueryInterval")
}
if m.TxNotInMempoolTimeout == 0 {
return errors.New("must provide TxNotInMempoolTimeout")
}
if m.SafeAbortNonceTooLowCount == 0 {
return errors.New("SafeAbortNonceTooLowCount must not be 0")
}
if m.BlobTipCapDynamic {
if m.BlobTipCapPercentile < 1 || m.BlobTipCapPercentile > 100 {
return fmt.Errorf("BlobTipCapPercentile must be between 1 and 100, got %d", m.BlobTipCapPercentile)
}
if m.BlobTipCapRange < 1 {
return fmt.Errorf("BlobTipCapRange must be at least 1, got %d", m.BlobTipCapRange)
}
}
if err := m.SignerCLIConfig.Check(); err != nil {
return err
}
atMostOneIsSet := func(options ...bool) bool {
boolToInt := func(b bool) int {
if b {
return 1
}
return 0
}
sum := 0
for _, option := range options {
sum += boolToInt(option)
}
return sum == 1 || sum == 0
}
if !atMostOneIsSet(m.PrivateKey != "", m.Mnemonic != "", m.SignerCLIConfig.Enabled()) {
return errors.New("can only provide at most one of: [private key, mnemonic, remote signer]")
}
return nil
}
func ReadCLIConfig(ctx cliiface.Context) CLIConfig {
return CLIConfig{
L1RPCURL: ctx.String(L1RPCFlagName),
Mnemonic: ctx.String(MnemonicFlagName),
HDPath: ctx.String(HDPathFlagName),
SequencerHDPath: ctx.String(SequencerHDPathFlag.Name),
L2OutputHDPath: ctx.String(L2OutputHDPathFlag.Name),
PrivateKey: ctx.String(PrivateKeyFlagName),
SignerCLIConfig: opsigner.ReadCLIConfig(ctx),
NumConfirmations: ctx.Uint64(NumConfirmationsFlagName),
SafeAbortNonceTooLowCount: ctx.Uint64(SafeAbortNonceTooLowCountFlagName),
FeeLimitMultiplier: ctx.Uint64(FeeLimitMultiplierFlagName),
FeeLimitThresholdGwei: ctx.Float64(FeeLimitThresholdFlagName),
MinBaseFeeGwei: ctx.Float64(MinBaseFeeFlagName),
MaxBaseFeeGwei: ctx.Float64(MaxBaseFeeFlagName),
MinTipCapGwei: ctx.Float64(MinTipCapFlagName),
MaxTipCapGwei: ctx.Float64(MaxTipCapFlagName),
RebroadcastInterval: ctx.Duration(RebroadcastIntervalFlagName),
ResubmissionTimeout: ctx.Duration(ResubmissionTimeoutFlagName),
ReceiptQueryInterval: ctx.Duration(ReceiptQueryIntervalFlagName),
NetworkTimeout: ctx.Duration(NetworkTimeoutFlagName),
RetryInterval: ctx.Duration(RetryIntervalFlagName),
MaxRetries: ctx.Uint64(MaxRetriesFlagName),
TxSendTimeout: ctx.Duration(TxSendTimeoutFlagName),
TxNotInMempoolTimeout: ctx.Duration(TxNotInMempoolTimeoutFlagName),
AlreadyPublishedCustomErrs: ctx.StringSlice(AlreadyPublishedCustomErrsFlagName),
CellProofTime: ctx.Uint64(CellProofTimeFlagName),
BlobTipCapDynamic: ctx.Bool(BlobTipCapDynamicFlagName),
BlobTipCapPercentile: ctx.Int(BlobTipCapPercentileFlagName),
BlobTipCapRange: ctx.Int(BlobTipCapRangeFlagName),
}
}
func NewConfig(cfg CLIConfig, l log.Logger) (*Config, error) {
if err := cfg.Check(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), cfg.NetworkTimeout)
defer cancel()
l1, err := ethclient.DialContext(ctx, cfg.L1RPCURL)
if err != nil {
return nil, fmt.Errorf("could not dial eth client: %w", err)
}
ctx, cancel = context.WithTimeout(context.Background(), cfg.NetworkTimeout)
defer cancel()
chainID, err := l1.ChainID(ctx)
if err != nil {
return nil, fmt.Errorf("could not fetch L1 chain ID: %w", err)
}
// Allow backwards compatible ways of specifying the HD path
hdPath := cfg.HDPath
if hdPath == "" && cfg.SequencerHDPath != "" {
hdPath = cfg.SequencerHDPath
} else if hdPath == "" && cfg.L2OutputHDPath != "" {
hdPath = cfg.L2OutputHDPath
}
signerFactory, from, err := opcrypto.SignerFactoryFromConfig(l, cfg.PrivateKey, cfg.Mnemonic, hdPath, cfg.SignerCLIConfig)
if err != nil {
return nil, fmt.Errorf("could not init signer: %w", err)
}
feeLimitThreshold, err := eth.GweiToWei(cfg.FeeLimitThresholdGwei)
if err != nil {
return nil, fmt.Errorf("invalid fee limit threshold: %w", err)
}
minBaseFee, err := eth.GweiToWei(cfg.MinBaseFeeGwei)
if err != nil {
return nil, fmt.Errorf("invalid min base fee: %w", err)
}
minTipCap, err := eth.GweiToWei(cfg.MinTipCapGwei)
if err != nil {
return nil, fmt.Errorf("invalid min tip cap: %w", err)
}
var maxBaseFee, maxTipCap *big.Int
if cfg.MaxBaseFeeGwei > 0 {
maxBaseFee, err = eth.GweiToWei(cfg.MaxBaseFeeGwei)
if err != nil {
return nil, fmt.Errorf("invalid max base fee: %w", err)
}
}
if cfg.MaxTipCapGwei > 0 {
maxTipCap, err = eth.GweiToWei(cfg.MaxTipCapGwei)
if err != nil {
return nil, fmt.Errorf("invalid max tip cap: %w", err)
}
}
cellProofTime := fallbackToOsakaCellProofTimeIfKnown(chainID, cfg.CellProofTime)
res := Config{
Backend: l1,
ChainID: chainID,
Signer: signerFactory(chainID),
From: from,
TxSendTimeout: cfg.TxSendTimeout,
TxNotInMempoolTimeout: cfg.TxNotInMempoolTimeout,
NetworkTimeout: cfg.NetworkTimeout,
RetryInterval: cfg.RetryInterval,
MaxRetries: cfg.MaxRetries,
ReceiptQueryInterval: cfg.ReceiptQueryInterval,
NumConfirmations: cfg.NumConfirmations,
SafeAbortNonceTooLowCount: cfg.SafeAbortNonceTooLowCount,
AlreadyPublishedCustomErrs: cfg.AlreadyPublishedCustomErrs,
CellProofTime: cellProofTime,
}
if cfg.BlobTipCapDynamic {
l1ChainID := eth.ChainIDFromBig(chainID)
l1ChainConfig := eth.L1ChainConfigByChainID(l1ChainID)
if l1ChainConfig == nil {
return nil, fmt.Errorf("failed to initialize blob tip oracle: L1 chain config not found for chain ID %s", chainID)
} else {
res.BlobTipCapDynamic.Store(cfg.BlobTipCapDynamic)
res.BTOConfig = &BTOConfig{
Backend: l1,
ChainConfig: l1ChainConfig,
BlobTipCapRange: cfg.BlobTipCapRange,
BlobTipCapPercentile: cfg.BlobTipCapPercentile,
}
}
}
res.RebroadcastInterval.Store(int64(cfg.RebroadcastInterval))
res.ResubmissionTimeout.Store(int64(cfg.ResubmissionTimeout))
res.FeeLimitThreshold.Store(feeLimitThreshold)
res.FeeLimitMultiplier.Store(cfg.FeeLimitMultiplier)
res.MinBaseFee.Store(minBaseFee)
res.MaxBaseFee.Store(maxBaseFee)
res.MinTipCap.Store(minTipCap)
res.MaxTipCap.Store(maxTipCap)
res.MinBlobTxFee.Store(defaultMinBlobTxFee)
return &res, nil
}
func fallbackToOsakaCellProofTimeIfKnown(chainID *big.Int, cellProofTime uint64) uint64 {
if cellProofTime != defaultCellProofTime {
return cellProofTime // We only fallback if nothing is set
}
l1ChainConfig := eth.L1ChainConfigByChainID(eth.ChainIDFromBig(chainID))
if l1ChainConfig != nil && l1ChainConfig.OsakaTime != nil {
return *l1ChainConfig.OsakaTime
}
return math.MaxUint64 // Network not known and no override specified, so we never use cell proofs
}
// Config houses parameters for altering the behavior of a SimpleTxManager.
type Config struct {
Backend ETHBackend
// RebroadcastInterval is the interval at which a published transaction
// will be rebroadcasted if it has not yet been mined.
RebroadcastInterval atomic.Int64
// ResubmissionTimeout is the interval at which, if no previously
// published transaction has been mined, the new tx with a bumped gas
// price will be published. Only one publication at MaxGasPrice will be
// attempted.
ResubmissionTimeout atomic.Int64
// The multiplier applied to fee suggestions to put a hard limit on fee increases.
FeeLimitMultiplier atomic.Uint64
// Minimum threshold (in Wei) at which the FeeLimitMultiplier takes effect.
// On low-fee networks, like test networks, this allows for arbitrary fee bumps
// below this threshold.
FeeLimitThreshold atomic.Pointer[big.Int]
// Minimum base fee (in Wei) to assume when determining tx fees.
MinBaseFee atomic.Pointer[big.Int]
// Maximum base fee (in Wei) to assume when determining tx fees.
MaxBaseFee atomic.Pointer[big.Int]
// Minimum tip cap (in Wei) to enforce when determining tx fees.
MinTipCap atomic.Pointer[big.Int]
// Maximum tip cap (in Wei) to enforce when determining tx fees.
MaxTipCap atomic.Pointer[big.Int]
MinBlobTxFee atomic.Pointer[big.Int]
// ChainID is the chain ID of the L1 chain.
ChainID *big.Int
// TxSendTimeout is how long to wait for sending a transaction.
// By default it is unbounded. If set, this is recommended to be at least 20 minutes.
TxSendTimeout time.Duration
// TxNotInMempoolTimeout is how long to wait before aborting a transaction send if the transaction does not
// make it to the mempool. If the tx is in the mempool, TxSendTimeout is used instead.
TxNotInMempoolTimeout time.Duration
// NetworkTimeout is the allowed duration for a single network request.
// This is intended to be used for network requests that can be replayed.
NetworkTimeout time.Duration
// RetryInterval is the interval at which the tx manager will retry
// sending a transaction if it fails with a non-fatal error (e.g. the
// gapped nonce error in the blob pool).
RetryInterval time.Duration
// MaxRetries is the maximum number of times to retry sending a
// transaction. This is used to limit the number of times we retry
// sending a transaction if it fails with a non-fatal error (e.g. the
// gapped nonce error in the blob pool).
MaxRetries uint64
// ReceiptQueryInterval is the interval at which the tx manager will
// query the backend to check for confirmations after a tx at a
// specific gas price has been published.
ReceiptQueryInterval time.Duration
// NumConfirmations specifies how many blocks are need to consider a
// transaction confirmed.
NumConfirmations uint64
// SafeAbortNonceTooLowCount specifies how many ErrNonceTooLow observations
// are required to give up on a tx at a particular nonce without receiving
// confirmation.
SafeAbortNonceTooLowCount uint64
// Signer is used to sign transactions when the gas price is increased.
Signer opcrypto.SignerFn
From common.Address
// GasPriceEstimatorFn is used to estimate the gas price for a transaction.
// If nil, DefaultGasPriceEstimatorFn is used.
GasPriceEstimatorFn GasPriceEstimatorFn
// List of custom RPC error messages that indicate that a transaction has
// already been published.
AlreadyPublishedCustomErrs []string
// CellProofTime is the time at which cell proofs are enabled in blob transaction (for Fusaka (EIP-7742) compatibility).
CellProofTime uint64
// BlobTipCapDynamic enables dynamic blob tip cap from the blob tip oracle
// instead of using static tip cap for blob transactions.
BlobTipCapDynamic atomic.Bool
// BTOConfig is the configuration for the BlobTipOracle.
// If nil, the BlobTipOracle will not be used.
BTOConfig *BTOConfig
}
func (m *Config) Check() error {
if m.Backend == nil {
return errors.New("must provide the Backend")
}
if m.NumConfirmations == 0 {
return errors.New("NumConfirmations must not be 0")
}
if m.NetworkTimeout == 0 {
return errors.New("must provide NetworkTimeout")
}
if m.FeeLimitMultiplier.Load() == 0 {
return errors.New("must provide FeeLimitMultiplier")
}
minBaseFee := m.MinBaseFee.Load()
minTipCap := m.MinTipCap.Load()
if minBaseFee != nil && minTipCap != nil && minBaseFee.Cmp(minTipCap) == -1 {
return fmt.Errorf("minBaseFee smaller than minTipCap, have %v < %v",
minBaseFee, minTipCap)
}
if m.ResubmissionTimeout.Load() == 0 {
return errors.New("must provide ResubmissionTimeout")
}
if m.ReceiptQueryInterval == 0 {
return errors.New("must provide ReceiptQueryInterval")
}
if m.TxNotInMempoolTimeout == 0 {
return errors.New("must provide TxNotInMempoolTimeout")
}
if m.SafeAbortNonceTooLowCount == 0 {
return errors.New("SafeAbortNonceTooLowCount must not be 0")
}
if m.Signer == nil {
return errors.New("must provide the Signer")
}
if m.ChainID == nil {
return errors.New("must provide the ChainID")
}
return nil
}