Skip to content
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

feat: Update go-multiaddr to v0.15-pre #3145

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions core/peer/addrinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func SplitAddr(m ma.Multiaddr) (transport ma.Multiaddr, id ID) {
}

transport, p2ppart := ma.SplitLast(m)
if p2ppart == nil || p2ppart.Protocol().Code != ma.P_P2P {
if p2ppart.Empty() || p2ppart.Protocol().Code != ma.P_P2P {
return m, ""
}
id = ID(p2ppart.RawValue()) // already validated by the multiaddr library.
Expand Down Expand Up @@ -109,11 +109,11 @@ func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) {
return nil, err
}
if len(pi.Addrs) == 0 {
return []ma.Multiaddr{p2ppart}, nil
return []ma.Multiaddr{p2ppart.AsMultiaddr()}, nil
}
addrs := make([]ma.Multiaddr, 0, len(pi.Addrs))
for _, addr := range pi.Addrs {
addrs = append(addrs, addr.Encapsulate(p2ppart))
addrs = append(addrs, addr.Encapsulate(p2ppart.AsMultiaddr()))
}
return addrs, nil
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,5 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.3.0 // indirect
)

replace github.com/multiformats/go-multiaddr => github.com/multiformats/go-multiaddr v0.14.1-0.20250129195403-f43a27871caa
61 changes: 56 additions & 5 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion p2p/discovery/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *mdnsService) getIPs(addrs []ma.Multiaddr) ([]string, error) {
var ip4, ip6 string
for _, addr := range addrs {
first, _ := ma.SplitFirst(addr)
if first == nil {
if first.Empty() {
continue
}
if ip4 == "" && first.Protocol().Code == ma.P_IP4 {
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.AsMultiaddr()
if !rest.Empty() {
addr = addr.Encapsulate(rest)
}
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/host/autorelay/addrsplosion.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func isRelayAddr(a ma.Multiaddr) bool {
}

func isDNSAddr(a ma.Multiaddr) bool {
if first, _ := ma.SplitFirst(a); first != nil {
if first, _ := ma.SplitFirst(a); !first.Empty() {
switch first.Protocol().Code {
case ma.P_DNS, ma.P_DNS4, ma.P_DNS6, ma.P_DNSADDR:
return true
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 @@ -943,11 +943,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.Empty() || !manet.IsPublicAddr(ip.AsMultiaddr()) {
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
4 changes: 2 additions & 2 deletions 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.Empty() || rest.Empty() {
continue
}

Expand All @@ -169,7 +169,7 @@ func (nmgr *natManager) doSync() {

// Extract the port/protocol
proto, _ := ma.SplitFirst(rest)
if proto == nil {
if proto.Empty() {
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.EncapsulateC(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 append(beforeHTTPS, tlsComponent, httpComponent), isHTTPMultiaddr
}

return ma.Join(beforeHTTPS, tlsComponent, httpComponent, afterHTTPS), isHTTPMultiaddr
t := append(beforeHTTPS, 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)
// 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.AsMultiaddr(), circuitAddr)
}

func (c *Conn) LocalMultiaddr() ma.Multiaddr {
Expand Down
2 changes: 1 addition & 1 deletion p2p/protocol/circuitv2/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func makeReservationMsg(
switch {
case id == "":
// No ID, we'll add one to the address
addr = addr.Encapsulate(selfP2PAddr)
addr = addr.EncapsulateC(selfP2PAddr)
case id == selfID:
// This address already has our ID in it.
// Do nothing
Expand Down
Loading