Commit 6dcb3b35 authored by Conner Fromknecht's avatar Conner Fromknecht

feat: extract driver configuration outside of NewBatchSubmitter

parent 21928467
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/rpc"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
...@@ -21,6 +20,7 @@ import ( ...@@ -21,6 +20,7 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli" "github.com/urfave/cli"
...@@ -37,8 +37,8 @@ const ( ...@@ -37,8 +37,8 @@ const (
// use of a closure allows the parameters bound to the top-level main package, // use of a closure allows the parameters bound to the top-level main package,
// e.g. GitVersion, to be captured and used once the function is executed. // e.g. GitVersion, to be captured and used once the function is executed.
func Main(gitVersion string) func(ctx *cli.Context) error { func Main(gitVersion string) func(ctx *cli.Context) error {
return func(ctx *cli.Context) error { return func(cliCtx *cli.Context) error {
cfg, err := NewConfig(ctx) cfg, err := NewConfig(cliCtx)
if err != nil { if err != nil {
return err return err
} }
...@@ -51,197 +51,182 @@ func Main(gitVersion string) func(ctx *cli.Context) error { ...@@ -51,197 +51,182 @@ func Main(gitVersion string) func(ctx *cli.Context) error {
log.Info("Initializing batch submitter") log.Info("Initializing batch submitter")
batchSubmitter, err := NewBatchSubmitter(cfg, gitVersion) ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Set up our logging. If Sentry is enabled, we will use our custom log
// handler that logs to stdout and forwards any error messages to Sentry
// for collection. Otherwise, logs will only be posted to stdout.
var logHandler log.Handler
if cfg.SentryEnable {
err := sentry.Init(sentry.ClientOptions{
Dsn: cfg.SentryDsn,
Environment: cfg.EthNetworkName,
Release: "batch-submitter@" + gitVersion,
TracesSampleRate: traceRateToFloat64(cfg.SentryTraceRate),
Debug: false,
})
if err != nil {
return err
}
logHandler = SentryStreamHandler(os.Stdout, log.TerminalFormat(true))
} else {
logHandler = log.StreamHandler(os.Stdout, log.TerminalFormat(true))
}
logLevel, err := log.LvlFromString(cfg.LogLevel)
if err != nil { if err != nil {
log.Error("Unable to create batch submitter", "error", err)
return err return err
} }
log.Info("Starting batch submitter") log.Root().SetHandler(log.LvlFilterHandler(logLevel, logHandler))
if err := batchSubmitter.Start(); err != nil { // Parse sequencer private key and CTC contract address.
sequencerPrivKey, ctcAddress, err := parseWalletPrivKeyAndContractAddr(
"Sequencer", cfg.Mnemonic, cfg.SequencerHDPath,
cfg.SequencerPrivateKey, cfg.CTCAddress,
)
if err != nil {
return err return err
} }
defer batchSubmitter.Stop()
log.Info("Batch submitter started") // Parse proposer private key and SCC contract address.
proposerPrivKey, sccAddress, err := parseWalletPrivKeyAndContractAddr(
"Proposer", cfg.Mnemonic, cfg.ProposerHDPath,
cfg.ProposerPrivateKey, cfg.SCCAddress,
)
if err != nil {
return err
}
<-(chan struct{})(nil) // Connect to L1 and L2 providers. Perform these last since they are the
// most expensive.
l1Client, err := dialL1EthClientWithTimeout(ctx, cfg.L1EthRpc, cfg.DisableHTTP2)
if err != nil {
return err
}
return nil l2Client, err := dialL2EthClientWithTimeout(ctx, cfg.L2EthRpc, cfg.DisableHTTP2)
} if err != nil {
} return err
}
// BatchSubmitter is a service that configures the necessary resources for if cfg.MetricsServerEnable {
// running the TxBatchSubmitter and StateBatchSubmitter sub-services. go runMetricsServer(cfg.MetricsHostname, cfg.MetricsPort)
type BatchSubmitter struct { }
ctx context.Context
cfg Config
l1Client *ethclient.Client
l2Client *l2ethclient.Client
sequencerPrivKey *ecdsa.PrivateKey
proposerPrivKey *ecdsa.PrivateKey
ctcAddress common.Address
sccAddress common.Address
batchTxService *Service
batchStateService *Service
}
// NewBatchSubmitter initializes the BatchSubmitter, gathering any resources chainID, err := l1Client.ChainID(ctx)
// that will be needed by the TxBatchSubmitter and StateBatchSubmitter
// sub-services.
func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) {
ctx := context.Background()
// Set up our logging. If Sentry is enabled, we will use our custom
// log handler that logs to stdout and forwards any error messages to
// Sentry for collection. Otherwise, logs will only be posted to stdout.
var logHandler log.Handler
if cfg.SentryEnable {
err := sentry.Init(sentry.ClientOptions{
Dsn: cfg.SentryDsn,
Environment: cfg.EthNetworkName,
Release: "batch-submitter@" + gitVersion,
TracesSampleRate: traceRateToFloat64(cfg.SentryTraceRate),
Debug: false,
})
if err != nil { if err != nil {
return nil, err return err
} }
logHandler = SentryStreamHandler(os.Stdout, log.TerminalFormat(true)) txManagerConfig := txmgr.Config{
} else { ResubmissionTimeout: cfg.ResubmissionTimeout,
logHandler = log.StreamHandler(os.Stdout, log.TerminalFormat(true)) ReceiptQueryInterval: time.Second,
} NumConfirmations: cfg.NumConfirmations,
}
logLevel, err := log.LvlFromString(cfg.LogLevel)
if err != nil {
return nil, err
}
log.Root().SetHandler(log.LvlFilterHandler(logLevel, logHandler)) var services []*Service
if cfg.RunTxBatchSubmitter {
batchTxDriver, err := sequencer.NewDriver(sequencer.Config{
Name: "Sequencer",
L1Client: l1Client,
L2Client: l2Client,
BlockOffset: cfg.BlockOffset,
MaxTxSize: cfg.MaxL1TxSize,
CTCAddr: ctcAddress,
ChainID: chainID,
PrivKey: sequencerPrivKey,
})
if err != nil {
return err
}
// Parse sequencer private key and CTC contract address. services = append(services, NewService(ServiceConfig{
sequencerPrivKey, ctcAddress, err := parseWalletPrivKeyAndContractAddr( Context: ctx,
"Sequencer", cfg.Mnemonic, cfg.SequencerHDPath, Driver: batchTxDriver,
cfg.SequencerPrivateKey, cfg.CTCAddress, PollInterval: cfg.PollInterval,
) ClearPendingTx: cfg.ClearPendingTxs,
if err != nil { L1Client: l1Client,
return nil, err TxManagerConfig: txManagerConfig,
} }))
}
// Parse proposer private key and SCC contract address. if cfg.RunStateBatchSubmitter {
proposerPrivKey, sccAddress, err := parseWalletPrivKeyAndContractAddr( batchStateDriver, err := proposer.NewDriver(proposer.Config{
"Proposer", cfg.Mnemonic, cfg.ProposerHDPath, Name: "Proposer",
cfg.ProposerPrivateKey, cfg.SCCAddress, L1Client: l1Client,
) L2Client: l2Client,
if err != nil { BlockOffset: cfg.BlockOffset,
return nil, err MaxTxSize: cfg.MaxL1TxSize,
} SCCAddr: sccAddress,
CTCAddr: ctcAddress,
ChainID: chainID,
PrivKey: proposerPrivKey,
})
if err != nil {
return err
}
// Connect to L1 and L2 providers. Perform these last since they are the services = append(services, NewService(ServiceConfig{
// most expensive. Context: ctx,
l1Client, err := dialL1EthClientWithTimeout(ctx, cfg.L1EthRpc, cfg.DisableHTTP2) Driver: batchStateDriver,
if err != nil { PollInterval: cfg.PollInterval,
return nil, err ClearPendingTx: cfg.ClearPendingTxs,
} L1Client: l1Client,
TxManagerConfig: txManagerConfig,
}))
}
l2Client, err := dialL2EthClientWithTimeout(ctx, cfg.L2EthRpc, cfg.DisableHTTP2) batchSubmitter, err := NewBatchSubmitter(ctx, cancel, services)
if err != nil { if err != nil {
return nil, err log.Error("Unable to create batch submitter", "error", err)
} return err
}
if cfg.MetricsServerEnable { log.Info("Starting batch submitter")
go runMetricsServer(cfg.MetricsHostname, cfg.MetricsPort)
}
chainID, err := l1Client.ChainID(ctx) if err := batchSubmitter.Start(); err != nil {
if err != nil { return err
return nil, err }
} defer batchSubmitter.Stop()
txManagerConfig := txmgr.Config{ log.Info("Batch submitter started")
ResubmissionTimeout: cfg.ResubmissionTimeout,
ReceiptQueryInterval: time.Second,
NumConfirmations: cfg.NumConfirmations,
}
var batchTxService *Service <-(chan struct{})(nil)
if cfg.RunTxBatchSubmitter {
batchTxDriver, err := sequencer.NewDriver(sequencer.Config{
Name: "Sequencer",
L1Client: l1Client,
L2Client: l2Client,
BlockOffset: cfg.BlockOffset,
MaxTxSize: cfg.MaxL1TxSize,
CTCAddr: ctcAddress,
ChainID: chainID,
PrivKey: sequencerPrivKey,
})
if err != nil {
return nil, err
}
batchTxService = NewService(ServiceConfig{ return nil
Context: ctx,
Driver: batchTxDriver,
PollInterval: cfg.PollInterval,
ClearPendingTx: cfg.ClearPendingTxs,
L1Client: l1Client,
TxManagerConfig: txManagerConfig,
})
} }
}
var batchStateService *Service // BatchSubmitter is a service that configures the necessary resources for
if cfg.RunStateBatchSubmitter { // running the TxBatchSubmitter and StateBatchSubmitter sub-services.
batchStateDriver, err := proposer.NewDriver(proposer.Config{ type BatchSubmitter struct {
Name: "Proposer", ctx context.Context
L1Client: l1Client, services []*Service
L2Client: l2Client, cancel func()
BlockOffset: cfg.BlockOffset, }
MaxTxSize: cfg.MaxL1TxSize,
SCCAddr: sccAddress,
CTCAddr: ctcAddress,
ChainID: chainID,
PrivKey: proposerPrivKey,
})
if err != nil {
return nil, err
}
batchStateService = NewService(ServiceConfig{ // NewBatchSubmitter initializes the BatchSubmitter, gathering any resources
Context: ctx, // that will be needed by the TxBatchSubmitter and StateBatchSubmitter
Driver: batchStateDriver, // sub-services.
PollInterval: cfg.PollInterval, func NewBatchSubmitter(
ClearPendingTx: cfg.ClearPendingTxs, ctx context.Context,
L1Client: l1Client, cancel func(),
TxManagerConfig: txManagerConfig, services []*Service,
}) ) (*BatchSubmitter, error) {
}
return &BatchSubmitter{ return &BatchSubmitter{
ctx: ctx, ctx: ctx,
cfg: cfg, services: services,
l1Client: l1Client, cancel: cancel,
l2Client: l2Client,
sequencerPrivKey: sequencerPrivKey,
proposerPrivKey: proposerPrivKey,
ctcAddress: ctcAddress,
sccAddress: sccAddress,
batchTxService: batchTxService,
batchStateService: batchStateService,
}, nil }, nil
} }
func (b *BatchSubmitter) Start() error { func (b *BatchSubmitter) Start() error {
if b.cfg.RunTxBatchSubmitter { for _, service := range b.services {
if err := b.batchTxService.Start(); err != nil { if err := service.Start(); err != nil {
return err
}
}
if b.cfg.RunStateBatchSubmitter {
if err := b.batchStateService.Start(); err != nil {
return err return err
} }
} }
...@@ -249,11 +234,9 @@ func (b *BatchSubmitter) Start() error { ...@@ -249,11 +234,9 @@ func (b *BatchSubmitter) Start() error {
} }
func (b *BatchSubmitter) Stop() { func (b *BatchSubmitter) Stop() {
if b.cfg.RunTxBatchSubmitter { b.cancel()
_ = b.batchTxService.Stop() for _, service := range b.services {
} _ = service.Stop()
if b.cfg.RunStateBatchSubmitter {
_ = b.batchStateService.Stop()
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment