2018-05-24 00:31:17 +02:00
|
|
|
from lbrynet.dht import constants
|
|
|
|
from lbrynet.dht.distance import Distance
|
|
|
|
import logging
|
|
|
|
|
2018-07-21 23:19:30 +02:00
|
|
|
from tests.functional.dht.dht_test_environment import TestKademliaBase
|
|
|
|
|
2018-05-24 00:31:17 +02:00
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
class TestFindNode(TestKademliaBase):
|
|
|
|
"""
|
|
|
|
This tests the local routing table lookup for a node, every node should return the sorted k contacts closest
|
|
|
|
to the querying node (even if the key being looked up is known)
|
|
|
|
"""
|
|
|
|
network_size = 35
|
|
|
|
|
|
|
|
def test_find_node(self):
|
2018-07-21 23:19:30 +02:00
|
|
|
last_node_id = self.nodes[-1].node_id
|
|
|
|
to_last_node = Distance(last_node_id)
|
2018-05-24 00:31:17 +02:00
|
|
|
for n in self.nodes:
|
2018-07-21 23:19:30 +02:00
|
|
|
find_close_nodes_result = n._routingTable.findCloseNodes(last_node_id, constants.k)
|
2018-05-24 00:31:17 +02:00
|
|
|
self.assertTrue(len(find_close_nodes_result) == constants.k)
|
2018-07-21 23:19:30 +02:00
|
|
|
found_ids = [c.id for c in find_close_nodes_result]
|
|
|
|
self.assertListEqual(found_ids, sorted(found_ids, key=lambda x: to_last_node(x)))
|
|
|
|
if last_node_id in [c.id for c in n.contacts]:
|
2018-05-24 00:31:17 +02:00
|
|
|
self.assertTrue(found_ids[0] == last_node_id)
|
|
|
|
else:
|
|
|
|
self.assertTrue(last_node_id not in found_ids)
|