Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace types.Error with error interface (#122) #125

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 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 All @@ -64,11 +56,7 @@ func (s *Service) processBlocksSequentially(ctx context.Context) *types.Error {
for i := lastProcessedHeight + 1; i <= uint64(latestHeight); i++ {
select {
case <-ctx.Done():
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("context cancelled during block processing"),
)
return fmt.Errorf("context cancelled during block processing")
default:
events, err := s.getEventsFromBlock(ctx, int64(i))
if err != nil {
Expand All @@ -82,11 +70,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
2 changes: 1 addition & 1 deletion internal/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return value is used only in this case log.Error().Err(err).Msg("Error polling") which expects error anyway

return &Poller{
interval: interval,
quit: make(chan struct{}),
Expand Down
Loading