Skip to content

Commit 42f07ae

Browse files
authored
Merge branch 'main' into add-read-write-lock
2 parents 9cd3805 + 719246c commit 42f07ae

File tree

14 files changed

+258
-27
lines changed

14 files changed

+258
-27
lines changed

libp2p/abc.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
Pubsub,
5151
)
5252

53+
from typing import TYPE_CHECKING
54+
55+
if TYPE_CHECKING:
56+
from libp2p.protocol_muxer.multiselect import Multiselect
57+
5358
from libp2p.pubsub.pb import (
5459
rpc_pb2,
5560
)
@@ -1545,9 +1550,8 @@ def get_network(self) -> INetworkService:
15451550
15461551
"""
15471552

1548-
# FIXME: Replace with correct return type
15491553
@abstractmethod
1550-
def get_mux(self) -> Any:
1554+
def get_mux(self) -> "Multiselect":
15511555
"""
15521556
Retrieve the muxer instance for the host.
15531557
@@ -2158,6 +2162,7 @@ def add_handler(self, protocol: TProtocol, handler: StreamHandlerFn) -> None:
21582162
21592163
"""
21602164

2165+
@abstractmethod
21612166
def get_protocols(self) -> tuple[TProtocol | None, ...]:
21622167
"""
21632168
Retrieve the protocols for which handlers have been registered.
@@ -2168,7 +2173,6 @@ def get_protocols(self) -> tuple[TProtocol | None, ...]:
21682173
A tuple of registered protocol names.
21692174
21702175
"""
2171-
return tuple(self.handlers.keys())
21722176

21732177
@abstractmethod
21742178
async def negotiate(

libp2p/identity/identify/identify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _mk_identify_protobuf(
5959
) -> Identify:
6060
public_key = host.get_public_key()
6161
laddrs = host.get_addrs()
62-
protocols = host.get_mux().get_protocols()
62+
protocols = tuple(str(p) for p in host.get_mux().get_protocols() if p is not None)
6363

6464
observed_addr = observed_multiaddr.to_bytes() if observed_multiaddr else b""
6565
return Identify(

libp2p/peer/peerinfo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
)
44
from typing import (
55
Any,
6+
cast,
67
)
78

89
import multiaddr
10+
from multiaddr.protocols import Protocol
911

1012
from .id import (
1113
ID,
@@ -42,7 +44,8 @@ def info_from_p2p_addr(addr: multiaddr.Multiaddr) -> PeerInfo:
4244
p2p_protocols = p2p_part.protocols()
4345
if not p2p_protocols:
4446
raise InvalidAddrError("The last part of the address has no protocols")
45-
last_protocol = p2p_protocols[0]
47+
last_protocol = cast(Protocol, p2p_part.protocols()[0])
48+
4649
if last_protocol is None:
4750
raise InvalidAddrError("The last protocol is None")
4851

libp2p/peer/peerstore.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ def peer_ids(self) -> list[ID]:
6464
return list(self.peer_data_map.keys())
6565

6666
def clear_peerdata(self, peer_id: ID) -> None:
67-
"""Clears the peer data of the peer"""
67+
"""Clears all data associated with the given peer_id."""
68+
if peer_id in self.peer_data_map:
69+
del self.peer_data_map[peer_id]
70+
else:
71+
raise PeerStoreError("peer ID not found")
6872

6973
def valid_peer_ids(self) -> list[ID]:
7074
"""

libp2p/protocol_muxer/multiselect.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ async def negotiate(
101101
except trio.TooSlowError:
102102
raise MultiselectError("handshake read timeout")
103103

104+
def get_protocols(self) -> tuple[TProtocol | None, ...]:
105+
"""
106+
Retrieve the protocols for which handlers have been registered.
107+
108+
Returns
109+
-------
110+
tuple[TProtocol, ...]
111+
A tuple of registered protocol names.
112+
113+
"""
114+
return tuple(self.handlers.keys())
115+
104116
async def handshake(self, communicator: IMultiselectCommunicator) -> None:
105117
"""
106118
Perform handshake to agree on multiselect protocol.

libp2p/relay/circuit_v2/discovery.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ async def _check_via_peerstore(self, peer_id: ID) -> bool | None:
234234

235235
if not callable(proto_getter):
236236
return None
237-
237+
if peer_id not in peerstore.peer_ids():
238+
return None
238239
try:
239240
# Try to get protocols
240241
proto_result = proto_getter(peer_id)
@@ -283,8 +284,6 @@ async def _check_via_mux(self, peer_id: ID) -> bool | None:
283284
return None
284285

285286
mux = self.host.get_mux()
286-
if not hasattr(mux, "protocols"):
287-
return None
288287

289288
peer_protocols = set()
290289
# Get protocols from mux with proper type safety
@@ -293,7 +292,9 @@ async def _check_via_mux(self, peer_id: ID) -> bool | None:
293292
# Get protocols with proper typing
294293
mux_protocols = mux.get_protocols()
295294
if isinstance(mux_protocols, (list, tuple)):
296-
available_protocols = list(mux_protocols)
295+
available_protocols = [
296+
p for p in mux.get_protocols() if p is not None
297+
]
297298

298299
for protocol in available_protocols:
299300
try:
@@ -313,7 +314,7 @@ async def _check_via_mux(self, peer_id: ID) -> bool | None:
313314

314315
self._protocol_cache[peer_id] = peer_protocols
315316
protocol_str = str(PROTOCOL_ID)
316-
for protocol in peer_protocols:
317+
for protocol in map(TProtocol, peer_protocols):
317318
if protocol == protocol_str:
318319
return True
319320
return False

newsfragments/746.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improved type safety in `get_mux()` and `get_protocols()` by returning properly typed values instead
2+
of `Any`. Also updated `identify.py` and `discovery.py` to handle `None` values safely and
3+
compare protocols correctly.

newsfragments/749.internal.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add comprehensive tests for relay_discovery method in circuit_relay_v2

newsfragments/750.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add logic to clear_peerdata method in peerstore

newsfragments/757.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fixed malformed PeerId in test_peerinfo

0 commit comments

Comments
 (0)