Skip to content

Commit

Permalink
♻️ Update config and improve main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Jan 25, 2024
1 parent 09749c8 commit 6880f39
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
35 changes: 21 additions & 14 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ import (
"github.com/spf13/viper"
)

var apiServer *api.HTTPServer
var faultDetector *faultdetector.FaultDetector
type app struct {
apiServer *api.HTTPServer
faultDetector *faultdetector.FaultDetector
ctx context.Context
wg *sync.WaitGroup
}

var appObj = app{}

func main() {
ctx, cancel := context.WithCancel(context.Background())
appObj.ctx = ctx
defer cancel()

logger, err := log.NewDefaultProductionLogger()
Expand All @@ -40,33 +47,33 @@ func main() {
}

wg := sync.WaitGroup{}

appObj.wg = &wg
doneChan := make(chan struct{})
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
errorChan := make(chan error, 1)

// Start Fault Detector
faultDetector, err = faultdetector.NewFaultDetector(
appObj.faultDetector, err = faultdetector.NewFaultDetector(
ctx,
logger,
errorChan,
&wg,
config,
appObj.wg,
config.FaultDetectorConfig,
)
if err != nil {
panic(err)
}
wg.Add(1)
go faultDetector.Start()
appObj.wg.Add(1)
go appObj.faultDetector.Start()

// Start API Server
apiServer = api.NewHTTPServer(ctx, logger, &wg, config, errorChan)
wg.Add(1)
go apiServer.Start()
appObj.apiServer = api.NewHTTPServer(ctx, logger, appObj.wg, config, errorChan)
appObj.wg.Add(1)
go appObj.apiServer.Start()

go func() {
wg.Wait()
appObj.wg.Wait()
close(doneChan)
}()

Expand Down Expand Up @@ -120,8 +127,8 @@ func getAppConfig(logger log.Logger, configFilepath string) (*config.Config, err
}

func performCleanup(logger log.Logger) {
faultDetector.Stop()
err := apiServer.Stop()
appObj.faultDetector.Stop()
err := appObj.apiServer.Stop()
if err != nil {
logger.Error("Server shutdown not successful: %w", err)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const (
// Config struct is used to store the contents of the parsed config file.
// The properties (sub-properties) should map on-to-one with the config file.
type Config struct {
System *System `mapstructure:"system"`
Api *Api `mapstructure:"api"`
FaultDetector *FaultDetector `mapstructure:"fault_detector"`
System *System `mapstructure:"system"`
Api *Api `mapstructure:"api"`
FaultDetectorConfig *FaultDetectorConfig `mapstructure:"fault_detector"`
}

// System struct is used to store the contents of the 'system' property from the parsed config file.
Expand All @@ -44,7 +44,7 @@ type Server struct {
}

// FaultDetector struct is used to store the contents of the 'fault_detector' property from the parsed config file.
type FaultDetector struct {
type FaultDetectorConfig struct {
L1RPCEndpoint string `mapstructure:"l1_rpc_endpoint"`
L2RPCEndpoint string `mapstructure:"l2_rpc_endpoint"`
Startbatchindex int64 `mapstructure:"start_batch_index"`
Expand All @@ -69,7 +69,7 @@ func (c *Config) Validate() error {

sysConfigError := c.System.Validate()
apiConfigError := c.Api.Validate()
fdConfigError := c.FaultDetector.Validate()
fdConfigError := c.FaultDetectorConfig.Validate()

validationErrors = multierr.Combine(sysConfigError, apiConfigError, fdConfigError)

Expand Down Expand Up @@ -136,7 +136,7 @@ func (c *Server) Validate() error {
}

// Validate runs validations against an instance of the FaultDetector struct and returns an error when applicable.
func (c *FaultDetector) Validate() error {
func (c *FaultDetectorConfig) Validate() error {
var validationErrors error

l1ProviderMatched, _ := regexp.MatchString(ProviderRegex, c.L1RPCEndpoint)
Expand Down
24 changes: 12 additions & 12 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ func TestValidate_Api(t *testing.T) {
func TestValidate_FaultDetector(t *testing.T) {
testCases := []struct {
name string
config *FaultDetector
config *FaultDetectorConfig
want error
}{
{
name: "should return nil when correct http/https endpoints are given",
config: &FaultDetector{
config: &FaultDetectorConfig{
L1RPCEndpoint: "https://xyz.com",
L2RPCEndpoint: "http://xyz.com",
Startbatchindex: 100,
Expand All @@ -249,7 +249,7 @@ func TestValidate_FaultDetector(t *testing.T) {
},
{
name: "should return nil when correct ws/wss endpoints are given",
config: &FaultDetector{
config: &FaultDetectorConfig{
L1RPCEndpoint: "wss://xyz.com",
L2RPCEndpoint: "ws://xyz.com",
Startbatchindex: 100,
Expand All @@ -259,7 +259,7 @@ func TestValidate_FaultDetector(t *testing.T) {
},
{
name: "should return error when invalid l1 provider endpoint is given",
config: &FaultDetector{
config: &FaultDetectorConfig{
L1RPCEndpoint: "://xyz.com",
L2RPCEndpoint: "http://xyz.com",
Startbatchindex: 100,
Expand All @@ -269,7 +269,7 @@ func TestValidate_FaultDetector(t *testing.T) {
},
{
name: "should return error when invalid l2 provider endpoint is given",
config: &FaultDetector{
config: &FaultDetectorConfig{
L1RPCEndpoint: "http://xyz.com",
L2RPCEndpoint: "ht://xyz.com",
Startbatchindex: 100,
Expand All @@ -279,7 +279,7 @@ func TestValidate_FaultDetector(t *testing.T) {
},
{
name: "should return error when invalid address length",
config: &FaultDetector{
config: &FaultDetectorConfig{
L1RPCEndpoint: "http://xyz.com",
L2RPCEndpoint: "http://xyz.com",
Startbatchindex: 100,
Expand All @@ -289,7 +289,7 @@ func TestValidate_FaultDetector(t *testing.T) {
},
{
name: "should return error when invalid address beginning",
config: &FaultDetector{
config: &FaultDetectorConfig{
L1RPCEndpoint: "http://xyz.com",
L2RPCEndpoint: "http://xyz.com",
Startbatchindex: 100,
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestValidate_Config(t *testing.T) {
"/api",
[]string{"v1"},
},
&FaultDetector{
&FaultDetectorConfig{
L1RPCEndpoint: "https://xyz.com",
L2RPCEndpoint: "http://xyz.com",
Startbatchindex: 100,
Expand All @@ -399,7 +399,7 @@ func TestValidate_Config(t *testing.T) {
"/api",
[]string{"v1"},
},
&FaultDetector{
&FaultDetectorConfig{
L1RPCEndpoint: "https://xyz.com",
L2RPCEndpoint: "http://xyz.com",
Startbatchindex: 100,
Expand All @@ -424,7 +424,7 @@ func TestValidate_Config(t *testing.T) {
"api/",
[]string{"v1", "version1"},
},
&FaultDetector{
&FaultDetectorConfig{
L1RPCEndpoint: "https://xyz.com",
L2RPCEndpoint: "http://xyz.com",
Startbatchindex: 100,
Expand Down Expand Up @@ -458,7 +458,7 @@ func TestValidate_Config(t *testing.T) {
"/api",
[]string{"v1"},
},
&FaultDetector{
&FaultDetectorConfig{
L1RPCEndpoint: "htps://xyz.com",
L2RPCEndpoint: "http://xyz.com",
Startbatchindex: 100,
Expand Down Expand Up @@ -490,7 +490,7 @@ func TestValidate_Config(t *testing.T) {
"/api",
[]string{"v1"},
},
&FaultDetector{
&FaultDetectorConfig{
L1RPCEndpoint: "https://xyz.com",
L2RPCEndpoint: "htps://xyz.com",
Startbatchindex: 100,
Expand Down
8 changes: 3 additions & 5 deletions pkg/faultdetector/faultdetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ type FaultDetector struct {
}

// NewFaultDetector will return [FaultDetector] with the initialized providers and configuration.
func NewFaultDetector(ctx context.Context, logger log.Logger, errorChan chan error, wg *sync.WaitGroup, config *config.Config) (*FaultDetector, error) {
faultDetectorConfig := config.FaultDetector

func NewFaultDetector(ctx context.Context, logger log.Logger, errorChan chan error, wg *sync.WaitGroup, faultDetectorConfig *config.FaultDetectorConfig) (*FaultDetector, error) {
// Initialize API Providers
l1RpcApi, err := chain.GetAPIClient(ctx, config.FaultDetector.L1RPCEndpoint, logger)
l1RpcApi, err := chain.GetAPIClient(ctx, faultDetectorConfig.L1RPCEndpoint, logger)
if err != nil {
return nil, err
}

l2RpcApi, err := chain.GetAPIClient(ctx, config.FaultDetector.L2RPCEndpoint, logger)
l2RpcApi, err := chain.GetAPIClient(ctx, faultDetectorConfig.L2RPCEndpoint, logger)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 6880f39

Please sign in to comment.