Skip to content

Commit

Permalink
Replace utils.Contains with slices.Contains (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored Jan 17, 2025
1 parent e7fa5d5 commit d21713e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
10 changes: 5 additions & 5 deletions internal/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"time"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
ftypes "github.com/babylonlabs-io/babylon/x/finality/types"
abcitypes "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
proto "github.com/cosmos/gogoproto/proto"
"github.com/rs/zerolog/log"
"slices"
)

type EventTypes string
Expand Down Expand Up @@ -174,7 +174,7 @@ func (s *Service) validateCovenantQuorumReachedEvent(ctx context.Context, event
}

// Check if the current state is qualified for the transition
if !utils.Contains(qualifiedStates, delegation.State) {
if !slices.Contains(qualifiedStates, delegation.State) {
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Stringer("currentState", delegation.State).
Expand Down Expand Up @@ -232,7 +232,7 @@ func (s *Service) validateBTCDelegationInclusionProofReceivedEvent(ctx context.C
}

// Check if the current state is qualified for the transition
if !utils.Contains(qualifiedStates, delegation.State) {
if !slices.Contains(qualifiedStates, delegation.State) {
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Stringer("currentState", delegation.State).
Expand Down Expand Up @@ -273,7 +273,7 @@ func (s *Service) validateBTCDelegationUnbondedEarlyEvent(ctx context.Context, e
}

// Check if the current state is qualified for the transition
if !utils.Contains(types.QualifiedStatesForUnbondedEarly(), delegation.State) {
if !slices.Contains(types.QualifiedStatesForUnbondedEarly(), delegation.State) {
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Stringer("currentState", delegation.State).
Expand Down Expand Up @@ -302,7 +302,7 @@ func (s *Service) validateBTCDelegationExpiredEvent(ctx context.Context, event *
}

// Check if the current state is qualified for the transition
if !utils.Contains(types.QualifiedStatesForExpired(), delegation.State) {
if !slices.Contains(types.QualifiedStatesForExpired(), delegation.State) {
log.Debug().
Str("stakingTxHashHex", event.StakingTxHash).
Stringer("currentState", delegation.State).
Expand Down
5 changes: 3 additions & 2 deletions internal/services/watch_btc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/btcsuite/btcd/wire"
notifier "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/rs/zerolog/log"
"slices"
)

func (s *Service) watchForSpendStakingTx(
Expand Down Expand Up @@ -120,7 +121,7 @@ func (s *Service) watchForSpendSlashingChange(
}

qualifiedStates := types.QualifiedStatesForWithdrawn()
if qualifiedStates == nil || !utils.Contains(qualifiedStates, *delegationState) {
if qualifiedStates == nil || !slices.Contains(qualifiedStates, *delegationState) {
log.Error().
Str("staking_tx", delegation.StakingTxHashHex).
Stringer("state", delegationState).
Expand Down Expand Up @@ -379,7 +380,7 @@ func (s *Service) handleWithdrawal(
}

qualifiedStates := types.QualifiedStatesForWithdrawn()
if qualifiedStates == nil || !utils.Contains(qualifiedStates, *delegationState) {
if qualifiedStates == nil || !slices.Contains(qualifiedStates, *delegationState) {
log.Error().
Str("staking_tx", delegation.StakingTxHashHex).
Stringer("current_state", delegationState).
Expand Down
10 changes: 0 additions & 10 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ func SafeUnescape(s string) string {
return unquoted
}

// Contains checks if a slice contains a specific element
func Contains[T comparable](slice []T, item T) bool {
for _, elem := range slice {
if elem == item {
return true
}
}
return false
}

func GetTxHash(txBytes []byte) (chainhash.Hash, error) {
var msgTx wire.MsgTx
if err := msgTx.Deserialize(bytes.NewReader(txBytes)); err != nil {
Expand Down

0 comments on commit d21713e

Please sign in to comment.