mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Run black over repo
This commit is contained in:
@ -2,6 +2,7 @@ import asyncio
|
||||
|
||||
from .raw_connection_interface import IRawConnection
|
||||
|
||||
|
||||
class RawConnection(IRawConnection):
|
||||
|
||||
conn_ip: str
|
||||
@ -11,12 +12,14 @@ class RawConnection(IRawConnection):
|
||||
_next_id: int
|
||||
initiator: bool
|
||||
|
||||
def __init__(self,
|
||||
ip: str,
|
||||
port: str,
|
||||
reader: asyncio.StreamReader,
|
||||
writer: asyncio.StreamWriter,
|
||||
initiator: bool) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
ip: str,
|
||||
port: str,
|
||||
reader: asyncio.StreamReader,
|
||||
writer: asyncio.StreamWriter,
|
||||
initiator: bool,
|
||||
) -> None:
|
||||
# pylint: disable=too-many-arguments
|
||||
self.conn_ip = ip
|
||||
self.conn_port = port
|
||||
@ -32,7 +35,7 @@ class RawConnection(IRawConnection):
|
||||
|
||||
async def read(self) -> bytes:
|
||||
line = await self.reader.readline()
|
||||
adjusted_line = line.decode().rstrip('\n')
|
||||
adjusted_line = line.decode().rstrip("\n")
|
||||
|
||||
# TODO: figure out a way to remove \n without going back and forth with
|
||||
# encoding and decoding
|
||||
|
||||
@ -1,14 +1,5 @@
|
||||
from abc import (
|
||||
ABC,
|
||||
abstractmethod,
|
||||
)
|
||||
from typing import (
|
||||
Awaitable,
|
||||
Callable,
|
||||
Dict,
|
||||
Sequence,
|
||||
TYPE_CHECKING,
|
||||
)
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Awaitable, Callable, Dict, Sequence, TYPE_CHECKING
|
||||
|
||||
from multiaddr import Multiaddr
|
||||
|
||||
@ -49,7 +40,9 @@ class INetwork(ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def set_stream_handler(self, protocol_id: str, stream_handler: StreamHandlerFn) -> bool:
|
||||
def set_stream_handler(
|
||||
self, protocol_id: str, stream_handler: StreamHandlerFn
|
||||
) -> bool:
|
||||
"""
|
||||
:param protocol_id: protocol id used on stream
|
||||
:param stream_handler: a stream handler instance
|
||||
@ -57,9 +50,7 @@ class INetwork(ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def new_stream(self,
|
||||
peer_id: ID,
|
||||
protocol_ids: Sequence[str]) -> INetStream:
|
||||
async def new_stream(self, peer_id: ID, protocol_ids: Sequence[str]) -> INetStream:
|
||||
"""
|
||||
:param peer_id: peer_id of destination
|
||||
:param protocol_ids: available protocol ids to use for stream
|
||||
@ -74,7 +65,7 @@ class INetwork(ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def notify(self, notifee: 'INotifee') -> bool:
|
||||
def notify(self, notifee: "INotifee") -> bool:
|
||||
"""
|
||||
:param notifee: object implementing Notifee interface
|
||||
:return: true if notifee registered successfully, false otherwise
|
||||
|
||||
@ -1,7 +1,4 @@
|
||||
from abc import (
|
||||
ABC,
|
||||
abstractmethod,
|
||||
)
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from multiaddr import Multiaddr
|
||||
@ -15,44 +12,43 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
class INotifee(ABC):
|
||||
|
||||
@abstractmethod
|
||||
async def opened_stream(self, network: 'INetwork', stream: INetStream) -> None:
|
||||
async def opened_stream(self, network: "INetwork", stream: INetStream) -> None:
|
||||
"""
|
||||
:param network: network the stream was opened on
|
||||
:param stream: stream that was opened
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def closed_stream(self, network: 'INetwork', stream: INetStream) -> None:
|
||||
async def closed_stream(self, network: "INetwork", stream: INetStream) -> None:
|
||||
"""
|
||||
:param network: network the stream was closed on
|
||||
:param stream: stream that was closed
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def connected(self, network: 'INetwork', conn: IMuxedConn) -> None:
|
||||
async def connected(self, network: "INetwork", conn: IMuxedConn) -> None:
|
||||
"""
|
||||
:param network: network the connection was opened on
|
||||
:param conn: connection that was opened
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def disconnected(self, network: 'INetwork', conn: IMuxedConn) -> None:
|
||||
async def disconnected(self, network: "INetwork", conn: IMuxedConn) -> None:
|
||||
"""
|
||||
:param network: network the connection was closed on
|
||||
:param conn: connection that was closed
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def listen(self, network: 'INetwork', multiaddr: Multiaddr) -> None:
|
||||
async def listen(self, network: "INetwork", multiaddr: Multiaddr) -> None:
|
||||
"""
|
||||
:param network: network the listener is listening on
|
||||
:param multiaddr: multiaddress listener is listening on
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def listen_close(self, network: 'INetwork', multiaddr: Multiaddr) -> None:
|
||||
async def listen_close(self, network: "INetwork", multiaddr: Multiaddr) -> None:
|
||||
"""
|
||||
:param network: network the connection was opened on
|
||||
:param multiaddr: multiaddress listener is no longer listening on
|
||||
|
||||
@ -1,18 +1,9 @@
|
||||
import asyncio
|
||||
from typing import (
|
||||
Awaitable,
|
||||
Callable,
|
||||
Dict,
|
||||
List,
|
||||
Sequence,
|
||||
)
|
||||
from typing import Awaitable, Callable, Dict, List, Sequence
|
||||
|
||||
from multiaddr import Multiaddr
|
||||
|
||||
from libp2p.peer.id import (
|
||||
ID,
|
||||
id_b58_decode,
|
||||
)
|
||||
from libp2p.peer.id import ID, id_b58_decode
|
||||
from libp2p.peer.peerstore import PeerStore
|
||||
from libp2p.protocol_muxer.multiselect import Multiselect
|
||||
from libp2p.protocol_muxer.multiselect_client import MultiselectClient
|
||||
@ -51,12 +42,14 @@ class Swarm(INetwork):
|
||||
|
||||
notifees: List[INotifee]
|
||||
|
||||
def __init__(self,
|
||||
peer_id: ID,
|
||||
peerstore: PeerStore,
|
||||
upgrader: TransportUpgrader,
|
||||
transport: ITransport,
|
||||
router: IPeerRouting):
|
||||
def __init__(
|
||||
self,
|
||||
peer_id: ID,
|
||||
peerstore: PeerStore,
|
||||
upgrader: TransportUpgrader,
|
||||
transport: ITransport,
|
||||
router: IPeerRouting,
|
||||
):
|
||||
self.self_id = peer_id
|
||||
self.peerstore = peerstore
|
||||
self.upgrader = upgrader
|
||||
@ -79,7 +72,9 @@ class Swarm(INetwork):
|
||||
def get_peer_id(self) -> ID:
|
||||
return self.self_id
|
||||
|
||||
def set_stream_handler(self, protocol_id: str, stream_handler: StreamHandlerFn) -> bool:
|
||||
def set_stream_handler(
|
||||
self, protocol_id: str, stream_handler: StreamHandlerFn
|
||||
) -> bool:
|
||||
"""
|
||||
:param protocol_id: protocol id used on stream
|
||||
:param stream_handler: a stream handler instance
|
||||
@ -119,8 +114,9 @@ class Swarm(INetwork):
|
||||
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
|
||||
# the conn and then mux the conn
|
||||
secured_conn = await self.upgrader.upgrade_security(raw_conn, peer_id, True)
|
||||
muxed_conn = self.upgrader.upgrade_connection(secured_conn, \
|
||||
self.generic_protocol_handler, peer_id)
|
||||
muxed_conn = self.upgrader.upgrade_connection(
|
||||
secured_conn, self.generic_protocol_handler, peer_id
|
||||
)
|
||||
|
||||
# Store muxed connection in connections
|
||||
self.connections[peer_id] = muxed_conn
|
||||
@ -154,8 +150,7 @@ class Swarm(INetwork):
|
||||
|
||||
# Perform protocol muxing to determine protocol to use
|
||||
selected_protocol = await self.multiselect_client.select_one_of(
|
||||
list(protocol_ids),
|
||||
muxed_stream,
|
||||
list(protocol_ids), muxed_stream
|
||||
)
|
||||
|
||||
# Create a net stream with the selected protocol
|
||||
@ -186,8 +181,9 @@ class Swarm(INetwork):
|
||||
if str(multiaddr) in self.listeners:
|
||||
return True
|
||||
|
||||
async def conn_handler(reader: asyncio.StreamReader,
|
||||
writer: asyncio.StreamWriter) -> None:
|
||||
async def conn_handler(
|
||||
reader: asyncio.StreamReader, writer: asyncio.StreamWriter
|
||||
) -> None:
|
||||
# Read in first message (should be peer_id of initiator) and ack
|
||||
peer_id = id_b58_decode((await reader.read(1024)).decode())
|
||||
|
||||
@ -196,14 +192,22 @@ class Swarm(INetwork):
|
||||
|
||||
# Upgrade reader/write to a net_stream and pass \
|
||||
# to appropriate stream handler (using multiaddr)
|
||||
raw_conn = RawConnection(multiaddr.value_for_protocol('ip4'),
|
||||
multiaddr.value_for_protocol('tcp'), reader, writer, False)
|
||||
raw_conn = RawConnection(
|
||||
multiaddr.value_for_protocol("ip4"),
|
||||
multiaddr.value_for_protocol("tcp"),
|
||||
reader,
|
||||
writer,
|
||||
False,
|
||||
)
|
||||
|
||||
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
|
||||
# the conn and then mux the conn
|
||||
secured_conn = await self.upgrader.upgrade_security(raw_conn, peer_id, False)
|
||||
muxed_conn = self.upgrader.upgrade_connection(secured_conn, \
|
||||
self.generic_protocol_handler, peer_id)
|
||||
secured_conn = await self.upgrader.upgrade_security(
|
||||
raw_conn, peer_id, False
|
||||
)
|
||||
muxed_conn = self.upgrader.upgrade_connection(
|
||||
secured_conn, self.generic_protocol_handler, peer_id
|
||||
)
|
||||
|
||||
# Store muxed_conn with peer id
|
||||
self.connections[peer_id] = muxed_conn
|
||||
|
||||
Reference in New Issue
Block a user