def testBucketFull(self): kbucket = KBucket(2) kbucket.add(contact, 1) kbucket.add(contact2, 1) self.assertRaises(Exception, kbucket.add, contact3, 1)
def testUpdateAContact(self): kbucket = KBucket(2) kbucket.add(contact, 1) kbucket.add(contact2, 1) kbucket.add(contact, 1) self.assertEqual(2, len(kbucket.getBucket(1))) self.assertEqual(contact, kbucket.getBucket(1)[1])
def testAppendNewContact(self): kbucket = KBucket(2) kbucket.add(contact, 1) kbucket.add(contact2, 1) self.assertEqual(contact, kbucket.getBucket(1)[0]) self.assertEqual(contact2, kbucket.getBucket(1)[1])
class Node: def __init__(self, k): self._id = hashlib.sha256(str(random.randint(0, 2**32 - 1)).encode('utf-8')).hexdigest()[:20] self._kbucket = KBucket(k) def getid(self): return self._id def find_node(self, nodeid): distance = xor(self._id.encode('utf-8'), nodeid.encode('utf-8')) return self._kbucket.getkclose(distance) def addcontact(self, contact): contactId = contact[0] distance = xor(self._id.encode('utf-8'), contactId.encode('utf-8')) if(log2(distance).__round__() - 1 <= log2(distance)): index = log2(distance).__round__() - 1 else: index = log2(distance).__round__() log("addding {} to bucket {}".format(contactId, index)) self._kbucket.add(contact, index)
class Node: def __init__(self, k): self._id = hashlib.sha256( str(random.randint(0, 2**32 - 1)).encode('utf-8')).hexdigest()[:20] self._kbucket = KBucket(k) def getid(self): return self._id def find_node(self, nodeid): distance = xor(self._id.encode('utf-8'), nodeid.encode('utf-8')) return self._kbucket.getkclose(distance) def addcontact(self, contact): contactId = contact[0] distance = xor(self._id.encode('utf-8'), contactId.encode('utf-8')) if (log2(distance).__round__() - 1 <= log2(distance)): index = log2(distance).__round__() - 1 else: index = log2(distance).__round__() log("addding {} to bucket {}".format(contactId, index)) self._kbucket.add(contact, index)
def __init__(self, k): self._id = hashlib.sha256( str(random.randint(0, 2**32 - 1)).encode('utf-8')).hexdigest()[:20] self._kbucket = KBucket(k)
def testAddAContact(self): kbucket = KBucket(2) kbucket.add(contact, 1) self.assertEqual(contact, kbucket.getBucket(1)[0])
def testGetKCloseContactsWhenBucketIsNotFull(self): kbucket = KBucket(2) kbucket.add(contact, 1) kbucket.add(contact2, 2) kbucket.add(contact3, 5) self.assertEqual([contact, contact2], kbucket.getkclose(1)) self.assertEqual([contact3, contact2], kbucket.getkclose(5)) kbucket = KBucket(4) kbucket.add("a", 1) kbucket.add("b", 1) kbucket.add("c", 2) kbucket.add("d", 6) kbucket.add("e", 6) kbucket.add("f", 6) kbucket.add("g", 6) self.assertEqual(["a", "b", "c", "d"], kbucket.getkclose(1)) self.assertEqual(["d", "e", "f", "g"], kbucket.getkclose(6)) self.assertEqual(["c", "a", "b", "d"], kbucket.getkclose(2))
def testGetKCloseSimple(self): kbucket = KBucket(2) kbucket.add(contact, 1) kbucket.add(contact2, 1) self.assertEqual([contact, contact2], kbucket.getkclose(1))
def __init__(self, k): self._id = hashlib.sha256(str(random.randint(0, 2**32 - 1)).encode('utf-8')).hexdigest()[:20] self._kbucket = KBucket(k)
def testAddAContact(self): kbucket = KBucket(2) kbucket.add(contact, 1); self.assertEqual(contact, kbucket.getBucket(1)[0])