From c9c26f5888164c88aebdb5abeed8f6162c8ac265 Mon Sep 17 00:00:00 2001 From: Kirill Date: Fri, 17 Jan 2025 12:40:44 +0400 Subject: [PATCH 1/2] Replace utils.Contains() with slices.Contains() from stdlib --- internal/services/events.go | 10 +++++----- internal/services/watch_btc_events.go | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/internal/services/events.go b/internal/services/events.go index 608a8dd..a89c1f5 100644 --- a/internal/services/events.go +++ b/internal/services/events.go @@ -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 @@ -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). @@ -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). @@ -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). @@ -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). diff --git a/internal/services/watch_btc_events.go b/internal/services/watch_btc_events.go index 37e2e90..d3f681e 100644 --- a/internal/services/watch_btc_events.go +++ b/internal/services/watch_btc_events.go @@ -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( @@ -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). @@ -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). From 32a6880d6cdce527e4f02d9d5eee82bdffb04127 Mon Sep 17 00:00:00 2001 From: Kirill Date: Fri, 17 Jan 2025 12:41:09 +0400 Subject: [PATCH 2/2] Remove utils.Contains() --- internal/utils/utils.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 69f9560..cf559c6 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -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 {