Apply PR feedback

This commit is contained in:
NIC619 2019-07-30 23:41:28 +08:00
parent 76de01a17d
commit 5e215901c0
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
8 changed files with 17 additions and 20 deletions

View File

@ -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

View File

@ -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)

View File

@ -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()

View File

@ -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

View File

@ -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:

View File

@ -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])

View File

@ -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):

View File

@ -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