Algorithm-Kademlia
view release on metacpan or search on metacpan
eg/kademlia_demo.pl view on Meta::CPAN
use v5.40;
use lib '../lib';
use Algorithm::Kademlia;
#
my $local_id = pack 'H*', '00' x 32;
my $rt = Algorithm::Kademlia::RoutingTable->new( local_id_bin => $local_id );
# Fill with some dummy peers
for ( 1 .. 50 ) {
my $pid = pack 'C*', map { int rand 256 } 1 .. 32;
$rt->add_peer( $pid, { index => $_ } );
}
use v5.40;
use Test2::V0;
use lib '../lib';
use Algorithm::Kademlia qw[xor_distance xor_bucket_index];
subtest 'XOR Distance' => sub {
my $id1 = pack 'H*', '00' x 32;
my $id2 = pack 'H*', '01' . ( '00' x 31 );
my $dist = xor_distance( $id1, $id2 );
is unpack( 'H*', $dist ), '01' . ( '00' x 31 ), 'Distance correct';
};
subtest 'Bucket Index' => sub {
my $id1 = pack 'H*', '00' x 32;
t/02_storage.t view on Meta::CPAN
use v5.40;
use Test2::V0;
use lib '../lib';
use Algorithm::Kademlia;
#
subtest 'Basic Storage' => sub {
my $storage = Algorithm::Kademlia::Storage->new();
my $key = 'foo';
my $val = 'bar';
$storage->put( $key, $val );
is $storage->get($key)->value, $val, 'Value stored and retrieved';
is $storage->get('missing'), undef, 'Missing key returns undef';
};
t/05_routing_table.t view on Meta::CPAN
use v5.40;
use Test2::V0;
use lib '../lib';
use Algorithm::Kademlia;
#
my $local = pack 'H*', '00' x 32;
my $rt = Algorithm::Kademlia::RoutingTable->new( local_id_bin => $local, k => 2 );
#
subtest 'Add/Retrieve Peer' => sub {
my $peer = pack 'H*', 'ff' . ( '00' x 31 );
$rt->add_peer( $peer, { name => 'Alice' } );
my @closest = $rt->find_closest($peer);
is $closest[0]{data}{name}, 'Alice', 'Stored and retrieved peer';
t/06_search.t view on Meta::CPAN
use v5.40;
use Test2::V0;
use lib '../lib';
use Algorithm::Kademlia;
#
my $target = pack 'H*', 'f0' . ( '00' x 19 );
my $search = Algorithm::Kademlia::Search->new( target_id_bin => $target, k => 2, alpha => 2 );
#
subtest 'Iterative Search State' => sub {
my $p1 = { id => pack( 'H*', 'f1' . ( '00' x 19 ) ), data => { ip => '1.1.1.1' } };
my $p2 = { id => pack( 'H*', 'f2' . ( '00' x 19 ) ), data => { ip => '2.2.2.2' } };
my $p3 = { id => pack( 'H*', 'a0' . ( '00' x 19 ) ), data => { ip => '3.3.3.3' } };
$search->add_candidates( $p1, $p2, $p3 );
( run in 0.897 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )