From ff6fdccea423f6604f3e020a3cf2ab598d6123d0 Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Sun, 25 Nov 2018 01:45:13 -0500 Subject: [PATCH] refactor libp2p --- examples/chat/chat.py | 10 ++++----- libp2p/libp2p.py | 43 ++++++++++++++++--------------------- tests/libp2p/test_libp2p.py | 9 +++----- 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/examples/chat/chat.py b/examples/chat/chat.py index 6fce80f..5c10ca1 100755 --- a/examples/chat/chat.py +++ b/examples/chat/chat.py @@ -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) diff --git a/libp2p/libp2p.py b/libp2p/libp2p.py index 6695200..f312eff 100644 --- a/libp2p/libp2p.py +++ b/libp2p/libp2p.py @@ -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 is None: + new_key = RSA.generate(2048, e=65537) + id_opt = new_key.publickey().exportKey("PEM") + # private_key = new_key.exportKey("PEM") - if id_opt: - self.id_opt = id_opt - else: - new_key = RSA.generate(2048, e=65537) - self.id_opt = new_key.publickey().exportKey("PEM") - self.private_key = new_key.exportKey("PEM") + 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() - self.transport_opt = transport_opt - self.muxer_opt = muxer_opt - self.sec_opt = sec_opt - self.peerstore = peerstore + upgrader = TransportUpgrader(sec_opt, transport_opt) + swarm = Swarm(id_opt, peerstore, upgrader) + tcp = TCP() + swarm.add_transport(tcp) + await swarm.listen(transport_opt[0]) - async def new_node(self): + # TODO enable support for other host type + # TODO routing unimplemented + host = BasicHost(swarm) - upgrader = TransportUpgrader(self.sec_opt, self.transport_opt) - swarm = Swarm(self.id_opt, self.peerstore, upgrader) - tcp = TCP() - swarm.add_transport(tcp) - await swarm.listen(self.transport_opt[0]) - host = BasicHost(swarm) - - # TODO MuxedConnection currently contains all muxing logic (move to a Muxer) - # TODO routing unimplemented - return host + return host diff --git a/tests/libp2p/test_libp2p.py b/tests/libp2p/test_libp2p.py index 1a8ddc3..eb376e2 100644 --- a/tests/libp2p/test_libp2p.py +++ b/tests/libp2p/test_libp2p.py @@ -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: