updated peerinfo encoding stored in DHT

This commit is contained in:
Alex Haynes 2019-04-28 17:38:40 -04:00
parent 8ab387ac05
commit 2efc0d3b5c
3 changed files with 57 additions and 22 deletions

View File

@ -44,11 +44,15 @@ class KadPeerInfo(PeerInfo):
return iter([self.peer_id, self.ip, self.port])
def __repr__(self):
return repr([self.xor_id, self.ip, self.port])
return repr([self.xor_id, self.ip, self.port, self.peer_id])
def __str__(self):
return "%s:%s" % (self.ip, str(self.port))
def encode(self):
return str(self.peer_id) + "\n" + \
str("/ip4/" + str(self.ip) + "/udp/" + str(self.port))
class KadPeerHeap:
"""
A heap of peers ordered by distance to a given node.

View File

@ -1,7 +1,7 @@
import ast
from libp2p.routing.interfaces import IPeerRouting
from libp2p.kademlia.utils import digest
from libp2p.peer.peerinfo import PeerInfo
from libp2p.peer.peerdata import PeerData
from libp2p.kademlia.kad_peerinfo import create_kad_peerinfo
class KadmeliaPeerRouter(IPeerRouting):
@ -19,14 +19,19 @@ class KadmeliaPeerRouter(IPeerRouting):
# switching peer_id to xor_id used by kademlia as node_id
xor_id = peer_id.get_xor_id()
value = await self.server.get(xor_id)
return value
return decode_peerinfo(value)
# def decode_peerinfo(encoded):
# if isinstance(encoded, bytes):
# encoded = encoded.decode()
# lines = encoded.splitlines()
# peer_id = lines[0]
# addrs = lines[1:]
# peer_data = PeerData()
# peer_data.add_addrs(addrs)
# return PeerInfo(peer_id, addrs)
def decode_peerinfo(encoded):
if isinstance(encoded, bytes):
encoded = encoded.decode()
print(encoded)
try:
lines = ast.literal_eval(encoded)
except SyntaxError:
return None
# xor_id = lines[0]
ip = lines[1] # pylint: disable=invalid-name
port = lines[2]
peer_id = lines[3]
peer_info = create_kad_peerinfo(peer_id, ip, port)
return peer_info

View File

@ -13,15 +13,14 @@ async def test_simple_two_nodes():
node_a_value = await node_b.bootstrap([("127.0.0.1", 5678)])
node_a_kad_peerinfo = node_a_value[0]
await node_a.set(node_a_kad_peerinfo.xor_id,
str(node_a_kad_peerinfo.ip)\
+ "/" + str(node_a_kad_peerinfo.port))
repr(node_a_kad_peerinfo))
router = KadmeliaPeerRouter(node_b)
returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj)
assert returned_info == str(node_a_kad_peerinfo.ip)\
+ "/" + str(node_a_kad_peerinfo.port)
print(repr(returned_info))
print(repr(node_a_kad_peerinfo))
assert repr(returned_info) == repr(node_a_kad_peerinfo)
@pytest.mark.asyncio
async def test_simple_three_nodes():
@ -39,9 +38,36 @@ async def test_simple_three_nodes():
await node_c.bootstrap([("127.0.0.1", 5702)])
await node_a.set(node_a_kad_peerinfo.xor_id,
str(node_a_kad_peerinfo.ip)\
+ "/" + str(node_a_kad_peerinfo.port))
repr(node_a_kad_peerinfo))
router = KadmeliaPeerRouter(node_c)
returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj)
assert returned_info == str(node_a_kad_peerinfo.ip) + "/" + str(node_a_kad_peerinfo.port)
assert str(returned_info) == str(node_a_kad_peerinfo)
@pytest.mark.asyncio
async def test_simple_four_nodes():
node_a = KademliaServer()
await node_a.listen(5801)
node_b = KademliaServer()
await node_b.listen(5802)
node_c = KademliaServer()
await node_c.listen(5803)
node_d = KademliaServer()
await node_d.listen(5804)
node_a_value = await node_b.bootstrap([("127.0.0.1", 5801)])
node_a_kad_peerinfo = node_a_value[0]
await node_c.bootstrap([("127.0.0.1", 5802)])
await node_d.bootstrap([("127.0.0.1", 5803)])
await node_b.set(node_a_kad_peerinfo.xor_id,
repr(node_a_kad_peerinfo))
router = KadmeliaPeerRouter(node_d)
returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj)
assert str(returned_info) == str(node_a_kad_peerinfo)