From 4681e1e08d5b2f59314c37c640c8ad5d71167e2c Mon Sep 17 00:00:00 2001 From: Robert Zajac Date: Sun, 11 Nov 2018 12:36:15 -0500 Subject: [PATCH 1/2] libp2p end to end test --- host/basic_host.py | 6 ++++++ libp2p/libp2p.py | 8 +++++--- network/swarm.py | 4 ++-- tests/libp2p/__init__.py | 0 tests/libp2p/test_libp2p.py | 30 ++++++++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 tests/libp2p/__init__.py create mode 100644 tests/libp2p/test_libp2p.py diff --git a/host/basic_host.py b/host/basic_host.py index 133571c..76bc299 100644 --- a/host/basic_host.py +++ b/host/basic_host.py @@ -10,6 +10,7 @@ class BasicHost(IHost): # default options constructor def __init__(self, _network): self.network = _network + self.peerstore = self.network.peerstore # self.stream_handlers = {} def get_id(self): @@ -24,6 +25,11 @@ class BasicHost(IHost): """ return self.network + def get_peerstore(self): + """ + :return: peerstore of the host (same one as in its network instance) + """ + def get_mux(self): """ :return: mux instance of host diff --git a/libp2p/libp2p.py b/libp2p/libp2p.py index 2c0b1eb..6f21e23 100644 --- a/libp2p/libp2p.py +++ b/libp2p/libp2p.py @@ -1,4 +1,3 @@ -from .config import Config from ..peer.peerstore import PeerStore from ..network.swarm import Swarm from ..host.basic_host import BasicHost @@ -7,7 +6,7 @@ from ..transport.upgrader import TransportUpgrader class Libp2p(object): def __init__(self, idOpt, \ - transportOpt = ["/ip4/0.0.0.0/tcp/0"], \ + transportOpt = ["/ip4/127.0.0.1/tcp/10000"], \ muxerOpt = ["mplex/6.7.0"], \ secOpt = ["secio"], \ peerstore = PeerStore()): @@ -16,6 +15,7 @@ class Libp2p(object): self.idOpt = idOpt else: # TODO generate RSA public key pair + pass self.transportOpt = transportOpt self.muxerOpt = muxerOpt @@ -32,4 +32,6 @@ class Libp2p(object): # TODO listen on addrs - # TODO swarm add transports \ No newline at end of file + # TODO swarm add transports + + # TODO: return host diff --git a/network/swarm.py b/network/swarm.py index 231ee46..bc63c90 100644 --- a/network/swarm.py +++ b/network/swarm.py @@ -6,7 +6,7 @@ class Swarm(INetwork): def __init__(self, my_peer_id, peerstore): self.my_peer_id = my_peer_id - self.peer_store = peer_store + self.peerstore = peerstore self.connections = {} def set_stream_handler(self, stream_handler): @@ -33,7 +33,7 @@ class Swarm(INetwork): if peer_id in self.connections: muxed_connection = self.connections[peer_id] else: - addrs = self.peer_store.addrs(peer_id) + addrs = self.peerstore.addrs(peer_id) stream_ip = addrs.get_protocol_value("ip") stream_port = addrs.get_protocol_value("port") if len(addrs) > 0: diff --git a/tests/libp2p/__init__.py b/tests/libp2p/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/libp2p/test_libp2p.py b/tests/libp2p/test_libp2p.py new file mode 100644 index 0000000..6214363 --- /dev/null +++ b/tests/libp2p/test_libp2p.py @@ -0,0 +1,30 @@ +from libp2p.libp2p import Libp2p + + +# TODO: are these connections async? how do we wait on responses? +def test_simple_messages(): + lib = Libp2p() + + hostA = lib.new_node() + hostB = lib.new_node() + + def stream_handler(stream): + print("stream received in host B") + + read_string = stream.read().decode() + print("host B received: " + read_string) + + response = "ack: " + read_string + stream.write(response.encode()) + + hostB.set_stream_handler("/echo/1.0.0", stream_handler) + + # associate the peer with local ip address (see default parameters of Libp2p()) + hostA.get_peerstore().add_addr("hostB", "/ip4/127.0.0.1/tcp/10000") + + stream = hostA.new_stream("hostB", "/echo/1.0.0") + message = "hello" + stream.write(message.encode()) + + response = stream.read().decode() + assert response == ("ack: " + message) From 976153c990a19af84f680009ccbca0a6db08ce78 Mon Sep 17 00:00:00 2001 From: Robert Zajac Date: Sun, 11 Nov 2018 12:38:55 -0500 Subject: [PATCH 2/2] host returning peerstore --- host/basic_host.py | 1 + 1 file changed, 1 insertion(+) diff --git a/host/basic_host.py b/host/basic_host.py index 76bc299..09789f6 100644 --- a/host/basic_host.py +++ b/host/basic_host.py @@ -29,6 +29,7 @@ class BasicHost(IHost): """ :return: peerstore of the host (same one as in its network instance) """ + return self.peerstore def get_mux(self): """