Algorithm-Kademlia

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


```perl
my $local = pack('H*', '00' x 20);
my $peer  = pack('H*', '80' . ('00' x 19)); # 160th bit differs
my $idx   = xor_bucket_index($local, $peer);
say $idx; # 159
```

# Algorithm::Kademlia::RoutingTable

This class implements the 160 k-bucket structure (or appropriate size for the ID length provided). Peers are bucketed
by their XOR distance from the local node ID.

## `new( local_id_bin => ..., [ k => 20 ] )`

Constructor. `local_id_bin` is the binary string of the local node's ID.

```perl
my $rt = Algorithm::Kademlia::RoutingTable->new(
    local_id_bin => $my_id_bin,
    k            => 20

lib/Algorithm/Kademlia.pm  view on Meta::CPAN

            }
        }
        return undef;    # Same ID
    }
    class Algorithm::Kademlia::RoutingTable v1.1.0 {
        field $local_id_bin : param : writer : reader;
        field $k : param //= 20;
        field @buckets : reader;
        #
        ADJUST {
            my $id_len      = length $local_id_bin;
            my $num_buckets = $id_len * 8;
            @buckets = map { [] } 0 .. $num_buckets - 1
        }

        method add_peer ( $peer_id_bin, $peer_data ) {
            my $idx = Algorithm::Kademlia::xor_bucket_index( $local_id_bin, $peer_id_bin );
            return undef unless defined $idx;
            my $bucket = $buckets[$idx];

            # Find existing

lib/Algorithm/Kademlia.pod  view on Meta::CPAN

The index corresponds to the bit position of the most significant bit that differs between the two IDs. For 160-bit
IDs, the result is between 0 and 159.

    my $local = pack('H*', '00' x 20);
    my $peer  = pack('H*', '80' . ('00' x 19)); # 160th bit differs
    my $idx   = xor_bucket_index($local, $peer);
    say $idx; # 159

=head1 Algorithm::Kademlia::RoutingTable

This class implements the 160 k-bucket structure (or appropriate size for the ID length provided). Peers are bucketed
by their XOR distance from the local node ID.

=head2 C<new( local_id_bin =E<gt> ..., [ k =E<gt> 20 ] )>

Constructor. C<local_id_bin> is the binary string of the local node's ID.

    my $rt = Algorithm::Kademlia::RoutingTable->new(
        local_id_bin => $my_id_bin,
        k            => 20
    );



( run in 2.110 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )