From 154344ceb2b723ac070d39dcf2e6fd1911d46f68 Mon Sep 17 00:00:00 2001 From: Kirill Date: Thu, 16 Jan 2025 11:32:05 +0400 Subject: [PATCH] Replace types.Error with error interface (#122) --- internal/services/bootstrap.go | 20 ++++---------------- internal/services/events.go | 4 ++-- internal/services/expiry_checker.go | 25 ++++++------------------- internal/services/global_params.go | 23 ++++++----------------- internal/utils/poller/poller.go | 5 ++--- 5 files changed, 20 insertions(+), 57 deletions(-) diff --git a/internal/services/bootstrap.go b/internal/services/bootstrap.go index ec4068f..21663f6 100644 --- a/internal/services/bootstrap.go +++ b/internal/services/bootstrap.go @@ -28,24 +28,16 @@ func (s *Service) StartBbnBlockProcessor(ctx context.Context) { // starting from the last processed height up to the latest chain height. // It extracts events from each block and forwards them to the event processor. // Returns an error if it fails to get block results or process events. -func (s *Service) processBlocksSequentially(ctx context.Context) *types.Error { +func (s *Service) processBlocksSequentially(ctx context.Context) error { lastProcessedHeight, dbErr := s.db.GetLastProcessedBbnHeight(ctx) if dbErr != nil { - return types.NewError( - http.StatusInternalServerError, - types.InternalServiceError, - fmt.Errorf("failed to get last processed height: %w", dbErr), - ) + return fmt.Errorf("failed to get last processed height: %w", dbErr) } for { select { case <-ctx.Done(): - return types.NewError( - http.StatusInternalServerError, - types.InternalServiceError, - fmt.Errorf("context cancelled during BBN block processor"), - ) + return fmt.Errorf("context cancelled during BBN block processor") case height := <-s.latestHeightChan: // Drain channel to get the most recent height @@ -82,11 +74,7 @@ func (s *Service) processBlocksSequentially(ctx context.Context) *types.Error { } if dbErr := s.db.UpdateLastProcessedBbnHeight(ctx, i); dbErr != nil { - return types.NewError( - http.StatusInternalServerError, - types.InternalServiceError, - fmt.Errorf("failed to update last processed height in database: %w", dbErr), - ) + return fmt.Errorf("failed to update last processed height in database: %w", dbErr) } lastProcessedHeight = i } diff --git a/internal/services/events.go b/internal/services/events.go index bdb9f1f..19767e3 100644 --- a/internal/services/events.go +++ b/internal/services/events.go @@ -48,12 +48,12 @@ func (s *Service) processEvent( ctx context.Context, event BbnEvent, blockHeight int64, -) *types.Error { +) error { // Note: We no longer need to check for the event category here. We can directly // process the event based on its type. bbnEvent := event.Event - var err *types.Error + var err error switch EventTypes(bbnEvent.Type) { case EventFinalityProviderCreatedType: diff --git a/internal/services/expiry_checker.go b/internal/services/expiry_checker.go index 7aff635..18759ad 100644 --- a/internal/services/expiry_checker.go +++ b/internal/services/expiry_checker.go @@ -3,7 +3,6 @@ package services import ( "context" "fmt" - "net/http" "github.com/babylonlabs-io/babylon-staking-indexer/internal/db" "github.com/babylonlabs-io/babylon-staking-indexer/internal/types" "github.com/babylonlabs-io/babylon-staking-indexer/internal/utils/poller" @@ -18,29 +17,21 @@ func (s *Service) StartExpiryChecker(ctx context.Context) { go expiryCheckerPoller.Start(ctx) } -func (s *Service) checkExpiry(ctx context.Context) *types.Error { +func (s *Service) checkExpiry(ctx context.Context) error { btcTip, err := s.btc.GetTipHeight() if err != nil { - return types.NewInternalServiceError( - fmt.Errorf("failed to get BTC tip height: %w", err), - ) + return fmt.Errorf("failed to get BTC tip height: %w", err) } expiredDelegations, err := s.db.FindExpiredDelegations(ctx, btcTip, s.cfg.Poller.ExpiredDelegationsLimit) if err != nil { - return types.NewInternalServiceError( - fmt.Errorf("failed to find expired delegations: %w", err), - ) + return fmt.Errorf("failed to find expired delegations: %w", err) } for _, tlDoc := range expiredDelegations { delegation, err := s.db.GetBTCDelegationByStakingTxHash(ctx, tlDoc.StakingTxHashHex) if err != nil { - return types.NewError( - http.StatusInternalServerError, - types.InternalServiceError, - fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", err), - ) + return fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", err) } log.Debug(). @@ -67,9 +58,7 @@ func (s *Service) checkExpiry(ctx context.Context) *types.Error { log.Error(). Str("staking_tx", delegation.StakingTxHashHex). Msg("failed to update BTC delegation state to withdrawable") - return types.NewInternalServiceError( - fmt.Errorf("failed to update BTC delegation state to withdrawable: %w", err), - ) + return fmt.Errorf("failed to update BTC delegation state to withdrawable: %w", err) } } else { // This means the state transitioned to withdrawable so we need to emit the event @@ -82,9 +71,7 @@ func (s *Service) checkExpiry(ctx context.Context) *types.Error { log.Error(). Str("staking_tx", delegation.StakingTxHashHex). Msg("failed to delete expired delegation") - return types.NewInternalServiceError( - fmt.Errorf("failed to delete expired delegation: %w", err), - ) + return fmt.Errorf("failed to delete expired delegation: %w", err) } } diff --git a/internal/services/global_params.go b/internal/services/global_params.go index 717c7d6..be1a23a 100644 --- a/internal/services/global_params.go +++ b/internal/services/global_params.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/babylonlabs-io/babylon-staking-indexer/internal/types" "github.com/babylonlabs-io/babylon-staking-indexer/internal/utils/poller" ) @@ -16,38 +15,28 @@ func (s *Service) SyncGlobalParams(ctx context.Context) { go paramsPoller.Start(ctx) } -func (s *Service) fetchAndSaveParams(ctx context.Context) *types.Error { +func (s *Service) fetchAndSaveParams(ctx context.Context) error { checkpointParams, err := s.bbn.GetCheckpointParams(ctx) if err != nil { // TODO: Add metrics and replace internal service error with a more specific // error code so that the poller can catch and emit the error metrics - return types.NewInternalServiceError( - fmt.Errorf("failed to get checkpoint params: %w", err), - ) + return fmt.Errorf("failed to get checkpoint params: %w", err) } if err := s.db.SaveCheckpointParams(ctx, checkpointParams); err != nil { - return types.NewInternalServiceError( - fmt.Errorf("failed to save checkpoint params: %w", err), - ) + return fmt.Errorf("failed to save checkpoint params: %w", err) } allStakingParams, err := s.bbn.GetAllStakingParams(ctx) if err != nil { - return types.NewInternalServiceError( - fmt.Errorf("failed to get staking params: %w", err), - ) + return fmt.Errorf("failed to get staking params: %w", err) } for version, params := range allStakingParams { if params == nil { - return types.NewInternalServiceError( - fmt.Errorf("nil staking params for version %d", version), - ) + return fmt.Errorf("nil staking params for version %d", version) } if err := s.db.SaveStakingParams(ctx, version, params); err != nil { - return types.NewInternalServiceError( - fmt.Errorf("failed to save staking params: %w", err), - ) + return fmt.Errorf("failed to save staking params: %w", err) } } diff --git a/internal/utils/poller/poller.go b/internal/utils/poller/poller.go index f17aee6..fbc8ffc 100644 --- a/internal/utils/poller/poller.go +++ b/internal/utils/poller/poller.go @@ -4,17 +4,16 @@ import ( "context" "time" - "github.com/babylonlabs-io/babylon-staking-indexer/internal/types" "github.com/rs/zerolog/log" ) type Poller struct { interval time.Duration quit chan struct{} - pollMethod func(ctx context.Context) *types.Error + pollMethod func(ctx context.Context) error } -func NewPoller(interval time.Duration, pollMethod func(ctx context.Context) *types.Error) *Poller { +func NewPoller(interval time.Duration, pollMethod func(ctx context.Context) error) *Poller { return &Poller{ interval: interval, quit: make(chan struct{}),