diff --git a/libp2p/routing/kademlia/kademlia_peer_router.py b/libp2p/routing/kademlia/kademlia_peer_router.py index 958066c..3a0a3ba 100644 --- a/libp2p/routing/kademlia/kademlia_peer_router.py +++ b/libp2p/routing/kademlia/kademlia_peer_router.py @@ -19,15 +19,14 @@ 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 decode_peerinfo(value) + return 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() +# lines = encoded.splitlines() +# peer_id = lines[0] +# addrs = lines[1:] +# peer_data = PeerData() +# peer_data.add_addrs(addrs) +# return PeerInfo(peer_id, addrs) diff --git a/tests/routing/test_kad_peer_router.py b/tests/routing/test_kad_peer_router.py index 9c7f402..146f6d2 100644 --- a/tests/routing/test_kad_peer_router.py +++ b/tests/routing/test_kad_peer_router.py @@ -1,25 +1,47 @@ -import asyncio import pytest from libp2p.kademlia.network import KademliaServer from libp2p.routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter -from libp2p.peer.id import id_b58_encode @pytest.mark.asyncio -async def test_example(): +async def test_simple_two_nodes(): node_a = KademliaServer() await node_a.listen(5678) node_b = KademliaServer() await node_b.listen(5679) - value = await node_b.bootstrap([("127.0.0.1", 5678)]) - peer_info = value[0] - peer_id = peer_info.peer_id_obj - print(id_b58_encode(peer_id)) - # await node_a.set(peer_info.xor_id, str(peer_info.ip) + "/" + str(peer_info.port)) - # router = KadmeliaPeerRouter(node_b) - # value = await router.find_peer(peer_id) - # print("value vvvv") - # print(value.xor_) - # assert value == str(peer_info.ip) + "/" + str(peer_info.port) + 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)) + + 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) + +@pytest.mark.asyncio +async def test_simple_three_nodes(): + node_a = KademliaServer() + await node_a.listen(5701) + + node_b = KademliaServer() + await node_b.listen(5702) + + node_c = KademliaServer() + await node_c.listen(5703) + + node_a_value = await node_b.bootstrap([("127.0.0.1", 5701)]) + node_a_kad_peerinfo = node_a_value[0] + + 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)) + + 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)