-
Notifications
You must be signed in to change notification settings - Fork 1
Add binary search to get bucket for node #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: p2p
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,10 +191,13 @@ def __contains__(self, node): | |
| def __len__(self): | ||
| return len(self.nodes) | ||
|
|
||
| def __le__(self, other): | ||
| def __lt__(self, other): | ||
| if not isinstance(other, self.__class__): | ||
| raise TypeError("Cannot compare KBucket with type {}.".format(other.__class__)) | ||
| return self.end <= other.end | ||
| # Check for invalid state of KBuckets | ||
| if not self.end < other.start: | ||
| raise ValueError("Invalid Buckets.") | ||
| return self.end < other.start | ||
|
|
||
|
|
||
| class RoutingTable: | ||
|
|
@@ -270,15 +273,14 @@ def neighbours(self, node_id, k=k_bucket_size): | |
|
|
||
| def binary_get_bucket_for_node(buckets, node): | ||
| """Return the bucket for a given node.""" | ||
| sorted_buckets = sorted(buckets) | ||
| bucket_ends = [bucket.end for bucket in sorted_buckets] | ||
| bucket_ends = [bucket.end for bucket in buckets] | ||
| bucket_position = bisect.bisect_left(bucket_ends, node.id) | ||
| # Prevents edge cases where bisect_left returns an out of range index | ||
| try: | ||
| bucket = sorted_buckets[bucket_position] | ||
| bucket = buckets[bucket_position] | ||
| except IndexError: | ||
| raise ValueError("No bucket found for node with id {}".format(node.id)) | ||
| bucket = sorted_buckets[bucket_position] | ||
| bucket = buckets[bucket_position] | ||
| if not bucket.start <= node.id <= bucket.end: | ||
| raise ValueError("No bucket found for node with id {}".format(node.id)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can this happen? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gsalgado Well, kind of as a safe-check, and I was thinking about if a list of buckets was input that was out of range or had breaks - i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's not possible given how KBucket.split() is implemented, but more importantly, if the position returned by bisect exists on the list, this check would never fail, would it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| return bucket | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm reading this code right, this method can only return True or a ValueError, but never False. Is that what you intended, and, if so, why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, good catch. Was just tinkering with a way to check for invalid state of KBuckets - forgot it was there when I pushed.