From c33e7cefb32db22ae510bef7bca522320639defe Mon Sep 17 00:00:00 2001
From: nagdahimanshu <himanshu.nagda@lightcurve.io>
Date: Fri, 23 Feb 2024 15:10:20 +0100
Subject: [PATCH] :bug: Fix identified issues

---
 cmd/faultdetector/main.go               | 23 ++++++++++++-----------
 config.yaml                             |  6 +++---
 pkg/config/config.go                    |  2 +-
 pkg/faultdetector/faultdetector.go      | 24 ++++++++++++------------
 pkg/faultdetector/helper.go             |  6 +++---
 pkg/utils/notification/channel/slack.go |  2 +-
 6 files changed, 32 insertions(+), 31 deletions(-)

diff --git a/cmd/faultdetector/main.go b/cmd/faultdetector/main.go
index f06478f..2c72858 100644
--- a/cmd/faultdetector/main.go
+++ b/cmd/faultdetector/main.go
@@ -50,7 +50,7 @@ func NewApp(ctx context.Context, logger log.Logger) (*App, error) {
 	flag.Parse()
 	config, err := getAppConfig(logger, *configFilepath)
 	if err != nil {
-		logger.Errorf("Failed at parsing config with error %w", err)
+		logger.Errorf("Failed at parsing config with error %v", err)
 		return nil, err
 	}
 
@@ -66,7 +66,7 @@ func NewApp(ctx context.Context, logger log.Logger) (*App, error) {
 			config.Notification,
 		)
 		if err != nil {
-			logger.Errorf("Failed to initialize notification service, error: %w", err)
+			logger.Errorf("Failed to initialize notification service, error: %v", err)
 			return nil, err
 		}
 	}
@@ -136,7 +136,7 @@ func (app *App) Start() {
 			app.logger.Errorf("Received error of %v", err)
 			if app.notification != nil {
 				if err := app.notification.Notify("Error while starting application"); err != nil {
-					app.logger.Errorf("Failed to send notification, error: %w", err)
+					app.logger.Errorf("Failed to send notification, error: %v", err)
 				}
 			}
 			return
@@ -148,7 +148,7 @@ func (app *App) stop() {
 	app.faultDetector.Stop()
 	err := app.apiServer.Stop()
 	if err != nil {
-		app.logger.Error("Server shutdown not successful: %w", err)
+		app.logger.Error("Server shutdown not successful: %v", err)
 	}
 }
 
@@ -158,13 +158,13 @@ func main() {
 
 	logger, err := log.NewDefaultProductionLogger()
 	if err != nil {
-		logger.Errorf("Failed to create logger, %w", err)
+		logger.Errorf("Failed to create logger, %v", err)
 		return
 	}
 
 	app, err := NewApp(ctx, logger)
 	if err != nil {
-		logger.Errorf("Failed to create app, %w", err)
+		logger.Errorf("Failed to create app, %v", err)
 		return
 	}
 
@@ -178,14 +178,15 @@ func getAppConfig(logger log.Logger, configFilepath string) (*config.Config, err
 	configFilenameWithExt := path.Base(configFilepath)
 
 	splits := strings.FieldsFunc(configFilenameWithExt, func(r rune) bool { return r == '.' })
-	configType := splits[len(splits)-1] // Config file extension
+	configType := splits[len(splits)-1]               // Config file extension
+	configFilenameWithoutExt := splits[len(splits)-1] // Config file name without extension
 
+	viper.SetConfigName(configFilenameWithoutExt)
+	viper.SetConfigType(configType)
+	viper.AddConfigPath(configDir)
+	viper.AddConfigPath("$HOME/.op-fault-detector")
 	viper.AddConfigPath(".")
 	viper.AddConfigPath("..")
-	viper.AddConfigPath("$HOME/.op-fault-detector")
-	viper.AddConfigPath(configDir)
-	viper.SetConfigName(configFilenameWithExt)
-	viper.SetConfigType(configType)
 	err := viper.ReadInConfig()
 	if err != nil {
 		return nil, fmt.Errorf("failed to load the config from disk: %w", err)
diff --git a/config.yaml b/config.yaml
index a5812a6..f2c6595 100644
--- a/config.yaml
+++ b/config.yaml
@@ -16,11 +16,11 @@ fault_detector:
   l1_rpc_endpoint: "https://rpc.notadegen.com/eth"
   l2_rpc_endpoint: "https://mainnet.optimism.io/"
   start_batch_index: -1
-  l2_output_oracle_contract_address: "0x0000000000000000000000000000000000000000"
+  l2_output_oracle_contract_address: "0xA0E35F56C318DE1bD5D9ca6A94Fe7e37C5663348"
 
 
 # Notification service related configurations
 notification:
-  enable: false
+  enable: true
   slack:
-    channel_id: ""
+    channel_id: "test"
diff --git a/pkg/config/config.go b/pkg/config/config.go
index d284670..c321d7f 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -14,7 +14,7 @@ import (
 
 var (
 	providerEndpointRegex = regexp.MustCompile(`^(http|https|ws|wss)://`)
-	addressRegex          = regexp.MustCompile(`(\b0x[a-f0-9]{40}\b)`)
+	addressRegex          = regexp.MustCompile(`(\b0x[A-Fa-f0-9]{40}\b)`)
 	hostRegex             = regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`)
 	basePathRegex         = regexp.MustCompile(`^/?api$`)
 	registerVersionRegex  = regexp.MustCompile(`^v[1-9]\d*$`)
diff --git a/pkg/faultdetector/faultdetector.go b/pkg/faultdetector/faultdetector.go
index c1866b5..e5f92ab 100644
--- a/pkg/faultdetector/faultdetector.go
+++ b/pkg/faultdetector/faultdetector.go
@@ -74,19 +74,19 @@ func NewFaultDetector(ctx context.Context, logger log.Logger, errorChan chan err
 	// Initialize API Providers
 	l1RpcApi, err := chain.GetAPIClient(ctx, faultDetectorConfig.L1RPCEndpoint, logger)
 	if err != nil {
-		logger.Errorf("Failed to create API client for L1 Provider with given endpoint: %s, error: %w", faultDetectorConfig.L1RPCEndpoint, err)
+		logger.Errorf("Failed to create API client for L1 Provider with given endpoint: %s, error: %v", faultDetectorConfig.L1RPCEndpoint, err)
 		return nil, err
 	}
 
 	l2RpcApi, err := chain.GetAPIClient(ctx, faultDetectorConfig.L2RPCEndpoint, logger)
 	if err != nil {
-		logger.Errorf("Failed to create API client for L2 Provider with given endpoint: %s, error: %w", faultDetectorConfig.L2RPCEndpoint, err)
+		logger.Errorf("Failed to create API client for L2 Provider with given endpoint: %s, error: %v", faultDetectorConfig.L2RPCEndpoint, err)
 		return nil, err
 	}
 
 	l2ChainID, err := l2RpcApi.GetChainID(ctx)
 	if err != nil {
-		logger.Errorf("Failed to get L2 provider's chainID: %d, error: %w", encoding.MustConvertBigIntToUint64(l2ChainID), err)
+		logger.Errorf("Failed to get L2 provider's chainID: %d, error: %v", encoding.MustConvertBigIntToUint64(l2ChainID), err)
 		return nil, err
 	}
 
@@ -99,13 +99,13 @@ func NewFaultDetector(ctx context.Context, logger log.Logger, errorChan chan err
 
 	oracleContractAccessor, err := chain.NewOracleAccessor(ctx, chainConfig)
 	if err != nil {
-		logger.Errorf("Failed to create Oracle contract accessor with chainID: %d, L1 endpoint: %s and L2OutputOracleContractAddress: %s, error: %w", encoding.MustConvertBigIntToUint64(l2ChainID), faultDetectorConfig.L1RPCEndpoint, faultDetectorConfig.L2OutputOracleContractAddress, err)
+		logger.Errorf("Failed to create Oracle contract accessor with chainID: %d, L1 endpoint: %s and L2OutputOracleContractAddress: %s, error: %v", encoding.MustConvertBigIntToUint64(l2ChainID), faultDetectorConfig.L1RPCEndpoint, faultDetectorConfig.L2OutputOracleContractAddress, err)
 		return nil, err
 	}
 
 	finalizedPeriodSeconds, err := oracleContractAccessor.FinalizationPeriodSeconds()
 	if err != nil {
-		logger.Errorf("Failed to query `FinalizationPeriodSeconds` from Oracle contract accessor, error: %w", err)
+		logger.Errorf("Failed to query `FinalizationPeriodSeconds` from Oracle contract accessor, error: %v", err)
 		return nil, err
 	}
 
@@ -125,7 +125,7 @@ func NewFaultDetector(ctx context.Context, logger log.Logger, errorChan chan err
 			logger.Infof("No unfinalized batches found. skipping all batches.")
 			nextOutputIndex, err := oracleContractAccessor.GetNextOutputIndex()
 			if err != nil {
-				logger.Errorf("Failed to query next output index, error: %w", err)
+				logger.Errorf("Failed to query next output index, error: %v", err)
 				return nil, err
 			}
 			currentOutputIndex = encoding.MustConvertBigIntToUint64(nextOutputIndex) - 1
@@ -192,7 +192,7 @@ func (fd *FaultDetector) checkFault() error {
 
 	nextOutputIndex, err := fd.oracleContractAccessor.GetNextOutputIndex()
 	if err != nil {
-		fd.logger.Errorf("Failed to query next output index, error: %w.", err)
+		fd.logger.Errorf("Failed to query next output index, error: %v.", err)
 		fd.metrics.apiConnectionFailure.Inc()
 		return err
 	}
@@ -206,14 +206,14 @@ func (fd *FaultDetector) checkFault() error {
 
 	l2OutputData, err := fd.oracleContractAccessor.GetL2Output(encoding.MustConvertUint64ToBigInt(fd.currentOutputIndex))
 	if err != nil {
-		fd.logger.Errorf("Failed to fetch output associated with index: %d, error: %w.", fd.currentOutputIndex, err)
+		fd.logger.Errorf("Failed to fetch output associated with index: %d, error: %v.", fd.currentOutputIndex, err)
 		fd.metrics.apiConnectionFailure.Inc()
 		return err
 	}
 
 	latestBlockNumber, err := fd.l2RpcApi.GetLatestBlockNumber(fd.ctx)
 	if err != nil {
-		fd.logger.Errorf("Failed to query L2 latest block number: %d, error: %w", latestBlockNumber, err)
+		fd.logger.Errorf("Failed to query L2 latest block number: %d, error: %v", latestBlockNumber, err)
 		fd.metrics.apiConnectionFailure.Inc()
 		return err
 	}
@@ -227,14 +227,14 @@ func (fd *FaultDetector) checkFault() error {
 
 	outputBlockHeader, err := fd.l2RpcApi.GetBlockHeaderByNumber(fd.ctx, encoding.MustConvertUint64ToBigInt(l2OutputBlockNumber))
 	if err != nil {
-		fd.logger.Errorf("Failed to fetch block header by number: %d, error: %w.", l2OutputBlockNumber, err)
+		fd.logger.Errorf("Failed to fetch block header by number: %d, error: %v.", l2OutputBlockNumber, err)
 		fd.metrics.apiConnectionFailure.Inc()
 		return err
 	}
 
 	messagePasserProofResponse, err := fd.l2RpcApi.GetProof(fd.ctx, encoding.MustConvertUint64ToBigInt(l2OutputBlockNumber), common.HexToAddress(chain.L2BedrockMessagePasserAddress))
 	if err != nil {
-		fd.logger.Errorf("Failed to fetch message passer proof for the block with height: %d and address: %s, error: %w.", l2OutputBlockNumber, chain.L2BedrockMessagePasserAddress, err)
+		fd.logger.Errorf("Failed to fetch message passer proof for the block with height: %d and address: %s, error: %v.", l2OutputBlockNumber, chain.L2BedrockMessagePasserAddress, err)
 		fd.metrics.apiConnectionFailure.Inc()
 		return err
 	}
@@ -252,7 +252,7 @@ func (fd *FaultDetector) checkFault() error {
 		if fd.notification != nil {
 			notificationMessage := fmt.Sprintf("*Fault detected*, state root does not match:\noutputIndex: %d\nExpectedStateRoot: %s\nCalculatedStateRoot: %s\nFinalizationTime: %s", fd.currentOutputIndex, expectedOutputRoot, calculatedOutputRoot, finalizationTime)
 			if err := fd.notification.Notify(notificationMessage); err != nil {
-				fd.logger.Errorf("Error while sending notification, %w", err)
+				fd.logger.Errorf("Error while sending notification, %v", err)
 			}
 		}
 
diff --git a/pkg/faultdetector/helper.go b/pkg/faultdetector/helper.go
index 2574b8e..8a588b1 100644
--- a/pkg/faultdetector/helper.go
+++ b/pkg/faultdetector/helper.go
@@ -25,12 +25,12 @@ type OracleAccessor interface {
 func FindFirstUnfinalizedOutputIndex(ctx context.Context, logger log.Logger, fpw uint64, oracleAccessor OracleAccessor, l2RpcApi ChainAPIClient) (uint64, error) {
 	latestBlockHeader, err := l2RpcApi.GetLatestBlockHeader(ctx)
 	if err != nil {
-		logger.Errorf("Failed to get latest block header from L2 provider, error: %w", err)
+		logger.Errorf("Failed to get latest block header from L2 provider, error: %v", err)
 		return 0, err
 	}
 	totalOutputsBigInt, err := oracleAccessor.GetNextOutputIndex()
 	if err != nil {
-		logger.Errorf("Failed to get next output index, error: %w", err)
+		logger.Errorf("Failed to get next output index, error: %v", err)
 		return 0, err
 	}
 	totalOutputs := encoding.MustConvertBigIntToUint64(totalOutputsBigInt)
@@ -43,7 +43,7 @@ func FindFirstUnfinalizedOutputIndex(ctx context.Context, logger log.Logger, fpw
 		midBigInt := encoding.MustConvertUint64ToBigInt(mid)
 		outputData, err := oracleAccessor.GetL2Output(midBigInt)
 		if err != nil {
-			logger.Errorf("Failed to get L2 output for index: %d, error: %w", midBigInt, err)
+			logger.Errorf("Failed to get L2 output for index: %d, error: %v", midBigInt, err)
 			return 0, err
 		}
 		if outputData.L1Timestamp+fpw < latestBlockHeader.Time {
diff --git a/pkg/utils/notification/channel/slack.go b/pkg/utils/notification/channel/slack.go
index 47ef0be..7f4a446 100644
--- a/pkg/utils/notification/channel/slack.go
+++ b/pkg/utils/notification/channel/slack.go
@@ -50,7 +50,7 @@ func (s *Slack) Notify(msg string) error {
 		slack.MsgOptionText(msg, false),
 	)
 	if err != nil {
-		s.logger.Errorf("Failed to send notification to the channel %s, error: %w", s.channelID, err)
+		s.logger.Errorf("Failed to send notification to the channel %s, error: %v", s.channelID, err)
 		return err
 	}