Skip to content

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Nov 25, 2024
1 parent 3770708 commit caa2dcb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
10 changes: 10 additions & 0 deletions internal/services/delegation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
bbn "github.com/babylonlabs-io/babylon/types"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/rs/zerolog/log"
)

func (s *Service) registerUnbondingSpendNotification(
Expand All @@ -36,6 +37,11 @@ func (s *Service) registerUnbondingSpendNotification(
)
}

log.Info().
Str("staking_tx", delegation.StakingTxHashHex).
Str("unbonding_tx", unbondingTx.TxHash().String()).
Msg("registering early unbonding spend notification")

unbondingOutpoint := wire.OutPoint{
Hash: unbondingTx.TxHash(),
Index: 0, // unbonding tx has only 1 output
Expand Down Expand Up @@ -82,6 +88,10 @@ func (s *Service) registerStakingSpendNotification(
)
}

log.Info().
Str("staking_tx", delegation.StakingTxHashHex).
Msg("registering staking spend notification")

stakingOutpoint := wire.OutPoint{
Hash: *stakingTxHash,
Index: delegation.StakingOutputIdx,
Expand Down
61 changes: 55 additions & 6 deletions internal/services/watch_btc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ func (s *Service) watchForSpendStakingTx(
// Get spending details
select {
case spendDetail := <-spendEvent.Spend:
log.Info().
Str("staking_tx", delegation.StakingTxHashHex).
Str("spending_tx", spendDetail.SpendingTx.TxHash().String()).
Msg("staking tx has been spent")
if err := s.handleSpendingStakingTransaction(
quitCtx,
spendDetail.SpendingTx,
spendDetail.SpenderInputIndex,
uint32(spendDetail.SpendingHeight),
delegation,
); err != nil {
log.Error().Err(err).Msg("failed to handle spending staking transaction")
log.Error().
Err(err).
Str("staking_tx", delegation.StakingTxHashHex).
Str("spending_tx", spendDetail.SpendingTx.TxHash().String()).
Msg("failed to handle spending staking transaction")
return
}

Expand All @@ -60,14 +68,22 @@ func (s *Service) watchForSpendUnbondingTx(
// Get spending details
select {
case spendDetail := <-spendEvent.Spend:
log.Info().
Str("staking_tx", delegation.StakingTxHashHex).
Str("unbonding_tx", spendDetail.SpendingTx.TxHash().String()).
Msg("unbonding tx has been spent")
if err := s.handleSpendingUnbondingTransaction(
quitCtx,
spendDetail.SpendingTx,
uint32(spendDetail.SpendingHeight),
spendDetail.SpenderInputIndex,
delegation,
); err != nil {
log.Error().Err(err).Msg("failed to handle spending unbonding transaction")
log.Error().
Err(err).
Str("staking_tx", delegation.StakingTxHashHex).
Str("unbonding_tx", spendDetail.SpendingTx.TxHash().String()).
Msg("failed to handle spending unbonding transaction")
return
}

Expand All @@ -90,18 +106,24 @@ func (s *Service) watchForSpendSlashingChange(
select {
case spendDetail := <-spendEvent.Spend:
log.Info().
Str("delegation", delegation.StakingTxHashHex).
Str("staking_tx", delegation.StakingTxHashHex).
Str("spending_tx", spendDetail.SpendingTx.TxHash().String()).
Msg("slashing change output has been spent")
delegationState, err := s.db.GetBTCDelegationState(quitCtx, delegation.StakingTxHashHex)
if err != nil {
log.Error().Err(err).Msg("failed to get delegation state")
log.Error().
Err(err).
Str("staking_tx", delegation.StakingTxHashHex).
Msg("failed to get delegation state")
return
}

qualifiedStates := types.QualifiedStatesForSlashedWithdrawn()
if qualifiedStates == nil || !utils.Contains(qualifiedStates, *delegationState) {
log.Error().Msgf("current state %s is not qualified for slashed withdrawn", *delegationState)
log.Error().
Str("staking_tx", delegation.StakingTxHashHex).
Str("state", delegationState.String()).
Msg("current state is not qualified for slashed withdrawn")
return
}

Expand All @@ -113,7 +135,12 @@ func (s *Service) watchForSpendSlashingChange(
types.StateWithdrawn,
&delegationSubState,
); err != nil {
log.Error().Err(err).Msg("failed to update delegation state")
log.Error().
Err(err).
Str("staking_tx", delegation.StakingTxHashHex).
Str("state", types.StateWithdrawn.String()).
Str("sub_state", delegationSubState.String()).
Msg("failed to update delegation state to withdrawn")
return
}

Expand Down Expand Up @@ -142,6 +169,10 @@ func (s *Service) handleSpendingStakingTransaction(
return fmt.Errorf("failed to validate unbonding tx: %w", err)
}
if isUnbonding {
log.Info().
Str("staking_tx", delegation.StakingTxHashHex).
Str("unbonding_tx", spendingTx.TxHash().String()).
Msg("staking tx has been spent through unbonding path")
// It's a valid unbonding tx, no further action needed at this stage
return nil
}
Expand All @@ -150,6 +181,10 @@ func (s *Service) handleSpendingStakingTransaction(
withdrawalErr := s.validateWithdrawalTxFromStaking(spendingTx, spendingInputIdx, delegation, params)
if withdrawalErr == nil {
// It's a valid withdrawal, process it
log.Info().
Str("staking_tx", delegation.StakingTxHashHex).
Str("withdrawal_tx", spendingTx.TxHash().String()).
Msg("staking tx has been spent through withdrawal path")
return s.handleWithdrawal(ctx, delegation, types.SubStateTimelock)
}

Expand Down Expand Up @@ -193,6 +228,10 @@ func (s *Service) handleSpendingUnbondingTransaction(
withdrawalErr := s.validateWithdrawalTxFromUnbonding(spendingTx, delegation, spendingInputIdx, params)
if withdrawalErr == nil {
// It's a valid withdrawal, process it
log.Info().
Str("staking_tx", delegation.StakingTxHashHex).
Str("unbonding_tx", spendingTx.TxHash().String()).
Msg("unbonding tx has been spent through withdrawal path")
return s.handleWithdrawal(ctx, delegation, types.SubStateEarlyUnbonding)
}

Expand Down Expand Up @@ -236,6 +275,11 @@ func (s *Service) handleWithdrawal(
}

// Update to withdrawn state
log.Info().
Str("staking_tx", delegation.StakingTxHashHex).
Str("state", types.StateWithdrawn.String()).
Str("sub_state", subState.String()).
Msg("updating delegation state to withdrawn")
return s.db.UpdateBTCDelegationState(
ctx,
delegation.StakingTxHashHex,
Expand All @@ -251,6 +295,11 @@ func (s *Service) startWatchingSlashingChange(
delegation *model.BTCDelegationDetails,
subState types.DelegationSubState,
) error {
log.Info().
Str("staking_tx", delegation.StakingTxHashHex).
Str("slashing_tx", slashingTx.TxHash().String()).
Msg("watching for slashing change output")

// Create outpoint for the change output (index 1)
changeOutpoint := wire.OutPoint{
Hash: slashingTx.TxHash(),
Expand Down

0 comments on commit caa2dcb

Please sign in to comment.