Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Implement binary search for RoutingTable.get_bucket_for_node #91

@pipermerriam

Description

@pipermerriam

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:

  1. Implementing ordering for the Bucket class.
  2. 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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions