Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Jan 13, 2025
1 parent 1ea1ea3 commit 37d2ccf
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions internal/services/watch_btc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,28 +177,7 @@ func (s *Service) handleSpendingStakingTransaction(
if err != nil {
if errors.Is(err, types.ErrInvalidUnbondingTx) {
// TODO: Add metrics
log.Error().
Err(err).
Str("staking_tx", delegation.StakingTxHashHex).
Str("spending_tx", spendingTx.TxHash().String()).
Msg("found an unexpected unbonding tx")

registeredUnbondingTxBytes, parseErr := hex.DecodeString(delegation.UnbondingTx)
if parseErr != nil {
return fmt.Errorf("failed to decode unbonding tx: %w", parseErr)
}

registeredUnbondingTx, parseErr := bbn.NewBTCTxFromBytes(registeredUnbondingTxBytes)
if parseErr != nil {
return fmt.Errorf("failed to parse unbonding tx: %w", parseErr)
}

registeredUnbondingTxHash := registeredUnbondingTx.TxHash().String()
if registeredUnbondingTxHash != spendingTx.TxHash().String() {
return s.handleUnexpectedUnbondingTx(ctx, spendingTx, delegation)
}

return nil
return s.handleUnexpectedUnbondingTx(ctx, spendingTx, delegation)
}

return fmt.Errorf("failed to validate unbonding tx: %w", err)
Expand Down Expand Up @@ -371,24 +350,36 @@ func (s *Service) handleUnexpectedUnbondingTx(
spendingTx *wire.MsgTx,
delegation *model.BTCDelegationDetails,
) error {
log.Debug().
Str("staking_tx", delegation.StakingTxHashHex).
Str("unbonding_tx", spendingTx.TxHash().String()).
Msg("handling unexpected unbonding tx")
registeredUnbondingTxBytes, parseErr := hex.DecodeString(delegation.UnbondingTx)
if parseErr != nil {
return fmt.Errorf("failed to decode unbonding tx: %w", parseErr)
}

registeredUnbondingTx, parseErr := bbn.NewBTCTxFromBytes(registeredUnbondingTxBytes)
if parseErr != nil {
return fmt.Errorf("failed to parse unbonding tx: %w", parseErr)
}

// Emit consumer event
if err := s.emitUnbondingDelegationEvent(ctx, delegation); err != nil {
return err
// This should never happen as we've already validated it's an unexpected tx
if registeredUnbondingTx.TxHash().String() == spendingTx.TxHash().String() {
return fmt.Errorf("inconsistent state: tx %s was marked as unexpected but matches registered unbonding tx",
spendingTx.TxHash().String())
}

log.Error().
Str("staking_tx", delegation.StakingTxHashHex).
Str("spending_tx", spendingTx.TxHash().String()).
Str("registered_unbonding_tx", registeredUnbondingTx.TxHash().String()).
Msg("detected unexpected unbonding transaction")

// Update delegation state to unbonding
subState := types.SubStateEarlyUnbonding
if err := s.db.UpdateBTCDelegationState(
ctx,
delegation.StakingTxHashHex,
types.QualifiedStatesForUnbondedEarly(),
types.StateUnbonding,
&subState,
db.WithSubState(subState),
); err != nil {
return fmt.Errorf("failed to update BTC delegation state: %w", err)
}
Expand Down

0 comments on commit 37d2ccf

Please sign in to comment.