At the time of creation, this code only exists in the branch gsalgado:p2p under @gsalgado 's fork.
What is wrong?
The current implementation of evm.p2p.kademlia.RoutingTable.get_bucket_for_node is implemented such that it operates in linear runtime.
How can it be fixed
This can be fixed by doing two things:
- Implementing ordering for the
Bucket class.
- Using the
bisect module to lookup the appropriate bucket using a binary search.
In order to make the Bucket class orderable, you should use the functools.total_ordering class decorator. Buckets should be sorted by Bucket.end
Once the Bucket class can be ordered, you should use the bisect module to perform a binary search on the bucket list to find the appropriate bucket for the given node.
The criteria for a bucket/node match are: Bucket.start <= node.id <= Bucket.end
How to test it
This functionality should be written as a standalone function which takes a list of buckets and the node and returns the bucket. If no bucket is found for the given node then a ValueError should be raised.
Test cases you should ensure are covered are:
- empty list of buckets
- single bucket
- two buckets
- many buckets
- @gsalgado other things?