Skip to content

Commit

Permalink
fix panic (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 authored Nov 14, 2024
1 parent 421becc commit ea561a3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 53 deletions.
30 changes: 26 additions & 4 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,23 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
func (s *Service) processBTCDelegationUnbondedEarlyEvent(
ctx context.Context, event abcitypes.Event,
) *types.Error {
// Parse and validate event
unbondedEarlyEvent, err := s.parseAndValidateUnbondedEarlyEvent(ctx, event)
unbondedEarlyEvent, err := parseEvent[*bbntypes.EventBTCDelgationUnbondedEarly](
EventBTCDelgationUnbondedEarly,
event,
)
if err != nil {
return err
}

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

// Get delegation details
delegation, err := s.getDelegationDetails(ctx, unbondedEarlyEvent.StakingTxHash)
if err != nil {
Expand Down Expand Up @@ -193,12 +204,23 @@ func (s *Service) processBTCDelegationUnbondedEarlyEvent(
func (s *Service) processBTCDelegationExpiredEvent(
ctx context.Context, event abcitypes.Event,
) *types.Error {
// Parse and validate event
expiredEvent, err := s.parseAndValidateExpiredEvent(ctx, event)
expiredEvent, err := parseEvent[*bbntypes.EventBTCDelegationExpired](
EventBTCDelegationExpired,
event,
)
if err != nil {
return err
}

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

// Get delegation details
delegation, err := s.getDelegationDetails(ctx, expiredEvent.StakingTxHash)
if err != nil {
Expand Down
49 changes: 0 additions & 49 deletions internal/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"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"
sdk "github.com/cosmos/cosmos-sdk/types"
proto "github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -135,51 +134,3 @@ func parseEvent[T proto.Message](

return concreteMsg, nil
}

func (s *Service) parseAndValidateUnbondedEarlyEvent(
ctx context.Context,
event abcitypes.Event,
) (*bbntypes.EventBTCDelgationUnbondedEarly, *types.Error) {
// Parse event
unbondedEarlyEvent, err := parseEvent[*bbntypes.EventBTCDelgationUnbondedEarly](
EventBTCDelgationUnbondedEarly,
event,
)
if err != nil {
return nil, err
}

// Validate event
proceed, err := s.validateBTCDelegationUnbondedEarlyEvent(ctx, unbondedEarlyEvent)
if err != nil {
return nil, err
}
if !proceed {
return nil, nil
}

return unbondedEarlyEvent, nil
}

func (s *Service) parseAndValidateExpiredEvent(
ctx context.Context,
event abcitypes.Event,
) (*bbntypes.EventBTCDelegationExpired, *types.Error) {
expiredEvent, err := parseEvent[*bbntypes.EventBTCDelegationExpired](
EventBTCDelegationExpired,
event,
)
if err != nil {
return nil, err
}

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

return expiredEvent, nil
}

0 comments on commit ea561a3

Please sign in to comment.