mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 15:10:54 +00:00
ft. modernise py-libp2p (#618)
* fix pyproject.toml , add ruff * rm lock * make progress * add poetry lock ignore * fix type issues * fix tcp type errors * fix text example - type error - wrong args * add setuptools to dev * test ci * fix docs build * fix type issues for new_swarm & new_host * fix types in gossipsub * fix type issues in noise * wip: factories * revert factories * fix more type issues * more type fixes * fix: add null checks for noise protocol initialization and key handling * corrected argument-errors in peerId and Multiaddr in peer tests * fix: Noice - remove redundant type casts in BaseNoiseMsgReadWriter * fix: update test_notify.py to use SwarmFactory.create_batch_and_listen, fix type hints, and comment out ClosedStream assertions * Fix type checks for pubsub module Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com> * Fix type checks for pubsub module-tests Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com> * noise: add checks for uninitialized protocol and key states in PatternXX Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * pubsub: add None checks for optional fields in FloodSub and Pubsub Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * Fix type hints and improve testing Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * remove redundant checks Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * fix build issues * add optional to trio service * fix types * fix type errors * Fix type errors Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * fixed more-type checks in crypto and peer_data files * wip: factories * replaced union with optional * fix: type-error in interp-utils and peerinfo * replace pyright with pyrefly * add pyrefly.toml * wip: fix multiselect issues * try typecheck * base check * mcache test fixes , typecheck ci update * fix ci * will this work * minor fix * use poetry * fix wokflow * use cache,fix err * fix pyrefly.toml * fix pyrefly.toml * fix cache in ci * deploy commit * add main baseline * update to v5 * improve typecheck ci (#14) * fix typo * remove holepunching code (#16) * fix gossipsub typeerrors (#17) * fix: ensure initiator user includes remote peer id in handshake (#15) * fix ci (#19) * typefix: custom_types | core/peerinfo/test_peer_info | io/abc | pubsub/floodsub | protocol_muxer/multiselect (#18) * fix: Typefixes in PeerInfo (#21) * fix minor type issue (#22) * fix type errors in pubsub (#24) * fix: Minor typefixes in tests (#23) * Fix failing tests for type-fixed test/pubsub (#8) * move pyrefly & ruff to pyproject.toml & rm .project-template (#28) * move the async_context file to tests/core * move crypto test to crypto folder * fix: some typefixes (#25) * fix type errors * fix type issues * fix: update gRPC API usage in autonat_pb2_grpc.py (#31) * md: typecheck ci * rm comments * clean up : from review suggestions * use | None over Optional as per new python standards * drop supporto for py3.9 * newsfragments --------- Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com> Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> Co-authored-by: acul71 <luca.pisani@birdo.net> Co-authored-by: kaneki003 <sakshamchauhan707@gmail.com> Co-authored-by: sukhman <sukhmansinghsaluja@gmail.com> Co-authored-by: varun-r-mallya <varunrmallya@gmail.com> Co-authored-by: varunrmallya <100590632+varun-r-mallya@users.noreply.github.com> Co-authored-by: lla-dane <abhinavagarwalla6@gmail.com> Co-authored-by: Collins <ArtemisfowlX@protonmail.com> Co-authored-by: Abhinav Agarwalla <120122716+lla-dane@users.noreply.github.com> Co-authored-by: guha-rahul <52607971+guha-rahul@users.noreply.github.com> Co-authored-by: Sukhman Singh <63765293+sukhman-sukh@users.noreply.github.com> Co-authored-by: acul71 <34693171+acul71@users.noreply.github.com> Co-authored-by: pacrob <5199899+pacrob@users.noreply.github.com>
This commit is contained in:
@ -1,7 +1,4 @@
|
||||
import logging
|
||||
from typing import (
|
||||
Optional,
|
||||
)
|
||||
|
||||
import trio
|
||||
|
||||
@ -168,7 +165,7 @@ class Mplex(IMuxedConn):
|
||||
raise MplexUnavailable
|
||||
|
||||
async def send_message(
|
||||
self, flag: HeaderTags, data: Optional[bytes], stream_id: StreamID
|
||||
self, flag: HeaderTags, data: bytes | None, stream_id: StreamID
|
||||
) -> int:
|
||||
"""
|
||||
Send a message over the connection.
|
||||
@ -366,6 +363,6 @@ class Mplex(IMuxedConn):
|
||||
self.event_closed.set()
|
||||
await self.new_stream_send_channel.aclose()
|
||||
|
||||
def get_remote_address(self) -> Optional[tuple[str, int]]:
|
||||
def get_remote_address(self) -> tuple[str, int] | None:
|
||||
"""Delegate to the underlying Mplex connection's secured_conn."""
|
||||
return self.secured_conn.get_remote_address()
|
||||
|
||||
@ -3,7 +3,6 @@ from types import (
|
||||
)
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Optional,
|
||||
)
|
||||
|
||||
import trio
|
||||
@ -40,9 +39,12 @@ class MplexStream(IMuxedStream):
|
||||
|
||||
name: str
|
||||
stream_id: StreamID
|
||||
muxed_conn: "Mplex"
|
||||
read_deadline: int
|
||||
write_deadline: int
|
||||
# NOTE: All methods used here are part of `Mplex` which is a derived
|
||||
# class of IMuxedConn. Ignoring this type assignment should not pose
|
||||
# any risk.
|
||||
muxed_conn: "Mplex" # type: ignore[assignment]
|
||||
read_deadline: int | None
|
||||
write_deadline: int | None
|
||||
|
||||
# TODO: Add lock for read/write to avoid interleaving receiving messages?
|
||||
close_lock: trio.Lock
|
||||
@ -92,7 +94,7 @@ class MplexStream(IMuxedStream):
|
||||
self._buf = self._buf[len(payload) :]
|
||||
return bytes(payload)
|
||||
|
||||
def _read_return_when_blocked(self) -> bytes:
|
||||
def _read_return_when_blocked(self) -> bytearray:
|
||||
buf = bytearray()
|
||||
while True:
|
||||
try:
|
||||
@ -102,7 +104,7 @@ class MplexStream(IMuxedStream):
|
||||
break
|
||||
return buf
|
||||
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
async def read(self, n: int | None = None) -> bytes:
|
||||
"""
|
||||
Read up to n bytes. Read possibly returns fewer than `n` bytes, if
|
||||
there are not enough bytes in the Mplex buffer. If `n is None`, read
|
||||
@ -257,7 +259,7 @@ class MplexStream(IMuxedStream):
|
||||
self.write_deadline = ttl
|
||||
return True
|
||||
|
||||
def get_remote_address(self) -> Optional[tuple[str, int]]:
|
||||
def get_remote_address(self) -> tuple[str, int] | None:
|
||||
"""Delegate to the parent Mplex connection."""
|
||||
return self.muxed_conn.get_remote_address()
|
||||
|
||||
@ -267,9 +269,9 @@ class MplexStream(IMuxedStream):
|
||||
|
||||
async def __aexit__(
|
||||
self,
|
||||
exc_type: Optional[type[BaseException]],
|
||||
exc_val: Optional[BaseException],
|
||||
exc_tb: Optional[TracebackType],
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: TracebackType | None,
|
||||
) -> None:
|
||||
"""Exit the async context manager and close the stream."""
|
||||
await self.close()
|
||||
|
||||
@ -95,7 +95,7 @@ class MuxerMultistream:
|
||||
if protocol == PROTOCOL_ID:
|
||||
async with trio.open_nursery():
|
||||
|
||||
def on_close() -> None:
|
||||
async def on_close() -> None:
|
||||
pass
|
||||
|
||||
return Yamux(
|
||||
|
||||
@ -3,8 +3,10 @@ Yamux stream multiplexer implementation for py-libp2p.
|
||||
This is the preferred multiplexing protocol due to its performance and feature set.
|
||||
Mplex is also available for legacy compatibility but may be deprecated in the future.
|
||||
"""
|
||||
|
||||
from collections.abc import (
|
||||
Awaitable,
|
||||
Callable,
|
||||
)
|
||||
import inspect
|
||||
import logging
|
||||
@ -13,8 +15,7 @@ from types import (
|
||||
TracebackType,
|
||||
)
|
||||
from typing import (
|
||||
Callable,
|
||||
Optional,
|
||||
Any,
|
||||
)
|
||||
|
||||
import trio
|
||||
@ -83,9 +84,9 @@ class YamuxStream(IMuxedStream):
|
||||
|
||||
async def __aexit__(
|
||||
self,
|
||||
exc_type: Optional[type[BaseException]],
|
||||
exc_val: Optional[BaseException],
|
||||
exc_tb: Optional[TracebackType],
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: TracebackType | None,
|
||||
) -> None:
|
||||
"""Exit the async context manager and close the stream."""
|
||||
await self.close()
|
||||
@ -126,7 +127,7 @@ class YamuxStream(IMuxedStream):
|
||||
if self.send_window < DEFAULT_WINDOW_SIZE // 2:
|
||||
await self.send_window_update()
|
||||
|
||||
async def send_window_update(self, increment: Optional[int] = None) -> None:
|
||||
async def send_window_update(self, increment: int | None = None) -> None:
|
||||
"""Send a window update to peer."""
|
||||
if increment is None:
|
||||
increment = DEFAULT_WINDOW_SIZE - self.recv_window
|
||||
@ -141,7 +142,7 @@ class YamuxStream(IMuxedStream):
|
||||
)
|
||||
await self.conn.secured_conn.write(header)
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
async def read(self, n: int | None = -1) -> bytes:
|
||||
# Handle None value for n by converting it to -1
|
||||
if n is None:
|
||||
n = -1
|
||||
@ -161,8 +162,7 @@ class YamuxStream(IMuxedStream):
|
||||
if buffer and len(buffer) > 0:
|
||||
# Wait for closure even if data is available
|
||||
logging.debug(
|
||||
f"Stream {self.stream_id}:"
|
||||
f"Waiting for FIN before returning data"
|
||||
f"Stream {self.stream_id}:Waiting for FIN before returning data"
|
||||
)
|
||||
await self.conn.stream_events[self.stream_id].wait()
|
||||
self.conn.stream_events[self.stream_id] = trio.Event()
|
||||
@ -240,7 +240,7 @@ class YamuxStream(IMuxedStream):
|
||||
"""
|
||||
raise NotImplementedError("Yamux does not support setting read deadlines")
|
||||
|
||||
def get_remote_address(self) -> Optional[tuple[str, int]]:
|
||||
def get_remote_address(self) -> tuple[str, int] | None:
|
||||
"""
|
||||
Returns the remote address of the underlying connection.
|
||||
"""
|
||||
@ -268,8 +268,8 @@ class Yamux(IMuxedConn):
|
||||
self,
|
||||
secured_conn: ISecureConn,
|
||||
peer_id: ID,
|
||||
is_initiator: Optional[bool] = None,
|
||||
on_close: Optional[Callable[[], Awaitable[None]]] = None,
|
||||
is_initiator: bool | None = None,
|
||||
on_close: Callable[[], Awaitable[Any]] | None = None,
|
||||
) -> None:
|
||||
self.secured_conn = secured_conn
|
||||
self.peer_id = peer_id
|
||||
@ -283,7 +283,7 @@ class Yamux(IMuxedConn):
|
||||
self.is_initiator_value = (
|
||||
is_initiator if is_initiator is not None else secured_conn.is_initiator
|
||||
)
|
||||
self.next_stream_id = 1 if self.is_initiator_value else 2
|
||||
self.next_stream_id: int = 1 if self.is_initiator_value else 2
|
||||
self.streams: dict[int, YamuxStream] = {}
|
||||
self.streams_lock = trio.Lock()
|
||||
self.new_stream_send_channel: MemorySendChannel[YamuxStream]
|
||||
@ -297,7 +297,7 @@ class Yamux(IMuxedConn):
|
||||
self.event_started = trio.Event()
|
||||
self.stream_buffers: dict[int, bytearray] = {}
|
||||
self.stream_events: dict[int, trio.Event] = {}
|
||||
self._nursery: Optional[Nursery] = None
|
||||
self._nursery: Nursery | None = None
|
||||
|
||||
async def start(self) -> None:
|
||||
logging.debug(f"Starting Yamux for {self.peer_id}")
|
||||
@ -465,8 +465,14 @@ class Yamux(IMuxedConn):
|
||||
|
||||
# Wait for data if stream is still open
|
||||
logging.debug(f"Waiting for data on stream {self.peer_id}:{stream_id}")
|
||||
await self.stream_events[stream_id].wait()
|
||||
self.stream_events[stream_id] = trio.Event()
|
||||
try:
|
||||
await self.stream_events[stream_id].wait()
|
||||
self.stream_events[stream_id] = trio.Event()
|
||||
except KeyError:
|
||||
raise MuxedStreamEOF("Stream was removed")
|
||||
|
||||
# This line should never be reached, but satisfies the type checker
|
||||
raise MuxedStreamEOF("Unexpected end of read_stream")
|
||||
|
||||
async def handle_incoming(self) -> None:
|
||||
while not self.event_shutting_down.is_set():
|
||||
@ -474,8 +480,7 @@ class Yamux(IMuxedConn):
|
||||
header = await self.secured_conn.read(HEADER_SIZE)
|
||||
if not header or len(header) < HEADER_SIZE:
|
||||
logging.debug(
|
||||
f"Connection closed or"
|
||||
f"incomplete header for peer {self.peer_id}"
|
||||
f"Connection closed orincomplete header for peer {self.peer_id}"
|
||||
)
|
||||
self.event_shutting_down.set()
|
||||
await self._cleanup_on_error()
|
||||
@ -544,8 +549,7 @@ class Yamux(IMuxedConn):
|
||||
)
|
||||
elif error_code == GO_AWAY_PROTOCOL_ERROR:
|
||||
logging.error(
|
||||
f"Received GO_AWAY for peer"
|
||||
f"{self.peer_id}: Protocol error"
|
||||
f"Received GO_AWAY for peer{self.peer_id}: Protocol error"
|
||||
)
|
||||
elif error_code == GO_AWAY_INTERNAL_ERROR:
|
||||
logging.error(
|
||||
|
||||
Reference in New Issue
Block a user