2023-07-12 00:50:31 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-18 20:51:22 +03:00
|
|
|
"net"
|
2023-07-15 01:17:02 +03:00
|
|
|
|
|
|
|
"github.com/ethereum-optimism/optimism/op-ufm/pkg/config"
|
|
|
|
"github.com/ethereum-optimism/optimism/op-ufm/pkg/metrics"
|
|
|
|
"github.com/ethereum-optimism/optimism/op-ufm/pkg/provider"
|
2023-07-12 00:50:31 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
Config *config.Config
|
2023-07-15 00:08:02 +03:00
|
|
|
Healthz *HealthzServer
|
|
|
|
Metrics *MetricsServer
|
2023-07-12 00:50:31 +03:00
|
|
|
Providers map[string]*provider.Provider
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(cfg *config.Config) *Service {
|
|
|
|
s := &Service{
|
|
|
|
Config: cfg,
|
2023-07-15 00:08:02 +03:00
|
|
|
Healthz: &HealthzServer{},
|
|
|
|
Metrics: &MetricsServer{},
|
2023-07-12 00:50:31 +03:00
|
|
|
Providers: make(map[string]*provider.Provider, len(cfg.Providers)),
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Start(ctx context.Context) {
|
|
|
|
log.Info("service starting")
|
|
|
|
if s.Config.Healthz.Enabled {
|
2023-07-18 20:51:22 +03:00
|
|
|
addr := net.JoinHostPort(s.Config.Healthz.Host, s.Config.Healthz.Port)
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Info("starting healthz server",
|
|
|
|
"addr", addr)
|
2023-07-14 02:09:56 +03:00
|
|
|
go func() {
|
2023-07-18 20:51:22 +03:00
|
|
|
if err := s.Healthz.Start(ctx, addr); err != nil {
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Error("error starting healthz server",
|
|
|
|
"err", err)
|
2023-07-14 02:09:56 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-07-15 00:08:02 +03:00
|
|
|
metrics.Debug = s.Config.Metrics.Debug
|
2023-07-14 02:09:56 +03:00
|
|
|
if s.Config.Metrics.Enabled {
|
2023-07-18 20:51:22 +03:00
|
|
|
addr := net.JoinHostPort(s.Config.Metrics.Host, s.Config.Metrics.Port)
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Info("starting metrics server",
|
|
|
|
"addr", addr)
|
2023-07-14 02:09:56 +03:00
|
|
|
go func() {
|
2023-07-15 00:08:02 +03:00
|
|
|
if err := s.Metrics.Start(ctx, addr); err != nil {
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Error("error starting metrics server",
|
|
|
|
"err", err)
|
2023-07-14 02:09:56 +03:00
|
|
|
}
|
|
|
|
}()
|
2023-07-12 00:50:31 +03:00
|
|
|
}
|
2023-07-12 22:20:23 +03:00
|
|
|
|
|
|
|
// map networks to its providers
|
|
|
|
networks := make(map[string][]string)
|
|
|
|
for name, providerConfig := range s.Config.Providers {
|
|
|
|
networks[providerConfig.Network] = append(networks[providerConfig.Network], name)
|
|
|
|
}
|
|
|
|
|
|
|
|
txpool := &provider.TransactionPool{}
|
|
|
|
for name, providers := range networks {
|
|
|
|
if len(providers) == 1 {
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Warn("can't measure first seen for network, please another provider",
|
|
|
|
"network", name)
|
2023-07-12 22:20:23 +03:00
|
|
|
}
|
|
|
|
(*txpool)[name] = &provider.NetworkTransactionPool{}
|
|
|
|
(*txpool)[name].Transactions = make(map[string]*provider.TransactionState)
|
2023-07-14 02:09:56 +03:00
|
|
|
// set expected number of providers for this network
|
|
|
|
// -1 since we don't wait for acking from the same provider
|
|
|
|
(*txpool)[name].Expected = len(providers) - 1
|
2023-07-12 22:20:23 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 00:50:31 +03:00
|
|
|
for name, providerConfig := range s.Config.Providers {
|
2023-07-12 22:20:23 +03:00
|
|
|
s.Providers[name] = provider.New(name,
|
|
|
|
providerConfig,
|
|
|
|
&s.Config.Signer,
|
|
|
|
s.Config.Wallets[providerConfig.Wallet],
|
|
|
|
(*txpool)[providerConfig.Network])
|
2023-07-12 00:50:31 +03:00
|
|
|
s.Providers[name].Start(ctx)
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Info("provider started",
|
|
|
|
"provider", name)
|
2023-07-12 00:50:31 +03:00
|
|
|
}
|
2023-07-14 02:09:56 +03:00
|
|
|
|
2023-07-12 00:50:31 +03:00
|
|
|
log.Info("service started")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Shutdown() {
|
|
|
|
log.Info("service shutting down")
|
|
|
|
if s.Config.Healthz.Enabled {
|
2024-05-30 21:41:03 +03:00
|
|
|
if err := s.Healthz.Shutdown(); err != nil {
|
|
|
|
log.Error("Error shutting down healthz server", err)
|
|
|
|
}
|
2023-07-12 00:50:31 +03:00
|
|
|
log.Info("healthz stopped")
|
|
|
|
}
|
2023-07-15 00:08:02 +03:00
|
|
|
if s.Config.Metrics.Enabled {
|
2024-05-30 21:41:03 +03:00
|
|
|
if err := s.Metrics.Shutdown(); err != nil {
|
|
|
|
log.Error("Error shutting down metrics server", err)
|
|
|
|
}
|
2023-07-15 00:08:02 +03:00
|
|
|
log.Info("metrics stopped")
|
|
|
|
}
|
2023-07-12 00:50:31 +03:00
|
|
|
for name, provider := range s.Providers {
|
|
|
|
provider.Shutdown()
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Info("provider stopped",
|
|
|
|
"provider", name)
|
2023-07-12 00:50:31 +03:00
|
|
|
}
|
|
|
|
log.Info("service stopped")
|
|
|
|
}
|