refactor libp2p
This commit is contained in:
parent
5548041a37
commit
ff6fdccea4
|
@ -6,7 +6,7 @@ sys.path.append(dirname(dirname(dirname(abspath(__file__)))))
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
import click
|
import click
|
||||||
from libp2p.libp2p import Libp2p
|
from libp2p.libp2p import *
|
||||||
from network.multiaddr import MultiAddr
|
from network.multiaddr import MultiAddr
|
||||||
|
|
||||||
# TODO: change once muxed_connection supports extracting protocol id from messages
|
# 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):
|
async def run(port, destination):
|
||||||
|
|
||||||
if not destination:
|
if not destination:
|
||||||
lib = Libp2p(transport_opt=["/ip4/127.0.0.1/tcp/%s/p2p/hostA" % port])
|
host = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/%s/p2p/hostA" % port])
|
||||||
host = await lib.new_node()
|
|
||||||
|
|
||||||
async def stream_handler(stream):
|
async def stream_handler(stream):
|
||||||
asyncio.ensure_future(read_data(stream))
|
asyncio.ensure_future(read_data(stream))
|
||||||
|
@ -57,13 +56,12 @@ async def run(port, destination):
|
||||||
if not port:
|
if not port:
|
||||||
raise RuntimeError("was not able to find the actual local 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("You can replace 127.0.0.1 with public IP as well.")
|
||||||
print("\nWaiting for incoming connection\n\n")
|
print("\nWaiting for incoming connection\n\n")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
lib = Libp2p(transport_opt=["/ip4/127.0.0.1/tcp/%s/p2p/hostB" % port])
|
host = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/%s/p2p/hostB" % port])
|
||||||
host = await lib.new_node()
|
|
||||||
|
|
||||||
# TODO: improve multiaddr module to have proper function to do this
|
# TODO: improve multiaddr module to have proper function to do this
|
||||||
multiaddr = MultiAddr(destination)
|
multiaddr = MultiAddr(destination)
|
||||||
|
|
|
@ -6,32 +6,27 @@ from transport.upgrader import TransportUpgrader
|
||||||
from transport.tcp.tcp import TCP
|
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"], \
|
if id_opt is None:
|
||||||
muxer_opt=["mplex/6.7.0"], sec_opt=["secio"], peerstore=PeerStore()):
|
new_key = RSA.generate(2048, e=65537)
|
||||||
|
id_opt = new_key.publickey().exportKey("PEM")
|
||||||
|
# private_key = new_key.exportKey("PEM")
|
||||||
|
|
||||||
if id_opt:
|
transport_opt = transport_opt or ["/ip4/127.0.0.1/tcp/8001"]
|
||||||
self.id_opt = id_opt
|
muxer_opt = muxer_opt or ["mplex/6.7.0"]
|
||||||
else:
|
sec_opt = sec_opt or ["secio"]
|
||||||
new_key = RSA.generate(2048, e=65537)
|
peerstore = peerstore or PeerStore()
|
||||||
self.id_opt = new_key.publickey().exportKey("PEM")
|
|
||||||
self.private_key = new_key.exportKey("PEM")
|
|
||||||
|
|
||||||
self.transport_opt = transport_opt
|
upgrader = TransportUpgrader(sec_opt, transport_opt)
|
||||||
self.muxer_opt = muxer_opt
|
swarm = Swarm(id_opt, peerstore, upgrader)
|
||||||
self.sec_opt = sec_opt
|
tcp = TCP()
|
||||||
self.peerstore = peerstore
|
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)
|
return host
|
||||||
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
|
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from libp2p.libp2p import Libp2p
|
from libp2p.libp2p import *
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_simple_messages():
|
async def test_simple_messages():
|
||||||
libA = Libp2p(transport_opt=["/ip4/127.0.0.1/tcp/8001/ipfs/hostA"])
|
hostA = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8001/ipfs/hostA"])
|
||||||
libB = Libp2p(transport_opt=["/ip4/127.0.0.1/tcp/8000/ipfs/hostB"])
|
hostB = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8000/ipfs/hostB"])
|
||||||
|
|
||||||
hostA = await libA.new_node()
|
|
||||||
hostB = await libB.new_node()
|
|
||||||
|
|
||||||
async def stream_handler(stream):
|
async def stream_handler(stream):
|
||||||
while True:
|
while True:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user