Skip to content

fix: Added multiselect type consistency in negotiate method #814

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

Closed
wants to merge 14 commits into from
Closed
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
3 changes: 3 additions & 0 deletions libp2p/host/basic_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ async def _swarm_stream_handler(self, net_stream: INetStream) -> None:
protocol, handler = await self.multiselect.negotiate(
MultiselectCommunicator(net_stream), self.negotiate_timeout
)
if protocol is None:
await net_stream.reset()
raise StreamFailure("No protocol selected")
except MultiselectError as error:
peer_id = net_stream.muxed_conn.peer_id
logger.debug(
Expand Down
2 changes: 1 addition & 1 deletion libp2p/protocol_muxer/multiselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def negotiate(
self,
communicator: IMultiselectCommunicator,
negotiate_timeout: int = DEFAULT_NEGOTIATE_TIMEOUT,
) -> tuple[TProtocol, StreamHandlerFn | None]:
) -> tuple[TProtocol | None, StreamHandlerFn | None]:
"""
Negotiate performs protocol selection.

Expand Down
7 changes: 6 additions & 1 deletion libp2p/security/security_multistream.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from libp2p.protocol_muxer.multiselect_communicator import (
MultiselectCommunicator,
)
from libp2p.transport.exceptions import (
SecurityUpgradeFailure,
)

"""
Represents a secured connection object, which includes a connection and details about
Expand Down Expand Up @@ -104,7 +107,7 @@ async def select_transport(
:param is_initiator: true if we are the initiator, false otherwise
:return: selected secure transport
"""
protocol: TProtocol
protocol: TProtocol | None
communicator = MultiselectCommunicator(conn)
if is_initiator:
# Select protocol if initiator
Expand All @@ -114,5 +117,7 @@ async def select_transport(
else:
# Select protocol if non-initiator
protocol, _ = await self.multiselect.negotiate(communicator)
if protocol is None:
raise SecurityUpgradeFailure("No protocol selected")
# Return transport from protocol
return self.transports[protocol]
7 changes: 6 additions & 1 deletion libp2p/stream_muxer/muxer_multistream.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
PROTOCOL_ID,
Yamux,
)
from libp2p.transport.exceptions import (
MuxerUpgradeFailure,
)


class MuxerMultistream:
Expand Down Expand Up @@ -73,14 +76,16 @@ async def select_transport(self, conn: IRawConnection) -> TMuxerClass:
:param conn: conn to choose a transport over
:return: selected muxer transport
"""
protocol: TProtocol
protocol: TProtocol | None
communicator = MultiselectCommunicator(conn)
if conn.is_initiator:
protocol = await self.multiselect_client.select_one_of(
tuple(self.transports.keys()), communicator
)
else:
protocol, _ = await self.multiselect.negotiate(communicator)
if protocol is None:
raise MuxerUpgradeFailure("No protocol selected")
return self.transports[protocol]

async def new_conn(self, conn: ISecureConn, peer_id: ID) -> IMuxedConn:
Expand Down
1 change: 1 addition & 0 deletions newsfragments/814.fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added multiselect type consistency in negotiate method. Updates all the usages of the method.