Skip to content

Commit

Permalink
feat: handle unexpected unbonding tx (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 authored Jan 16, 2025
1 parent 9506444 commit d226846
Show file tree
Hide file tree
Showing 3 changed files with 302 additions and 143 deletions.
14 changes: 14 additions & 0 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
return nil
}

// TODO: Indexer doesn't need to intercept processBTCDelegationUnbondedEarlyEvent
// as the unbonding tx will be discovered by the btc notifier
// we are keeping it for now to avoid breaking changes, but if the btc notifier has already identified
// then this event will be silently ignored with help of validateBTCDelegationUnbondedEarlyEvent
func (s *Service) processBTCDelegationUnbondedEarlyEvent(
ctx context.Context, event abcitypes.Event, bbnBlockHeight int64,
) *types.Error {
Expand Down Expand Up @@ -336,6 +340,7 @@ func (s *Service) processBTCDelegationUnbondedEarlyEvent(

log.Debug().
Str("staking_tx", unbondedEarlyEvent.StakingTxHash).
Stringer("current_state", delegation.State).
Stringer("new_state", types.StateUnbonding).
Str("early_unbonding_start_height", unbondedEarlyEvent.StartHeight).
Str("unbonding_time", strconv.FormatUint(uint64(delegation.UnbondingTime), 10)).
Expand All @@ -353,6 +358,15 @@ func (s *Service) processBTCDelegationUnbondedEarlyEvent(
db.WithSubState(subState),
db.WithBbnHeight(bbnBlockHeight),
); err != nil {
if db.IsNotFoundError(err) {
// maybe the btc notifier has already identified the unbonding tx and updated the state
log.Debug().
Str("staking_tx", delegation.StakingTxHashHex).
Interface("qualified_states", types.QualifiedStatesForUnbondedEarly()).
Msg("delegation not in qualified states for early unbonding update")
return nil
}

return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
Expand Down
14 changes: 3 additions & 11 deletions internal/services/delegation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,15 @@ import (
func (s *Service) registerUnbondingSpendNotification(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
) error {
unbondingTxBytes, parseErr := hex.DecodeString(delegation.UnbondingTx)
if parseErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to decode unbonding tx: %w", parseErr),
)
return fmt.Errorf("failed to decode unbonding tx: %w", parseErr)
}

unbondingTx, parseErr := bbn.NewBTCTxFromBytes(unbondingTxBytes)
if parseErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to parse unbonding tx: %w", parseErr),
)
return fmt.Errorf("failed to parse unbonding tx: %w", parseErr)
}

log.Debug().
Expand Down
Loading

0 comments on commit d226846

Please sign in to comment.