Skip to content

Commit

Permalink
fix err catch
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Oct 23, 2024
1 parent a224299 commit d7b53df
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
5 changes: 5 additions & 0 deletions internal/db/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ func (e *DuplicateKeyError) Error() string {
return e.Message
}

func (e *DuplicateKeyError) Is(target error) bool {
_, ok := target.(*DuplicateKeyError)
return ok
}

func IsDuplicateKeyError(err error) bool {
return errors.Is(err, &DuplicateKeyError{})
}
Expand Down
27 changes: 18 additions & 9 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,24 @@ func (s *Service) validateCovenantQuorumReachedEvent(ctx context.Context, event

// Delegation should not have the inclusion proof yet
if delegation.HasInclusionProof() {
return false, types.NewValidationFailedError(
fmt.Errorf("inclusion proof already received for BTC delegation: %s", event.StakingTxHash),
)
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Str("currentState", delegation.State.String()).
Str("newState", event.NewState).
Msg("Ignoring EventCovenantQuorumReached because inclusion proof already received")
return false, nil
}
} else if event.NewState == bbntypes.BTCDelegationStatus_ACTIVE.String() {
// This will happen if the inclusion proof is received in MsgCreateBTCDelegation, i.e the staker is following the old flow

// Delegation should have the inclusion proof
if !delegation.HasInclusionProof() {
return false, types.NewValidationFailedError(
fmt.Errorf("inclusion proof not received for BTC delegation: %s", event.StakingTxHash),
)
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Str("currentState", delegation.State.String()).
Str("newState", event.NewState).
Msg("Ignoring EventCovenantQuorumReached because inclusion proof not received")
return false, nil
}
}

Expand Down Expand Up @@ -325,9 +331,12 @@ func (s *Service) validateBTCDelegationInclusionProofReceivedEvent(ctx context.C
// Delegation should not have the inclusion proof yet
// After this event is processed, the inclusion proof will be set
if delegation.HasInclusionProof() {
return false, types.NewValidationFailedError(
fmt.Errorf("inclusion proof already received for BTC delegation: %s", event.StakingTxHash),
)
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Str("currentState", delegation.State.String()).
Str("newState", event.NewState).
Msg("Ignoring EventBTCDelegationInclusionProofReceived because inclusion proof already received")
return false, nil
}

return true, nil
Expand Down
36 changes: 20 additions & 16 deletions internal/services/finality-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
bbntypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
abcitypes "github.com/cometbft/cometbft/abci/types"
"github.com/rs/zerolog/log"
)

const (
Expand All @@ -28,21 +29,24 @@ func (s *Service) processNewFinalityProviderEvent(
return err
}

if err := s.validateFinalityProviderCreatedEvent(newFinalityProvider); err != nil {
return err
if validationErr := s.validateFinalityProviderCreatedEvent(newFinalityProvider); validationErr != nil {
return validationErr
}

if err := s.db.SaveNewFinalityProvider(
if dbErr := s.db.SaveNewFinalityProvider(
ctx, model.FromEventFinalityProviderCreated(newFinalityProvider),
); err != nil {
if db.IsDuplicateKeyError(err) {
); dbErr != nil {
if db.IsDuplicateKeyError(dbErr) {
// Finality provider already exists, ignore the event
log.Debug().
Str("btcPk", newFinalityProvider.BtcPkHex).
Msg("Ignoring EventFinalityProviderCreated because finality provider already exists")
return nil
}
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to save new finality provider: %w", err),
fmt.Errorf("failed to save new finality provider: %w", dbErr),
)
}

Expand All @@ -59,17 +63,17 @@ func (s *Service) processFinalityProviderEditedEvent(
return err
}

if err := s.validateFinalityProviderEditedEvent(finalityProviderEdited); err != nil {
return err
if validationErr := s.validateFinalityProviderEditedEvent(finalityProviderEdited); validationErr != nil {
return validationErr
}

if err := s.db.UpdateFinalityProviderDetailsFromEvent(
if dbErr := s.db.UpdateFinalityProviderDetailsFromEvent(
ctx, model.FromEventFinalityProviderEdited(finalityProviderEdited),
); err != nil {
); dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update finality provider details: %w", err),
fmt.Errorf("failed to update finality provider details: %w", dbErr),
)
}

Expand All @@ -86,18 +90,18 @@ func (s *Service) processFinalityProviderStateChangeEvent(
return err
}

if err := s.validateFinalityProviderStateChangeEvent(ctx, finalityProviderStateChange); err != nil {
return err
if validationErr := s.validateFinalityProviderStateChangeEvent(ctx, finalityProviderStateChange); validationErr != nil {
return validationErr
}

// If all validations pass, update the finality provider state
if err := s.db.UpdateFinalityProviderState(
if dbErr := s.db.UpdateFinalityProviderState(
ctx, finalityProviderStateChange.BtcPk, finalityProviderStateChange.NewState,
); err != nil {
); dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update finality provider state: %w", err),
fmt.Errorf("failed to update finality provider state: %w", dbErr),
)
}
return nil
Expand Down

0 comments on commit d7b53df

Please sign in to comment.