#dht #kademlia #hash-table #key #distributed #sha-3 #key-value

bin+lib kademlia-dht

A simple implementation of the Kademlia DHT

5 releases (3 stable)

1.2.0 Oct 27, 2019
1.1.0 Nov 1, 2018
1.0.0 Oct 6, 2018
0.2.0 Apr 12, 2018
0.1.0 Apr 5, 2018

#613 in Concurrency

MIT/Apache

46KB
885 lines

kademlia-dht-rs

Documentation License: MIT License: Apache 2.0 Build Status codecov

A flexible implementation of the Kademlia distributed hash table. This library crate was mainly created to better understand the Rust concurrency primitives. This implementation is fairly close to the spec described in the original Kademlia paper with the exception of a few design considerations.

Examples

extern crate kademlia_dht;
extern crate sha3;

use kademlia_dht::{Key, Node};
use sha3::{Digest, Sha3_256};
use std::thread;
use std::time::Duration;

fn clone_into_array<A, T>(slice: &[T]) -> A
where
    A: Sized + Default + AsMut<[T]>,
    T: Clone,
{
    let mut a = Default::default();
    <A as AsMut<[T]>>::as_mut(&mut a).clone_from_slice(slice);
    a
}

fn get_key(key: &str) -> Key {
    let mut hasher = Sha3_256::default();
    hasher.input(key.as_bytes());
    Key(clone_into_array(hasher.result().as_slice()))
}

fn main() {
    let mut node = Node::new("localhost", "8080", None);

    let key = get_key("Hello");
    let value = "World";

    node.insert(key, value);

    // inserting is asynchronous, so sleep for a second
    thread::sleep(Duration::from_millis(1000));

    assert_eq!(node.get(&key).unwrap(), value);
}

Usage

Add this to your Cargo.toml:

[dependencies]
kademlia-dht = "*"

and this to your crate root if you are using Rust 2015:

extern crate kademlia_dht;

Design Considerations

  • Many of the paper's original optimizations were not implemented due to their complexity for arguably little gain.
  • Each node's routing table uses a growable vector to represent the binary tree of k-buckets. The vector grows as the k-bucket closest to the node's ID exceeds capacity. The relaxation of k-bucket splitting proposed in Section 4.2 is not implemented.
  • Caching and key republishing described in Section 2.5 is not implemented to simplify the number of moving parts and active threads. It is up to the user of the library to ensure that keys are being republished.
  • The recursive lookup of nodes uses strict parallelism to tightly bound the number of active RPCs rather than the loose parallelism implied by the paper.
  • Each key is 256 bits as opposed to 160 bits so that consumers can use SHA-3 instead of SHA-1.

Changelog

See CHANGELOG for more details.

References

License

kademlia-dht-rs is dual-licensed under the terms of either the MIT License or the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for more details.

Dependencies

~5.5MB
~86K SLoC