Skip to content

Commit

Permalink
fix slashed event
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Jan 5, 2025
1 parent 2f6429e commit 064ba15
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
28 changes: 22 additions & 6 deletions internal/db/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"context"
"errors"
"fmt"
"log"

"github.com/rs/zerolog/log"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
Expand Down Expand Up @@ -182,10 +183,23 @@ func (db *Database) GetBTCDelegationByStakingTxHash(
func (db *Database) UpdateDelegationsStateByFinalityProvider(
ctx context.Context,
fpBTCPKHex string,
qualifiedPreviousStates []types.DelegationState,
newState types.DelegationState,
) error {
if len(qualifiedPreviousStates) == 0 {
return fmt.Errorf("qualified previous states array cannot be empty")
}

// Convert states to strings
qualifiedStateStrs := make([]string, len(qualifiedPreviousStates))
for i, state := range qualifiedPreviousStates {
qualifiedStateStrs[i] = state.String()
}

// Build filter with both FP and qualified states
filter := bson.M{
"finality_provider_btc_pks_hex": fpBTCPKHex,
"state": bson.M{"$in": qualifiedStateStrs},
}

update := bson.M{
Expand All @@ -201,11 +215,13 @@ func (db *Database) UpdateDelegationsStateByFinalityProvider(
return fmt.Errorf("failed to update delegations: %w", err)
}

log.Printf("Updated %d delegations for finality provider %s to state %s",
result.ModifiedCount,
fpBTCPKHex,
newState.String(),
)
log.Debug().
Str("finality_provider", fpBTCPKHex).
Strs("qualified_states", qualifiedStateStrs).
Str("new_state", newState.String()).
Int64("modified_count", result.ModifiedCount).
Msg("Updated delegations for finality provider")

return nil
}

Expand Down
7 changes: 5 additions & 2 deletions internal/db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,15 @@ type DbInterface interface {
* UpdateDelegationsStateByFinalityProvider updates the BTC delegation state by the finality provider public key.
* @param ctx The context
* @param fpBtcPkHex The finality provider public key
* @param qualifiedPreviousStates The qualified previous states
* @param newState The new state
* @param qualifiedStates The qualified states
* @return An error if the operation failed
*/
UpdateDelegationsStateByFinalityProvider(
ctx context.Context, fpBtcPkHex string, newState types.DelegationState,
ctx context.Context,
fpBtcPkHex string,
qualifiedPreviousStates []types.DelegationState,
newState types.DelegationState,
) error
/**
* GetDelegationsByFinalityProvider retrieves the BTC delegations by the finality provider public key.
Expand Down
18 changes: 17 additions & 1 deletion internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 @@ -493,7 +494,7 @@ func (s *Service) processSlashedFinalityProviderEvent(
fpBTCPKHex := evidence.FpBtcPk.MarshalHex()

if dbErr := s.db.UpdateDelegationsStateByFinalityProvider(
ctx, fpBTCPKHex, types.StateSlashed,
ctx, fpBTCPKHex, types.QualifiedStatesForSlashedDelegation(), types.StateSlashed,
); dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
Expand All @@ -513,13 +514,28 @@ func (s *Service) processSlashedFinalityProviderEvent(

for _, delegation := range delegations {
if !delegation.HasInclusionProof() {
// If the delegation was never active/has no inclusion proof
// no need to emit the event, as it doesn't contribute to stats
log.Debug().
Str("staking_tx", delegation.StakingTxHashHex).
Str("event_type", EventSlashedFinalityProvider.String()).
Str("current_state", delegation.State.String()).
Str("reason", "missing_inclusion_proof").
Msg("skipping slashed delegation event")
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
4 changes: 2 additions & 2 deletions internal/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (s *Service) validateBTCDelegationUnbondedEarlyEvent(ctx context.Context, e
if utils.Contains(types.OutdatedStatesForUnbondedEarly(), delegation.State) {
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Str("currentState", event.NewState).
Str("currentState", delegation.State.String()).
Str("event_type", "EventBTCDelgationUnbondedEarly").
Msg("Current state is outdated for transition")
return false, true, nil
Expand Down Expand Up @@ -387,7 +387,7 @@ func (s *Service) validateBTCDelegationExpiredEvent(ctx context.Context, event *
if utils.Contains(types.OutdatedStatesForExpired(), delegation.State) {
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Str("currentState", event.NewState).
Str("currentState", delegation.State.String()).
Str("event_type", "EventBTCDelegationExpired").
Msg("Current state is outdated for transition")
return false, true, nil
Expand Down
5 changes: 5 additions & 0 deletions internal/types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ 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}
}

// QualifiedStatesForWithdrawn returns the qualified current states for Withdrawn event
func QualifiedStatesForWithdrawn() []DelegationState {
// StateActive/StateUnbonding/StateSlashed is included b/c its possible that expiry checker
Expand Down
10 changes: 5 additions & 5 deletions tests/mocks/mock_db_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 064ba15

Please sign in to comment.