refactor libp2p

This commit is contained in:
zixuanzh 2018-11-25 01:45:13 -05:00
parent 5548041a37
commit ff6fdccea4
3 changed files with 26 additions and 36 deletions

View File

@ -6,7 +6,7 @@ sys.path.append(dirname(dirname(dirname(abspath(__file__)))))
import asyncio
import click
from libp2p.libp2p import Libp2p
from libp2p.libp2p import *
from network.multiaddr import MultiAddr
# TODO: change once muxed_connection supports extracting protocol id from messages
@ -37,8 +37,7 @@ async def write_data(stream):
async def run(port, destination):
if not destination:
lib = Libp2p(transport_opt=["/ip4/127.0.0.1/tcp/%s/p2p/hostA" % port])
host = await lib.new_node()
host = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/%s/p2p/hostA" % port])
async def stream_handler(stream):
asyncio.ensure_future(read_data(stream))
@ -57,13 +56,12 @@ async def run(port, destination):
if not port:
raise RuntimeError("was not able to find the actual local port")
print("Run './chat.py --port %s -d /ip4/127.0.0.1/tcp/%s/p2p/%s' on another console.\n" % (int(port)+1, port, host.get_id().pretty()))
print("Run './examples/chat/chat.py --port %s -d /ip4/127.0.0.1/tcp/%s/p2p/%s' on another console.\n" % (int(port)+1, port, host.get_id().pretty()))
print("You can replace 127.0.0.1 with public IP as well.")
print("\nWaiting for incoming connection\n\n")
else:
lib = Libp2p(transport_opt=["/ip4/127.0.0.1/tcp/%s/p2p/hostB" % port])
host = await lib.new_node()
host = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/%s/p2p/hostB" % port])
# TODO: improve multiaddr module to have proper function to do this
multiaddr = MultiAddr(destination)

View File

@ -6,32 +6,27 @@ from transport.upgrader import TransportUpgrader
from transport.tcp.tcp import TCP
class Libp2p():
async def new_node(id_opt=None, transport_opt=None, \
muxer_opt=None, sec_opt=None, peerstore=None):
def __init__(self, id_opt=None, transport_opt=["/ip4/127.0.0.1/tcp/8001"], \
muxer_opt=["mplex/6.7.0"], sec_opt=["secio"], peerstore=PeerStore()):
if id_opt:
self.id_opt = id_opt
else:
if id_opt is None:
new_key = RSA.generate(2048, e=65537)
self.id_opt = new_key.publickey().exportKey("PEM")
self.private_key = new_key.exportKey("PEM")
id_opt = new_key.publickey().exportKey("PEM")
# private_key = new_key.exportKey("PEM")
self.transport_opt = transport_opt
self.muxer_opt = muxer_opt
self.sec_opt = sec_opt
self.peerstore = peerstore
transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"]
muxer_opt = muxer_opt or ["mplex/6.7.0"]
sec_opt = sec_opt or ["secio"]
peerstore = peerstore or PeerStore()
async def new_node(self):
upgrader = TransportUpgrader(self.sec_opt, self.transport_opt)
swarm = Swarm(self.id_opt, self.peerstore, upgrader)
upgrader = TransportUpgrader(sec_opt, transport_opt)
swarm = Swarm(id_opt, peerstore, upgrader)
tcp = TCP()
swarm.add_transport(tcp)
await swarm.listen(self.transport_opt[0])
await swarm.listen(transport_opt[0])
# TODO enable support for other host type
# TODO routing unimplemented
host = BasicHost(swarm)
# TODO MuxedConnection currently contains all muxing logic (move to a Muxer)
# TODO routing unimplemented
return host

View File

@ -1,14 +1,11 @@
import pytest
from libp2p.libp2p import Libp2p
from libp2p.libp2p import *
@pytest.mark.asyncio
async def test_simple_messages():
libA = Libp2p(transport_opt=["/ip4/127.0.0.1/tcp/8001/ipfs/hostA"])
libB = Libp2p(transport_opt=["/ip4/127.0.0.1/tcp/8000/ipfs/hostB"])
hostA = await libA.new_node()
hostB = await libB.new_node()
hostA = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8001/ipfs/hostA"])
hostB = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8000/ipfs/hostB"])
async def stream_handler(stream):
while True: