working on adding tests

This commit is contained in:
Alex Haynes 2019-04-27 21:59:25 -04:00
parent 98f8df25ab
commit fdaa4f56b9
4 changed files with 28 additions and 2 deletions

View File

@ -15,6 +15,7 @@ class KadPeerInfo(PeerInfo):
def __init__(self, peer_id, peer_data=None):
super(KadPeerInfo, self).__init__(peer_id, peer_data)
self.peer_id_obj = peer_id
self.peer_id = peer_id.get_raw_id()
self.xor_id = peer_id.get_xor_id()

View File

@ -10,7 +10,7 @@ class KadmeliaPeerRouter(IPeerRouting):
def __init__(self, dht_server):
self.server = dht_server
def find_peer(self, peer_id):
async def find_peer(self, peer_id):
"""
Find specific Peer
FindPeer searches for a peer with given peer_id, returns a peer.PeerInfo
@ -18,7 +18,7 @@ class KadmeliaPeerRouter(IPeerRouting):
"""
# switching peer_id to xor_id used by kademlia as node_id
xor_id = peer_id.get_xor_id()
value = self.server.get(xor_id)
value = await self.server.get(xor_id)
return decode_peerinfo(value)

View File

View File

@ -0,0 +1,25 @@
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():
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)