Skip to content

Commit

Permalink
🐛 Fix identified issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Feb 23, 2024
1 parent 9bc8ccd commit c33e7ce
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
23 changes: 12 additions & 11 deletions cmd/faultdetector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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
}

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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*$`)
Expand Down
24 changes: 12 additions & 12 deletions pkg/faultdetector/faultdetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/faultdetector/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/notification/channel/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit c33e7ce

Please sign in to comment.