refactor new_node

This commit is contained in:
zixuanzh 2019-03-17 19:20:46 -04:00
parent d42a0d7e9f
commit fea26d9087

View File

@ -11,6 +11,9 @@ from .transport.tcp.tcp import TCP
async def cleanup_done_tasks(): async def cleanup_done_tasks():
"""
clean up asyncio done tasks to free up resources
"""
while True: while True:
for task in asyncio.all_tasks(): for task in asyncio.all_tasks():
if task.done(): if task.done():
@ -20,30 +23,59 @@ async def cleanup_done_tasks():
# Some sleep necessary to context switch # Some sleep necessary to context switch
await asyncio.sleep(3) await asyncio.sleep(3)
async def new_node( def initialize_default_swarm(
id_opt=None, transport_opt=None, id_opt=None, transport_opt=None,
muxer_opt=None, sec_opt=None, peerstore=None): muxer_opt=None, sec_opt=None, peerstore_opt=None):
"""
if id_opt is 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
"""
# pylint: disable=too-many-arguments
if not id_opt:
new_key = RSA.generate(2048, e=65537) new_key = RSA.generate(2048, e=65537)
id_opt = id_from_public_key(new_key.publickey()) id_opt = id_from_public_key(new_key.publickey())
# private_key = new_key.exportKey("PEM") # private_key = new_key.exportKey("PEM")
transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"] transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"]
transport_opt = [multiaddr.Multiaddr(t) for t in transport_opt] transport = [multiaddr.Multiaddr(t) for t in transport_opt]
muxer_opt = muxer_opt or ["mplex/6.7.0"] # TODO wire muxer up with swarm
sec_opt = sec_opt or ["secio"] # muxer = muxer_opt or ["mplex/6.7.0"]
peerstore = peerstore or PeerStore() sec = sec_opt or ["secio"]
peerstore = peerstore_opt or PeerStore()
upgrader = TransportUpgrader(sec_opt, transport_opt) upgrader = TransportUpgrader(sec, transport)
swarm = Swarm(id_opt, peerstore, upgrader) swarm_opt = Swarm(id_opt, peerstore, upgrader)
tcp = TCP() tcp = TCP()
swarm.add_transport(tcp) swarm_opt.add_transport(tcp)
await swarm.listen(transport_opt[0])
return swarm_opt
async def new_node(
swarm_opt=None, id_opt=None, transport_opt=None,
muxer_opt=None, sec_opt=None, peerstore_opt=None):
"""
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 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)
# TODO enable support for other host type # TODO enable support for other host type
# TODO routing unimplemented # TODO routing unimplemented
host = BasicHost(swarm) host = BasicHost(swarm_opt)
# Kick off cleanup job # Kick off cleanup job
asyncio.ensure_future(cleanup_done_tasks()) asyncio.ensure_future(cleanup_done_tasks())