PR feedback: async with host.run()

This commit is contained in:
mhchia
2020-01-26 16:44:42 +08:00
parent dcc4aa52fc
commit 6e01a7da31
8 changed files with 75 additions and 71 deletions

View File

@ -1,14 +1,9 @@
from typing import AsyncIterator, Sequence
from async_generator import asynccontextmanager
from async_service import background_trio_service
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.network.network_interface import INetwork, INetworkService
from libp2p.network.network_interface import INetworkService
from libp2p.network.swarm import Swarm
from libp2p.peer.id import ID
from libp2p.peer.peerstore import PeerStore
@ -32,14 +27,14 @@ def generate_peer_id_from(key_pair: KeyPair) -> ID:
return ID.from_pubkey(public_key)
def initialize_default_swarm(
def new_swarm(
key_pair: KeyPair = None,
muxer_opt: TMuxerOptions = None,
sec_opt: TSecurityOptions = None,
peerstore_opt: IPeerStore = None,
) -> INetworkService:
"""
initialize swarm when no swarm is passed in.
Create a swarm instance based on the parameters.
:param key_pair: optional choice of the ``KeyPair``
:param muxer_opt: optional choice of stream muxer
@ -48,7 +43,7 @@ def initialize_default_swarm(
:return: return a default swarm instance
"""
if not key_pair:
if key_pair is None:
key_pair = generate_new_rsa_identity()
id_opt = generate_peer_id_from(key_pair)
@ -72,45 +67,32 @@ def initialize_default_swarm(
return Swarm(id_opt, peerstore, upgrader, transport)
def _new_host(swarm_opt: INetwork, disc_opt: IPeerRouting = None) -> IHost:
"""
create new libp2p host.
:param swarm_opt: optional swarm
:param disc_opt: optional discovery
:return: return a host instance
"""
# 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)
return host
@asynccontextmanager
async def new_host_trio(
listen_addrs: Sequence[str],
def new_host(
key_pair: KeyPair = None,
swarm_opt: INetwork = None,
muxer_opt: TMuxerOptions = None,
sec_opt: TSecurityOptions = None,
peerstore_opt: IPeerStore = None,
disc_opt: IPeerRouting = None,
) -> AsyncIterator[IHost]:
swarm = initialize_default_swarm(
) -> IHost:
"""
Create a new libp2p host based on the given parameters.
:param key_pair: optional choice of the ``KeyPair``
:param muxer_opt: optional choice of stream muxer
:param sec_opt: optional choice of security upgrade
:param peerstore_opt: optional peerstore
:param disc_opt: optional discovery
:return: return a host instance
"""
swarm = new_swarm(
key_pair=key_pair,
muxer_opt=muxer_opt,
sec_opt=sec_opt,
peerstore_opt=peerstore_opt,
)
async with background_trio_service(swarm):
await swarm.listen(*listen_addrs)
host = _new_host(swarm_opt=swarm, disc_opt=disc_opt)
yield host
# TODO: Support asyncio
host: IHost
if disc_opt:
host = RoutedHost(swarm, disc_opt)
else:
host = BasicHost(swarm)
return host