2019-02-25 09:58:23 +08:00
|
|
|
import asyncio
|
2019-09-24 12:51:59 +08:00
|
|
|
from typing import Sequence
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2019-08-16 09:36:50 +08:00
|
|
|
from libp2p.crypto.keys import KeyPair
|
2019-08-14 09:18:12 +08:00
|
|
|
from libp2p.crypto.rsa import create_new_key_pair
|
2019-08-14 08:43:21 +08:00
|
|
|
from libp2p.host.basic_host import BasicHost
|
2019-10-16 01:02:03 +08:00
|
|
|
from libp2p.host.host_interface import IHost
|
2019-10-14 06:29:28 +08:00
|
|
|
from libp2p.host.routed_host import RoutedHost
|
2019-08-14 08:43:21 +08:00
|
|
|
from libp2p.kademlia.network import KademliaServer
|
2019-08-11 16:47:54 +08:00
|
|
|
from libp2p.kademlia.storage import IStorage
|
|
|
|
from libp2p.network.network_interface import INetwork
|
2019-08-14 08:43:21 +08:00
|
|
|
from libp2p.network.swarm import Swarm
|
|
|
|
from libp2p.peer.id import ID
|
|
|
|
from libp2p.peer.peerstore import PeerStore
|
2019-08-11 16:47:54 +08:00
|
|
|
from libp2p.peer.peerstore_interface import IPeerStore
|
|
|
|
from libp2p.routing.interfaces import IPeerRouting
|
2019-08-14 08:43:21 +08:00
|
|
|
from libp2p.routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter
|
2019-08-20 11:02:21 +08:00
|
|
|
from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, InsecureTransport
|
2019-08-24 06:00:11 +08:00
|
|
|
import libp2p.security.secio.transport as secio
|
2019-08-20 11:02:21 +08:00
|
|
|
from libp2p.stream_muxer.mplex.mplex import MPLEX_PROTOCOL_ID, Mplex
|
2019-08-14 08:43:21 +08:00
|
|
|
from libp2p.transport.tcp.tcp import TCP
|
2019-09-24 12:51:59 +08:00
|
|
|
from libp2p.transport.typing import TMuxerOptions, TSecurityOptions
|
2019-08-14 08:43:21 +08:00
|
|
|
from libp2p.transport.upgrader import TransportUpgrader
|
|
|
|
from libp2p.typing import TProtocol
|
2019-01-10 02:38:56 +08:00
|
|
|
|
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
async def cleanup_done_tasks() -> None:
|
2019-10-24 14:41:10 +08:00
|
|
|
"""clean up asyncio done tasks to free up resources."""
|
2019-02-25 09:58:23 +08:00
|
|
|
while True:
|
|
|
|
for task in asyncio.all_tasks():
|
|
|
|
if task.done():
|
|
|
|
await task
|
|
|
|
|
|
|
|
# Need not run often
|
|
|
|
# Some sleep necessary to context switch
|
|
|
|
await asyncio.sleep(3)
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
|
2019-08-03 08:39:19 +08:00
|
|
|
def generate_new_rsa_identity() -> KeyPair:
|
|
|
|
return create_new_key_pair()
|
|
|
|
|
|
|
|
|
2019-09-04 04:26:58 +08:00
|
|
|
def generate_peer_id_from(key_pair: KeyPair) -> ID:
|
2019-08-03 08:39:19 +08:00
|
|
|
public_key = key_pair.public_key
|
|
|
|
return ID.from_pubkey(public_key)
|
2019-04-21 05:35:05 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
def initialize_default_kademlia_router(
|
2019-10-16 01:11:11 +08:00
|
|
|
ksize: int = 20, alpha: int = 3, id_opt: ID = None, storage: IStorage = None
|
2019-08-11 16:47:54 +08:00
|
|
|
) -> KadmeliaPeerRouter:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
initialize kadmelia router when no kademlia router is passed in.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-04-21 05:35:05 +08:00
|
|
|
:param ksize: The k parameter from the paper
|
|
|
|
:param alpha: The alpha parameter from the paper
|
|
|
|
:param id_opt: optional id for host
|
|
|
|
:param storage: An instance that implements
|
2019-11-27 09:33:50 +08:00
|
|
|
:class:`~kademlia.storage.IStorage`
|
2019-04-21 05:35:05 +08:00
|
|
|
:return: return a default kademlia instance
|
|
|
|
"""
|
|
|
|
if not id_opt:
|
2019-09-04 04:26:58 +08:00
|
|
|
key_pair = generate_new_rsa_identity()
|
|
|
|
id_opt = generate_peer_id_from(key_pair)
|
2019-04-21 05:35:05 +08:00
|
|
|
|
2019-07-31 21:35:50 +08:00
|
|
|
node_id = id_opt.to_bytes()
|
2019-08-11 16:47:54 +08:00
|
|
|
# ignore type for Kademlia module
|
|
|
|
server = KademliaServer( # type: ignore
|
|
|
|
ksize=ksize, alpha=alpha, node_id=node_id, storage=storage
|
|
|
|
)
|
2019-04-25 10:11:54 +08:00
|
|
|
return KadmeliaPeerRouter(server)
|
2019-04-21 05:35:05 +08:00
|
|
|
|
2019-02-28 05:49:51 +08:00
|
|
|
|
2019-03-18 07:20:46 +08:00
|
|
|
def initialize_default_swarm(
|
2019-10-16 01:11:11 +08:00
|
|
|
key_pair: KeyPair,
|
|
|
|
id_opt: ID = None,
|
|
|
|
transport_opt: Sequence[str] = None,
|
|
|
|
muxer_opt: TMuxerOptions = None,
|
|
|
|
sec_opt: TSecurityOptions = None,
|
|
|
|
peerstore_opt: IPeerStore = None,
|
2019-08-11 16:47:54 +08:00
|
|
|
) -> Swarm:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
initialize swarm when no swarm is passed in.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-03-18 07:20:46 +08:00
|
|
|
:param id_opt: optional id for host
|
|
|
|
:param transport_opt: optional choice of transport upgrade
|
|
|
|
:param muxer_opt: optional choice of stream muxer
|
|
|
|
:param sec_opt: optional choice of security upgrade
|
|
|
|
:param peerstore_opt: optional peerstore
|
|
|
|
:return: return a default swarm instance
|
|
|
|
"""
|
2019-04-21 05:35:05 +08:00
|
|
|
|
2019-03-18 07:20:46 +08:00
|
|
|
if not id_opt:
|
2019-09-04 04:26:58 +08:00
|
|
|
id_opt = generate_peer_id_from(key_pair)
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2019-08-16 14:12:10 +08:00
|
|
|
# TODO: Parse `transport_opt` to determine transport
|
2019-04-25 10:36:09 +08:00
|
|
|
transport = TCP()
|
|
|
|
|
2019-08-20 11:02:21 +08:00
|
|
|
muxer_transports_by_protocol = muxer_opt or {MPLEX_PROTOCOL_ID: Mplex}
|
2019-08-03 07:04:46 +08:00
|
|
|
security_transports_by_protocol = sec_opt or {
|
2019-08-24 06:00:11 +08:00
|
|
|
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair),
|
2019-08-24 17:43:27 +08:00
|
|
|
TProtocol(secio.ID): secio.Transport(key_pair),
|
2019-08-03 07:04:46 +08:00
|
|
|
}
|
2019-08-16 14:12:10 +08:00
|
|
|
upgrader = TransportUpgrader(
|
|
|
|
security_transports_by_protocol, muxer_transports_by_protocol
|
|
|
|
)
|
2019-04-25 10:36:09 +08:00
|
|
|
|
2019-03-18 07:20:46 +08:00
|
|
|
peerstore = peerstore_opt or PeerStore()
|
2019-11-25 16:55:55 +08:00
|
|
|
# Store our key pair in peerstore
|
2019-11-26 11:52:31 +08:00
|
|
|
peerstore.add_key_pair(id_opt, key_pair)
|
2019-11-25 16:55:55 +08:00
|
|
|
|
2019-06-22 01:51:11 +08:00
|
|
|
# TODO: Initialize discovery if not presented
|
2019-10-14 06:29:28 +08:00
|
|
|
return Swarm(id_opt, peerstore, upgrader, transport)
|
2019-03-18 07:20:46 +08:00
|
|
|
|
2019-02-28 05:49:51 +08:00
|
|
|
|
2019-03-18 07:20:46 +08:00
|
|
|
async def new_node(
|
2019-10-16 01:11:11 +08:00
|
|
|
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,
|
2019-11-21 11:47:54 +08:00
|
|
|
) -> BasicHost:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
create new libp2p node.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-08-16 09:36:50 +08:00
|
|
|
:param key_pair: key pair for deriving an identity
|
2019-06-22 01:51:11 +08:00
|
|
|
:param swarm_opt: optional swarm
|
2019-03-18 07:20:46 +08:00
|
|
|
:param id_opt: optional id for host
|
|
|
|
:param transport_opt: optional choice of transport upgrade
|
|
|
|
:param muxer_opt: optional choice of stream muxer
|
|
|
|
:param sec_opt: optional choice of security upgrade
|
|
|
|
:param peerstore_opt: optional peerstore
|
2019-06-22 01:51:11 +08:00
|
|
|
:param disc_opt: optional discovery
|
|
|
|
:return: return a host instance
|
2019-03-18 07:20:46 +08:00
|
|
|
"""
|
2019-04-21 05:35:05 +08:00
|
|
|
|
2019-08-16 09:36:50 +08:00
|
|
|
if not key_pair:
|
2019-08-03 08:39:19 +08:00
|
|
|
key_pair = generate_new_rsa_identity()
|
|
|
|
|
2019-09-04 04:26:58 +08:00
|
|
|
id_opt = generate_peer_id_from(key_pair)
|
2019-04-21 05:35:05 +08:00
|
|
|
|
2019-03-18 07:20:46 +08:00
|
|
|
if not swarm_opt:
|
|
|
|
swarm_opt = initialize_default_swarm(
|
2019-08-16 09:36:50 +08:00
|
|
|
key_pair=key_pair,
|
2019-08-01 06:00:12 +08:00
|
|
|
id_opt=id_opt,
|
|
|
|
transport_opt=transport_opt,
|
|
|
|
muxer_opt=muxer_opt,
|
|
|
|
sec_opt=sec_opt,
|
|
|
|
peerstore_opt=peerstore_opt,
|
|
|
|
)
|
2019-01-10 02:38:56 +08:00
|
|
|
|
|
|
|
# TODO enable support for other host type
|
|
|
|
# TODO routing unimplemented
|
2019-10-16 01:02:03 +08:00
|
|
|
host: IHost # If not explicitly typed, MyPy raises error
|
2019-10-14 06:29:28 +08:00
|
|
|
if disc_opt:
|
2019-11-25 16:55:55 +08:00
|
|
|
host = RoutedHost(swarm_opt, disc_opt)
|
2019-10-14 06:29:28 +08:00
|
|
|
else:
|
2019-11-25 16:55:55 +08:00
|
|
|
host = BasicHost(swarm_opt)
|
2019-10-14 06:29:28 +08:00
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
# Kick off cleanup job
|
|
|
|
asyncio.ensure_future(cleanup_done_tasks())
|
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
return host
|