Skip to content

Commit

Permalink
Replace types.Error with error interface (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan committed Jan 16, 2025
1 parent d0d1caf commit 154344c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 57 deletions.
20 changes: 4 additions & 16 deletions internal/services/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 6 additions & 19 deletions internal/services/expiry_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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().
Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down
23 changes: 6 additions & 17 deletions internal/services/global_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

Expand Down
5 changes: 2 additions & 3 deletions internal/utils/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
Expand Down

0 comments on commit 154344c

Please sign in to comment.