Skip to content

Commit 2ed2587

Browse files
authored
fix: removed dummy ID(b) from upgrade_security for inbound connections (#681)
* fix: removed dummy ID(b) from upgrade_security for inbound connections * added newsfragment * updated newsfragment
1 parent d61bca7 commit 2ed2587

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

libp2p/network/swarm.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ async def dial_addr(self, addr: Multiaddr, peer_id: ID) -> INetConn:
187187
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
188188
# the conn and then mux the conn
189189
try:
190-
secured_conn = await self.upgrader.upgrade_security(raw_conn, peer_id, True)
190+
secured_conn = await self.upgrader.upgrade_security(raw_conn, True, peer_id)
191191
except SecurityUpgradeFailure as error:
192192
logger.debug("failed to upgrade security for peer %s", peer_id)
193193
await raw_conn.close()
@@ -257,10 +257,7 @@ async def conn_handler(
257257
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first
258258
# secure the conn and then mux the conn
259259
try:
260-
# FIXME: This dummy `ID(b"")` for the remote peer is useless.
261-
secured_conn = await self.upgrader.upgrade_security(
262-
raw_conn, ID(b""), False
263-
)
260+
secured_conn = await self.upgrader.upgrade_security(raw_conn, False)
264261
except SecurityUpgradeFailure as error:
265262
logger.debug("failed to upgrade security for peer at %s", maddr)
266263
await raw_conn.close()

libp2p/transport/upgrader.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,16 @@ def upgrade_listener(self, transport: ITransport, listeners: IListener) -> None:
4848
# TODO: Figure out what to do with this function.
4949

5050
async def upgrade_security(
51-
self, raw_conn: IRawConnection, peer_id: ID, is_initiator: bool
51+
self,
52+
raw_conn: IRawConnection,
53+
is_initiator: bool,
54+
peer_id: ID | None = None,
5255
) -> ISecureConn:
5356
"""Upgrade conn to a secured connection."""
5457
try:
5558
if is_initiator:
59+
if peer_id is None:
60+
raise ValueError("peer_id must be provided for outbout connection")
5661
return await self.security_multistream.secure_outbound(
5762
raw_conn, peer_id
5863
)

newsfragments/681.breaking.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reordered the arguments to `upgrade_security` to place `is_initiator` before `peer_id`, and made `peer_id` optional.
2+
This allows the method to reflect the fact that peer identity is not required for inbound connections.

0 commit comments

Comments
 (0)