From 5e215901c09c29345ff069c1a1791e3070d5e0fc Mon Sep 17 00:00:00 2001 From: NIC619 Date: Tue, 30 Jul 2019 23:41:28 +0800 Subject: [PATCH] Apply PR feedback --- libp2p/network/network_interface.py | 2 +- libp2p/network/swarm.py | 8 ++++---- libp2p/peer/id.py | 13 ++++++------- libp2p/peer/peerdata.py | 2 +- libp2p/pubsub/gossipsub.py | 1 + libp2p/pubsub/pubsub.py | 2 +- libp2p/stream_muxer/muxed_stream_interface.py | 8 ++------ tox.ini | 1 + 8 files changed, 17 insertions(+), 20 deletions(-) diff --git a/libp2p/network/network_interface.py b/libp2p/network/network_interface.py index f34a752..1a84c0e 100644 --- a/libp2p/network/network_interface.py +++ b/libp2p/network/network_interface.py @@ -67,7 +67,7 @@ class INetwork(ABC): """ @abstractmethod - async def listen(self, *args: Multiaddr) -> bool: + async def listen(self, *args: Sequence[Multiaddr]) -> bool: """ :param *args: one or many multiaddrs to start listening on :return: True if at least one success diff --git a/libp2p/network/swarm.py b/libp2p/network/swarm.py index bedba40..a259fce 100644 --- a/libp2p/network/swarm.py +++ b/libp2p/network/swarm.py @@ -18,11 +18,11 @@ from libp2p.protocol_muxer.multiselect import Multiselect from libp2p.protocol_muxer.multiselect_client import MultiselectClient from libp2p.routing.interfaces import IPeerRouting from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn +from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream from libp2p.transport.upgrader import TransportUpgrader from libp2p.transport.transport_interface import ITransport from libp2p.transport.listener_interface import IListener -from libp2p.stream_muxer.mplex.mplex_stream import MplexStream from .network_interface import INetwork from .notifee_interface import INotifee @@ -168,7 +168,7 @@ class Swarm(INetwork): return net_stream - async def listen(self, *args: Multiaddr) -> bool: + async def listen(self, *args: Sequence[Multiaddr]) -> bool: """ :param *args: one or many multiaddrs to start listening on :return: true if at least one success @@ -252,7 +252,7 @@ class Swarm(INetwork): # TODO: `disconnect`? -GenericProtocolHandlerFn = Callable[[MplexStream], Awaitable[None]] +GenericProtocolHandlerFn = Callable[[IMuxedStream], Awaitable[None]] def create_generic_protocol_handler(swarm: Swarm) -> GenericProtocolHandlerFn: @@ -264,7 +264,7 @@ def create_generic_protocol_handler(swarm: Swarm) -> GenericProtocolHandlerFn: """ multiselect = swarm.multiselect - async def generic_protocol_handler(muxed_stream: MplexStream) -> None: + async def generic_protocol_handler(muxed_stream: IMuxedStream) -> None: # Perform protocol muxing to determine protocol to use protocol, handler = await multiselect.negotiate(muxed_stream) diff --git a/libp2p/peer/id.py b/libp2p/peer/id.py index 4e82ca2..8561e0e 100644 --- a/libp2p/peer/id.py +++ b/libp2p/peer/id.py @@ -20,16 +20,15 @@ MAX_INLINE_KEY_LENGTH = 42 class ID: - _id_str: str + _id_str: bytes - def __init__(self, id_str: str) -> None: + def __init__(self, id_str: bytes) -> None: self._id_str = id_str - # FIXME: Should return type `bytes` - def to_bytes(self) -> str: + def to_bytes(self) -> bytes: return self._id_str - def get_raw_id(self) -> str: + def get_raw_id(self) -> bytes: return self._id_str def pretty(self) -> str: @@ -86,6 +85,6 @@ def id_from_private_key(key: RsaKey) -> ID: return id_from_public_key(key.publickey()) def digest(data: Union[str, bytes]) -> bytes: - if not isinstance(data, bytes): - data = str(data).encode('utf8') + if isinstance(data, str): + data = data.encode('utf8') return hashlib.sha1(data).digest() diff --git a/libp2p/peer/peerdata.py b/libp2p/peer/peerdata.py index bb4962b..68749c8 100644 --- a/libp2p/peer/peerdata.py +++ b/libp2p/peer/peerdata.py @@ -31,7 +31,7 @@ class PeerData(IPeerData): self.protocols = list(protocols) def add_addrs(self, addrs: Sequence[Multiaddr]) -> None: - self.addrs.extend(list(addrs)) + self.addrs.extend(addrs) def get_addrs(self) -> List[Multiaddr]: return self.addrs diff --git a/libp2p/pubsub/gossipsub.py b/libp2p/pubsub/gossipsub.py index 4f2c88e..f0d903b 100644 --- a/libp2p/pubsub/gossipsub.py +++ b/libp2p/pubsub/gossipsub.py @@ -533,6 +533,7 @@ class GossipSub(IPubsubRouter): from_id_str = sender_peer_id # FIXME: Update type of message ID + # FIXME: Find a better way to parse the msg ids msg_ids: List[Any] = [literal_eval(msg) for msg in iwant_msg.messageIDs] msgs_to_forward: List[rpc_pb2.Message] = [] for msg_id_iwant in msg_ids: diff --git a/libp2p/pubsub/pubsub.py b/libp2p/pubsub/pubsub.py index 43fc763..744c37c 100644 --- a/libp2p/pubsub/pubsub.py +++ b/libp2p/pubsub/pubsub.py @@ -373,6 +373,6 @@ class Pubsub: self.seen_messages[msg_id] = 1 def _is_subscribed_to_msg(self, msg: rpc_pb2.Message) -> bool: - if not bool(self.my_topics): + if not self.my_topics: return False return all([topic in self.my_topics for topic in msg.topicIDs]) diff --git a/libp2p/stream_muxer/muxed_stream_interface.py b/libp2p/stream_muxer/muxed_stream_interface.py index 2b4dac3..eb6f267 100644 --- a/libp2p/stream_muxer/muxed_stream_interface.py +++ b/libp2p/stream_muxer/muxed_stream_interface.py @@ -2,17 +2,13 @@ from abc import ( ABC, abstractmethod, ) -from typing import ( - TYPE_CHECKING, -) -if TYPE_CHECKING: - from libp2p.stream_muxer.mplex.mplex import Mplex +from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn class IMuxedStream(ABC): - mplex_conn: 'Mplex' + mplex_conn: IMuxedConn @abstractmethod def read(self): diff --git a/tox.ini b/tox.ini index 4e87b31..ea6af8f 100644 --- a/tox.ini +++ b/tox.ini @@ -19,5 +19,6 @@ basepython = basepython = python3 extras = dev commands = + # TODO: Add the tests/ folder back to pylint pylint --rcfile={toxinidir}/.pylintrc libp2p examples mypy -p libp2p -p examples --config-file {toxinidir}/mypy.ini