Skip to content

Commit

Permalink
♻️ Implement initialization of fault detector
Browse files Browse the repository at this point in the history
♻️ Update config and improve main.go

♻️ Multiple improvements based on feedback
- Improve usage of logging
- Fix incorrect go routine usage
- Improve app struct
- Install latest version of optimism for bindings

♻️ Apply feedback
- Add error to logs within the fault detector service
- Declare compiled regular expressions as var
- Fix the usage of quit channel for ticker
- Use uint64 for faultProofWindow and currentOutputIndex

♻️ Apply feedbacks
- Make regex vars private
- Declare quitTickerChan as struct

♻️ Create app struct and implement start/stop
  • Loading branch information
ishantiw committed Jan 26, 2024
1 parent e35a0d0 commit 9053da5
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 129 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.1.0",
"configurations": [
{
"name": "Run Fault Detector",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceRoot}/cmd",
"args": ["--config", "./config.yaml"],
"showLog": true
}
]
}
105 changes: 79 additions & 26 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,124 @@ import (

"github.com/LiskHQ/op-fault-detector/pkg/api"
"github.com/LiskHQ/op-fault-detector/pkg/config"
"github.com/LiskHQ/op-fault-detector/pkg/faultdetector"
"github.com/LiskHQ/op-fault-detector/pkg/log"
"github.com/spf13/viper"
)

var apiServer *api.HTTPServer
// App encapsulates start and stop logic for the whole application.
type App struct {
ctx context.Context
logger log.Logger
errChan chan error
config *config.Config
wg *sync.WaitGroup
apiServer *api.HTTPServer
faultDetector *faultdetector.FaultDetector
}

func main() {
// NewApp returns [App] with all the initialized services and variables.
func NewApp(logger log.Logger) (*App, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

logger, err := log.NewDefaultProductionLogger()
if err != nil {
panic(err)
}

configFilepath := flag.String("config", "./config.yaml", "Path to the config file")
flag.Parse()
config, err := getAppConfig(logger, *configFilepath)
if err != nil {
panic(err)
logger.Errorf("Failed at parsing config with error %w", err)
return nil, err
}

wg := sync.WaitGroup{}
errorChan := make(chan error, 1)

// Start Fault Detector
faultDetector, err := faultdetector.NewFaultDetector(
ctx,
logger,
errorChan,
&wg,
config.FaultDetectorConfig,
)
if err != nil {
logger.Errorf("Failed to create fault detector service.")
return nil, err
}

// Start API Server
apiServer := api.NewHTTPServer(ctx, logger, &wg, config, errorChan)

return &App{
ctx: ctx,
logger: logger,
errChan: errorChan,
config: config,
wg: &wg,
apiServer: apiServer,
faultDetector: faultDetector,
}, nil
}

// This will start the application by starting API Server and Fault Detector services.
func (app *App) Start() {
doneChan := make(chan struct{})
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

// Start Fault Detector
app.wg.Add(1)
go app.faultDetector.Start()

// Start API Server
serverChan := make(chan error, 1)
apiServer = api.NewHTTPServer(ctx, logger, &wg, config, serverChan)
wg.Add(1)
go apiServer.Start()
app.wg.Add(1)
go app.apiServer.Start()

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

for {
select {
case <-doneChan:
performCleanup(logger)
app.stop()
return

case <-signalChan:
performCleanup(logger)
app.stop()
return

case err := <-serverChan:
logger.Errorf("Received error of %v", err)
case err := <-app.errChan:
app.logger.Errorf("Received error of %v", err)
return
}
}
}

func (app *App) stop() {
app.faultDetector.Stop()
err := app.apiServer.Stop()
if err != nil {
app.logger.Error("Server shutdown not successful: %w", err)
}
}

func main() {
logger, err := log.NewDefaultProductionLogger()
if err != nil {
logger.Errorf("Failed to create logger, %w", err)
return
}

app, err := NewApp(logger)
if err != nil {
logger.Errorf("Failed to create app, %w", err)
return
}

logger.Infof("Starting app...")
app.Start()
}

// getAppConfig is the function that takes in the absolute path to the config file, parses the content and returns it.
func getAppConfig(logger log.Logger, configFilepath string) (*config.Config, error) {
configDir := path.Dir(configFilepath)
Expand Down Expand Up @@ -104,10 +164,3 @@ func getAppConfig(logger log.Logger, configFilepath string) (*config.Config, err

return &config, nil
}

func performCleanup(logger log.Logger) {
err := apiServer.Stop()
if err != nil {
logger.Error("Server shutdown not successful: %w", err)
}
}
4 changes: 4 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ api:

# Faultdetector configurations
fault_detector:
l1_rpc_endpoint: "https://rpc.notadegen.com/eth"
l2_rpc_endpoint: "https://mainnet.optimism.io/"
start_batch_index: 0
l2_output_oracle_contract_address: "0x0000000000000000000000000000000000000000"
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/LiskHQ/op-fault-detector
go 1.21.5

require (
github.com/ethereum-optimism/optimism/op-bindings v0.10.14
github.com/ethereum-optimism/optimism v1.4.2
github.com/ethereum/go-ethereum v1.13.10
github.com/gin-gonic/gin v1.9.1
github.com/magiconair/properties v1.8.7
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
go.uber.org/multierr v1.10.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.26.0
)

Expand All @@ -25,7 +25,8 @@ require (
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20231211205419-ff2e152c624f // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
Expand All @@ -35,7 +36,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
Expand Down
Loading

0 comments on commit 9053da5

Please sign in to comment.