Refactored for 'lint' testenv

This commit is contained in:
Aratz M. Lasa
2019-10-15 19:02:03 +02:00
parent 65b5e7aeea
commit d1d91e4091
7 changed files with 584 additions and 32 deletions

View File

@ -4,6 +4,7 @@ from typing import Sequence
from libp2p.crypto.keys import KeyPair
from libp2p.crypto.rsa import create_new_key_pair
from libp2p.host.basic_host import BasicHost
from libp2p.host.host_interface import IHost
from libp2p.host.routed_host import RoutedHost
from libp2p.kademlia.network import KademliaServer
from libp2p.kademlia.storage import IStorage
@ -47,7 +48,7 @@ def generate_peer_id_from(key_pair: KeyPair) -> ID:
def initialize_default_kademlia_router(
ksize: int = 20, alpha: int = 3, id_opt: ID = None, storage: IStorage = None
ksize: int = 20, alpha: int = 3, id_opt: ID = None, storage: IStorage = None
) -> KadmeliaPeerRouter:
"""
initialize kadmelia router when no kademlia router is passed in
@ -71,13 +72,13 @@ def initialize_default_kademlia_router(
def initialize_default_swarm(
key_pair: KeyPair,
id_opt: ID = None,
transport_opt: Sequence[str] = None,
muxer_opt: TMuxerOptions = None,
sec_opt: TSecurityOptions = None,
peerstore_opt: IPeerStore = None,
disc_opt: IPeerRouting = None,
key_pair: KeyPair,
id_opt: ID = None,
transport_opt: Sequence[str] = None,
muxer_opt: TMuxerOptions = None,
sec_opt: TSecurityOptions = None,
peerstore_opt: IPeerStore = None,
disc_opt: IPeerRouting = None,
) -> Swarm:
"""
initialize swarm when no swarm is passed in
@ -111,14 +112,14 @@ def initialize_default_swarm(
async def new_node(
key_pair: KeyPair = None,
swarm_opt: INetwork = None,
transport_opt: Sequence[str] = None,
muxer_opt: TMuxerOptions = None,
sec_opt: TSecurityOptions = None,
peerstore_opt: IPeerStore = None,
disc_opt: IPeerRouting = None,
) -> BasicHost:
key_pair: KeyPair = None,
swarm_opt: INetwork = None,
transport_opt: Sequence[str] = None,
muxer_opt: TMuxerOptions = None,
sec_opt: TSecurityOptions = None,
peerstore_opt: IPeerStore = None,
disc_opt: IPeerRouting = None,
) -> IHost:
"""
create new libp2p node
:param key_pair: key pair for deriving an identity
@ -150,12 +151,12 @@ async def new_node(
# TODO enable support for other host type
# TODO routing unimplemented
host: IHost # If not explicitly typed, MyPy raises error
if disc_opt:
host = RoutedHost(swarm_opt, disc_opt)
else:
host = BasicHost(swarm_opt)
# Kick off cleanup job
asyncio.ensure_future(cleanup_done_tasks())

View File

@ -14,6 +14,7 @@ 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
# Upon host creation, host takes in options,
@ -83,7 +84,7 @@ class BasicHost(IHost):
return addrs
def set_stream_handler(
self, protocol_id: TProtocol, stream_handler: StreamHandlerFn
self, protocol_id: TProtocol, stream_handler: StreamHandlerFn
) -> None:
"""
set stream handler for given `protocol_id`
@ -93,7 +94,7 @@ class BasicHost(IHost):
self.multiselect.add_handler(protocol_id, stream_handler)
async def new_stream(
self, peer_id: ID, protocol_ids: Sequence[TProtocol]
self, peer_id: ID, protocol_ids: Sequence[TProtocol]
) -> INetStream:
"""
:param peer_id: peer_id that host is connecting

View File

@ -1,5 +1,5 @@
import json
from typing import List, Sequence
from typing import Any, List, Sequence
import multiaddr
@ -7,7 +7,6 @@ from .id import ID
class PeerInfo:
peer_id: ID
addrs: List[multiaddr.Multiaddr]
@ -16,15 +15,24 @@ class PeerInfo:
self.addrs = list(addrs)
def to_string(self) -> str:
return json.dumps([self.peer_id.to_string(), list(map(lambda a: str(a), self.addrs))])
return json.dumps(
[self.peer_id.to_string(), list(map(lambda a: str(a), self.addrs))]
)
def __eq__(self, other: Any) -> bool:
return isinstance(other, PeerInfo) and self.peer_id == other.peer_id and self.addrs == other.addrs
return (
isinstance(other, PeerInfo)
and self.peer_id == other.peer_id
and self.addrs == other.addrs
)
@classmethod
def info_from_string(cls, info: str) -> "PeerInfo":
peer_id, raw_addrs = json.loads(info)
return PeerInfo(ID.from_base58(peer_id), list(map(lambda a: multiaddr.Multiaddr(a), raw_addrs)))
return PeerInfo(
ID.from_base58(peer_id),
list(map(lambda a: multiaddr.Multiaddr(a), raw_addrs)),
)
def info_from_p2p_addr(addr: multiaddr.Multiaddr) -> PeerInfo:

View File

@ -20,4 +20,6 @@ class KadmeliaPeerRouter(IPeerRouting):
xor_id = peer_id.xor_id
# ignore type for kad
value = await self.server.get(xor_id) # type: ignore
return PeerInfo.info_from_string(value) if value else None # TODO: should raise error if None?
return (
PeerInfo.info_from_string(value) if value else None
) # TODO: should raise error if None?