run lint and fix errors, except mypy

This commit is contained in:
pacrob
2024-02-19 15:56:20 -07:00
parent 42605c0288
commit 94483714a3
171 changed files with 4809 additions and 2290 deletions

View File

@ -1,4 +1,6 @@
from enum import Enum
from enum import (
Enum,
)
class HeaderTags(Enum):

View File

@ -1,4 +1,6 @@
from typing import NamedTuple
from typing import (
NamedTuple,
)
class StreamID(NamedTuple):

View File

@ -1,15 +1,34 @@
import logging
from typing import Dict, Optional, Tuple
from typing import (
Dict,
Optional,
Tuple,
)
import trio
from libp2p.exceptions import ParseError
from libp2p.io.exceptions import IncompleteReadError
from libp2p.network.connection.exceptions import RawConnError
from libp2p.peer.id import ID
from libp2p.security.secure_conn_interface import ISecureConn
from libp2p.stream_muxer.abc import IMuxedConn, IMuxedStream
from libp2p.typing import TProtocol
from libp2p.exceptions import (
ParseError,
)
from libp2p.io.exceptions import (
IncompleteReadError,
)
from libp2p.network.connection.exceptions import (
RawConnError,
)
from libp2p.peer.id import (
ID,
)
from libp2p.security.secure_conn_interface import (
ISecureConn,
)
from libp2p.stream_muxer.abc import (
IMuxedConn,
IMuxedStream,
)
from libp2p.typing import (
TProtocol,
)
from libp2p.utils import (
decode_uvarint_from_stream,
encode_uvarint,
@ -17,10 +36,18 @@ from libp2p.utils import (
read_varint_prefixed_bytes,
)
from .constants import HeaderTags
from .datastructures import StreamID
from .exceptions import MplexUnavailable
from .mplex_stream import MplexStream
from .constants import (
HeaderTags,
)
from .datastructures import (
StreamID,
)
from .exceptions import (
MplexUnavailable,
)
from .mplex_stream import (
MplexStream,
)
MPLEX_PROTOCOL_ID = TProtocol("/mplex/6.7.0")
# Ref: https://github.com/libp2p/go-mplex/blob/414db61813d9ad3e6f4a7db5c1b1612de343ace9/multiplex.go#L115 # noqa: E501
@ -49,7 +76,7 @@ class Mplex(IMuxedConn):
def __init__(self, secured_conn: ISecureConn, peer_id: ID) -> None:
"""
create a new muxed connection.
Create a new muxed connection.
:param secured_conn: an instance of ``ISecureConn``
:param generic_protocol_handler: generic protocol handler
@ -81,7 +108,9 @@ class Mplex(IMuxedConn):
return self.secured_conn.is_initiator
async def close(self) -> None:
"""close the stream muxer and underlying secured connection."""
"""
Close the stream muxer and underlying secured connection.
"""
if self.event_shutting_down.is_set():
return
# Set the `event_shutting_down`, to allow graceful shutdown.
@ -93,7 +122,7 @@ class Mplex(IMuxedConn):
@property
def is_closed(self) -> bool:
"""
check connection is fully closed.
Check connection is fully closed.
:return: true if successful
"""
@ -121,7 +150,7 @@ class Mplex(IMuxedConn):
async def open_stream(self) -> IMuxedStream:
"""
creates a new muxed_stream.
Create a new muxed_stream.
:return: a new ``MplexStream``
"""
@ -134,7 +163,9 @@ class Mplex(IMuxedConn):
return stream
async def accept_stream(self) -> IMuxedStream:
"""accepts a muxed stream opened by the other end."""
"""
Accept a muxed stream opened by the other end.
"""
try:
return await self.new_stream_receive_channel.receive()
except trio.EndOfChannel:
@ -144,7 +175,7 @@ class Mplex(IMuxedConn):
self, flag: HeaderTags, data: Optional[bytes], stream_id: StreamID
) -> int:
"""
sends a message over the connection.
Send a message over the connection.
:param flag: header to use
:param data: data to send in the message
@ -162,7 +193,7 @@ class Mplex(IMuxedConn):
async def write_to_stream(self, _bytes: bytes) -> None:
"""
writes a byte array to a secured connection.
Write a byte array to a secured connection.
:param _bytes: byte array to write
:return: length written
@ -175,8 +206,10 @@ class Mplex(IMuxedConn):
) from e
async def handle_incoming(self) -> None:
"""Read a message off of the secured connection and add it to the
corresponding message buffer."""
"""
Read a message off of the secured connection and add it to the
corresponding message buffer.
"""
self.event_started.set()
while True:
try:
@ -194,19 +227,19 @@ class Mplex(IMuxedConn):
:return: stream_id, flag, message contents
"""
try:
header = await decode_uvarint_from_stream(self.secured_conn)
except (ParseError, RawConnError, IncompleteReadError) as error:
raise MplexUnavailable(
f"failed to read the header correctly from the underlying connection: {error}"
"failed to read the header correctly from the underlying connection: "
f"{error}"
)
try:
message = await read_varint_prefixed_bytes(self.secured_conn)
except (ParseError, RawConnError, IncompleteReadError) as error:
raise MplexUnavailable(
"failed to read the message body correctly from the underlying connection: "
f"{error}"
"failed to read the message body correctly from the underlying "
f"connection: {error}"
)
flag = header & 0x07

View File

@ -1,16 +1,32 @@
from typing import TYPE_CHECKING
from typing import (
TYPE_CHECKING,
)
import trio
from libp2p.stream_muxer.abc import IMuxedStream
from libp2p.stream_muxer.exceptions import MuxedConnUnavailable
from libp2p.stream_muxer.abc import (
IMuxedStream,
)
from libp2p.stream_muxer.exceptions import (
MuxedConnUnavailable,
)
from .constants import HeaderTags
from .datastructures import StreamID
from .exceptions import MplexStreamClosed, MplexStreamEOF, MplexStreamReset
from .constants import (
HeaderTags,
)
from .datastructures import (
StreamID,
)
from .exceptions import (
MplexStreamClosed,
MplexStreamEOF,
MplexStreamReset,
)
if TYPE_CHECKING:
from libp2p.stream_muxer.mplex.mplex import Mplex
from libp2p.stream_muxer.mplex.mplex import (
Mplex,
)
class MplexStream(IMuxedStream):
@ -44,7 +60,7 @@ class MplexStream(IMuxedStream):
incoming_data_channel: "trio.MemoryReceiveChannel[bytes]",
) -> None:
"""
create new MuxedStream in muxer.
Create new MuxedStream in muxer.
:param stream_id: stream id of this stream
:param muxed_conn: muxed connection of this muxed_stream
@ -93,8 +109,8 @@ class MplexStream(IMuxedStream):
"""
if n is not None and n < 0:
raise ValueError(
f"the number of bytes to read `n` must be non-negative or "
"`None` to indicate read until EOF"
"the number of bytes to read `n` must be non-negative or "
f"`None` to indicate read until EOF, got n={n}"
)
if self.event_reset.is_set():
raise MplexStreamReset
@ -102,16 +118,16 @@ class MplexStream(IMuxedStream):
return await self._read_until_eof()
if len(self._buf) == 0:
data: bytes
# Peek whether there is data available. If yes, we just read until there is no data,
# and then return.
# Peek whether there is data available. If yes, we just read until there is
# no data, then return.
try:
data = self.incoming_data_channel.receive_nowait()
self._buf.extend(data)
except trio.EndOfChannel:
raise MplexStreamEOF
except trio.WouldBlock:
# We know `receive` will be blocked here. Wait for data here with `receive` and
# catch all kinds of errors here.
# We know `receive` will be blocked here. Wait for data here with
# `receive` and catch all kinds of errors here.
try:
data = await self.incoming_data_channel.receive()
self._buf.extend(data)
@ -121,8 +137,8 @@ class MplexStream(IMuxedStream):
if self.event_remote_closed.is_set():
raise MplexStreamEOF
except trio.ClosedResourceError as error:
# Probably `incoming_data_channel` is closed in `reset` when we are waiting
# for `receive`.
# Probably `incoming_data_channel` is closed in `reset` when we are
# waiting for `receive`.
if self.event_reset.is_set():
raise MplexStreamReset
raise Exception(
@ -136,7 +152,7 @@ class MplexStream(IMuxedStream):
async def write(self, data: bytes) -> None:
"""
write to stream.
Write to stream.
:return: number of bytes written
"""
@ -150,8 +166,10 @@ class MplexStream(IMuxedStream):
await self.muxed_conn.send_message(flag, data, self.stream_id)
async def close(self) -> None:
"""Closing a stream closes it for writing and closes the remote end for
reading but allows writing in the other direction."""
"""
Closing a stream closes it for writing and closes the remote end for
reading but allows writing in the other direction.
"""
# TODO error handling with timeout
async with self.close_lock:
@ -175,7 +193,7 @@ class MplexStream(IMuxedStream):
self.muxed_conn.streams.pop(self.stream_id, None)
async def reset(self) -> None:
"""closes both ends of the stream tells this remote side to hang up."""
"""Close both ends of the stream tells this remote side to hang up."""
async with self.close_lock:
# Both sides have been closed. No need to event_reset.
if self.event_remote_closed.is_set() and self.event_local_closed.is_set():
@ -190,7 +208,8 @@ class MplexStream(IMuxedStream):
if self.is_initiator
else HeaderTags.ResetReceiver
)
# Try to send reset message to the other side. Ignore if there is anything wrong.
# Try to send reset message to the other side.
# Ignore if there is anything wrong.
try:
await self.muxed_conn.send_message(flag, None, self.stream_id)
except MuxedConnUnavailable:
@ -208,7 +227,7 @@ class MplexStream(IMuxedStream):
# TODO deadline not in use
def set_deadline(self, ttl: int) -> bool:
"""
set deadline for muxed stream.
Set deadline for muxed stream.
:return: True if successful
"""
@ -218,7 +237,7 @@ class MplexStream(IMuxedStream):
def set_read_deadline(self, ttl: int) -> bool:
"""
set read deadline for muxed stream.
Set read deadline for muxed stream.
:return: True if successful
"""
@ -227,7 +246,7 @@ class MplexStream(IMuxedStream):
def set_write_deadline(self, ttl: int) -> bool:
"""
set write deadline for muxed stream.
Set write deadline for muxed stream.
:return: True if successful
"""