From 670958f9465ef667bef6f4542dc8ca77dc979d5c Mon Sep 17 00:00:00 2001 From: Brian Muller Date: Sun, 19 Jan 2014 14:02:13 -0500 Subject: [PATCH] now splitting buckets completely per section 4.2 of paper --- kademlia/routing.py | 9 +++++++-- kademlia/tests/utils.py | 2 +- kademlia/utils.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/kademlia/routing.py b/kademlia/routing.py index df79a0f..20b24f0 100644 --- a/kademlia/routing.py +++ b/kademlia/routing.py @@ -59,6 +59,10 @@ class KBucket(object): return False return True + def depth(self): + sp = sharedPrefix([n.id for n in self.nodes.values()]) + return len(sp) + def head(self): return self.nodes.values()[0] @@ -120,7 +124,6 @@ class RoutingTable(object): one, two = self.buckets[index].split() self.buckets[index] = one self.buckets.insert(index + 1, two) - # todo split one/two if needed based on section 4.2 def getLonelyBuckets(self): """ @@ -141,7 +144,9 @@ class RoutingTable(object): if bucket.addNode(node): return - if bucket.hasInRange(self.node): + # Per section 4.2 of paper, split if the bucket has the node in it's range + # or if the depth is not congruent to 0 mod 5 + if bucket.hasInRange(self.node) or bucket.depth() % 5 != 0: self.splitBucket(index) self.addContact(node) else: diff --git a/kademlia/tests/utils.py b/kademlia/tests/utils.py index 1a13508..1c4da17 100644 --- a/kademlia/tests/utils.py +++ b/kademlia/tests/utils.py @@ -21,7 +21,7 @@ def mknode(id=None, ip=None, port=None, intid=None): class FakeProtocol(object): def __init__(self, sourceID, ksize=20): - self.router = RoutingTable(self, ksize) + self.router = RoutingTable(self, ksize, Node(sourceID)) self.storage = {} self.sourceID = sourceID diff --git a/kademlia/utils.py b/kademlia/utils.py index 6047640..4fd0d7b 100644 --- a/kademlia/utils.py +++ b/kademlia/utils.py @@ -39,3 +39,19 @@ class OrderedSet(list): if thing in self: self.remove(thing) self.append(thing) + + +def sharedPrefix(args): + """ + Find the shared prefix between the strings. + + For instance, sharedPrefix(['blahblah', 'blahwhat']) is + 'blah'. + """ + i = 0 + while i < min(map(len, args)): + if len(set(map(operator.itemgetter(i), args))) != 1: + break + i += 1 + return args[0][:i] +