updated peerinfo encoding stored in DHT
This commit is contained in:
parent
8ab387ac05
commit
2efc0d3b5c
|
@ -44,11 +44,15 @@ class KadPeerInfo(PeerInfo):
|
||||||
return iter([self.peer_id, self.ip, self.port])
|
return iter([self.peer_id, self.ip, self.port])
|
||||||
|
|
||||||
def __repr__(self):
|
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):
|
def __str__(self):
|
||||||
return "%s:%s" % (self.ip, str(self.port))
|
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:
|
class KadPeerHeap:
|
||||||
"""
|
"""
|
||||||
A heap of peers ordered by distance to a given node.
|
A heap of peers ordered by distance to a given node.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
import ast
|
||||||
|
|
||||||
from libp2p.routing.interfaces import IPeerRouting
|
from libp2p.routing.interfaces import IPeerRouting
|
||||||
from libp2p.kademlia.utils import digest
|
from libp2p.kademlia.kad_peerinfo import create_kad_peerinfo
|
||||||
from libp2p.peer.peerinfo import PeerInfo
|
|
||||||
from libp2p.peer.peerdata import PeerData
|
|
||||||
|
|
||||||
|
|
||||||
class KadmeliaPeerRouter(IPeerRouting):
|
class KadmeliaPeerRouter(IPeerRouting):
|
||||||
|
@ -19,14 +19,19 @@ class KadmeliaPeerRouter(IPeerRouting):
|
||||||
# switching peer_id to xor_id used by kademlia as node_id
|
# switching peer_id to xor_id used by kademlia as node_id
|
||||||
xor_id = peer_id.get_xor_id()
|
xor_id = peer_id.get_xor_id()
|
||||||
value = await self.server.get(xor_id)
|
value = await self.server.get(xor_id)
|
||||||
return value
|
return decode_peerinfo(value)
|
||||||
|
|
||||||
# def decode_peerinfo(encoded):
|
def decode_peerinfo(encoded):
|
||||||
# if isinstance(encoded, bytes):
|
if isinstance(encoded, bytes):
|
||||||
# encoded = encoded.decode()
|
encoded = encoded.decode()
|
||||||
# lines = encoded.splitlines()
|
print(encoded)
|
||||||
# peer_id = lines[0]
|
try:
|
||||||
# addrs = lines[1:]
|
lines = ast.literal_eval(encoded)
|
||||||
# peer_data = PeerData()
|
except SyntaxError:
|
||||||
# peer_data.add_addrs(addrs)
|
return None
|
||||||
# return PeerInfo(peer_id, addrs)
|
# 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
|
||||||
|
|
|
@ -13,15 +13,14 @@ async def test_simple_two_nodes():
|
||||||
|
|
||||||
node_a_value = await node_b.bootstrap([("127.0.0.1", 5678)])
|
node_a_value = await node_b.bootstrap([("127.0.0.1", 5678)])
|
||||||
node_a_kad_peerinfo = node_a_value[0]
|
node_a_kad_peerinfo = node_a_value[0]
|
||||||
|
|
||||||
await node_a.set(node_a_kad_peerinfo.xor_id,
|
await node_a.set(node_a_kad_peerinfo.xor_id,
|
||||||
str(node_a_kad_peerinfo.ip)\
|
repr(node_a_kad_peerinfo))
|
||||||
+ "/" + str(node_a_kad_peerinfo.port))
|
|
||||||
|
|
||||||
router = KadmeliaPeerRouter(node_b)
|
router = KadmeliaPeerRouter(node_b)
|
||||||
returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj)
|
returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj)
|
||||||
assert returned_info == str(node_a_kad_peerinfo.ip)\
|
print(repr(returned_info))
|
||||||
+ "/" + str(node_a_kad_peerinfo.port)
|
print(repr(node_a_kad_peerinfo))
|
||||||
|
assert repr(returned_info) == repr(node_a_kad_peerinfo)
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_simple_three_nodes():
|
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_c.bootstrap([("127.0.0.1", 5702)])
|
||||||
await node_a.set(node_a_kad_peerinfo.xor_id,
|
await node_a.set(node_a_kad_peerinfo.xor_id,
|
||||||
str(node_a_kad_peerinfo.ip)\
|
repr(node_a_kad_peerinfo))
|
||||||
+ "/" + str(node_a_kad_peerinfo.port))
|
|
||||||
|
|
||||||
router = KadmeliaPeerRouter(node_c)
|
router = KadmeliaPeerRouter(node_c)
|
||||||
returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj)
|
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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user