From 125abe7415b65ca68e7812544d7c4064ffeeacdf Mon Sep 17 00:00:00 2001 From: Brian Muller Date: Sat, 6 Aug 2016 11:15:53 -0400 Subject: [PATCH] fix issue with bit prefix finding in sharedPrefix --- kademlia/routing.py | 4 ++-- kademlia/utils.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/kademlia/routing.py b/kademlia/routing.py index 0f9aa4c..c047025 100644 --- a/kademlia/routing.py +++ b/kademlia/routing.py @@ -4,7 +4,7 @@ import operator import asyncio from collections import OrderedDict -from kademlia.utils import OrderedSet, sharedPrefix +from kademlia.utils import OrderedSet, sharedPrefix, bytesToBitString class KBucket(object): @@ -65,7 +65,7 @@ class KBucket(object): return True def depth(self): - sp = sharedPrefix([n.id for n in self.nodes.values()]) + sp = sharedPrefix([bytesToBitString(n.id) for n in self.nodes.values()]) return len(sp) def head(self): diff --git a/kademlia/utils.py b/kademlia/utils.py index 7528703..45d646e 100644 --- a/kademlia/utils.py +++ b/kademlia/utils.py @@ -49,3 +49,8 @@ def sharedPrefix(args): break i += 1 return args[0][:i] + + +def bytesToBitString(bytes): + bits = [bin(byte)[2:].rjust(8, '0') for byte in bytes] + return "".join(bits)