Skip to content

Commit 74995bf

Browse files
AaronChen0fjl
andauthored
all: use slices.Contains (#29459)
Co-authored-by: Felix Lange <[email protected]>
1 parent cc348a6 commit 74995bf

File tree

5 files changed

+9
-34
lines changed

5 files changed

+9
-34
lines changed

cmd/devp2p/internal/v5test/discv5tests.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v5test
1919
import (
2020
"bytes"
2121
"net"
22+
"slices"
2223
"sync"
2324
"time"
2425

@@ -266,7 +267,7 @@ func (s *Suite) TestFindnodeResults(t *utesting.T) {
266267
n := bn.conn.localNode.Node()
267268
expect[n.ID()] = n
268269
d := uint(enode.LogDist(n.ID(), s.Dest.ID()))
269-
if !containsUint(dists, d) {
270+
if !slices.Contains(dists, d) {
270271
dists = append(dists, d)
271272
}
272273
}

cmd/devp2p/internal/v5test/framework.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,3 @@ func checkRecords(records []*enr.Record) ([]*enode.Node, error) {
252252
}
253253
return nodes, nil
254254
}
255-
256-
func containsUint(ints []uint, x uint) bool {
257-
for i := range ints {
258-
if ints[i] == x {
259-
return true
260-
}
261-
}
262-
return false
263-
}

eth/filters/filter.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"math/big"
23+
"slices"
2324

2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/core/bloombits"
@@ -347,16 +348,6 @@ func (f *Filter) pendingLogs() []*types.Log {
347348
return nil
348349
}
349350

350-
// includes returns true if the element is present in the list.
351-
func includes[T comparable](things []T, element T) bool {
352-
for _, thing := range things {
353-
if thing == element {
354-
return true
355-
}
356-
}
357-
return false
358-
}
359-
360351
// filterLogs creates a slice of logs matching the given criteria.
361352
func filterLogs(logs []*types.Log, fromBlock, toBlock *big.Int, addresses []common.Address, topics [][]common.Hash) []*types.Log {
362353
var check = func(log *types.Log) bool {
@@ -366,7 +357,7 @@ func filterLogs(logs []*types.Log, fromBlock, toBlock *big.Int, addresses []comm
366357
if toBlock != nil && toBlock.Int64() >= 0 && toBlock.Uint64() < log.BlockNumber {
367358
return false
368359
}
369-
if len(addresses) > 0 && !includes(addresses, log.Address) {
360+
if len(addresses) > 0 && !slices.Contains(addresses, log.Address) {
370361
return false
371362
}
372363
// If the to filtered topics is greater than the amount of topics in logs, skip.
@@ -377,7 +368,7 @@ func filterLogs(logs []*types.Log, fromBlock, toBlock *big.Int, addresses []comm
377368
if len(sub) == 0 {
378369
continue // empty rule set == wildcard
379370
}
380-
if !includes(sub, log.Topics[i]) {
371+
if !slices.Contains(sub, log.Topics[i]) {
381372
return false
382373
}
383374
}

node/node.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"path/filepath"
2727
"reflect"
28+
"slices"
2829
"strings"
2930
"sync"
3031

@@ -278,16 +279,6 @@ func (n *Node) openEndpoints() error {
278279
return err
279280
}
280281

281-
// containsLifecycle checks if 'lfs' contains 'l'.
282-
func containsLifecycle(lfs []Lifecycle, l Lifecycle) bool {
283-
for _, obj := range lfs {
284-
if obj == l {
285-
return true
286-
}
287-
}
288-
return false
289-
}
290-
291282
// stopServices terminates running services, RPC and p2p networking.
292283
// It is the inverse of Start.
293284
func (n *Node) stopServices(running []Lifecycle) error {
@@ -571,7 +562,7 @@ func (n *Node) RegisterLifecycle(lifecycle Lifecycle) {
571562
if n.state != initializingState {
572563
panic("can't register lifecycle on running/stopped node")
573564
}
574-
if containsLifecycle(n.lifecycles, lifecycle) {
565+
if slices.Contains(n.lifecycles, lifecycle) {
575566
panic(fmt.Sprintf("attempt to register lifecycle %T more than once", lifecycle))
576567
}
577568
n.lifecycles = append(n.lifecycles, lifecycle)

node/node_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
"net/http"
2525
"reflect"
26+
"slices"
2627
"strings"
2728
"testing"
2829

@@ -116,7 +117,7 @@ func TestLifecycleRegistry_Successful(t *testing.T) {
116117
noop := NewNoop()
117118
stack.RegisterLifecycle(noop)
118119

119-
if !containsLifecycle(stack.lifecycles, noop) {
120+
if !slices.Contains(stack.lifecycles, Lifecycle(noop)) {
120121
t.Fatalf("lifecycle was not properly registered on the node, %v", err)
121122
}
122123
}

0 commit comments

Comments
 (0)