Skip to content

Commit 2491f94

Browse files
authored
Merge pull request libp2p#1080 from yashksaini-coder/Fix/Decouple-peer-ID-postfix
Decouple Peer ID Postfix from `get_addrs` & Introduce `get_transport_addrs`
2 parents 1a0582b + d7e2d9a commit 2491f94

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

examples/ping/ping.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,16 @@ async def run(port: int, destination: str, psk: int, transport: str) -> None:
9393

9494
# Get all available addresses with peer ID
9595
all_addrs = host.get_addrs()
96+
transport_addrs = host.get_transport_addrs()
9697

9798
print("Listener ready, listening on:\n")
9899
for addr in all_addrs:
99100
print(f"{addr}")
100101

102+
print("\nRaw transport addresses (without peer ID):")
103+
for addr in transport_addrs:
104+
print(f"{addr}")
105+
101106
print(
102107
f"\nRun this from the same folder in another console:\n\n"
103108
f"ping-demo -d {host.get_addrs()[0]} -psk {psk} -t {transport}\n"

libp2p/abc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,19 @@ def get_addrs(self) -> list[Multiaddr]:
18191819
18201820
"""
18211821

1822+
@abstractmethod
1823+
def get_transport_addrs(self) -> list[Multiaddr]:
1824+
"""
1825+
Retrieve the raw multiaddr addresses this host is listening to,
1826+
without the /p2p/{peer_id} suffix.
1827+
1828+
Returns
1829+
-------
1830+
list[Multiaddr]
1831+
A list of raw multiaddresses.
1832+
1833+
"""
1834+
18221835
@abstractmethod
18231836
def get_peerstore(self) -> IPeerStore:
18241837
"""

libp2p/host/basic_host.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,26 @@ def get_mux(self) -> Multiselect:
312312
"""
313313
return self.multiselect
314314

315-
def get_addrs(self) -> list[multiaddr.Multiaddr]:
315+
def get_transport_addrs(self) -> list[multiaddr.Multiaddr]:
316316
"""
317-
:return: all the multiaddr addresses this host is listening to
317+
Return the raw multiaddr addresses this host is listening to,
318+
without the /p2p/{peer_id} suffix.
318319
"""
319-
# TODO: We don't need "/p2p/{peer_id}" postfix actually.
320-
p2p_part = multiaddr.Multiaddr(f"/p2p/{self.get_id()!s}")
321-
322320
addrs: list[multiaddr.Multiaddr] = []
323321
for transport in self._network.listeners.values():
324-
for addr in transport.get_addrs():
325-
addrs.append(addr.encapsulate(p2p_part))
322+
addrs.extend(transport.get_addrs())
326323
return addrs
327324

325+
def get_addrs(self) -> list[multiaddr.Multiaddr]:
326+
"""
327+
Return all the multiaddr addresses this host is listening to.
328+
329+
Note: This method appends the /p2p/{peer_id} suffix to the addresses.
330+
Use get_transport_addrs() for raw transport addresses.
331+
"""
332+
p2p_part = multiaddr.Multiaddr(f"/p2p/{self.get_id()!s}")
333+
return [addr.encapsulate(p2p_part) for addr in self.get_transport_addrs()]
334+
328335
def get_connected_peers(self) -> list[ID]:
329336
"""
330337
:return: all the ids of peers this host is currently connected to

newsfragments/1073.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Introduced ``get_transport_addrs()`` method to ``BasicHost`` for retrieving raw transport addresses without the peer ID suffix.
2+
Refactored ``get_addrs()`` to utilize this new method, maintaining backward compatibility.

tests/core/host/test_basic_host.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
)
55

66
import pytest
7+
from multiaddr import Multiaddr
78

89
from libp2p import (
910
new_swarm,
@@ -59,3 +60,35 @@ async def fake_negotiate(comm, timeout):
5960

6061
# Ensure reset was called since negotiation failed
6162
net_stream.reset.assert_awaited()
63+
64+
65+
def test_get_addrs_and_transport_addrs():
66+
key_pair = create_new_key_pair()
67+
swarm = new_swarm(key_pair)
68+
host = BasicHost(swarm)
69+
70+
# Mock the network listeners
71+
mock_transport = MagicMock()
72+
raw_addr = Multiaddr("/ip4/127.0.0.1/tcp/8000")
73+
mock_transport.get_addrs.return_value = [raw_addr]
74+
75+
# Inject into swarm listeners
76+
# swarm.listeners is a dict
77+
swarm.listeners = {"tcp": mock_transport}
78+
79+
# Test get_transport_addrs
80+
transport_addrs = host.get_transport_addrs()
81+
assert len(transport_addrs) == 1
82+
assert transport_addrs[0] == raw_addr
83+
assert str(transport_addrs[0]) == "/ip4/127.0.0.1/tcp/8000"
84+
85+
# Test get_addrs
86+
addrs = host.get_addrs()
87+
assert len(addrs) == 1
88+
addr_str = str(addrs[0])
89+
peer_id_str = str(host.get_id())
90+
assert peer_id_str in addr_str
91+
# multiaddr might normalize /p2p/ to /ipfs/
92+
assert addr_str.endswith(f"/p2p/{peer_id_str}") or addr_str.endswith(
93+
f"/ipfs/{peer_id_str}"
94+
)

0 commit comments

Comments
 (0)