From 4b797c708f9aa44aabc41b44ef5c26374bbb6e62 Mon Sep 17 00:00:00 2001 From: nuxtreact Date: Tue, 10 Feb 2026 23:32:40 +0800 Subject: [PATCH] refactor: use slices.Contains to simplify code Signed-off-by: nuxtreact --- app/ante/vesting.go | 16 ++++++++-------- cmd/zetaclientd/utils.go | 7 +++---- cmd/zetae2e/local/monitor_priority_txs.go | 9 ++------- rpc/namespaces/ethereum/eth/filters/utils.go | 11 ++--------- x/crosschain/keeper/cctx.go | 11 +++-------- x/observer/keeper/observer_set.go | 9 +++------ 6 files changed, 21 insertions(+), 42 deletions(-) diff --git a/app/ante/vesting.go b/app/ante/vesting.go index b24f169bbb..d0953898ac 100644 --- a/app/ante/vesting.go +++ b/app/ante/vesting.go @@ -1,6 +1,8 @@ package ante import ( + "slices" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -35,14 +37,12 @@ func (vad VestingAccountDecorator) AnteHandle( for _, msg := range tx.GetMsgs() { typeURL := sdk.MsgTypeURL(msg) - for _, disabledTypeURL := range vad.disabledMsgTypeURLs { - if typeURL == disabledTypeURL { - return ctx, errorsmod.Wrapf( - sdkerrors.ErrUnauthorized, - "MsgTypeURL %s not supported", - typeURL, - ) - } + if slices.Contains(vad.disabledMsgTypeURLs, typeURL) { + return ctx, errorsmod.Wrapf( + sdkerrors.ErrUnauthorized, + "MsgTypeURL %s not supported", + typeURL, + ) } } diff --git a/cmd/zetaclientd/utils.go b/cmd/zetaclientd/utils.go index 60cb7c9361..9617f123f0 100644 --- a/cmd/zetaclientd/utils.go +++ b/cmd/zetaclientd/utils.go @@ -3,6 +3,7 @@ package main import ( "context" "os" + "slices" "strconv" "github.com/pkg/errors" @@ -22,10 +23,8 @@ func isObserverNode(ctx context.Context, zc *zetacore.Client) (bool, error) { operatorAddress := zc.GetKeys().GetOperatorAddress().String() - for _, observer := range observers { - if observer == operatorAddress { - return true, nil - } + if slices.Contains(observers, operatorAddress) { + return true, nil } return false, nil diff --git a/cmd/zetae2e/local/monitor_priority_txs.go b/cmd/zetae2e/local/monitor_priority_txs.go index 4cede7f5c8..6c8d8975d6 100644 --- a/cmd/zetae2e/local/monitor_priority_txs.go +++ b/cmd/zetae2e/local/monitor_priority_txs.go @@ -3,6 +3,7 @@ package local import ( "context" "errors" + "slices" "strings" "time" @@ -102,13 +103,7 @@ func isMsgTypeURLSystemTx(attr types.EventAttribute) bool { "\"/zetachain.zetacore.observer.MsgVoteBlame\"", } - for _, url := range systemTxsMsgTypeUrls { - if url == attr.Value { - return true - } - } - - return false + return slices.Contains(systemTxsMsgTypeUrls, attr.Value) } func isActionNonSystemTx(attr types.EventAttribute) bool { diff --git a/rpc/namespaces/ethereum/eth/filters/utils.go b/rpc/namespaces/ethereum/eth/filters/utils.go index e1da4908b1..2b1301bc5f 100644 --- a/rpc/namespaces/ethereum/eth/filters/utils.go +++ b/rpc/namespaces/ethereum/eth/filters/utils.go @@ -2,10 +2,10 @@ package filters import ( "math/big" + "slices" "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" - "golang.org/x/exp/slices" ) // FilterLogs creates a slice of logs matching the given criteria. @@ -37,14 +37,7 @@ Logs: continue } for i, sub := range topics { - match := len(sub) == 0 // empty rule set == wildcard - for _, topic := range sub { - if log.Topics[i] == topic { - match = true - break - } - } - if !match { + if !slices.Contains(sub, log.Topics[i]) { continue Logs } } diff --git a/x/crosschain/keeper/cctx.go b/x/crosschain/keeper/cctx.go index 7671bee488..5d6f482dfd 100644 --- a/x/crosschain/keeper/cctx.go +++ b/x/crosschain/keeper/cctx.go @@ -1,6 +1,8 @@ package keeper import ( + "slices" + "cosmossdk.io/store/prefix" storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -47,14 +49,7 @@ func (k Keeper) updateInboundHashToCCTX( ) { in, _ := k.GetInboundHashToCctx(ctx, cctx.InboundParams.ObservedHash) in.InboundHash = cctx.InboundParams.ObservedHash - found := false - for _, cctxIndex := range in.CctxIndex { - if cctxIndex == cctx.Index { - found = true - break - } - } - if !found { + if !slices.Contains(in.CctxIndex, cctx.Index) { in.CctxIndex = append(in.CctxIndex, cctx.Index) } k.SetInboundHashToCctx(ctx, in) diff --git a/x/observer/keeper/observer_set.go b/x/observer/keeper/observer_set.go index 792b6d575a..7d9b5714c5 100644 --- a/x/observer/keeper/observer_set.go +++ b/x/observer/keeper/observer_set.go @@ -1,6 +1,8 @@ package keeper import ( + "slices" + "cosmossdk.io/errors" "cosmossdk.io/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,12 +31,7 @@ func (k Keeper) IsAddressPartOfObserverSet(ctx sdk.Context, address string) bool if !found { return false } - for _, addr := range observerSet.ObserverList { - if addr == address { - return true - } - } - return false + return slices.Contains(observerSet.ObserverList, address) } // AddObserverToSet adds an observer to the observer set.It makes sure the updated observer set is valid.