Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle withdrawal after slashing #56

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 47 additions & 23 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ func (s *Service) processCovenantQuorumReachedEvent(
return err
}

proceed, err := s.validateCovenantQuorumReachedEvent(ctx, covenantQuorumReachedEvent)
shouldProcess, err := s.validateCovenantQuorumReachedEvent(ctx, covenantQuorumReachedEvent)
if err != nil {
return err
}
if !proceed {
if !shouldProcess {
// Ignore the event silently
return nil
}
Expand Down Expand Up @@ -168,11 +168,11 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
return err
}

proceed, err := s.validateBTCDelegationInclusionProofReceivedEvent(ctx, inclusionProofEvent)
shouldProcess, err := s.validateBTCDelegationInclusionProofReceivedEvent(ctx, inclusionProofEvent)
if err != nil {
return err
}
if !proceed {
if !shouldProcess {
// Ignore the event silently
return nil
}
Expand Down Expand Up @@ -225,10 +225,13 @@ func (s *Service) processBTCDelegationUnbondedEarlyEvent(
return nil
}

// Get delegation details
delegation, err := s.getDelegationDetails(ctx, unbondedEarlyEvent.StakingTxHash)
if err != nil {
return err
delegation, dbErr := s.db.GetBTCDelegationByStakingTxHash(ctx, unbondedEarlyEvent.StakingTxHash)
if dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", dbErr),
)
}

// Emit consumer event
Expand All @@ -241,8 +244,8 @@ func (s *Service) processBTCDelegationUnbondedEarlyEvent(
return err
}

// Start watching for spend
if err := s.startWatchingUnbondingSpend(ctx, delegation); err != nil {
// Register unbonding spend notification
if err := s.registerUnbondingSpendNotification(ctx, delegation); err != nil {
return err
}

Expand All @@ -269,24 +272,49 @@ func (s *Service) processBTCDelegationExpiredEvent(
return nil
}

// Get delegation details
delegation, err := s.getDelegationDetails(ctx, expiredEvent.StakingTxHash)
if err != nil {
return err
delegation, dbErr := s.db.GetBTCDelegationByStakingTxHash(ctx, expiredEvent.StakingTxHash)
if dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", dbErr),
)
}

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

// Handle expiry process
if err := s.handleExpiryProcess(ctx, delegation); err != nil {
return err
// Save timelock expire
if err := s.db.SaveNewTimeLockExpire(
ctx,
delegation.StakingTxHashHex,
delegation.EndHeight,
types.ExpiredTxType.String(),
); err != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to save timelock expire: %w", err),
)
}

// Start watching for spend
if err := s.startWatchingStakingSpend(ctx, delegation); err != nil {
// Update delegation state
if err := s.db.UpdateBTCDelegationState(
ctx,
delegation.StakingTxHashHex,
types.StateUnbonding,
); err != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update BTC delegation state: %w", err),
)
}

// Register spend notification
if err := s.registerStakingSpendNotification(ctx, delegation); err != nil {
return err
}

Expand Down Expand Up @@ -326,9 +354,5 @@ func (s *Service) processSlashedFinalityProviderEvent(
)
}

// TODO: babylon needs to emit slashing tx
// so indexer can start watching for slashing spend
// to identify if staker has withdrawn after slashing

return nil
}
54 changes: 2 additions & 52 deletions internal/services/delegation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ import (
"github.com/btcsuite/btcd/wire"
)

// Delegation helper functions
func (s *Service) getDelegationDetails(
ctx context.Context,
stakingTxHash string,
) (*model.BTCDelegationDetails, *types.Error) {
delegation, dbErr := s.db.GetBTCDelegationByStakingTxHash(ctx, stakingTxHash)
if dbErr != nil {
return nil, types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", dbErr),
)
}
return delegation, nil
}

func (s *Service) handleUnbondingProcess(
ctx context.Context,
event *bbntypes.EventBTCDelgationUnbondedEarly,
Expand Down Expand Up @@ -77,7 +61,7 @@ func (s *Service) handleUnbondingProcess(
return nil
}

func (s *Service) startWatchingUnbondingSpend(
func (s *Service) registerUnbondingSpendNotification(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
Expand Down Expand Up @@ -123,41 +107,7 @@ func (s *Service) startWatchingUnbondingSpend(
return nil
}

func (s *Service) handleExpiryProcess(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
// Save timelock expire
if err := s.db.SaveNewTimeLockExpire(
ctx,
delegation.StakingTxHashHex,
delegation.EndHeight,
types.ExpiredTxType.String(),
); err != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to save timelock expire: %w", err),
)
}

// Update delegation state
if err := s.db.UpdateBTCDelegationState(
ctx,
delegation.StakingTxHashHex,
types.StateUnbonding,
); err != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update BTC delegation state: %w", err),
)
}

return nil
}

func (s *Service) startWatchingStakingSpend(
func (s *Service) registerStakingSpendNotification(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
Expand Down
Loading
Loading