Skip to content

Commit

Permalink
fix: avoid infinite expiry checking loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Jan 7, 2025
1 parent 293768a commit 40faacd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
34 changes: 16 additions & 18 deletions internal/services/expiry_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"strconv"

"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"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils/poller"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -52,28 +52,26 @@ func (s *Service) checkExpiry(ctx context.Context) *types.Error {
Str("expire_height", strconv.FormatUint(uint64(tlDoc.ExpireHeight), 10)).
Msg("checking if delegation is expired")

// Check if the delegation is in a qualified state to transition to Withdrawable
if !utils.Contains(types.QualifiedStatesForWithdrawable(), delegation.State) {
log.Debug().
Str("staking_tx", delegation.StakingTxHashHex).
Str("current_state", delegation.State.String()).
Msg("current state is not qualified for withdrawable")
continue
}

if err := s.db.UpdateBTCDelegationState(
stateUpdateErr := s.db.UpdateBTCDelegationState(
ctx,
delegation.StakingTxHashHex,
types.QualifiedStatesForWithdrawable(),
types.StateWithdrawable,
&tlDoc.DelegationSubState,
); err != nil {
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),
)
)
if stateUpdateErr != nil {
if db.IsNotFoundError(stateUpdateErr) {
log.Debug().
Str("staking_tx", delegation.StakingTxHashHex).
Msg("Skip updating BTC delegation state to withdrawable as the it's outdated")
} else {
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),
)
}
}

if err := s.db.DeleteExpiredDelegation(ctx, delegation.StakingTxHashHex); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/services/watch_btc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ func (s *Service) startWatchingSlashingChange(
}
slashingChangeTimelockExpireHeight := spendingHeight + stakingParams.UnbondingTimeBlocks

// Save timelock expire to mark it as Withdrawn (sub state - timelock_slashing/early_unbonding_slashing)
// Save timelock expire to mark it as Withdrawable when timelock expires
// (sub state - timelock_slashing/early_unbonding_slashing)
if err := s.db.SaveNewTimeLockExpire(
ctx,
delegation.StakingTxHashHex,
Expand Down
4 changes: 3 additions & 1 deletion internal/types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ func QualifiedStatesForWithdrawn() []DelegationState {
}

// QualifiedStatesForWithdrawable returns the qualified current states for Withdrawable event
// The "StateWithdrawable" is included b/c sub state can be changed to if
// user did not withdraw ontime. e.g TIMELOCK change to TIMELOCK_SLASHING
func QualifiedStatesForWithdrawable() []DelegationState {
return []DelegationState{StateUnbonding, StateSlashed}
return []DelegationState{StateUnbonding, StateSlashed, StateWithdrawable}
}

type DelegationSubState string
Expand Down

0 comments on commit 40faacd

Please sign in to comment.