Skip to content

Commit

Permalink
handle unexpected unbonding tx
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Jan 13, 2025
1 parent 4decb6e commit 42701ef
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion internal/services/watch_btc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"

Expand Down Expand Up @@ -179,7 +180,22 @@ func (s *Service) handleSpendingStakingTransaction(
Err(err).
Str("staking_tx", delegation.StakingTxHashHex).
Str("spending_tx", spendingTx.TxHash().String()).
Msg("found an invalid unbonding tx")
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
}
Expand Down Expand Up @@ -346,6 +362,36 @@ func (s *Service) handleWithdrawal(
)
}

func (s *Service) handleUnexpectedUnbondingTx(
ctx context.Context,
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")

// Emit consumer event
if err := s.emitUnbondingDelegationEvent(ctx, delegation); err != nil {
return err
}

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

Check failure on line 391 in internal/services/watch_btc_events.go

View workflow job for this annotation

GitHub Actions / lint_test / build

cannot use &subState (value of type *"github.com/babylonlabs-io/babylon-staking-indexer/internal/types".DelegationSubState) as "github.com/babylonlabs-io/babylon-staking-indexer/internal/db".UpdateOption value in argument to s.db.UpdateBTCDelegationState

Check failure on line 391 in internal/services/watch_btc_events.go

View workflow job for this annotation

GitHub Actions / lint_test / integration-tests

cannot use &subState (value of type *"github.com/babylonlabs-io/babylon-staking-indexer/internal/types".DelegationSubState) as "github.com/babylonlabs-io/babylon-staking-indexer/internal/db".UpdateOption value in argument to s.db.UpdateBTCDelegationState

Check failure on line 391 in internal/services/watch_btc_events.go

View workflow job for this annotation

GitHub Actions / lint_test / lint

cannot use &subState (value of type *"github.com/babylonlabs-io/babylon-staking-indexer/internal/types".DelegationSubState) as "github.com/babylonlabs-io/babylon-staking-indexer/internal/db".UpdateOption value in argument to s.db.UpdateBTCDelegationState (typecheck)

Check failure on line 391 in internal/services/watch_btc_events.go

View workflow job for this annotation

GitHub Actions / lint_test / lint

cannot use &subState (value of type *"github.com/babylonlabs-io/babylon-staking-indexer/internal/types".DelegationSubState) as "github.com/babylonlabs-io/babylon-staking-indexer/internal/db".UpdateOption value in argument to s.db.UpdateBTCDelegationState) (typecheck)

Check failure on line 391 in internal/services/watch_btc_events.go

View workflow job for this annotation

GitHub Actions / lint_test / lint

cannot use &subState (value of type *"github.com/babylonlabs-io/babylon-staking-indexer/internal/types".DelegationSubState) as "github.com/babylonlabs-io/babylon-staking-indexer/internal/db".UpdateOption value in argument to s.db.UpdateBTCDelegationState) (typecheck)

Check failure on line 391 in internal/services/watch_btc_events.go

View workflow job for this annotation

GitHub Actions / lint_test / unit-tests

cannot use &subState (value of type *"github.com/babylonlabs-io/babylon-staking-indexer/internal/types".DelegationSubState) as "github.com/babylonlabs-io/babylon-staking-indexer/internal/db".UpdateOption value in argument to s.db.UpdateBTCDelegationState
return nil
}

func (s *Service) startWatchingSlashingChange(
ctx context.Context,
slashingTx *wire.MsgTx,
Expand Down

0 comments on commit 42701ef

Please sign in to comment.