mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-03-17 18:51:27 +00:00
Merge pull request #838 from unniznd/fix_multiselect_negotiate_type
fix: Added multiselect type consistency in negotiate method
This commit is contained in:
@ -297,6 +297,11 @@ class BasicHost(IHost):
|
|||||||
protocol, handler = await self.multiselect.negotiate(
|
protocol, handler = await self.multiselect.negotiate(
|
||||||
MultiselectCommunicator(net_stream), self.negotiate_timeout
|
MultiselectCommunicator(net_stream), self.negotiate_timeout
|
||||||
)
|
)
|
||||||
|
if protocol is None:
|
||||||
|
await net_stream.reset()
|
||||||
|
raise StreamFailure(
|
||||||
|
"Failed to negotiate protocol: no protocol selected"
|
||||||
|
)
|
||||||
except MultiselectError as error:
|
except MultiselectError as error:
|
||||||
peer_id = net_stream.muxed_conn.peer_id
|
peer_id = net_stream.muxed_conn.peer_id
|
||||||
logger.debug(
|
logger.debug(
|
||||||
|
|||||||
@ -118,6 +118,8 @@ class SecurityMultistream(ABC):
|
|||||||
# Select protocol if non-initiator
|
# Select protocol if non-initiator
|
||||||
protocol, _ = await self.multiselect.negotiate(communicator)
|
protocol, _ = await self.multiselect.negotiate(communicator)
|
||||||
if protocol is None:
|
if protocol is None:
|
||||||
raise MultiselectError("fail to negotiate a security protocol")
|
raise MultiselectError(
|
||||||
|
"Failed to negotiate a security protocol: no protocol selected"
|
||||||
|
)
|
||||||
# Return transport from protocol
|
# Return transport from protocol
|
||||||
return self.transports[protocol]
|
return self.transports[protocol]
|
||||||
|
|||||||
@ -85,7 +85,9 @@ class MuxerMultistream:
|
|||||||
else:
|
else:
|
||||||
protocol, _ = await self.multiselect.negotiate(communicator)
|
protocol, _ = await self.multiselect.negotiate(communicator)
|
||||||
if protocol is None:
|
if protocol is None:
|
||||||
raise MultiselectError("fail to negotiate a stream muxer protocol")
|
raise MultiselectError(
|
||||||
|
"Fail to negotiate a stream muxer protocol: no protocol selected"
|
||||||
|
)
|
||||||
return self.transports[protocol]
|
return self.transports[protocol]
|
||||||
|
|
||||||
async def new_conn(self, conn: ISecureConn, peer_id: ID) -> IMuxedConn:
|
async def new_conn(self, conn: ISecureConn, peer_id: ID) -> IMuxedConn:
|
||||||
|
|||||||
1
newsfragments/837.bugfix.rst
Normal file
1
newsfragments/837.bugfix.rst
Normal file
@ -0,0 +1 @@
|
|||||||
|
Added multiselect type consistency in negotiate method. Updates all the usages of the method.
|
||||||
@ -1,3 +1,10 @@
|
|||||||
|
from unittest.mock import (
|
||||||
|
AsyncMock,
|
||||||
|
MagicMock,
|
||||||
|
)
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from libp2p import (
|
from libp2p import (
|
||||||
new_swarm,
|
new_swarm,
|
||||||
)
|
)
|
||||||
@ -10,6 +17,9 @@ from libp2p.host.basic_host import (
|
|||||||
from libp2p.host.defaults import (
|
from libp2p.host.defaults import (
|
||||||
get_default_protocols,
|
get_default_protocols,
|
||||||
)
|
)
|
||||||
|
from libp2p.host.exceptions import (
|
||||||
|
StreamFailure,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_default_protocols():
|
def test_default_protocols():
|
||||||
@ -22,3 +32,30 @@ def test_default_protocols():
|
|||||||
# NOTE: comparing keys for equality as handlers may be closures that do not compare
|
# NOTE: comparing keys for equality as handlers may be closures that do not compare
|
||||||
# in the way this test is concerned with
|
# in the way this test is concerned with
|
||||||
assert handlers.keys() == get_default_protocols(host).keys()
|
assert handlers.keys() == get_default_protocols(host).keys()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.trio
|
||||||
|
async def test_swarm_stream_handler_no_protocol_selected(monkeypatch):
|
||||||
|
key_pair = create_new_key_pair()
|
||||||
|
swarm = new_swarm(key_pair)
|
||||||
|
host = BasicHost(swarm)
|
||||||
|
|
||||||
|
# Create a mock net_stream
|
||||||
|
net_stream = MagicMock()
|
||||||
|
net_stream.reset = AsyncMock()
|
||||||
|
net_stream.muxed_conn.peer_id = "peer-test"
|
||||||
|
|
||||||
|
# Monkeypatch negotiate to simulate "no protocol selected"
|
||||||
|
async def fake_negotiate(comm, timeout):
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
monkeypatch.setattr(host.multiselect, "negotiate", fake_negotiate)
|
||||||
|
|
||||||
|
# Now run the handler and expect StreamFailure
|
||||||
|
with pytest.raises(
|
||||||
|
StreamFailure, match="Failed to negotiate protocol: no protocol selected"
|
||||||
|
):
|
||||||
|
await host._swarm_stream_handler(net_stream)
|
||||||
|
|
||||||
|
# Ensure reset was called since negotiation failed
|
||||||
|
net_stream.reset.assert_awaited()
|
||||||
|
|||||||
Reference in New Issue
Block a user