Add basic support for multiaddr addresses and improvement around peer id (#75)

* Improved peer ID construction and usage

* peer id object is directly passed to the network

no need to cast from a string to an ID

* don't base64 encode the peer id when loading from public key

* use proper multiaddr address

- keep multiaddr object into peerstore instead of string
- update network code to use new multiaddr lib
- update tests and example

* don't instanciate peerstore object in constructor

This has side effect where the same peerstore
is used for different instance of Libp2p

* add connect method to basic_host

* use zaibon's fork of sbuss/py-multiaddr

* lint
This commit is contained in:
Christophe de Carvalho
2018-11-29 16:06:40 +01:00
committed by Robert Zajac
parent 7fa674dee2
commit 611de28aca
15 changed files with 279 additions and 74 deletions

View File

@ -8,6 +8,8 @@ import asyncio
import click
from libp2p.libp2p import *
from network.multiaddr import MultiAddr
from peer.peerinfo import info_from_p2p_addr
# TODO: change once muxed_connection supports extracting protocol id from messages
PROTOCOL_ID = '/echo/1.0.0'
@ -37,7 +39,7 @@ async def write_data(stream):
async def run(port, destination):
if not destination:
host = await new_node(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" % port])
async def stream_handler(stream):
asyncio.ensure_future(read_data(stream))
@ -48,38 +50,31 @@ async def run(port, destination):
port = None
for listener in host.network.listeners.values():
for addr in listener.get_addrs():
addr_dict = addr.to_options()
if addr_dict['transport'] == 'tcp':
port = addr_dict['port']
break
port = int(addr.value_for_protocol('tcp'))
if not port:
raise RuntimeError("was not able to find the actual local port")
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("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:
host = await new_node(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" % port])
# TODO: improve multiaddr module to have proper function to do this
multiaddr = MultiAddr(destination)
ss = multiaddr.get_multiaddr_string().split('/')
peer_id = ss[-1]
addr = '/'.join(ss[:-2])
# Associate the peer with local ip address (see default parameters of Libp2p())
host.get_peerstore().add_addr(peer_id, addr, 10)
m = multiaddr.Multiaddr(destination)
info = info_from_p2p_addr(m)
# Associate the peer with local ip address
await host.connect(info)
# Start a stream with the destination.
# Multiaddress of the destination peer is fetched from the peerstore using 'peerId'.
stream = await host.new_stream(peer_id, [PROTOCOL_ID])
stream = await host.new_stream(info.peer_id, [PROTOCOL_ID])
asyncio.ensure_future(read_data(stream))
asyncio.ensure_future(write_data(stream))
print("Already connected to peer %s" % addr)
print("Already connected to peer %s" % info.addrs[0])
@click.command()