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:
Arush Kurundodi
2025-06-09 23:09:59 +05:30
committed by GitHub
parent d020bbc066
commit bdadec7519
111 changed files with 1537 additions and 1401 deletions

View File

@ -7,6 +7,9 @@ from trio.testing import (
memory_stream_pair,
)
from libp2p.abc import (
IRawConnection,
)
from libp2p.crypto.ed25519 import (
create_new_key_pair,
)
@ -29,18 +32,19 @@ from libp2p.stream_muxer.yamux.yamux import (
)
class TrioStreamAdapter:
def __init__(self, send_stream, receive_stream):
class TrioStreamAdapter(IRawConnection):
def __init__(self, send_stream, receive_stream, is_initiator: bool = False):
self.send_stream = send_stream
self.receive_stream = receive_stream
self.is_initiator = is_initiator
async def write(self, data):
async def write(self, data: bytes) -> None:
logging.debug(f"Writing {len(data)} bytes")
with trio.move_on_after(2):
await self.send_stream.send_all(data)
async def read(self, n=-1):
if n == -1:
async def read(self, n: int | None = None) -> bytes:
if n is None or n == -1:
raise ValueError("Reading unbounded not supported")
logging.debug(f"Attempting to read {n} bytes")
with trio.move_on_after(2):
@ -48,9 +52,13 @@ class TrioStreamAdapter:
logging.debug(f"Read {len(data)} bytes")
return data
async def close(self):
async def close(self) -> None:
logging.debug("Closing stream")
def get_remote_address(self) -> tuple[str, int] | None:
# Return None since this is a test adapter without real network info
return None
@pytest.fixture
def key_pair():
@ -68,8 +76,8 @@ async def secure_conn_pair(key_pair, peer_id):
client_send, server_receive = memory_stream_pair()
server_send, client_receive = memory_stream_pair()
client_rw = TrioStreamAdapter(client_send, client_receive)
server_rw = TrioStreamAdapter(server_send, server_receive)
client_rw = TrioStreamAdapter(client_send, client_receive, is_initiator=True)
server_rw = TrioStreamAdapter(server_send, server_receive, is_initiator=False)
insecure_transport = InsecureTransport(key_pair)
@ -196,9 +204,9 @@ async def test_yamux_stream_close(yamux_pair):
await trio.sleep(0.1)
# Now both directions are closed, so stream should be fully closed
assert (
client_stream.closed
), "Client stream should be fully closed after bidirectional close"
assert client_stream.closed, (
"Client stream should be fully closed after bidirectional close"
)
# Writing should still fail
with pytest.raises(MuxedStreamError):
@ -215,8 +223,12 @@ async def test_yamux_stream_reset(yamux_pair):
server_stream = await server_yamux.accept_stream()
await client_stream.reset()
# After reset, reading should raise MuxedStreamReset or MuxedStreamEOF
with pytest.raises((MuxedStreamEOF, MuxedStreamError)):
try:
await server_stream.read()
except (MuxedStreamEOF, MuxedStreamError):
pass
else:
pytest.fail("Expected MuxedStreamEOF or MuxedStreamError")
# Verify subsequent operations fail with StreamReset or EOF
with pytest.raises(MuxedStreamError):
await server_stream.read()
@ -269,9 +281,9 @@ async def test_yamux_flow_control(yamux_pair):
await client_stream.write(large_data)
# Check that window was reduced
assert (
client_stream.send_window < initial_window
), "Window should be reduced after sending"
assert client_stream.send_window < initial_window, (
"Window should be reduced after sending"
)
# Read the data on the server side
received = b""
@ -307,9 +319,9 @@ async def test_yamux_flow_control(yamux_pair):
f" {client_stream.send_window},"
f"initial half: {initial_window // 2}"
)
assert (
client_stream.send_window > initial_window // 2
), "Window should be increased after update"
assert client_stream.send_window > initial_window // 2, (
"Window should be increased after update"
)
await client_stream.close()
await server_stream.close()
@ -349,17 +361,17 @@ async def test_yamux_half_close(yamux_pair):
test_data = b"server response after client close"
# The server shouldn't be marked as send_closed yet
assert (
not server_stream.send_closed
), "Server stream shouldn't be marked as send_closed"
assert not server_stream.send_closed, (
"Server stream shouldn't be marked as send_closed"
)
await server_stream.write(test_data)
# Client can still read
received = await client_stream.read(len(test_data))
assert (
received == test_data
), "Client should still be able to read after sending FIN"
assert received == test_data, (
"Client should still be able to read after sending FIN"
)
# Now server closes its sending side
await server_stream.close()
@ -406,9 +418,9 @@ async def test_yamux_go_away_with_error(yamux_pair):
await trio.sleep(0.2)
# Verify server recognized shutdown
assert (
server_yamux.event_shutting_down.is_set()
), "Server should be shutting down after GO_AWAY"
assert server_yamux.event_shutting_down.is_set(), (
"Server should be shutting down after GO_AWAY"
)
logging.debug("test_yamux_go_away_with_error complete")