Merge branch 'master' into rsa-public-key

This commit is contained in:
Alex Haynes 2018-11-11 13:23:34 -05:00 committed by GitHub
commit 1e2fc19f9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 6 deletions

View File

@ -10,6 +10,7 @@ class BasicHost(IHost):
# default options constructor # default options constructor
def __init__(self, _network): def __init__(self, _network):
self.network = _network self.network = _network
self.peerstore = self.network.peerstore
# self.stream_handlers = {} # self.stream_handlers = {}
def get_id(self): def get_id(self):
@ -24,6 +25,12 @@ class BasicHost(IHost):
""" """
return self.network return self.network
def get_peerstore(self):
"""
:return: peerstore of the host (same one as in its network instance)
"""
return self.peerstore
def get_mux(self): def get_mux(self):
""" """
:return: mux instance of host :return: mux instance of host

View File

@ -1,4 +1,3 @@
from .config import Config
from ..peer.peerstore import PeerStore from ..peer.peerstore import PeerStore
from ..network.swarm import Swarm from ..network.swarm import Swarm
from ..host.basic_host import BasicHost from ..host.basic_host import BasicHost
@ -8,7 +7,7 @@ from Crypto.PublicKey import RSA
class Libp2p(object): class Libp2p(object):
def __init__(self, idOpt, \ 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"], \ muxerOpt = ["mplex/6.7.0"], \
secOpt = ["secio"], \ secOpt = ["secio"], \
peerstore = PeerStore()): peerstore = PeerStore()):
@ -20,7 +19,7 @@ class Libp2p(object):
new_key = RSA.generate(2048, e=65537) new_key = RSA.generate(2048, e=65537)
self.idOpt = new_key.publickey().exportKey("PEM") self.idOpt = new_key.publickey().exportKey("PEM")
self.private_key = new_key.exportKey("PEM") self.private_key = new_key.exportKey("PEM")
self.transportOpt = transportOpt self.transportOpt = transportOpt
self.muxerOpt = muxerOpt self.muxerOpt = muxerOpt
self.secOpt = secOpt self.secOpt = secOpt
@ -36,4 +35,6 @@ class Libp2p(object):
# TODO listen on addrs # TODO listen on addrs
# TODO swarm add transports # TODO swarm add transports
# TODO: return host

View File

@ -6,7 +6,7 @@ class Swarm(INetwork):
def __init__(self, my_peer_id, peerstore): def __init__(self, my_peer_id, peerstore):
self.my_peer_id = my_peer_id self.my_peer_id = my_peer_id
self.peer_store = peerstore self.peerstore = peerstore
self.connections = {} self.connections = {}
def set_stream_handler(self, stream_handler): def set_stream_handler(self, stream_handler):
@ -33,7 +33,7 @@ class Swarm(INetwork):
if peer_id in self.connections: if peer_id in self.connections:
muxed_connection = self.connections[peer_id] muxed_connection = self.connections[peer_id]
else: else:
addrs = self.peer_store.addrs(peer_id) addrs = self.peerstore.addrs(peer_id)
stream_ip = addrs.get_protocol_value("ip") stream_ip = addrs.get_protocol_value("ip")
stream_port = addrs.get_protocol_value("port") stream_port = addrs.get_protocol_value("port")
if len(addrs) > 0: if len(addrs) > 0:

0
tests/libp2p/__init__.py Normal file
View File

View File

@ -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)