From 924e965537432b7e73de7ddd838aa39ee9333ecd Mon Sep 17 00:00:00 2001 From: NIC619 Date: Thu, 1 Aug 2019 13:25:20 +0800 Subject: [PATCH] Change argument name of `Network.listen` and blakc format --- libp2p/kademlia/kad_peerinfo.py | 5 ++++- libp2p/kademlia/network.py | 8 ++++---- libp2p/network/network_interface.py | 4 ++-- libp2p/network/swarm.py | 8 ++++---- libp2p/peer/id.py | 10 +++++----- libp2p/pubsub/gossipsub.py | 15 ++------------- tests/peer/test_peerid.py | 8 ++------ 7 files changed, 23 insertions(+), 35 deletions(-) diff --git a/libp2p/kademlia/kad_peerinfo.py b/libp2p/kademlia/kad_peerinfo.py index a66d5f7..0805e2d 100644 --- a/libp2p/kademlia/kad_peerinfo.py +++ b/libp2p/kademlia/kad_peerinfo.py @@ -136,8 +136,11 @@ class KadPeerHeap: def get_uncontacted(self): return [n for n in self if n.peer_id_bytes not in self.contacted] + def create_kad_peerinfo(node_id_bytes=None, sender_ip=None, sender_port=None): - node_id = ID(node_id_bytes) if node_id_bytes else ID(digest(random.getrandbits(255))) + node_id = ( + ID(node_id_bytes) if node_id_bytes else ID(digest(random.getrandbits(255))) + ) peer_data = None if sender_ip and sender_port: peer_data = PeerData() # pylint: disable=no-value-for-parameter diff --git a/libp2p/kademlia/network.py b/libp2p/kademlia/network.py index 09a3463..ed8457e 100644 --- a/libp2p/kademlia/network.py +++ b/libp2p/kademlia/network.py @@ -212,10 +212,10 @@ class KademliaServer: """ log.info("Saving state to %s", fname) data = { - 'ksize': self.ksize, - 'alpha': self.alpha, - 'id': self.node.peer_id_bytes, - 'neighbors': self.bootstrappable_neighbors() + "ksize": self.ksize, + "alpha": self.alpha, + "id": self.node.peer_id_bytes, + "neighbors": self.bootstrappable_neighbors(), } if not data["neighbors"]: log.warning("No known neighbors, so not writing to cache.") diff --git a/libp2p/network/network_interface.py b/libp2p/network/network_interface.py index 1afcabf..4337535 100644 --- a/libp2p/network/network_interface.py +++ b/libp2p/network/network_interface.py @@ -58,9 +58,9 @@ class INetwork(ABC): """ @abstractmethod - async def listen(self, *args: Sequence[Multiaddr]) -> bool: + async def listen(self, multiaddrs: Sequence[Multiaddr]) -> bool: """ - :param *args: one or many multiaddrs to start listening on + :param multiaddrs: 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 2b9c9e1..f8bfbcd 100644 --- a/libp2p/network/swarm.py +++ b/libp2p/network/swarm.py @@ -163,12 +163,12 @@ class Swarm(INetwork): return net_stream - async def listen(self, *args: Sequence[Multiaddr]) -> bool: + async def listen(self, multiaddrs: Sequence[Multiaddr]) -> bool: """ - :param *args: one or many multiaddrs to start listening on + :param multiaddrs: one or many multiaddrs to start listening on :return: true if at least one success - For each multiaddr in args + For each multiaddr Check if a listener for multiaddr exists already If listener already exists, continue Otherwise: @@ -177,7 +177,7 @@ class Swarm(INetwork): Call listener listen with the multiaddr Map multiaddr to listener """ - for multiaddr in args: + for multiaddr in multiaddrs: if str(multiaddr) in self.listeners: return True diff --git a/libp2p/peer/id.py b/libp2p/peer/id.py index 4918c2c..73c65bc 100644 --- a/libp2p/peer/id.py +++ b/libp2p/peer/id.py @@ -42,7 +42,7 @@ class ID: __repr__ = __str__ def __eq__(self, other: object) -> bool: - #pylint: disable=protected-access, no-else-return + # pylint: disable=protected-access, no-else-return if isinstance(other, bytes): return self._bytes == other elif isinstance(other, ID): @@ -54,13 +54,13 @@ class ID: return hash(self._bytes) @classmethod - def from_base58(cls, b58_encoded_peer_id_str: str) -> 'ID': + def from_base58(cls, b58_encoded_peer_id_str: str) -> "ID": peer_id_bytes = base58.b58decode(b58_encoded_peer_id_str) pid = ID(peer_id_bytes) return pid @classmethod - def from_pubkey(cls, key: RsaKey) -> 'ID': + def from_pubkey(cls, key: RsaKey) -> "ID": # export into binary format key_bin = key.exportKey("DER") @@ -73,7 +73,7 @@ class ID: return cls(mh_digest.encode()) @classmethod - def from_privkey(cls, key: RsaKey) -> 'ID': + def from_privkey(cls, key: RsaKey) -> "ID": return cls.from_pubkey(key.publickey()) @@ -81,7 +81,7 @@ def id_b58_encode(peer_id: ID) -> str: """ return a b58-encoded string """ - #pylint: disable=protected-access + # pylint: disable=protected-access return base58.b58encode(peer_id.to_bytes()).decode() diff --git a/libp2p/pubsub/gossipsub.py b/libp2p/pubsub/gossipsub.py index 5f68a7b..5a3c91d 100644 --- a/libp2p/pubsub/gossipsub.py +++ b/libp2p/pubsub/gossipsub.py @@ -1,14 +1,7 @@ from ast import literal_eval import asyncio import random -from typing import ( - Any, - Dict, - Iterable, - List, - Set, - Sequence, -) +from typing import Any, Dict, Iterable, List, Set, Sequence from libp2p.peer.id import ID @@ -287,11 +280,7 @@ class GossipSub(IPubsubRouter): return "unknown" async def deliver_messages_to_peers( - self, - peers: List[ID], - msg_sender: ID, - origin_id: ID, - serialized_packet: bytes, + self, peers: List[ID], msg_sender: ID, origin_id: ID, serialized_packet: bytes ) -> None: for peer_id_in_topic in peers: # Forward to all peers that are not the diff --git a/tests/peer/test_peerid.py b/tests/peer/test_peerid.py index e7d9569..00e2e0f 100644 --- a/tests/peer/test_peerid.py +++ b/tests/peer/test_peerid.py @@ -3,10 +3,7 @@ import multihash import pytest import base58 from Crypto.PublicKey import RSA -from libp2p.peer.id import ( - ID, - id_b58_encode, -) +from libp2p.peer.id import ID, id_b58_encode ALPHABETS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" @@ -17,11 +14,10 @@ def test_init(): for _ in range(10): random_id_string += random.SystemRandom().choice(ALPHABETS) peer_id = ID(random_id_string.encode()) - #pylint: disable=protected-access + # pylint: disable=protected-access assert peer_id == random_id_string.encode() - def test_no_init_value(): with pytest.raises(Exception) as _: # pylint: disable=no-value-for-parameter