From f5d01eaea94381873058df6660455a4ec9ec9d2b Mon Sep 17 00:00:00 2001 From: shandongzhejiang Date: Wed, 25 Jun 2025 14:51:13 +0800 Subject: [PATCH] refactor: use slices.Contains to simplify code Signed-off-by: shandongzhejiang --- dot/network/authoritydiscovery/service.go | 7 +++---- dot/rpc/modules/rpc.go | 9 ++------- dot/state/block_test.go | 12 ++---------- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/dot/network/authoritydiscovery/service.go b/dot/network/authoritydiscovery/service.go index 57fc42384c..16b5a7ecde 100644 --- a/dot/network/authoritydiscovery/service.go +++ b/dot/network/authoritydiscovery/service.go @@ -6,6 +6,7 @@ package authoritydiscovery import ( "context" "errors" + "slices" "sync" "github.com/libp2p/go-libp2p/core/peer" @@ -293,10 +294,8 @@ func appendUniqueAuthorityID(slice []AuthorityID, auth AuthorityID) []AuthorityI } func appendUniquePeerID(slice []peer.ID, peerID peer.ID) []peer.ID { - for _, existing := range slice { - if existing == peerID { - return slice - } + if slices.Contains(slice, peerID) { + return slice } return append(slice, peerID) } diff --git a/dot/rpc/modules/rpc.go b/dot/rpc/modules/rpc.go index 9170d76e06..fa7982c544 100644 --- a/dot/rpc/modules/rpc.go +++ b/dot/rpc/modules/rpc.go @@ -5,6 +5,7 @@ package modules import ( "net/http" + "slices" ) var ( @@ -55,11 +56,5 @@ func (rm *RPCModule) Methods(r *http.Request, req *EmptyRequest, res *MethodsRes // IsUnsafe returns true if the `name` has the suffix func IsUnsafe(name string) bool { - for _, unsafe := range UnsafeMethods { - if name == unsafe { - return true - } - } - - return false + return slices.Contains(UnsafeMethods, name) } diff --git a/dot/state/block_test.go b/dot/state/block_test.go index af8b6a53a1..8558fdf3dc 100644 --- a/dot/state/block_test.go +++ b/dot/state/block_test.go @@ -5,6 +5,7 @@ package state import ( "errors" + "slices" "testing" "time" @@ -460,22 +461,13 @@ func TestFinalization_DeleteBlock(t *testing.T) { after := bs.bt.GetAllBlocks() - isIn := func(arr []common.Hash, b common.Hash) bool { - for _, a := range arr { - if b == a { - return true - } - } - return false - } - // assert that every block except finalised has been deleted for _, b := range before { if b == fin { continue } - if isIn(after, b) { + if slices.Contains(after, b) { continue }