all: use slices.Contains (#29459)

Co-authored-by: Felix Lange <fjl@twurst.com>
pull/29474/head
Aaron Chen 6 months ago committed by GitHub
parent cc348a601e
commit 74995bf8a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      cmd/devp2p/internal/v5test/discv5tests.go
  2. 9
      cmd/devp2p/internal/v5test/framework.go
  3. 15
      eth/filters/filter.go
  4. 13
      node/node.go
  5. 3
      node/node_test.go

@ -19,6 +19,7 @@ package v5test
import ( import (
"bytes" "bytes"
"net" "net"
"slices"
"sync" "sync"
"time" "time"
@ -266,7 +267,7 @@ func (s *Suite) TestFindnodeResults(t *utesting.T) {
n := bn.conn.localNode.Node() n := bn.conn.localNode.Node()
expect[n.ID()] = n expect[n.ID()] = n
d := uint(enode.LogDist(n.ID(), s.Dest.ID())) d := uint(enode.LogDist(n.ID(), s.Dest.ID()))
if !containsUint(dists, d) { if !slices.Contains(dists, d) {
dists = append(dists, d) dists = append(dists, d)
} }
} }

@ -252,12 +252,3 @@ func checkRecords(records []*enr.Record) ([]*enode.Node, error) {
} }
return nodes, nil return nodes, nil
} }
func containsUint(ints []uint, x uint) bool {
for i := range ints {
if ints[i] == x {
return true
}
}
return false
}

@ -20,6 +20,7 @@ import (
"context" "context"
"errors" "errors"
"math/big" "math/big"
"slices"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/bloombits" "github.com/ethereum/go-ethereum/core/bloombits"
@ -347,16 +348,6 @@ func (f *Filter) pendingLogs() []*types.Log {
return nil return nil
} }
// includes returns true if the element is present in the list.
func includes[T comparable](things []T, element T) bool {
for _, thing := range things {
if thing == element {
return true
}
}
return false
}
// filterLogs creates a slice of logs matching the given criteria. // filterLogs creates a slice of logs matching the given criteria.
func filterLogs(logs []*types.Log, fromBlock, toBlock *big.Int, addresses []common.Address, topics [][]common.Hash) []*types.Log { func filterLogs(logs []*types.Log, fromBlock, toBlock *big.Int, addresses []common.Address, topics [][]common.Hash) []*types.Log {
var check = func(log *types.Log) bool { var check = func(log *types.Log) bool {
@ -366,7 +357,7 @@ func filterLogs(logs []*types.Log, fromBlock, toBlock *big.Int, addresses []comm
if toBlock != nil && toBlock.Int64() >= 0 && toBlock.Uint64() < log.BlockNumber { if toBlock != nil && toBlock.Int64() >= 0 && toBlock.Uint64() < log.BlockNumber {
return false return false
} }
if len(addresses) > 0 && !includes(addresses, log.Address) { if len(addresses) > 0 && !slices.Contains(addresses, log.Address) {
return false return false
} }
// If the to filtered topics is greater than the amount of topics in logs, skip. // 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
if len(sub) == 0 { if len(sub) == 0 {
continue // empty rule set == wildcard continue // empty rule set == wildcard
} }
if !includes(sub, log.Topics[i]) { if !slices.Contains(sub, log.Topics[i]) {
return false return false
} }
} }

@ -25,6 +25,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"slices"
"strings" "strings"
"sync" "sync"
@ -278,16 +279,6 @@ func (n *Node) openEndpoints() error {
return err return err
} }
// containsLifecycle checks if 'lfs' contains 'l'.
func containsLifecycle(lfs []Lifecycle, l Lifecycle) bool {
for _, obj := range lfs {
if obj == l {
return true
}
}
return false
}
// stopServices terminates running services, RPC and p2p networking. // stopServices terminates running services, RPC and p2p networking.
// It is the inverse of Start. // It is the inverse of Start.
func (n *Node) stopServices(running []Lifecycle) error { func (n *Node) stopServices(running []Lifecycle) error {
@ -571,7 +562,7 @@ func (n *Node) RegisterLifecycle(lifecycle Lifecycle) {
if n.state != initializingState { if n.state != initializingState {
panic("can't register lifecycle on running/stopped node") panic("can't register lifecycle on running/stopped node")
} }
if containsLifecycle(n.lifecycles, lifecycle) { if slices.Contains(n.lifecycles, lifecycle) {
panic(fmt.Sprintf("attempt to register lifecycle %T more than once", lifecycle)) panic(fmt.Sprintf("attempt to register lifecycle %T more than once", lifecycle))
} }
n.lifecycles = append(n.lifecycles, lifecycle) n.lifecycles = append(n.lifecycles, lifecycle)

@ -23,6 +23,7 @@ import (
"net" "net"
"net/http" "net/http"
"reflect" "reflect"
"slices"
"strings" "strings"
"testing" "testing"
@ -116,7 +117,7 @@ func TestLifecycleRegistry_Successful(t *testing.T) {
noop := NewNoop() noop := NewNoop()
stack.RegisterLifecycle(noop) stack.RegisterLifecycle(noop)
if !containsLifecycle(stack.lifecycles, noop) { if !slices.Contains(stack.lifecycles, Lifecycle(noop)) {
t.Fatalf("lifecycle was not properly registered on the node, %v", err) t.Fatalf("lifecycle was not properly registered on the node, %v", err)
} }
} }

Loading…
Cancel
Save