Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Jan 6, 2025
1 parent 7618482 commit 7774cc5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 139 deletions.
70 changes: 7 additions & 63 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
bbntypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
ftypes "github.com/babylonlabs-io/babylon/x/finality/types"
abcitypes "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -70,8 +69,6 @@ func (s *Service) processNewBTCDelegationEvent(
)
}

// TODO: start watching for BTC confirmation if we need PendingBTCConfirmation state

return nil
}

Expand Down Expand Up @@ -285,32 +282,12 @@ func (s *Service) processBTCDelegationUnbondedEarlyEvent(
return err
}

shouldProcess, shouldEmitEvent, err := s.validateBTCDelegationUnbondedEarlyEvent(ctx, unbondedEarlyEvent)
shouldProcess, err := s.validateBTCDelegationUnbondedEarlyEvent(ctx, unbondedEarlyEvent)
if err != nil {
return err
}
if !shouldProcess {
// Event is valid but should be skipped
if shouldEmitEvent {
log.Debug().
Str("staking_tx", unbondedEarlyEvent.StakingTxHash).
Str("event_type", EventBTCDelgationUnbondedEarly.String()).
Msg("skip processing but emit unbonding event")

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
if err := s.emitUnbondingDelegationEvent(ctx, delegation); err != nil {
return err
}
}
// Ignore the event silently
return nil
}

Expand Down Expand Up @@ -393,32 +370,13 @@ func (s *Service) processBTCDelegationExpiredEvent(
return err
}

shouldProcess, shouldEmitEvent, err := s.validateBTCDelegationExpiredEvent(ctx, expiredEvent)
shouldProcess, err := s.validateBTCDelegationExpiredEvent(ctx, expiredEvent)
if err != nil {
return err
}

if !shouldProcess {
// Event is valid but should be skipped
if shouldEmitEvent {
log.Debug().
Str("staking_tx", expiredEvent.StakingTxHash).
Str("event_type", EventBTCDelegationExpired.String()).
Msg("skip processing but emit unbonding event")

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.emitUnbondingDelegationEvent(ctx, delegation); err != nil {
return err
}
}
// Ignore the event silently
return nil
}

Expand Down Expand Up @@ -481,14 +439,10 @@ func (s *Service) processSlashedFinalityProviderEvent(
return err
}

shouldProcess, err := s.validateSlashedFinalityProviderEvent(ctx, slashedFinalityProviderEvent)
err = s.validateSlashedFinalityProviderEvent(ctx, slashedFinalityProviderEvent)
if err != nil {
return err
}
if !shouldProcess {
// Event is valid but should be skipped
return nil
}

evidence := slashedFinalityProviderEvent.Evidence
fpBTCPKHex := evidence.FpBtcPk.MarshalHex()
Expand All @@ -514,6 +468,7 @@ func (s *Service) processSlashedFinalityProviderEvent(

// TODO: ideally indexer should simply emit the slashed FP
// queue handlers should handle the rest

for _, delegation := range delegations {
if !delegation.HasInclusionProof() {
// If the delegation was never active/has no inclusion proof
Expand All @@ -527,17 +482,6 @@ func (s *Service) processSlashedFinalityProviderEvent(
continue
}

if !utils.Contains(types.QualifiedStatesForSlashedDelegation(), delegation.State) {
// If the current state is not qualified, no need to emit the event
log.Debug().
Str("staking_tx", delegation.StakingTxHashHex).
Str("event_type", EventSlashedFinalityProvider.String()).
Str("current_state", delegation.State.String()).
Str("reason", "not_qualified").
Msg("skipped slashed delegation event")
continue
}

if err := s.emitUnbondingDelegationEvent(ctx, delegation); err != nil {
return err
}
Expand Down
54 changes: 16 additions & 38 deletions internal/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ func (s *Service) validateBTCDelegationInclusionProofReceivedEvent(ctx context.C
return true, nil
}

func (s *Service) validateBTCDelegationUnbondedEarlyEvent(ctx context.Context, event *bstypes.EventBTCDelgationUnbondedEarly) (bool, bool, *types.Error) {
func (s *Service) validateBTCDelegationUnbondedEarlyEvent(ctx context.Context, event *bstypes.EventBTCDelgationUnbondedEarly) (bool, *types.Error) {
// Check if the staking tx hash is present
if event.StakingTxHash == "" {
return false, false, types.NewErrorWithMsg(
return false, types.NewErrorWithMsg(
http.StatusInternalServerError,
types.InternalServiceError,
"unbonded early event missing staking tx hash",
Expand All @@ -310,53 +310,42 @@ func (s *Service) validateBTCDelegationUnbondedEarlyEvent(ctx context.Context, e

// Validate the event state
if event.NewState != bstypes.BTCDelegationStatus_UNBONDED.String() {
return false, false, types.NewValidationFailedError(
return false, types.NewValidationFailedError(
fmt.Errorf("invalid delegation state from Babylon when processing EventBTCDelgationUnbondedEarly: expected UNBONDED, got %s", event.NewState),
)
}

// Fetch the current delegation state from the database
delegation, dbErr := s.db.GetBTCDelegationByStakingTxHash(ctx, event.StakingTxHash)
if dbErr != nil {
return false, false, types.NewError(
return false, types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", dbErr),
)
}

// Check if the current state is outdated for the transition
// We should emit the event if the current state is outdated
if utils.Contains(types.OutdatedStatesForUnbondedEarly(), delegation.State) {
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Str("currentState", delegation.State.String()).
Str("event_type", "EventBTCDelgationUnbondedEarly").
Msg("Current state is outdated for transition")
return false, true, nil
}

// Check if the current state is qualified for the transition
if !utils.Contains(types.QualifiedStatesForUnbondedEarly(), delegation.State) {
log.Error().
Str("stakingTxHashHex", event.StakingTxHash).
Str("currentState", delegation.State.String()).
Str("event_type", "EventBTCDelgationUnbondedEarly").
Msg("Current state is not qualified for transition")
return false, false, types.NewErrorWithMsg(
return false, types.NewErrorWithMsg(
http.StatusForbidden,
types.Forbidden,
"current state is not qualified for transition",
)
}

return true, true, nil
return true, nil
}

func (s *Service) validateBTCDelegationExpiredEvent(ctx context.Context, event *bstypes.EventBTCDelegationExpired) (bool, bool, *types.Error) {
func (s *Service) validateBTCDelegationExpiredEvent(ctx context.Context, event *bstypes.EventBTCDelegationExpired) (bool, *types.Error) {
// Check if the staking tx hash is present
if event.StakingTxHash == "" {
return false, false, types.NewErrorWithMsg(
return false, types.NewErrorWithMsg(
http.StatusInternalServerError,
types.InternalServiceError,
"expired event missing staking tx hash",
Expand All @@ -365,7 +354,7 @@ func (s *Service) validateBTCDelegationExpiredEvent(ctx context.Context, event *

// Validate the event state
if event.NewState != bstypes.BTCDelegationStatus_EXPIRED.String() {
return false, false, types.NewErrorWithMsg(
return false, types.NewErrorWithMsg(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Sprintf("invalid delegation state from Babylon when processing EventBTCDelegationExpired: expected EXPIRED, got %s", event.NewState),
Expand All @@ -375,44 +364,33 @@ func (s *Service) validateBTCDelegationExpiredEvent(ctx context.Context, event *
// Fetch the current delegation state from the database
delegation, dbErr := s.db.GetBTCDelegationByStakingTxHash(ctx, event.StakingTxHash)
if dbErr != nil {
return false, false, types.NewError(
return false, types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", dbErr),
)
}

// Check if the current state is outdated for the transition
// We should emit the event if the current state is outdated
if utils.Contains(types.OutdatedStatesForExpired(), delegation.State) {
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Str("currentState", delegation.State.String()).
Str("event_type", "EventBTCDelegationExpired").
Msg("Current state is outdated for transition")
return false, true, nil
}

// Check if the current state is qualified for the transition
if !utils.Contains(types.QualifiedStatesForExpired(), delegation.State) {
log.Error().
Str("stakingTxHashHex", event.StakingTxHash).
Str("currentState", delegation.State.String()).
Str("event_type", "EventBTCDelegationExpired").
Msg("current state is not qualified for transition")
return false, false, types.NewErrorWithMsg(
return false, types.NewErrorWithMsg(
http.StatusForbidden,
types.Forbidden,
"current state is not qualified for transition",
)
}

return true, true, nil
return true, nil
}

func (s *Service) validateSlashedFinalityProviderEvent(ctx context.Context, event *ftypes.EventSlashedFinalityProvider) (bool, *types.Error) {
func (s *Service) validateSlashedFinalityProviderEvent(ctx context.Context, event *ftypes.EventSlashedFinalityProvider) *types.Error {
if event.Evidence == nil {
return false, types.NewErrorWithMsg(
return types.NewErrorWithMsg(
http.StatusInternalServerError,
types.InternalServiceError,
"slashed finality provider event missing evidence",
Expand All @@ -421,14 +399,14 @@ func (s *Service) validateSlashedFinalityProviderEvent(ctx context.Context, even

_, err := event.Evidence.ExtractBTCSK()
if err != nil {
return false, types.NewError(
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to extract BTC SK of the slashed finality provider: %w", err),
)
}

return true, nil
return nil
}

func sanitizeEvent(event abcitypes.Event) abcitypes.Event {
Expand Down
29 changes: 1 addition & 28 deletions internal/services/expiry_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,14 @@ func (s *Service) checkExpiry(ctx context.Context) *types.Error {
Str("expire_height", strconv.FormatUint(uint64(tlDoc.ExpireHeight), 10)).
Msg("checking if delegation is withdrawable")

if utils.Contains(types.OutdatedStatesForWithdrawable(), delegation.State) {
log.Debug().
Str("staking_tx", delegation.StakingTxHashHex).
Str("current_state", delegation.State.String()).
Msg("current state is outdated for withdrawable")

if err := s.emitWithdrawableDelegationEvent(ctx, delegation); err != nil {
log.Error().
Str("staking_tx", delegation.StakingTxHashHex).
Msg("failed to emit withdrawable delegation event")
return err
}

if err := s.db.DeleteExpiredDelegation(ctx, delegation.StakingTxHashHex); err != nil {
log.Error().
Str("staking_tx", delegation.StakingTxHashHex).
Msg("failed to delete expired delegation")
return types.NewInternalServiceError(
fmt.Errorf("failed to delete expired delegation: %w", err),
)
}

continue
}

// Check if the delegation is in a qualified state to transition to Withdrawable
if !utils.Contains(types.QualifiedStatesForWithdrawable(), delegation.State) {
log.Error().
Str("staking_tx", delegation.StakingTxHashHex).
Str("current_state", delegation.State.String()).
Msg("current state is not qualified for withdrawable")

return types.NewInternalServiceError(
fmt.Errorf("current state is not qualified for withdrawable"),
)
continue
}

if err := s.db.UpdateBTCDelegationState(
Expand Down
10 changes: 0 additions & 10 deletions internal/types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,11 @@ func QualifiedStatesForUnbondedEarly() []DelegationState {
return []DelegationState{StateActive}
}

// OutdatedStatesForUnbondedEarly returns the outdated current states for UnbondedEarly event
func OutdatedStatesForUnbondedEarly() []DelegationState {
return []DelegationState{StateUnbonding, StateWithdrawable, StateWithdrawn}
}

// QualifiedStatesForExpired returns the qualified current states for Expired event
func QualifiedStatesForExpired() []DelegationState {
return []DelegationState{StateActive}
}

// OutdatedStatesForExpired returns the outdated current states for Expired event
func OutdatedStatesForExpired() []DelegationState {
return []DelegationState{StateUnbonding, StateWithdrawable, StateWithdrawn}
}

// QualifiedStatesForSlashedDelegation returns the qualified current states when a delegation is slashed
func QualifiedStatesForSlashedDelegation() []DelegationState {
return []DelegationState{StatePending, StateVerified, StateActive, StateUnbonding, StateWithdrawable}
Expand Down

0 comments on commit 7774cc5

Please sign in to comment.