Skip to content

Commit 8be6847

Browse files
committed
node: relayTransportAddr as a slice of the circuit's components
Review on #667. Multiaddr is a slice of components in go-multiaddr v0.16, so the relay's address is the components before the /p2p/<relay>/p2p-circuit tail, cloned; TestRelayTransportAddr pins the addressed, bare and direct cases.
1 parent b221cee commit 8be6847

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎internal/node/mcp.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,17 +591,17 @@ func circuitRelay(ma multiaddr.Multiaddr) peer.ID {
591591
}
592592

593593
// relayTransportAddr is the relay's own address carried in a circuit
594-
// address, or nil for a bare /p2p/<relay>/p2p-circuit.
594+
// address, the components before /p2p/<relay>/p2p-circuit, or nil for a
595+
// bare circuit or any other address.
595596
func relayTransportAddr(ma multiaddr.Multiaddr) multiaddr.Multiaddr {
597+
if circuitRelay(ma) == "" {
598+
return nil
599+
}
596600
parts := multiaddr.Split(ma)
597601
if len(parts) < 3 {
598602
return nil
599603
}
600-
var via multiaddr.Multiaddr
601-
for _, c := range parts[:len(parts)-2] {
602-
via = via.AppendComponent(&c)
603-
}
604-
return via
604+
return multiaddr.Multiaddr(slices.Clone(parts[:len(parts)-2]))
605605
}
606606

607607
// transitCatalogRefreshInterval bounds how often a relay the catalog does

‎internal/node/mcp_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,27 @@ func TestSplitToolName(t *testing.T) {
309309
})
310310
}
311311
}
312+
313+
// The relay's own address is what a circuit address carries before its
314+
// /p2p/<relay>/p2p-circuit tail; a bare circuit and a direct address carry
315+
// none.
316+
func TestRelayTransportAddr(t *testing.T) {
317+
relayHost, err := mocknet.New().GenPeer()
318+
if err != nil {
319+
t.Fatal(err)
320+
}
321+
relay := relayHost.ID().String()
322+
for _, tc := range []struct {
323+
addr, want string
324+
}{
325+
{"/ip4/136.113.153.46/udp/4501/quic-v1/p2p/" + relay + "/p2p-circuit", "/ip4/136.113.153.46/udp/4501/quic-v1"},
326+
{"/dns4/bootstrap.example/tcp/4501/p2p/" + relay + "/p2p-circuit", "/dns4/bootstrap.example/tcp/4501"},
327+
{"/p2p/" + relay + "/p2p-circuit", ""},
328+
{"/ip4/10.0.0.1/tcp/5002", ""},
329+
} {
330+
got := relayTransportAddr(multiaddr.StringCast(tc.addr))
331+
if (got == nil) != (tc.want == "") || (got != nil && got.String() != tc.want) {
332+
t.Errorf("relayTransportAddr(%s) = %v, want %q", tc.addr, got, tc.want)
333+
}
334+
}
335+
}

0 commit comments

Comments
 (0)