Skip to content

Commit f15f7a2

Browse files
committed
Fix lint error in CI
1 parent 3c60b92 commit f15f7a2

File tree

4 files changed

+47
-51
lines changed

4 files changed

+47
-51
lines changed

libp2p/transport/webrtc/async_bridge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ async def set_remote_description(
125125
raise
126126

127127
async def add_ice_candidate(
128-
self, peer_connection: RTCPeerConnection, candidate: RTCIceCandidate
128+
self, peer_connection: RTCPeerConnection, candidate: RTCIceCandidate | None
129129
) -> None:
130130
"""Add ICE candidate with proper async bridging"""
131131
try:

libp2p/transport/webrtc/constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from libp2p.custom_types import TProtocol
2+
13
# Default ICE servers for NAT traversal
24
DEFAULT_ICE_SERVERS = [
35
{"urls": "stun:stun.l.google.com:19302"},
@@ -7,7 +9,7 @@
79
]
810

911
# WebRTC signaling protocol
10-
SIGNALING_PROTOCOL = "/libp2p/webrtc/signal/1.0.0"
12+
SIGNALING_PROTOCOL = TProtocol("/libp2p/webrtc/signal/1.0.0")
1113

1214
# WebRTC muxer protocol
1315
MUXER_PROTOCOL = "/webrtc"

libp2p/transport/webrtc/private_to_private/initiate_connection.py

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import json
22
import logging
33
from typing import Any
4-
from aiortc.rtcicetransport import candidate_from_aioice
4+
55
from aioice.candidate import Candidate
66
from aiortc import (
77
RTCConfiguration,
8-
RTCIceCandidate,
98
RTCPeerConnection,
109
RTCSessionDescription,
1110
)
11+
from aiortc.rtcicetransport import candidate_from_aioice
1212
from multiaddr import Multiaddr
1313
import trio
1414

15-
from libp2p.abc import INetStream, IRawConnection
15+
from libp2p.abc import IHost, INetStream, IRawConnection
1616
from libp2p.peer.id import ID
1717

18-
from .pb import Message
1918
from ..async_bridge import TrioSafeWebRTCOperations
2019
from ..connection import WebRTCRawConnection
2120
from ..constants import (
@@ -24,10 +23,7 @@
2423
SDPHandshakeError,
2524
WebRTCError,
2625
)
27-
28-
from libp2p.abc import (
29-
IHost
30-
)
26+
from .pb import Message
3127

3228
logger = logging.getLogger("webrtc.private.initiate_connection")
3329

@@ -88,14 +84,14 @@ async def initiate_connection(
8884

8985
# Setup data channel ready event
9086
data_channel_ready = trio.Event()
91-
87+
9288
@data_channel.on("open")
93-
def on_data_channel_open():
89+
def on_data_channel_open() -> None:
9490
logger.info("Data channel opened")
9591
data_channel_ready.set()
9692

9793
@data_channel.on("error")
98-
def on_data_channel_error(error: Any):
94+
def on_data_channel_error(error: Any) -> None:
9995
logger.error(f"Data channel error: {error}")
10096

10197
# Register data channel event handlers
@@ -120,19 +116,18 @@ def on_data_channel_error(error: Any):
120116
offer_msg.data = offer.sdp
121117
await _send_signaling_message(signaling_stream, offer_msg)
122118

123-
# (Note: aiortc does not emit ice candidate event, per candidate (like js)
124-
# but sends it along SDP. To maintain interop, we extract adn resend in given format )
119+
# (Note: aiortc does not emit ice candidate event, per candidate (like js)
120+
# but sends it along SDP.
121+
# To maintain interop, we extract and resend in given format)
125122
await _send_ice_candidates(signaling_stream, peer_connection)
126-
123+
127124
# Wait for answer
128125
answer_msg = await _receive_signaling_message(signaling_stream, timeout)
129126
if answer_msg.type != Message.SDP_ANSWER:
130127
raise SDPHandshakeError(f"Expected answer, got: {answer_msg.type}")
131128

132129
# Set remote description
133-
answer = RTCSessionDescription(
134-
sdp=answer_msg.data, type='answer'
135-
)
130+
answer = RTCSessionDescription(sdp=answer_msg.data, type="answer")
136131
bridge = TrioSafeWebRTCOperations._get_bridge()
137132
async with bridge:
138133
await bridge.set_remote_description(peer_connection, answer)
@@ -146,6 +141,7 @@ def on_data_channel_error(error: Any):
146141

147142
# Wait for data channel to be ready
148143
connection_failed = trio.Event()
144+
149145
def on_connection_state_change() -> None:
150146
state = peer_connection.connectionState
151147
logger.debug(f"Connection state: {state}")
@@ -181,9 +177,9 @@ def on_connection_state_change() -> None:
181177
is_initiator=True,
182178
)
183179

184-
logger.debug('initiator connected, closing init channel')
180+
logger.debug("initiator connected, closing init channel")
185181
data_channel.close()
186-
182+
187183
logger.info(f"Successfully established WebRTC connection to {target_peer_id}")
188184
return connection
189185

@@ -217,12 +213,10 @@ async def _send_signaling_message(stream: INetStream, message: Message) -> None:
217213
raise
218214

219215

220-
async def _receive_signaling_message(
221-
stream: INetStream, timeout: float
222-
) -> Message:
216+
async def _receive_signaling_message(stream: INetStream, timeout: float) -> Message:
223217
"""Receive a signaling message from the stream"""
224218
try:
225-
with trio.move_on_after(timeout) as cancel_scope:
219+
with trio.move_on_after(timeout):
226220
# Read message data
227221
message_data = await stream.read()
228222
deserealized_msg = Message()
@@ -235,30 +229,29 @@ async def _receive_signaling_message(
235229
raise
236230

237231

238-
async def _send_ice_candidates(stream, peer_connection):
232+
async def _send_ice_candidates(
233+
stream: INetStream, peer_connection: RTCPeerConnection
234+
) -> None:
239235
# Get SDP offer from localDescription to extract ICE Candidate
240236
sdp = peer_connection.localDescription.sdp
241237
sdp_lines = sdp.splitlines()
242-
238+
243239
msg = Message()
244240
msg.type = Message.ICE_CANDIDATE
245241
# Extract ICE_Candidate and send each separately
246242
for line in sdp_lines:
247243
if line.startswith("a=candidate:"):
248-
cand_str = line[len("a="):]
249-
candidate_init = {
250-
"candidate": cand_str,
251-
"sdpMLineIndex": 0
252-
}
244+
cand_str = line[len("a=") :]
245+
candidate_init = {"candidate": cand_str, "sdpMLineIndex": 0}
253246
data = json.dumps(candidate_init)
254247
msg.data = data
255248
await _send_signaling_message(stream, msg)
256249
logger.debug("Sent ICE candidate init: %s", candidate_init)
257250
# Mark end-of-candidates
258251
msg = Message(type=Message.ICE_CANDIDATE, data=json.dumps(None))
259-
await _send_signaling_message(stream, msg)
252+
await _send_signaling_message(stream, msg)
260253
logger.debug("Sent end-of-ICE marker")
261-
254+
262255

263256
async def _handle_incoming_ice_candidates(
264257
stream: INetStream, peer_connection: RTCPeerConnection, timeout: float
@@ -274,31 +267,31 @@ async def _handle_incoming_ice_candidates(
274267
if cancel_scope.cancelled_caught:
275268
logger.warning("ICE candidate receive timeout")
276269
break
277-
270+
278271
# stream ended or we became connected
279272
if not message:
280273
logger.error("Null message recieved")
281274
break
282-
275+
283276
if message.type != Message.ICE_CANDIDATE:
284277
logger.error("ICE candidate message expected. Exiting...")
285278
raise WebRTCError("ICE candidate message expected.")
286-
break
287-
279+
break
280+
288281
# Candidate init cannot be null
289-
if message.data == '':
282+
if message.data == "":
290283
logger.debug("candidate received is empty")
291284
continue
292-
285+
293286
logger.debug("Recieved new ICE Candidate")
294287
try:
295288
candidate_init = json.loads(message.data)
296289
except json.JSONDecodeError:
297290
logger.error("Invalid ICE candidate JSON: %s", message.data)
298291
break
299-
292+
300293
bridge = TrioSafeWebRTCOperations._get_bridge()
301-
294+
302295
# None means ICE gathering is fully complete
303296
if candidate_init is None:
304297
logger.debug("Received ICE candidate null → end-of-ice signal")
@@ -308,7 +301,9 @@ async def _handle_incoming_ice_candidates(
308301

309302
# CandidateInit is expected to be a dict
310303
if isinstance(candidate_init, dict) and "candidate" in candidate_init:
311-
candidate = candidate_from_aioice(Candidate.from_sdp(candidate_init["candidate"]))
304+
candidate = candidate_from_aioice(
305+
Candidate.from_sdp(candidate_init["candidate"])
306+
)
312307
async with bridge:
313308
await bridge.add_ice_candidate(peer_connection, candidate)
314309
logger.debug("Added ICE candidate: %r", candidate_init)

libp2p/transport/webrtc/private_to_private/signaling_stream_handler.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
from libp2p.abc import INetStream, IRawConnection
1414
from libp2p.crypto.ed25519 import create_new_key_pair
1515
from libp2p.peer.id import ID
16-
from .pb import Message
16+
1717
from ..connection import WebRTCRawConnection
1818
from ..constants import WebRTCError
19+
from .pb import Message
1920

2021
logger = logging.getLogger("webrtc.private.signaling_stream_handler")
2122

@@ -72,12 +73,10 @@ def on_channel_open() -> None:
7273
raise WebRTCError("No offer data received")
7374
offer_message = Message()
7475
offer_message.ParseFromString(offer_data)
75-
if offer_message.get("type") != "offer":
76-
raise WebRTCError(f"Expected offer, got: {offer_message.get('type')}")
76+
if offer_message.type != Message.SDP_OFFER:
77+
raise WebRTCError(f"Expected offer, got: {offer_message.type}")
7778

78-
offer = RTCSessionDescription(
79-
sdp=offer_message["sdp"], type=offer_message["type"]
80-
)
79+
offer = RTCSessionDescription(sdp=offer_message.data, type="offer")
8180

8281
logger.info("Received SDP offer")
8382

@@ -86,7 +85,7 @@ def on_channel_open() -> None:
8685

8786
# Set remote description
8887
await aio_as_trio(peer_connection.setRemoteDescription)(offer)
89-
logger.info("Set remote description from offer")
88+
logger.debug("Set remote description from offer")
9089

9190
# Create and set local description (answer)
9291
answer = await aio_as_trio(peer_connection.createAnswer)()
@@ -98,7 +97,7 @@ def on_channel_open() -> None:
9897
answer_message = Message()
9998
answer_message.type = Message.SDP_ANSWER
10099
answer_message.data = answer.sdp
101-
await stream.write(answer_message)
100+
await stream.write(answer_message.SerializeToString())
102101
logger.info("Sent SDP answer")
103102

104103
except Exception as e:

0 commit comments

Comments
 (0)