2019-04-27 21:59:25 -04:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from libp2p.kademlia.network import KademliaServer
|
2019-08-02 22:36:19 -07:00
|
|
|
from libp2p.peer.id import ID
|
2019-10-15 20:32:25 +02:00
|
|
|
from libp2p.routing.kademlia.kademlia_peer_router import (
|
|
|
|
KadmeliaPeerRouter,
|
|
|
|
peer_info_to_str,
|
|
|
|
)
|
2019-04-27 21:59:25 -04:00
|
|
|
|
2019-07-31 15:00:12 -07:00
|
|
|
|
2019-04-27 21:59:25 -04:00
|
|
|
@pytest.mark.asyncio
|
2019-04-28 13:49:10 -04:00
|
|
|
async def test_simple_two_nodes():
|
2019-04-27 21:59:25 -04:00
|
|
|
node_a = KademliaServer()
|
|
|
|
await node_a.listen(5678)
|
|
|
|
|
|
|
|
node_b = KademliaServer()
|
|
|
|
await node_b.listen(5679)
|
|
|
|
|
2019-04-28 13:49:10 -04:00
|
|
|
node_a_value = await node_b.bootstrap([("127.0.0.1", 5678)])
|
|
|
|
node_a_kad_peerinfo = node_a_value[0]
|
2019-10-15 20:32:25 +02:00
|
|
|
await node_a.set(node_a_kad_peerinfo.xor_id, peer_info_to_str(node_a_kad_peerinfo))
|
2019-04-28 13:49:10 -04:00
|
|
|
|
|
|
|
router = KadmeliaPeerRouter(node_b)
|
2019-07-31 23:50:53 +08:00
|
|
|
returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id_bytes))
|
2019-10-15 01:01:16 +02:00
|
|
|
assert returned_info == node_a_kad_peerinfo
|
2019-04-28 13:49:10 -04:00
|
|
|
|
2019-07-31 15:00:12 -07:00
|
|
|
|
2019-04-28 13:49:10 -04:00
|
|
|
@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)])
|
2019-10-15 20:32:25 +02:00
|
|
|
await node_a.set(node_a_kad_peerinfo.xor_id, peer_info_to_str(node_a_kad_peerinfo))
|
2019-04-28 13:49:10 -04:00
|
|
|
|
|
|
|
router = KadmeliaPeerRouter(node_c)
|
2019-07-31 23:50:53 +08:00
|
|
|
returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id_bytes))
|
2019-10-15 01:01:16 +02:00
|
|
|
assert returned_info == node_a_kad_peerinfo
|
2019-04-28 17:38:40 -04:00
|
|
|
|
2019-07-31 15:00:12 -07:00
|
|
|
|
2019-04-28 17:38:40 -04:00
|
|
|
@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)])
|
|
|
|
|
2019-10-15 20:32:25 +02:00
|
|
|
await node_b.set(node_a_kad_peerinfo.xor_id, peer_info_to_str(node_a_kad_peerinfo))
|
2019-04-28 17:38:40 -04:00
|
|
|
|
|
|
|
router = KadmeliaPeerRouter(node_d)
|
2019-07-31 23:50:53 +08:00
|
|
|
returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id_bytes))
|
2019-10-15 01:01:16 +02:00
|
|
|
assert returned_info == node_a_kad_peerinfo
|