2019-02-25 09:58:23 +08:00
|
|
|
import asyncio
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
from Crypto.PublicKey import RSA
|
2019-07-09 23:55:43 +08:00
|
|
|
|
|
|
|
from .security.insecure_security import InsecureTransport
|
2019-01-10 02:38:56 +08:00
|
|
|
from .peer.peerstore import PeerStore
|
2019-07-31 19:31:58 +08:00
|
|
|
from .peer.id import ID
|
2019-01-10 02:38:56 +08:00
|
|
|
from .network.swarm import Swarm
|
|
|
|
from .host.basic_host import BasicHost
|
|
|
|
from .transport.upgrader import TransportUpgrader
|
|
|
|
from .transport.tcp.tcp import TCP
|
2019-04-21 05:35:05 +08:00
|
|
|
from .kademlia.network import KademliaServer
|
2019-04-25 10:11:54 +08:00
|
|
|
from .routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter
|
2019-01-10 02:38:56 +08:00
|
|
|
|
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
async def cleanup_done_tasks():
|
2019-03-18 07:20:46 +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-04-21 05:35:05 +08:00
|
|
|
def generate_id():
|
|
|
|
new_key = RSA.generate(2048, e=65537)
|
2019-07-31 19:31:58 +08:00
|
|
|
new_id = ID.from_pubkey(new_key.publickey())
|
2019-04-21 05:35:05 +08:00
|
|
|
# private_key = new_key.exportKey("PEM")
|
|
|
|
return new_id
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
|
|
|
|
def initialize_default_kademlia_router(ksize=20, alpha=3, id_opt=None, storage=None):
|
2019-04-21 05:35:05 +08:00
|
|
|
"""
|
2019-06-22 01:51:11 +08:00
|
|
|
initialize kadmelia router when no kademlia router is passed in
|
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
|
|
|
|
:interface:`~kademlia.storage.IStorage`
|
|
|
|
:return: return a default kademlia instance
|
|
|
|
"""
|
|
|
|
if not id_opt:
|
|
|
|
id_opt = generate_id()
|
|
|
|
|
2019-07-31 21:35:50 +08:00
|
|
|
node_id = id_opt.to_bytes()
|
2019-08-01 06:00:12 +08:00
|
|
|
server = KademliaServer(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-08-01 06:00:12 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
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
|
2019-06-22 01:51:11 +08:00
|
|
|
:param disc_opt: optional discovery
|
2019-03-18 07:20:46 +08:00
|
|
|
: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-04-21 05:35:05 +08:00
|
|
|
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-05-02 05:26:23 +08:00
|
|
|
sec = sec_opt or {"insecure/1.0.0": InsecureTransport("insecure")}
|
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-06-22 01:51:11 +08:00
|
|
|
# TODO: Initialize discovery if not presented
|
2019-08-01 06:00:12 +08:00
|
|
|
swarm_opt = Swarm(id_opt, peerstore, upgrader, transport, disc_opt)
|
2019-03-18 07:20:46 +08:00
|
|
|
|
|
|
|
return swarm_opt
|
|
|
|
|
2019-02-28 05:49:51 +08:00
|
|
|
|
2019-03-18 07:20:46 +08:00
|
|
|
async def new_node(
|
2019-08-01 06:00:12 +08:00
|
|
|
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
|
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
|
|
|
|
|
|
|
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(
|
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,
|
|
|
|
disc_opt=disc_opt,
|
|
|
|
)
|
2019-01-10 02:38:56 +08:00
|
|
|
|
|
|
|
# TODO enable support for other host type
|
|
|
|
# TODO routing unimplemented
|
2019-04-25 10:11:54 +08:00
|
|
|
host = BasicHost(swarm_opt)
|
2019-01-10 02:38:56 +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
|