Skip to content

jeffrey-xiao/kademlia-dht-rs

Repository files navigation

kademlia-dht-rs

kademlia-dht 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.

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published