Skip to content

chore: Update go-multiaddr to v0.15 #3145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 core/peer/addrinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) {
return nil, err
}
if len(pi.Addrs) == 0 {
return []ma.Multiaddr{p2ppart}, nil
return []ma.Multiaddr{p2ppart.Multiaddr()}, nil
}
addrs := make([]ma.Multiaddr, 0, len(pi.Addrs))
for _, addr := range pi.Addrs {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b
github.com/mr-tron/base58 v1.2.0
github.com/multiformats/go-base32 v0.1.0
github.com/multiformats/go-multiaddr v0.14.0
github.com/multiformats/go-multiaddr v0.15.0
github.com/multiformats/go-multiaddr-dns v0.4.1
github.com/multiformats/go-multiaddr-fmt v0.1.0
github.com/multiformats/go-multibase v0.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ github.com/multiformats/go-base32 v0.1.0/go.mod h1:Kj3tFY6zNr+ABYMqeUNeGvkIC/UYg
github.com/multiformats/go-base36 v0.2.0 h1:lFsAbNOGeKtuKozrtBsAkSVhv1p9D0/qedU9rQyccr0=
github.com/multiformats/go-base36 v0.2.0/go.mod h1:qvnKE++v+2MWCfePClUEjE78Z7P2a1UV0xHgWc0hkp4=
github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU55txyt0p4aiWVohjo=
github.com/multiformats/go-multiaddr v0.14.0 h1:bfrHrJhrRuh/NXH5mCnemjpbGjzRw/b+tJFOD41g2tU=
github.com/multiformats/go-multiaddr v0.14.0/go.mod h1:6EkVAxtznq2yC3QT5CM1UTAwG0GTP3EWAIcjHuzQ+r4=
github.com/multiformats/go-multiaddr v0.15.0 h1:zB/HeaI/apcZiTDwhY5YqMvNVl/oQYvs3XySU+qeAVo=
github.com/multiformats/go-multiaddr v0.15.0/go.mod h1:JSVUmXDjsVFiW7RjIFMP7+Ev+h1DTbiJgVeTV/tcmP0=
github.com/multiformats/go-multiaddr-dns v0.4.1 h1:whi/uCLbDS3mSEUMb1MsoT4uzUeZB0N32yzufqS0i5M=
github.com/multiformats/go-multiaddr-dns v0.4.1/go.mod h1:7hfthtB4E4pQwirrz+J0CcDUfbWzTqEzVyYKKIKpgkc=
github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E=
Expand Down
4 changes: 2 additions & 2 deletions p2p/host/autonat/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func (as *autoNATService) handleDial(p peer.ID, obsaddr ma.Multiaddr, mpi *pb.Me
default:
continue
}
addr = hostIP
if rest != nil {
addr = hostIP.Multiaddr()
if len(rest) > 0 {
addr = addr.Encapsulate(rest)
}
}
Expand Down
14 changes: 8 additions & 6 deletions p2p/host/autorelay/addrsplosion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
matest "github.com/multiformats/go-multiaddr/matest"
)

func TestCleanupAddrs(t *testing.T) {
Expand All @@ -21,7 +21,7 @@ func TestCleanupAddrs(t *testing.T) {
"/ip4/1.2.3.4/udp/4002/quic-v1",
"/dnsaddr/somedomain.com/tcp/4002/ws",
)
require.ElementsMatch(t, clean, cleanupAddressSet(addrs), "cleaned up set doesn't match expected")
matest.AssertMultiaddrsMatch(t, clean, cleanupAddressSet(addrs))
})

t.Run("with default port", func(t *testing.T) {
Expand All @@ -38,7 +38,7 @@ func TestCleanupAddrs(t *testing.T) {
"/ip4/1.2.3.4/tcp/4001",
"/ip4/1.2.3.4/udp/4002/quic-v1",
)
require.ElementsMatch(t, clean, cleanupAddressSet(addrs), "cleaned up set doesn't match expected")
matest.AssertMultiaddrsMatch(t, clean, cleanupAddressSet(addrs))
})

t.Run("with default port, but no private addrs", func(t *testing.T) {
Expand All @@ -54,7 +54,7 @@ func TestCleanupAddrs(t *testing.T) {
"/ip4/1.2.3.4/tcp/4001",
"/ip4/1.2.3.4/udp/4002/quic-v1",
)
require.ElementsMatch(t, clean, cleanupAddressSet(addrs), "cleaned up set doesn't match expected")
matest.AssertMultiaddrsMatch(t, clean, cleanupAddressSet(addrs))
})

t.Run("with non-standard port", func(t *testing.T) {
Expand All @@ -68,7 +68,9 @@ func TestCleanupAddrs(t *testing.T) {
clean := makeAddrList(
"/ip4/1.2.3.4/tcp/12345",
)
require.ElementsMatch(t, clean, cleanupAddressSet(addrs), "cleaned up set doesn't match expected")
if !matest.AssertEqualMultiaddrs(t, clean, cleanupAddressSet(addrs)) {
t.Log("cleaned up set doesn't match expected")
}
})

t.Run("with a clean address set", func(t *testing.T) {
Expand All @@ -77,7 +79,7 @@ func TestCleanupAddrs(t *testing.T) {
"/ip4/1.2.3.4/tcp/4001",
"/ip4/1.2.3.4/udp/4001/quic-v1",
)
require.ElementsMatch(t, addrs, cleanupAddressSet(addrs), "cleaned up set doesn't match expected")
matest.AssertMultiaddrsMatch(t, addrs, cleanupAddressSet(addrs))
})
}

Expand Down
4 changes: 2 additions & 2 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,11 +975,11 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
for _, obsMaddr := range observed {
// Extract a public observed addr.
ip, _ := ma.SplitFirst(obsMaddr)
if ip == nil || !manet.IsPublicAddr(ip) {
if ip == nil || !manet.IsPublicAddr(ip.Multiaddr()) {
continue
}

finalAddrs = append(finalAddrs, ma.Join(ip, extMaddrNoIP))
finalAddrs = append(finalAddrs, ip.Encapsulate(extMaddrNoIP))
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/protocol/identify"

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

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -300,7 +301,7 @@ func TestAllAddrsUnique(t *testing.T) {
}()
close(sendNewAddrs)
require.Len(t, h.Addrs(), 2)
require.ElementsMatch(t, []ma.Multiaddr{ma.StringCast("/ip4/1.2.3.4/tcp/1"), ma.StringCast("/ip4/1.2.3.4/udp/1/quic-v1")}, h.Addrs())
matest.AssertEqualMultiaddrs(t, []ma.Multiaddr{ma.StringCast("/ip4/1.2.3.4/tcp/1"), ma.StringCast("/ip4/1.2.3.4/udp/1/quic-v1")}, h.Addrs())
time.Sleep(2*addrChangeTickrInterval + 1*time.Second) // the background loop runs every 5 seconds. Wait for 2x that time.
close(done)
cnt := <-out
Expand Down Expand Up @@ -650,13 +651,13 @@ func TestAddrChangeImmediatelyIfAddressNonEmpty(t *testing.T) {

// assert it's on the signed record
rc := peerRecordFromEnvelope(t, evt.SignedPeerRecord)
require.Equal(t, taddrs, rc.Addrs)
matest.AssertEqualMultiaddrs(t, taddrs, rc.Addrs)

// assert it's in the peerstore
ev := h.Peerstore().(peerstore.CertifiedAddrBook).GetPeerRecord(h.ID())
require.NotNil(t, ev)
rc = peerRecordFromEnvelope(t, ev)
require.Equal(t, taddrs, rc.Addrs)
matest.AssertEqualMultiaddrs(t, taddrs, rc.Addrs)
}

func TestStatefulAddrEvents(t *testing.T) {
Expand Down Expand Up @@ -759,13 +760,13 @@ func TestHostAddrChangeDetection(t *testing.T) {

// assert it's on the signed record
rc := peerRecordFromEnvelope(t, evt.SignedPeerRecord)
require.Equal(t, addrSets[i], rc.Addrs)
matest.AssertMultiaddrsMatch(t, addrSets[i], rc.Addrs)

// assert it's in the peerstore
ev := h.Peerstore().(peerstore.CertifiedAddrBook).GetPeerRecord(h.ID())
require.NotNil(t, ev)
rc = peerRecordFromEnvelope(t, ev)
require.Equal(t, addrSets[i], rc.Addrs)
matest.AssertMultiaddrsMatch(t, addrSets[i], rc.Addrs)
}
}

Expand Down
2 changes: 1 addition & 1 deletion p2p/host/basic/natmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (nmgr *natManager) doSync() {
for _, maddr := range nmgr.net.ListenAddresses() {
// Strip the IP
maIP, rest := ma.SplitFirst(maddr)
if maIP == nil || rest == nil {
if maIP == nil || len(rest) == 0 {
continue
}

Expand Down
8 changes: 4 additions & 4 deletions p2p/host/basic/natmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ func TestMapping(t *testing.T) {
externalAddr := netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 4321)
// pretend that we have a TCP mapping
mockNAT.EXPECT().GetMapping("tcp", 1234).Return(externalAddr, true)
require.Equal(t, ma.StringCast("/ip4/1.2.3.4/tcp/4321"), m.GetMapping(ma.StringCast("/ip4/0.0.0.0/tcp/1234")))
require.Equal(t, "/ip4/1.2.3.4/tcp/4321", m.GetMapping(ma.StringCast("/ip4/0.0.0.0/tcp/1234")).String())

// pretend that we have a QUIC mapping
mockNAT.EXPECT().GetMapping("udp", 1234).Return(externalAddr, true)
require.Equal(t, ma.StringCast("/ip4/1.2.3.4/udp/4321/quic-v1"), m.GetMapping(ma.StringCast("/ip4/0.0.0.0/udp/1234/quic-v1")))
require.Equal(t, "/ip4/1.2.3.4/udp/4321/quic-v1", m.GetMapping(ma.StringCast("/ip4/0.0.0.0/udp/1234/quic-v1")).String())

// pretend that there's no mapping
mockNAT.EXPECT().GetMapping("tcp", 1234).Return(netip.AddrPort{}, false)
require.Nil(t, m.GetMapping(ma.StringCast("/ip4/0.0.0.0/tcp/1234")))

// make sure this works for WebSocket addresses as well
mockNAT.EXPECT().GetMapping("tcp", 1234).Return(externalAddr, true)
require.Equal(t, ma.StringCast("/ip4/1.2.3.4/tcp/4321/ws"), m.GetMapping(ma.StringCast("/ip4/0.0.0.0/tcp/1234/ws")))
require.Equal(t, "/ip4/1.2.3.4/tcp/4321/ws", m.GetMapping(ma.StringCast("/ip4/0.0.0.0/tcp/1234/ws")).String())

// make sure this works for WebTransport addresses as well
mockNAT.EXPECT().GetMapping("udp", 1234).Return(externalAddr, true)
require.Equal(t, ma.StringCast("/ip4/1.2.3.4/udp/4321/quic-v1/webtransport"), m.GetMapping(ma.StringCast("/ip4/0.0.0.0/udp/1234/quic-v1/webtransport")))
require.Equal(t, "/ip4/1.2.3.4/udp/4321/quic-v1/webtransport", m.GetMapping(ma.StringCast("/ip4/0.0.0.0/udp/1234/quic-v1/webtransport")).String())
}

func TestAddAndRemoveListeners(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions p2p/http/libp2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func relativeMultiaddrURIToAbs(original *url.URL, relative *url.URL) (*url.URL,
withoutPath, _ := ma.SplitFunc(originalMa, func(c ma.Component) bool {
return c.Protocol().Code == ma.P_HTTP_PATH
})
withNewPath := withoutPath.Encapsulate(relativePathComponent)
withNewPath := withoutPath.AppendComponent(relativePathComponent)
return url.Parse("multiaddr:" + withNewPath.String())
}

Expand Down Expand Up @@ -937,10 +937,12 @@ func normalizeHTTPMultiaddr(addr ma.Multiaddr) (ma.Multiaddr, bool) {

_, afterHTTPS := ma.SplitFirst(afterIncludingHTTPS)
if afterHTTPS == nil {
return ma.Join(beforeHTTPS, tlsComponent, httpComponent), isHTTPMultiaddr
return beforeHTTPS.AppendComponent(tlsComponent, httpComponent), isHTTPMultiaddr
}

return ma.Join(beforeHTTPS, tlsComponent, httpComponent, afterHTTPS), isHTTPMultiaddr
t := beforeHTTPS.AppendComponent(tlsComponent, httpComponent)
t = append(t, afterHTTPS...)
return t, isHTTPMultiaddr
}

// getAndStorePeerMetadata looks up the protocol path in the well-known mapping and
Expand Down
9 changes: 6 additions & 3 deletions p2p/metricshelper/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ func GetTransport(a ma.Multiaddr) string {
if a == nil {
return "other"
}
for _, t := range transports {
if _, err := a.ValueForProtocol(t); err == nil {
return ma.ProtocolWithCode(t).Name
for i := len(a) - 1; i >= 0; i-- {
p := a[i].Protocol()
for _, t := range transports {
if p.Code == t {
return p.Name
}
}
}
return "other"
Expand Down
8 changes: 4 additions & 4 deletions p2p/net/swarm/dial_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,11 @@ func checkDialWorkerLoopScheduling(t *testing.T, s1, s2 *Swarm, tc schedulingTes
// failDials is used to track dials which should fail in the future
// at appropriate moment a message is sent to dialState.ch to trigger
// failure
failDials := make(map[ma.Multiaddr]dialState)
failDials := make(map[*ma.Multiaddr]dialState)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add this transform, using the pointer for map key, to release notes if not already added.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already called out

// recvCh is used to receive dial notifications for dials that will fail
recvCh := make(chan struct{}, 100)
// allDials tracks all pending dials
allDials := make(map[ma.Multiaddr]dialState)
allDials := make(map[*ma.Multiaddr]dialState)
// addrs are the peer addresses the swarm will use for dialing
addrs := make([]ma.Multiaddr, 0)
// create pending dials
Expand Down Expand Up @@ -610,7 +610,7 @@ func checkDialWorkerLoopScheduling(t *testing.T, s1, s2 *Swarm, tc schedulingTes
}
addrs = append(addrs, inp.addr)
// add to pending dials
allDials[inp.addr] = dialState{
allDials[&inp.addr] = dialState{
ch: failCh,
addr: inp.addr,
delay: inp.delay,
Expand Down Expand Up @@ -695,7 +695,7 @@ loop:
failDials[a] = dialState{
ch: ds.ch,
failAt: cl.Now().Add(ds.failAfter),
addr: a,
addr: *a,
delay: ds.delay,
}
}
Expand Down
13 changes: 7 additions & 6 deletions p2p/net/swarm/swarm_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
matest "github.com/multiformats/go-multiaddr/matest"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -188,12 +189,12 @@ func TestAddrResolution(t *testing.T) {
require.NoError(t, err)

require.Len(t, mas, 1)
require.Contains(t, mas, addr2)
matest.AssertMultiaddrsContain(t, mas, addr2)

addrs := s.peers.Addrs(p1)
require.Len(t, addrs, 2)
require.Contains(t, addrs, addr1)
require.Contains(t, addrs, addr2)
matest.AssertMultiaddrsContain(t, addrs, addr1)
matest.AssertMultiaddrsContain(t, addrs, addr2)
}

func TestAddrResolutionRecursive(t *testing.T) {
Expand Down Expand Up @@ -234,8 +235,8 @@ func TestAddrResolutionRecursive(t *testing.T) {

addrs1 := s.Peerstore().Addrs(pi1.ID)
require.Len(t, addrs1, 2)
require.Contains(t, addrs1, addr1)
require.Contains(t, addrs1, addr2)
matest.AssertMultiaddrsContain(t, addrs1, addr1)
matest.AssertMultiaddrsContain(t, addrs1, addr2)

pi2, err := peer.AddrInfoFromP2pAddr(p2paddr2)
require.NoError(t, err)
Expand All @@ -247,7 +248,7 @@ func TestAddrResolutionRecursive(t *testing.T) {

addrs2 := s.Peerstore().Addrs(pi2.ID)
require.Len(t, addrs2, 1)
require.Contains(t, addrs2, addr1)
matest.AssertMultiaddrsContain(t, addrs2, addr1)
}

// see https://github.com/libp2p/go-libp2p/issues/2562
Expand Down
5 changes: 3 additions & 2 deletions p2p/protocol/circuitv2/client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ func (c *Conn) RemoteMultiaddr() ma.Multiaddr {
c.stream.Conn().RemotePeer().String(),
)
if err != nil {
panic(err)
log.Error(err)
return ma.Join(c.stream.Conn().RemoteMultiaddr(), circuitAddr)
}
return ma.Join(c.stream.Conn().RemoteMultiaddr(), relayAddr, circuitAddr)
return ma.Join(c.stream.Conn().RemoteMultiaddr(), relayAddr.Multiaddr(), circuitAddr)
}

func (c *Conn) LocalMultiaddr() ma.Multiaddr {
Expand Down
Loading
Loading