py-libp2p/libp2p/__init__.py

121 lines
3.7 KiB
Python
Raw Normal View History

import asyncio
2019-01-10 02:38:56 +08:00
import multiaddr
from Crypto.PublicKey import RSA
2019-01-10 02:38:56 +08:00
from .peer.peerstore import PeerStore
from .peer.id import id_from_public_key
from .network.swarm import Swarm
from .host.basic_host import BasicHost
from .kademlia.routed_host import RoutedHost
2019-01-10 02:38:56 +08:00
from .transport.upgrader import TransportUpgrader
from .transport.tcp.tcp import TCP
from .kademlia.network import KademliaServer
2019-01-10 02:38:56 +08:00
async def cleanup_done_tasks():
2019-03-18 07:20:46 +08:00
"""
clean up asyncio done tasks to free up resources
"""
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)
def generate_id():
new_key = RSA.generate(2048, e=65537)
new_id = id_from_public_key(new_key.publickey())
# private_key = new_key.exportKey("PEM")
return new_id
def initialize_default_kademlia(
ksize=20, alpha=3, id_opt=None, storage=None):
"""
initialize swam when no swarm is passed in
: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
:interface:`~kademlia.storage.IStorage`
:return: return a default kademlia instance
"""
if not id_opt:
id_opt = generate_id()
node_id = id_opt.get_raw_id()
return KademliaServer(ksize=ksize, alpha=alpha,
node_id=node_id, storage=storage)
2019-03-18 07:20:46 +08:00
def initialize_default_swarm(
2019-01-10 02:38:56 +08:00
id_opt=None, transport_opt=None,
2019-03-18 07:20:46 +08:00
muxer_opt=None, sec_opt=None, peerstore_opt=None):
"""
initialize swarm when no swarm is passed in
: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-03-18 09:30:56 +08:00
# pylint: disable=too-many-arguments, unused-argument
2019-03-18 07:20:46 +08:00
if not id_opt:
id_opt = generate_id()
2019-01-10 02:38:56 +08:00
2019-04-25 10:36:09 +08:00
# TODO parse transport_opt to determine transport
2019-01-10 02:38:56 +08:00
transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"]
2019-04-25 10:36:09 +08:00
transport = TCP()
# TODO TransportUpgrader is not doing anything really
# TODO parse muxer and sec to pass into TransportUpgrader
muxer = muxer_opt or ["mplex/6.7.0"]
2019-03-18 07:20:46 +08:00
sec = sec_opt or ["secio"]
2019-04-25 10:36:09 +08:00
upgrader = TransportUpgrader(sec, muxer)
2019-03-18 07:20:46 +08:00
peerstore = peerstore_opt or PeerStore()
2019-04-25 10:36:09 +08:00
swarm_opt = Swarm(id_opt, peerstore, upgrader, transport)
2019-03-18 07:20:46 +08:00
return swarm_opt
2019-03-18 07:20:46 +08:00
async def new_node(
swarm_opt=None, id_opt=None, transport_opt=None,
muxer_opt=None, sec_opt=None, peerstore_opt=None,
disc_opt=None):
2019-03-18 07:20:46 +08:00
"""
create new libp2p node
: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
"""
# pylint: disable=too-many-arguments
if not id_opt:
id_opt = generate_id()
2019-03-18 07:20:46 +08:00
if not swarm_opt:
swarm_opt = initialize_default_swarm(
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-04-25 08:59:22 +08:00
if not disc_opt:
host = BasicHost(swarm_opt)
else:
host = RoutedHost(swarm_opt, disc_opt)
2019-01-10 02:38:56 +08:00
# Kick off cleanup job
asyncio.ensure_future(cleanup_done_tasks())
2019-01-10 02:38:56 +08:00
return host