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,52 +51,12 @@ func Main(gitVersion string) func(ctx *cli.Context) error { ...@@ -51,52 +51,12 @@ 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())
if err != nil { defer cancel()
log.Error("Unable to create batch submitter", "error", err)
return err
}
log.Info("Starting batch submitter")
if err := batchSubmitter.Start(); err != nil {
return err
}
defer batchSubmitter.Stop()
log.Info("Batch submitter started")
<-(chan struct{})(nil)
return nil
}
}
// BatchSubmitter is a service that configures the necessary resources for
// running the TxBatchSubmitter and StateBatchSubmitter sub-services.
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
// 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 // Set up our logging. If Sentry is enabled, we will use our custom log
// log handler that logs to stdout and forwards any error messages to // handler that logs to stdout and forwards any error messages to Sentry
// Sentry for collection. Otherwise, logs will only be posted to stdout. // for collection. Otherwise, logs will only be posted to stdout.
var logHandler log.Handler var logHandler log.Handler
if cfg.SentryEnable { if cfg.SentryEnable {
err := sentry.Init(sentry.ClientOptions{ err := sentry.Init(sentry.ClientOptions{
...@@ -107,7 +67,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) { ...@@ -107,7 +67,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) {
Debug: false, Debug: false,
}) })
if err != nil { if err != nil {
return nil, err return err
} }
logHandler = SentryStreamHandler(os.Stdout, log.TerminalFormat(true)) logHandler = SentryStreamHandler(os.Stdout, log.TerminalFormat(true))
...@@ -117,7 +77,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) { ...@@ -117,7 +77,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) {
logLevel, err := log.LvlFromString(cfg.LogLevel) logLevel, err := log.LvlFromString(cfg.LogLevel)
if err != nil { if err != nil {
return nil, err return err
} }
log.Root().SetHandler(log.LvlFilterHandler(logLevel, logHandler)) log.Root().SetHandler(log.LvlFilterHandler(logLevel, logHandler))
...@@ -128,7 +88,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) { ...@@ -128,7 +88,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) {
cfg.SequencerPrivateKey, cfg.CTCAddress, cfg.SequencerPrivateKey, cfg.CTCAddress,
) )
if err != nil { if err != nil {
return nil, err return err
} }
// Parse proposer private key and SCC contract address. // Parse proposer private key and SCC contract address.
...@@ -137,19 +97,19 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) { ...@@ -137,19 +97,19 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) {
cfg.ProposerPrivateKey, cfg.SCCAddress, cfg.ProposerPrivateKey, cfg.SCCAddress,
) )
if err != nil { if err != nil {
return nil, err return err
} }
// Connect to L1 and L2 providers. Perform these last since they are the // Connect to L1 and L2 providers. Perform these last since they are the
// most expensive. // most expensive.
l1Client, err := dialL1EthClientWithTimeout(ctx, cfg.L1EthRpc, cfg.DisableHTTP2) l1Client, err := dialL1EthClientWithTimeout(ctx, cfg.L1EthRpc, cfg.DisableHTTP2)
if err != nil { if err != nil {
return nil, err return err
} }
l2Client, err := dialL2EthClientWithTimeout(ctx, cfg.L2EthRpc, cfg.DisableHTTP2) l2Client, err := dialL2EthClientWithTimeout(ctx, cfg.L2EthRpc, cfg.DisableHTTP2)
if err != nil { if err != nil {
return nil, err return err
} }
if cfg.MetricsServerEnable { if cfg.MetricsServerEnable {
...@@ -158,7 +118,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) { ...@@ -158,7 +118,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) {
chainID, err := l1Client.ChainID(ctx) chainID, err := l1Client.ChainID(ctx)
if err != nil { if err != nil {
return nil, err return err
} }
txManagerConfig := txmgr.Config{ txManagerConfig := txmgr.Config{
...@@ -167,7 +127,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) { ...@@ -167,7 +127,7 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) {
NumConfirmations: cfg.NumConfirmations, NumConfirmations: cfg.NumConfirmations,
} }
var batchTxService *Service var services []*Service
if cfg.RunTxBatchSubmitter { if cfg.RunTxBatchSubmitter {
batchTxDriver, err := sequencer.NewDriver(sequencer.Config{ batchTxDriver, err := sequencer.NewDriver(sequencer.Config{
Name: "Sequencer", Name: "Sequencer",
...@@ -180,20 +140,19 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) { ...@@ -180,20 +140,19 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) {
PrivKey: sequencerPrivKey, PrivKey: sequencerPrivKey,
}) })
if err != nil { if err != nil {
return nil, err return err
} }
batchTxService = NewService(ServiceConfig{ services = append(services, NewService(ServiceConfig{
Context: ctx, Context: ctx,
Driver: batchTxDriver, Driver: batchTxDriver,
PollInterval: cfg.PollInterval, PollInterval: cfg.PollInterval,
ClearPendingTx: cfg.ClearPendingTxs, ClearPendingTx: cfg.ClearPendingTxs,
L1Client: l1Client, L1Client: l1Client,
TxManagerConfig: txManagerConfig, TxManagerConfig: txManagerConfig,
}) }))
} }
var batchStateService *Service
if cfg.RunStateBatchSubmitter { if cfg.RunStateBatchSubmitter {
batchStateDriver, err := proposer.NewDriver(proposer.Config{ batchStateDriver, err := proposer.NewDriver(proposer.Config{
Name: "Proposer", Name: "Proposer",
...@@ -207,41 +166,67 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) { ...@@ -207,41 +166,67 @@ func NewBatchSubmitter(cfg Config, gitVersion string) (*BatchSubmitter, error) {
PrivKey: proposerPrivKey, PrivKey: proposerPrivKey,
}) })
if err != nil { if err != nil {
return nil, err return err
} }
batchStateService = NewService(ServiceConfig{ services = append(services, NewService(ServiceConfig{
Context: ctx, Context: ctx,
Driver: batchStateDriver, Driver: batchStateDriver,
PollInterval: cfg.PollInterval, PollInterval: cfg.PollInterval,
ClearPendingTx: cfg.ClearPendingTxs, ClearPendingTx: cfg.ClearPendingTxs,
L1Client: l1Client, L1Client: l1Client,
TxManagerConfig: txManagerConfig, TxManagerConfig: txManagerConfig,
}) }))
}
batchSubmitter, err := NewBatchSubmitter(ctx, cancel, services)
if err != nil {
log.Error("Unable to create batch submitter", "error", err)
return err
} }
log.Info("Starting batch submitter")
if err := batchSubmitter.Start(); err != nil {
return err
}
defer batchSubmitter.Stop()
log.Info("Batch submitter started")
<-(chan struct{})(nil)
return nil
}
}
// BatchSubmitter is a service that configures the necessary resources for
// running the TxBatchSubmitter and StateBatchSubmitter sub-services.
type BatchSubmitter struct {
ctx context.Context
services []*Service
cancel func()
}
// NewBatchSubmitter initializes the BatchSubmitter, gathering any resources
// that will be needed by the TxBatchSubmitter and StateBatchSubmitter
// sub-services.
func NewBatchSubmitter(
ctx context.Context,
cancel func(),
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