From 0bc195b6c4201436a30100da8cb82728e44ff736 Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Tue, 15 Oct 2024 15:58:47 +1100 Subject: [PATCH] resolve comments --- internal/clients/bbnclient/bbnclient.go | 24 +++++++++++++++++++----- internal/clients/bbnclient/interface.go | 2 +- internal/services/global-params.go | 5 +++++ internal/services/service.go | 8 ++++---- internal/types/error.go | 1 + 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/internal/clients/bbnclient/bbnclient.go b/internal/clients/bbnclient/bbnclient.go index 673b6e1..5922adc 100644 --- a/internal/clients/bbnclient/bbnclient.go +++ b/internal/clients/bbnclient/bbnclient.go @@ -55,15 +55,22 @@ func (c *BbnClient) GetCheckpointParams(ctx context.Context) (*CheckpointParams, if err != nil { return nil, types.NewErrorWithMsg( http.StatusInternalServerError, - types.InternalServiceError, + types.ClientRequestError, fmt.Sprintf("failed to get checkpoint params: %s", err.Error()), ) } + if err := params.Params.Validate(); err != nil { + return nil, types.NewErrorWithMsg( + http.StatusInternalServerError, + types.ValidationError, + fmt.Sprintf("failed to validate checkpoint params: %s", err.Error()), + ) + } return ¶ms.Params, nil } -func (c *BbnClient) GetAllStakingParams(ctx context.Context) (map[uint32]StakingParams, *types.Error) { - allParams := make(map[uint32]StakingParams) // Map to store versioned staking parameters +func (c *BbnClient) GetAllStakingParams(ctx context.Context) (map[uint32]*StakingParams, *types.Error) { + allParams := make(map[uint32]*StakingParams) // Map to store versioned staking parameters version := uint32(0) for { @@ -75,11 +82,18 @@ func (c *BbnClient) GetAllStakingParams(ctx context.Context) (map[uint32]Staking } return nil, types.NewErrorWithMsg( http.StatusInternalServerError, - types.InternalServiceError, + types.ClientRequestError, fmt.Sprintf("failed to get staking params for version %d: %s", version, err.Error()), ) } - allParams[version] = *FromBbnStakingParams(params.Params) + if err := params.Params.Validate(); err != nil { + return nil, types.NewErrorWithMsg( + http.StatusInternalServerError, + types.ValidationError, + fmt.Sprintf("failed to validate staking params for version %d: %s", version, err.Error()), + ) + } + allParams[version] = FromBbnStakingParams(params.Params) version++ } if len(allParams) == 0 { diff --git a/internal/clients/bbnclient/interface.go b/internal/clients/bbnclient/interface.go index 2a7702a..34c0de1 100644 --- a/internal/clients/bbnclient/interface.go +++ b/internal/clients/bbnclient/interface.go @@ -9,7 +9,7 @@ import ( type BbnInterface interface { GetCheckpointParams(ctx context.Context) (*CheckpointParams, *types.Error) - GetAllStakingParams(ctx context.Context) (map[uint32]StakingParams, *types.Error) + GetAllStakingParams(ctx context.Context) (map[uint32]*StakingParams, *types.Error) GetLatestBlockNumber(ctx context.Context) (int64, *types.Error) GetBlockResults( ctx context.Context, blockHeight int64, diff --git a/internal/services/global-params.go b/internal/services/global-params.go index 52e3102..ee5fb52 100644 --- a/internal/services/global-params.go +++ b/internal/services/global-params.go @@ -53,6 +53,11 @@ func (s *Service) fetchAndSaveParams(ctx context.Context) *types.Error { ) } for version, params := range allStakingParams { + if params == nil { + return types.NewInternalServiceError( + fmt.Errorf("nil staking params for version %d", version), + ) + } if err := s.db.SaveGlobalParams(ctx, &model.GolablParamDocument{ Type: STAKING_PARAMS_TYPE, Version: version, diff --git a/internal/services/service.go b/internal/services/service.go index eaa6c2d..ac26459 100644 --- a/internal/services/service.go +++ b/internal/services/service.go @@ -41,9 +41,9 @@ func (s *Service) StartIndexerSync(ctx context.Context) { // Sync global parameters s.SyncGlobalParams(ctx) // Start the bootstrap process - // s.BootstrapBbn(ctx) - // // Start the websocket event subscription process - // s.SubscribeToBbnEvents(ctx) - // // Keep processing events in the main thread + s.BootstrapBbn(ctx) + // Start the websocket event subscription process + s.SubscribeToBbnEvents(ctx) + // Keep processing events in the main thread s.StartBbnEventProcessor(ctx) } diff --git a/internal/types/error.go b/internal/types/error.go index be4b085..9322f8a 100644 --- a/internal/types/error.go +++ b/internal/types/error.go @@ -20,6 +20,7 @@ const ( Forbidden ErrorCode = "FORBIDDEN" UnprocessableEntity ErrorCode = "UNPROCESSABLE_ENTITY" RequestTimeout ErrorCode = "REQUEST_TIMEOUT" + ClientRequestError ErrorCode = "CLIENT_REQUEST_ERROR" ) // ApiError represents an error with an HTTP status code and an application-specific error code.