Skip to content

Commit

Permalink
fix errs
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Jan 12, 2025
1 parent e6c01e7 commit 240e45f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
27 changes: 8 additions & 19 deletions internal/services/pollers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"
"fmt"

"github.com/babylonlabs-io/staking-expiry-checker/internal/db"
"github.com/babylonlabs-io/staking-expiry-checker/internal/types"
Expand All @@ -25,8 +26,7 @@ func (s *Service) processBTCSubscriber(ctx context.Context) error {
pageToken,
)
if err != nil {
log.Error().Err(err).Msg("Failed to get delegations for BTC subscription")
return err
return fmt.Errorf("error getting BTC delegations by states: %w", err)
}

totalProcessed += len(result.Data)
Expand All @@ -47,7 +47,7 @@ func (s *Service) processBTCSubscriber(ctx context.Context) error {
Err(err).
Str("stakingTxHash", delegation.StakingTxHashHex).
Msg("Failed to register staking spend notification")
return err
return fmt.Errorf("failed to register staking spend notification: %w", err)
}

s.trackedSubs.AddSubscription(delegation.StakingTxHashHex)
Expand Down Expand Up @@ -75,40 +75,29 @@ func (s *Service) processBTCSubscriber(ctx context.Context) error {
func (s *Service) processExpiredDelegations(ctx context.Context) error {
btcTip, err := s.btc.GetBlockCount()
if err != nil {
log.Error().Err(err).Msg("Error getting BTC tip height")
return err
return fmt.Errorf("error getting BTC tip height: %w", err)
}

// Process a single batch of expired delegations without pagination.
// Since we delete each delegation after processing it, pagination is not needed.
expiredDelegations, err := s.db.FindExpiredDelegations(ctx, uint64(btcTip))
if err != nil {
log.Error().Err(err).Msg("Error finding expired delegations")
return err
return fmt.Errorf("error finding expired delegations: %w", err)
}

// Process each delegation in the batch
for _, delegation := range expiredDelegations {
txType, err := types.StakingTxTypeFromString(delegation.TxType)
if err != nil {
log.Error().
Err(err).
Str("txType", delegation.TxType).
Msg("Invalid timelock type")
return err
return fmt.Errorf("invalid timelock type: %w", err)
}

if err := s.TransitionToUnbondedState(ctx, txType, delegation.StakingTxHashHex); err != nil {
log.Error().
Err(err).
Str("stakingTxHashHex", delegation.StakingTxHashHex).
Msg("Error transitioning delegation to unbonded")
return err
return fmt.Errorf("error transitioning delegation to unbonded: %w", err)
}

if err := s.db.DeleteExpiredDelegation(ctx, delegation.ID); err != nil {
log.Error().Err(err).Msg("Error deleting expired delegation")
return err
return fmt.Errorf("error deleting expired delegation: %w", err)
}
}

Expand Down
14 changes: 10 additions & 4 deletions internal/services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ func (s *Service) startExpiryPoller(ctx context.Context) {
start := time.Now()
err := s.processExpiredDelegations(pollingCtx)
if err != nil {
log.Error().Err(err).Msg("Error processing expired delegations")
log.Error().
Err(err).
Msg("Error processing expired delegations")
}
duration := time.Since(start)
metrics.ObservePollerDuration("expiry_poller", duration, err)
cancel()
case <-ctx.Done():
log.Info().Msg("Expiry poller stopped due to context cancellation")
log.Info().
Msg("Expiry poller stopped due to context cancellation")
return
case <-s.quit:
return
Expand All @@ -134,13 +137,16 @@ func (s *Service) startBTCSubscriberPoller(ctx context.Context) {
start := time.Now()
err := s.processBTCSubscriber(pollingCtx)
if err != nil {
log.Error().Err(err).Msg("Error processing BTC subscriptions")
log.Error().
Err(err).
Msg("Error processing BTC subscriptions")
}
duration := time.Since(start)
metrics.ObservePollerDuration("btc_subscriber_poller", duration, err)
cancel()
case <-ctx.Done():
log.Info().Msg("BTC subscriber poller stopped due to context cancellation")
log.Info().
Msg("BTC subscriber poller stopped due to context cancellation")
return
case <-s.quit:
return
Expand Down
9 changes: 4 additions & 5 deletions internal/services/watch_btc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ func (s *Service) registerStakingSpendNotification(
return fmt.Errorf("failed to deserialize staking tx: %w", err)
}

log.Debug().
Str("stakingTxHash", stakingTxHashHex).
Msg("registering staking spend notification")

stakingOutpoint := wire.OutPoint{
Hash: *stakingTxHash,
Index: stakingOutputIdx,
Expand Down Expand Up @@ -570,11 +574,6 @@ func (s *Service) registerUnbondingSpendNotification(
return fmt.Errorf("failed to register spend ntfn for unbonding tx %s: %w", stakingTxHashHex, btcErr)
}

log.Debug().
Str("staking_tx", stakingTxHashHex).
Str("unbonding_tx", unbondingTx.TxHash().String()).
Msg("registered unbonding spend notification")

s.wg.Add(1)
go s.watchForSpendUnbondingTx(spendEv, stakingTxHashHex)

Expand Down

0 comments on commit 240e45f

Please sign in to comment.