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,28 +1,70 @@
import logging
from typing import TYPE_CHECKING, AsyncIterator, List, Sequence
from typing import (
TYPE_CHECKING,
AsyncIterator,
List,
Sequence,
)
from async_generator import asynccontextmanager
from async_service import background_trio_service
from async_generator import (
asynccontextmanager,
)
from async_service import (
background_trio_service,
)
import multiaddr
from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.host.defaults import get_default_protocols
from libp2p.host.exceptions import StreamFailure
from libp2p.network.network_interface import INetworkService
from libp2p.network.stream.net_stream_interface import INetStream
from libp2p.peer.id import ID
from libp2p.peer.peerinfo import PeerInfo
from libp2p.peer.peerstore_interface import IPeerStore
from libp2p.protocol_muxer.exceptions import MultiselectClientError, MultiselectError
from libp2p.protocol_muxer.multiselect import Multiselect
from libp2p.protocol_muxer.multiselect_client import MultiselectClient
from libp2p.protocol_muxer.multiselect_communicator import MultiselectCommunicator
from libp2p.typing import StreamHandlerFn, TProtocol
from libp2p.crypto.keys import (
PrivateKey,
PublicKey,
)
from libp2p.host.defaults import (
get_default_protocols,
)
from libp2p.host.exceptions import (
StreamFailure,
)
from libp2p.network.network_interface import (
INetworkService,
)
from libp2p.network.stream.net_stream_interface import (
INetStream,
)
from libp2p.peer.id import (
ID,
)
from libp2p.peer.peerinfo import (
PeerInfo,
)
from libp2p.peer.peerstore_interface import (
IPeerStore,
)
from libp2p.protocol_muxer.exceptions import (
MultiselectClientError,
MultiselectError,
)
from libp2p.protocol_muxer.multiselect import (
Multiselect,
)
from libp2p.protocol_muxer.multiselect_client import (
MultiselectClient,
)
from libp2p.protocol_muxer.multiselect_communicator import (
MultiselectCommunicator,
)
from libp2p.typing import (
StreamHandlerFn,
TProtocol,
)
from .host_interface import IHost
from .host_interface import (
IHost,
)
if TYPE_CHECKING:
from collections import OrderedDict
from collections import (
OrderedDict,
)
# Upon host creation, host takes in options,
# including the list of addresses on which to listen.
@ -108,7 +150,7 @@ class BasicHost(IHost):
self, listen_addrs: Sequence[multiaddr.Multiaddr]
) -> AsyncIterator[None]:
"""
run the host instance and listen to ``listen_addrs``.
Run the host instance and listen to ``listen_addrs``.
:param listen_addrs: a sequence of multiaddrs that we want to listen to
"""
@ -121,7 +163,7 @@ class BasicHost(IHost):
self, protocol_id: TProtocol, stream_handler: StreamHandlerFn
) -> None:
"""
set stream handler for given `protocol_id`
Set stream handler for given `protocol_id`
:param protocol_id: protocol id used on stream
:param stream_handler: a stream handler function
@ -136,7 +178,6 @@ class BasicHost(IHost):
:param protocol_ids: available protocol ids to use for stream
:return: stream: new stream created
"""
net_stream = await self._network.new_stream(peer_id)
# Perform protocol muxing to determine protocol to use
@ -154,7 +195,7 @@ class BasicHost(IHost):
async def connect(self, peer_info: PeerInfo) -> None:
"""
connect ensures there is a connection between this host and the peer
Ensure there is a connection between this host and the peer
with given `peer_info.peer_id`. connect will absorb the addresses in
peer_info into its internal peerstore. If there is not an active
connection, connect will issue a dial, and block until a connection is

View File

@ -1,14 +1,27 @@
from collections import OrderedDict
from typing import TYPE_CHECKING
from collections import (
OrderedDict,
)
from typing import (
TYPE_CHECKING,
)
from libp2p.host.host_interface import IHost
from libp2p.host.host_interface import (
IHost,
)
from libp2p.host.ping import (
handle_ping,
)
from libp2p.host.ping import ID as PingID
from libp2p.host.ping import handle_ping
from libp2p.identity.identify.protocol import (
identify_handler_for,
)
from libp2p.identity.identify.protocol import ID as IdentifyID
from libp2p.identity.identify.protocol import identify_handler_for
if TYPE_CHECKING:
from libp2p.typing import TProtocol, StreamHandlerFn
from libp2p.typing import (
StreamHandlerFn,
TProtocol,
)
def get_default_protocols(host: IHost) -> "OrderedDict[TProtocol, StreamHandlerFn]":

View File

@ -1,4 +1,6 @@
from libp2p.exceptions import BaseLibp2pError
from libp2p.exceptions import (
BaseLibp2pError,
)
class HostException(BaseLibp2pError):

View File

@ -1,14 +1,36 @@
from abc import ABC, abstractmethod
from typing import Any, AsyncContextManager, List, Sequence
from abc import (
ABC,
abstractmethod,
)
from typing import (
Any,
AsyncContextManager,
List,
Sequence,
)
import multiaddr
from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.network.network_interface import INetworkService
from libp2p.network.stream.net_stream_interface import INetStream
from libp2p.peer.id import ID
from libp2p.peer.peerinfo import PeerInfo
from libp2p.typing import StreamHandlerFn, TProtocol
from libp2p.crypto.keys import (
PrivateKey,
PublicKey,
)
from libp2p.network.network_interface import (
INetworkService,
)
from libp2p.network.stream.net_stream_interface import (
INetStream,
)
from libp2p.peer.id import (
ID,
)
from libp2p.peer.peerinfo import (
PeerInfo,
)
from libp2p.typing import (
StreamHandlerFn,
TProtocol,
)
class IHost(ABC):
@ -54,7 +76,7 @@ class IHost(ABC):
self, listen_addrs: Sequence[multiaddr.Multiaddr]
) -> AsyncContextManager[None]:
"""
run the host instance and listen to ``listen_addrs``.
Run the host instance and listen to ``listen_addrs``.
:param listen_addrs: a sequence of multiaddrs that we want to listen to
"""
@ -64,7 +86,7 @@ class IHost(ABC):
self, protocol_id: TProtocol, stream_handler: StreamHandlerFn
) -> None:
"""
set stream handler for host.
Set stream handler for host.
:param protocol_id: protocol id used on stream
:param stream_handler: a stream handler function
@ -85,7 +107,7 @@ class IHost(ABC):
@abstractmethod
async def connect(self, peer_info: PeerInfo) -> None:
"""
connect ensures there is a connection between this host and the peer
Ensure there is a connection between this host and the peer
with given peer_info.peer_id. connect will absorb the addresses in
peer_info into its internal peerstore. If there is not an active
connection, connect will issue a dial, and block until a connection is

View File

@ -2,10 +2,18 @@ import logging
import trio
from libp2p.network.stream.exceptions import StreamClosed, StreamEOF, StreamReset
from libp2p.network.stream.net_stream_interface import INetStream
from libp2p.network.stream.exceptions import (
StreamClosed,
StreamEOF,
StreamReset,
)
from libp2p.network.stream.net_stream_interface import (
INetStream,
)
from libp2p.peer.id import ID as PeerID
from libp2p.typing import TProtocol
from libp2p.typing import (
TProtocol,
)
ID = TProtocol("/ipfs/ping/1.0.0")
PING_LENGTH = 32
@ -15,8 +23,9 @@ logger = logging.getLogger("libp2p.host.ping")
async def _handle_ping(stream: INetStream, peer_id: PeerID) -> bool:
"""Return a boolean indicating if we expect more pings from the peer at
``peer_id``."""
"""
Return a boolean indicating if we expect more pings from the peer at ``peer_id``.
"""
try:
with trio.fail_after(RESP_TIMEOUT):
payload = await stream.read(PING_LENGTH)
@ -46,8 +55,10 @@ async def _handle_ping(stream: INetStream, peer_id: PeerID) -> bool:
async def handle_ping(stream: INetStream) -> None:
"""``handle_ping`` responds to incoming ping requests until one side errors
or closes the ``stream``."""
"""
Respond to incoming ping requests until one side errors
or closes the ``stream``.
"""
peer_id = stream.muxed_conn.peer_id
while True:

View File

@ -1,8 +1,18 @@
from libp2p.host.basic_host import BasicHost
from libp2p.host.exceptions import ConnectionFailure
from libp2p.network.network_interface import INetworkService
from libp2p.peer.peerinfo import PeerInfo
from libp2p.routing.interfaces import IPeerRouting
from libp2p.host.basic_host import (
BasicHost,
)
from libp2p.host.exceptions import (
ConnectionFailure,
)
from libp2p.network.network_interface import (
INetworkService,
)
from libp2p.peer.peerinfo import (
PeerInfo,
)
from libp2p.routing.interfaces import (
IPeerRouting,
)
# RoutedHost is a p2p Host that includes a routing system.
@ -16,7 +26,7 @@ class RoutedHost(BasicHost):
async def connect(self, peer_info: PeerInfo) -> None:
"""
connect ensures there is a connection between this host and the peer
Ensure there is a connection between this host and the peer
with given `peer_info.peer_id`. See (basic_host).connect for more
information.
@ -26,7 +36,8 @@ class RoutedHost(BasicHost):
:param peer_info: peer_info of the peer we want to connect to
:type peer_info: peer.peerinfo.PeerInfo
"""
# check if we were given some addresses, otherwise, find some with the routing system.
# check if we were given some addresses, otherwise, find some with the
# routing system.
if not peer_info.addrs:
found_peer_info = await self._router.find_peer(peer_info.peer_id)
if not found_peer_info: