Commit 084fffea authored by vyzo's avatar vyzo

make IsFdConsumingAddr a standalone utility func

parent df0ab8b9
......@@ -122,7 +122,7 @@ func NewSwarm(ctx context.Context, local peer.ID, peers peerstore.Peerstore, bwc
}
s.dsync = newDialSync(s.startDialWorker)
s.limiter = newDialLimiter(s.dialAddr, s.IsFdConsumingAddr)
s.limiter = newDialLimiter(s.dialAddr, isFdConsumingAddr)
s.proc = goprocessctx.WithContext(ctx)
s.ctx = goprocessctx.OnClosingContext(s.proc)
s.backf.init(s.ctx)
......
......@@ -630,7 +630,7 @@ func (s *Swarm) rankAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
if !manet.IsPrivateAddr(a) {
tier |= 0b0010
}
if s.IsFdConsumingAddr(a) {
if isFdConsumingAddr(a) {
tier |= 0b0001
}
......@@ -726,7 +726,7 @@ func (s *Swarm) dialAddr(ctx context.Context, p peer.ID, addr ma.Multiaddr) (tra
// A Non-circuit address which has the TCP/UNIX protocol is deemed FD consuming.
// For a circuit-relay address, we look at the address of the relay server/proxy
// and use the same logic as above to decide.
func (s *Swarm) IsFdConsumingAddr(addr ma.Multiaddr) bool {
func isFdConsumingAddr(addr ma.Multiaddr) bool {
first, _ := ma.SplitFunc(addr, func(c ma.Component) bool {
return c.Protocol().Code == ma.P_CIRCUIT
})
......
......@@ -15,7 +15,6 @@ import (
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/test"
. "github.com/libp2p/go-libp2p-swarm"
. "github.com/libp2p/go-libp2p-swarm/testing"
......@@ -387,53 +386,6 @@ func TestConnectionGating(t *testing.T) {
}
}
func TestIsFdConsuming(t *testing.T) {
tcs := map[string]struct {
addr string
isFdConsuming bool
}{
"tcp": {
addr: "/ip4/127.0.0.1/tcp/20",
isFdConsuming: true,
},
"quic": {
addr: "/ip4/127.0.0.1/udp/0/quic",
isFdConsuming: false,
},
"addr-without-registered-transport": {
addr: "/ip4/127.0.0.1/tcp/20/ws",
isFdConsuming: true,
},
"relay-tcp": {
addr: fmt.Sprintf("/ip4/127.0.0.1/tcp/20/p2p-circuit/p2p/%s", test.RandPeerIDFatal(t)),
isFdConsuming: true,
},
"relay-quic": {
addr: fmt.Sprintf("/ip4/127.0.0.1/udp/20/quic/p2p-circuit/p2p/%s", test.RandPeerIDFatal(t)),
isFdConsuming: false,
},
"relay-without-serveraddr": {
addr: fmt.Sprintf("/p2p-circuit/p2p/%s", test.RandPeerIDFatal(t)),
isFdConsuming: true,
},
"relay-without-registered-transport-server": {
addr: fmt.Sprintf("/ip4/127.0.0.1/tcp/20/ws/p2p-circuit/p2p/%s", test.RandPeerIDFatal(t)),
isFdConsuming: true,
},
}
ctx := context.Background()
sw := GenSwarm(t, ctx)
sk := sw.Peerstore().PrivKey(sw.LocalPeer())
require.NotNil(t, sk)
for name := range tcs {
maddr, err := ma.NewMultiaddr(tcs[name].addr)
require.NoError(t, err, name)
require.Equal(t, tcs[name].isFdConsuming, sw.IsFdConsumingAddr(maddr), name)
}
}
func TestNoDial(t *testing.T) {
ctx := context.Background()
swarms := makeSwarms(ctx, t, 2)
......
package swarm
import (
"fmt"
"testing"
"github.com/libp2p/go-libp2p-core/test"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)
func TestIsFdConsuming(t *testing.T) {
tcs := map[string]struct {
addr string
isFdConsuming bool
}{
"tcp": {
addr: "/ip4/127.0.0.1/tcp/20",
isFdConsuming: true,
},
"quic": {
addr: "/ip4/127.0.0.1/udp/0/quic",
isFdConsuming: false,
},
"addr-without-registered-transport": {
addr: "/ip4/127.0.0.1/tcp/20/ws",
isFdConsuming: true,
},
"relay-tcp": {
addr: fmt.Sprintf("/ip4/127.0.0.1/tcp/20/p2p-circuit/p2p/%s", test.RandPeerIDFatal(t)),
isFdConsuming: true,
},
"relay-quic": {
addr: fmt.Sprintf("/ip4/127.0.0.1/udp/20/quic/p2p-circuit/p2p/%s", test.RandPeerIDFatal(t)),
isFdConsuming: false,
},
"relay-without-serveraddr": {
addr: fmt.Sprintf("/p2p-circuit/p2p/%s", test.RandPeerIDFatal(t)),
isFdConsuming: true,
},
"relay-without-registered-transport-server": {
addr: fmt.Sprintf("/ip4/127.0.0.1/tcp/20/ws/p2p-circuit/p2p/%s", test.RandPeerIDFatal(t)),
isFdConsuming: true,
},
}
for name := range tcs {
maddr, err := ma.NewMultiaddr(tcs[name].addr)
require.NoError(t, err, name)
require.Equal(t, tcs[name].isFdConsuming, isFdConsumingAddr(maddr), name)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment