lbry-sdk/lbrynet/tests/dht/test_routing_table.py

188 lines
8.9 KiB
Python
Raw Normal View History

2015-08-20 17:27:15 +02:00
#!/usr/bin/env python
#
# This library is free software, distributed under the terms of
# the GNU Lesser General Public License Version 3, or any later version.
# See the COPYING file included in this archive
import hashlib
import unittest
import lbrynet.dht.constants
import lbrynet.dht.routingtable
import lbrynet.dht.contact
2017-10-10 19:31:18 +02:00
import lbrynet.dht.node
2015-08-20 17:27:15 +02:00
class FakeRPCProtocol(object):
""" Fake RPC protocol; allows lbrynet.dht.contact.Contact objects to "send" RPCs """
def sendRPC(self, *args, **kwargs):
return FakeDeferred()
class FakeDeferred(object):
""" Fake Twisted Deferred object; allows the routing table to add callbacks that do nothing """
def addCallback(self, *args, **kwargs):
return
2017-10-10 19:31:18 +02:00
2015-08-20 17:27:15 +02:00
def addErrback(self, *args, **kwargs):
return
class TreeRoutingTableTest(unittest.TestCase):
""" Test case for the RoutingTable class """
def setUp(self):
2017-10-10 19:31:18 +02:00
h = hashlib.sha384()
2015-08-20 17:27:15 +02:00
h.update('node1')
self.nodeID = h.digest()
self.protocol = FakeRPCProtocol()
self.routingTable = lbrynet.dht.routingtable.TreeRoutingTable(self.nodeID)
2017-10-10 19:31:18 +02:00
2015-08-20 17:27:15 +02:00
def testDistance(self):
""" Test to see if distance method returns correct result"""
2017-10-10 19:31:18 +02:00
2015-08-20 17:27:15 +02:00
# testList holds a couple 3-tuple (variable1, variable2, result)
2017-10-10 19:31:18 +02:00
basicTestList = [('123456789', '123456789', 0L), ('12345', '98765', 34527773184L)]
2015-08-20 17:27:15 +02:00
for test in basicTestList:
2017-10-10 19:31:18 +02:00
result = lbrynet.dht.node.Distance(test[0])(test[1])
self.failIf(result != test[2], 'Result of _distance() should be %s but %s returned' %
(test[2], result))
2015-08-20 17:27:15 +02:00
baseIp = '146.64.19.111'
ipTestList = ['146.64.29.222', '192.68.19.333']
2017-10-10 19:31:18 +02:00
distanceOne = lbrynet.dht.node.Distance(baseIp)(ipTestList[0])
distanceTwo = lbrynet.dht.node.Distance(baseIp)(ipTestList[1])
self.failIf(distanceOne > distanceTwo, '%s should be closer to the base ip %s than %s' %
(ipTestList[0], baseIp, ipTestList[1]))
2015-08-20 17:27:15 +02:00
def testAddContact(self):
""" Tests if a contact can be added and retrieved correctly """
# Create the contact
2017-10-10 19:31:18 +02:00
h = hashlib.sha384()
2015-08-20 17:27:15 +02:00
h.update('node2')
contactID = h.digest()
contact = lbrynet.dht.contact.Contact(contactID, '127.0.0.1', 91824, self.protocol)
# Now add it...
self.routingTable.addContact(contact)
# ...and request the closest nodes to it (will retrieve it)
closestNodes = self.routingTable.findCloseNodes(contactID, lbrynet.dht.constants.k)
2017-10-10 19:31:18 +02:00
self.failUnlessEqual(len(closestNodes), 1, 'Wrong amount of contacts returned; expected 1,'
' got %d' % len(closestNodes))
self.failUnless(contact in closestNodes, 'Added contact not found by issueing '
'_findCloseNodes()')
2015-08-20 17:27:15 +02:00
def testGetContact(self):
""" Tests if a specific existing contact can be retrieved correctly """
2017-10-10 19:31:18 +02:00
h = hashlib.sha384()
2015-08-20 17:27:15 +02:00
h.update('node2')
contactID = h.digest()
contact = lbrynet.dht.contact.Contact(contactID, '127.0.0.1', 91824, self.protocol)
# Now add it...
self.routingTable.addContact(contact)
# ...and get it again
sameContact = self.routingTable.getContact(contactID)
self.failUnlessEqual(contact, sameContact, 'getContact() should return the same contact')
2017-10-10 19:31:18 +02:00
2015-08-20 17:27:15 +02:00
def testAddParentNodeAsContact(self):
2017-10-10 19:31:18 +02:00
"""
Tests the routing table's behaviour when attempting to add its parent node as a contact
"""
2015-08-20 17:27:15 +02:00
# Create a contact with the same ID as the local node's ID
contact = lbrynet.dht.contact.Contact(self.nodeID, '127.0.0.1', 91824, self.protocol)
# Now try to add it
self.routingTable.addContact(contact)
# ...and request the closest nodes to it using FIND_NODE
closestNodes = self.routingTable.findCloseNodes(self.nodeID, lbrynet.dht.constants.k)
self.failIf(contact in closestNodes, 'Node added itself as a contact')
2017-10-10 19:31:18 +02:00
2015-08-20 17:27:15 +02:00
def testRemoveContact(self):
""" Tests contact removal """
# Create the contact
2017-10-10 19:31:18 +02:00
h = hashlib.sha384()
2015-08-20 17:27:15 +02:00
h.update('node2')
contactID = h.digest()
contact = lbrynet.dht.contact.Contact(contactID, '127.0.0.1', 91824, self.protocol)
# Now add it...
self.routingTable.addContact(contact)
# Verify addition
self.failUnlessEqual(len(self.routingTable._buckets[0]), 1, 'Contact not added properly')
# Now remove it
self.routingTable.removeContact(contact.id)
self.failUnlessEqual(len(self.routingTable._buckets[0]), 0, 'Contact not removed properly')
def testSplitBucket(self):
""" Tests if the the routing table correctly dynamically splits k-buckets """
2017-10-10 19:31:18 +02:00
self.failUnlessEqual(self.routingTable._buckets[0].rangeMax, 2**384,
'Initial k-bucket range should be 0 <= range < 2**384')
2015-08-20 17:27:15 +02:00
# Add k contacts
for i in range(lbrynet.dht.constants.k):
2017-10-10 19:31:18 +02:00
h = hashlib.sha384()
2015-08-20 17:27:15 +02:00
h.update('remote node %d' % i)
nodeID = h.digest()
contact = lbrynet.dht.contact.Contact(nodeID, '127.0.0.1', 91824, self.protocol)
self.routingTable.addContact(contact)
2017-10-10 19:31:18 +02:00
self.failUnlessEqual(len(self.routingTable._buckets), 1,
'Only k nodes have been added; the first k-bucket should now '
'be full, but should not yet be split')
2015-08-20 17:27:15 +02:00
# Now add 1 more contact
2017-10-10 19:31:18 +02:00
h = hashlib.sha384()
2015-08-20 17:27:15 +02:00
h.update('yet another remote node')
nodeID = h.digest()
contact = lbrynet.dht.contact.Contact(nodeID, '127.0.0.1', 91824, self.protocol)
self.routingTable.addContact(contact)
2017-10-10 19:31:18 +02:00
self.failUnlessEqual(len(self.routingTable._buckets), 2,
'k+1 nodes have been added; the first k-bucket should have been '
'split into two new buckets')
self.failIfEqual(self.routingTable._buckets[0].rangeMax, 2**384,
'K-bucket was split, but its range was not properly adjusted')
self.failUnlessEqual(self.routingTable._buckets[1].rangeMax, 2**384,
'K-bucket was split, but the second (new) bucket\'s '
'max range was not set properly')
self.failUnlessEqual(self.routingTable._buckets[0].rangeMax,
self.routingTable._buckets[1].rangeMin,
'K-bucket was split, but the min/max ranges were '
'not divided properly')
2015-08-20 17:27:15 +02:00
def testFullBucketNoSplit(self):
2017-10-10 19:31:18 +02:00
"""
Test that a bucket is not split if it full, but does not cover the range
containing the parent node's ID
"""
self.routingTable._parentNodeID = 49 * 'a'
# more than 384 bits; this will not be in the range of _any_ k-bucket
2015-08-20 17:27:15 +02:00
# Add k contacts
for i in range(lbrynet.dht.constants.k):
2017-10-10 19:31:18 +02:00
h = hashlib.sha384()
2015-08-20 17:27:15 +02:00
h.update('remote node %d' % i)
nodeID = h.digest()
contact = lbrynet.dht.contact.Contact(nodeID, '127.0.0.1', 91824, self.protocol)
self.routingTable.addContact(contact)
2017-10-10 19:31:18 +02:00
self.failUnlessEqual(len(self.routingTable._buckets), 1, 'Only k nodes have been added; '
'the first k-bucket should now be '
'full, and there should not be '
'more than 1 bucket')
self.failUnlessEqual(len(self.routingTable._buckets[0]._contacts), lbrynet.dht.constants.k,
'Bucket should have k contacts; expected %d got %d' %
(lbrynet.dht.constants.k,
len(self.routingTable._buckets[0]._contacts)))
2015-08-20 17:27:15 +02:00
# Now add 1 more contact
2017-10-10 19:31:18 +02:00
h = hashlib.sha384()
2015-08-20 17:27:15 +02:00
h.update('yet another remote node')
nodeID = h.digest()
contact = lbrynet.dht.contact.Contact(nodeID, '127.0.0.1', 91824, self.protocol)
self.routingTable.addContact(contact)
2017-10-10 19:31:18 +02:00
self.failUnlessEqual(len(self.routingTable._buckets), 1,
'There should not be more than 1 bucket, since the bucket '
'should not have been split (parent node ID not in range)')
self.failUnlessEqual(len(self.routingTable._buckets[0]._contacts),
lbrynet.dht.constants.k, 'Bucket should have k contacts; '
'expected %d got %d' %
(lbrynet.dht.constants.k,
len(self.routingTable._buckets[0]._contacts)))
self.failIf(contact in self.routingTable._buckets[0]._contacts,
'New contact should have been discarded (since RPC is faked in this test)')