Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type AutoNATConfig struct {

type Security struct {
ID protocol.ID
Constructor interface{}
Constructor any
}

// Config describes a set of settings for a libp2p node
Expand Down
2 changes: 1 addition & 1 deletion core/discovery/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Options struct {
Limit int

// Other (implementation-specific) options
Other map[interface{}]interface{}
Other map[any]any
}

// Apply applies the given options to this DiscoveryOpts
Expand Down
14 changes: 7 additions & 7 deletions core/event/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
)

// SubscriptionOpt represents a subscriber option. Use the options exposed by the implementation of choice.
type SubscriptionOpt = func(interface{}) error
type SubscriptionOpt = func(any) error

// EmitterOpt represents an emitter option. Use the options exposed by the implementation of choice.
type EmitterOpt = func(interface{}) error
type EmitterOpt = func(any) error

// CancelFunc closes a subscriber.
type CancelFunc = func()

// wildcardSubscriptionType is a virtual type to represent wildcard
// subscriptions.
type wildcardSubscriptionType interface{}
type wildcardSubscriptionType any

// WildcardSubscription is the type to subscribe to receive all events
// emitted in the eventbus.
Expand All @@ -30,15 +30,15 @@ type Emitter interface {
// calls to Emit will block.
//
// Calling this function with wrong event type will cause a panic.
Emit(evt interface{}) error
Emit(evt any) error
}

// Subscription represents a subscription to one or multiple event types.
type Subscription interface {
io.Closer

// Out returns the channel from which to consume events.
Out() <-chan interface{}
Out() <-chan any

// Name returns the name for the subscription
Name() string
Expand Down Expand Up @@ -79,7 +79,7 @@ type Bus interface {
// [...]
// }
// }
Subscribe(eventType interface{}, opts ...SubscriptionOpt) (Subscription, error)
Subscribe(eventType any, opts ...SubscriptionOpt) (Subscription, error)

// Emitter creates a new event emitter.
//
Expand All @@ -89,7 +89,7 @@ type Bus interface {
// em, err := eventbus.Emitter(new(EventT))
// defer em.Close() // MUST call this after being done with the emitter
// em.Emit(EventT{})
Emitter(eventType interface{}, opts ...EmitterOpt) (Emitter, error)
Emitter(eventType any, opts ...EmitterOpt) (Emitter, error)

// GetAllEventTypes returns all the event types that this bus knows about
// (having emitters and subscribers). It omits the WildcardSubscription.
Expand Down
2 changes: 1 addition & 1 deletion core/internal/catch/catch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var panicWriter io.Writer = os.Stderr

// HandlePanic handles and logs panics.
func HandlePanic(rerr interface{}, err *error, where string) {
func HandlePanic(rerr any, err *error, where string) {
if rerr != nil {
fmt.Fprintf(panicWriter, "caught panic: %s\n%s\n", rerr, debug.Stack())
*err = fmt.Errorf("panic in %s: %s", where, rerr)
Expand Down
16 changes: 8 additions & 8 deletions core/metrics/bandwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func round(bwc *BandwidthCounter, b *testing.B) {
start := make(chan struct{})
var wg sync.WaitGroup
wg.Add(10000)
for i := 0; i < 1000; i++ {
for i := range 1000 {
p := peer.ID(fmt.Sprintf("peer-%d", i))
for j := 0; j < 10; j++ {
for j := range 10 {
proto := protocol.ID(fmt.Sprintf("bitswap-%d", j))
go func() {
defer wg.Done()
<-start

for i := 0; i < 1000; i++ {
for range 1000 {
bwc.LogSentMessage(100)
bwc.LogSentMessageStream(100, proto, p)
time.Sleep(1 * time.Millisecond)
Expand All @@ -60,10 +60,10 @@ func round(bwc *BandwidthCounter, b *testing.B) {

func TestBandwidthCounter(t *testing.T) {
bwc := NewBandwidthCounter()
for i := 0; i < 40; i++ {
for i := 0; i < 100; i++ {
for range 40 {
for i := range 100 {
p := peer.ID(fmt.Sprintf("peer-%d", i))
for j := 0; j < 2; j++ {
for j := range 2 {
proto := protocol.ID(fmt.Sprintf("proto-%d", j))

// make sure the bandwidth counters are active
Expand All @@ -81,7 +81,7 @@ func TestBandwidthCounter(t *testing.T) {
assertProtocols := func(check func(Stats)) {
byProtocol := bwc.GetBandwidthByProtocol()
require.Len(t, byProtocol, 2, "expected 2 protocols")
for i := 0; i < 2; i++ {
for i := range 2 {
p := protocol.ID(fmt.Sprintf("proto-%d", i))
for _, stats := range [...]Stats{bwc.GetBandwidthForProtocol(p), byProtocol[p]} {
check(stats)
Expand All @@ -92,7 +92,7 @@ func TestBandwidthCounter(t *testing.T) {
assertPeers := func(check func(Stats)) {
byPeer := bwc.GetBandwidthByPeer()
require.Len(t, byPeer, 100, "expected 100 peers")
for i := 0; i < 100; i++ {
for i := range 100 {
p := peer.ID(fmt.Sprintf("peer-%d", i))
for _, stats := range [...]Stats{bwc.GetBandwidthForPeer(p), byPeer[p]} {
check(stats)
Expand Down
2 changes: 1 addition & 1 deletion core/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type Stats struct {
// relay.
Limited bool
// Extra stores additional metadata about this connection.
Extra map[interface{}]interface{}
Extra map[any]any
}

// StreamHandler is the type of function used to listen for
Expand Down
4 changes: 2 additions & 2 deletions core/peer/addrinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) {
return addrs, nil
}

func (pi *AddrInfo) Loggable() map[string]interface{} {
return map[string]interface{}{
func (pi *AddrInfo) Loggable() map[string]any {
return map[string]any{
"peerID": pi.ID.String(),
"addrs": pi.Addrs,
}
Expand Down
4 changes: 2 additions & 2 deletions core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const maxInlineKeyLength = 42
type ID string

// Loggable returns a pretty peer ID string in loggable JSON format.
func (id ID) Loggable() map[string]interface{} {
return map[string]interface{}{
func (id ID) Loggable() map[string]any {
return map[string]any{
"peerID": id.String(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/peer/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestSignedPeerRecordFromEnvelope(t *testing.T) {
// low clock precision. This makes sure we never get a duplicate.
func TestTimestampSeq(t *testing.T) {
var last uint64
for i := 0; i < 1000; i++ {
for range 1000 {
next := TimestampSeq()
if next <= last {
t.Errorf("non-increasing timestamp found: %d <= %d", next, last)
Expand Down
4 changes: 2 additions & 2 deletions core/peerstore/peerstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ type PeerMetadata interface {
// Get / Put is a simple registry for other peer-related key/value pairs.
// If we find something we use often, it should become its own set of
// methods. This is a last resort.
Get(p peer.ID, key string) (interface{}, error)
Put(p peer.ID, key string, val interface{}) error
Get(p peer.ID, key string) (any, error)
Put(p peer.ID, key string, val any) error

// RemovePeer removes all values stored for a peer.
RemovePeer(peer.ID)
Expand Down
6 changes: 3 additions & 3 deletions core/pnet/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func testDecodeBad(t *testing.T, windows bool) {

func testDecodeHex(t *testing.T, windows bool) {
b := bufWithBase("/base16/", windows)
for i := 0; i < 32; i++ {
for range 32 {
b.WriteString("FF")
}

Expand All @@ -67,7 +67,7 @@ func TestDecodeB64(t *testing.T) {
func testDecodeB64(t *testing.T, windows bool) {
b := bufWithBase("/base64/", windows)
key := make([]byte, 32)
for i := 0; i < 32; i++ {
for i := range 32 {
key[i] = byte(i)
}

Expand Down Expand Up @@ -102,7 +102,7 @@ func TestDecodeBin(t *testing.T) {
func testDecodeBin(t *testing.T, windows bool) {
b := bufWithBase("/bin/", windows)
key := make([]byte, 32)
for i := 0; i < 32; i++ {
for i := range 32 {
key[i] = byte(i)
}

Expand Down
4 changes: 2 additions & 2 deletions core/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func blankRecordForPayloadType(payloadType []byte) (Record, error) {
return asRecord, nil
}

func getValueType(i interface{}) reflect.Type {
func getValueType(i any) reflect.Type {
valueType := reflect.TypeOf(i)
if valueType.Kind() == reflect.Ptr {
if valueType.Kind() == reflect.Pointer {
valueType = valueType.Elem()
}
return valueType
Expand Down
10 changes: 5 additions & 5 deletions core/routing/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package routing

import "maps"

// Option is a single routing option.
type Option func(opts *Options) error

Expand All @@ -9,7 +11,7 @@ type Options struct {
Expired bool
Offline bool
// Other (ValueStore implementation specific) options.
Other map[interface{}]interface{}
Other map[any]any
}

// Apply applies the given options to this Options
Expand All @@ -27,10 +29,8 @@ func (opts *Options) ToOption() Option {
return func(nopts *Options) error {
*nopts = *opts
if opts.Other != nil {
nopts.Other = make(map[interface{}]interface{}, len(opts.Other))
for k, v := range opts.Other {
nopts.Other[k] = v
}
nopts.Other = make(map[any]any, len(opts.Other))
maps.Copy(nopts.Other, opts.Other)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion core/routing/query_serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (qe *QueryEvent) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
return json.Marshal(map[string]any{
"ID": qe.ID.String(),
"Type": int(qe.Type),
"Responses": qe.Responses,
Expand Down
2 changes: 1 addition & 1 deletion core/routing/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestEventsCancel(t *testing.T) {
wg.Add(2)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
for i := range 100 {
PublishQueryEvent(ctx, &QueryEvent{Extra: fmt.Sprint(i)})
}
close(goch)
Expand Down
12 changes: 3 additions & 9 deletions core/test/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package test

import (
"fmt"
"slices"
"testing"

ma "github.com/multiformats/go-multiaddr"
)

func GenerateTestAddrs(n int) []ma.Multiaddr {
out := make([]ma.Multiaddr, n)
for i := 0; i < n; i++ {
for i := range n {
a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/1.2.3.4/tcp/%d", i))
if err != nil {
continue
Expand All @@ -26,14 +27,7 @@ func AssertAddressesEqual(t *testing.T, exp, act []ma.Multiaddr) {
}

for _, a := range exp {
found := false

for _, b := range act {
if a.Equal(b) {
found = true
break
}
}
found := slices.ContainsFunc(act, a.Equal)

if !found {
t.Fatalf("expected address %s not found", a)
Expand Down
2 changes: 1 addition & 1 deletion gologshim/gologshim.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func parseIPFSGoLogEnv(loggingLevelEnvStr string) (slog.Level, map[string]slog.L
fallbackLvl := slog.LevelError
var systemToLevel map[string]slog.Level
if loggingLevelEnvStr != "" {
for _, kvs := range strings.Split(loggingLevelEnvStr, ",") {
for kvs := range strings.SplitSeq(loggingLevelEnvStr, ",") {
kv := strings.SplitN(kvs, "=", 2)
var lvl slog.Level
err := lvl.UnmarshalText([]byte(kv[len(kv)-1]))
Expand Down
6 changes: 3 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ListenAddrs(addrs ...ma.Multiaddr) Option {
// * Host
// * Network
// * Peerstore
func Security(name string, constructor interface{}) Option {
func Security(name string, constructor any) Option {
return func(cfg *Config) error {
if cfg.Insecure {
return fmt.Errorf("cannot use security transports with an insecure libp2p configuration")
Expand Down Expand Up @@ -99,7 +99,7 @@ func Muxer(name string, muxer network.Multiplexer) Option {
}
}

func QUICReuse(constructor interface{}, opts ...quicreuse.Option) Option {
func QUICReuse(constructor any, opts ...quicreuse.Option) Option {
return func(cfg *Config) error {
tag := `group:"quicreuseopts"`
typ := reflect.ValueOf(constructor).Type()
Expand Down Expand Up @@ -141,7 +141,7 @@ func QUICReuse(constructor interface{}, opts ...quicreuse.Option) Option {
// * Public Key
// * Address filter (filter.Filter)
// * Peerstore
func Transport(constructor interface{}, opts ...interface{}) Option {
func Transport(constructor any, opts ...any) Option {
return func(cfg *Config) error {
// generate a random identifier, so that fx can associate the constructor with its options
b := make([]byte, 8)
Expand Down
10 changes: 5 additions & 5 deletions p2p/discovery/backoff/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func minMaxJitterTest(jitter Jitter, t *testing.T) {

func TestNoJitter(t *testing.T) {
minMaxJitterTest(NoJitter, t)
for i := 0; i < 10; i++ {
for i := range 10 {
expected := time.Second * time.Duration(i)
if calculated := NoJitter(expected, time.Duration(0), time.Second*100, nil); calculated != expected {
t.Fatalf("expected %v, got %v", expected, calculated)
Expand All @@ -106,7 +106,7 @@ func TestFullJitter(t *testing.T) {

histogram := make([]int, numBuckets)

for i := 0; i < (numBuckets-1)*multiplier; i++ {
for range (numBuckets - 1) * multiplier {
started := time.Nanosecond * 50
calculated := FullJitter(started, 0, 100, rng)
histogram[calculated]++
Expand Down Expand Up @@ -148,7 +148,7 @@ func testManyBackoffFactoryHelper(concurrent int, bkf BackoffFactory) {
backoffCh := make(chan BackoffStrategy, concurrent)

errGrp := errgroup.Group{}
for i := 0; i < concurrent; i++ {
for range concurrent {
errGrp.Go(func() (err error) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -174,8 +174,8 @@ func testManyBackoffFactoryHelper(concurrent int, bkf BackoffFactory) {
}
}()

for i := 0; i < 5; i++ {
for j := 0; j < 10; j++ {
for range 5 {
for range 10 {
backoff.Delay()
}
backoff.Reset()
Expand Down
Loading
Loading