| Crates.io | mielin-mesh-core |
| lib.rs | mielin-mesh-core |
| version | 0.1.0-rc.1 |
| created_at | 2026-01-18 01:50:18.994703+00 |
| updated_at | 2026-01-18 01:50:18.994703+00 |
| description | Distributed hash table and routing implementation with Kademlia-based DHT and geographic awareness |
| homepage | |
| repository | https://github.com/cool-japan/mielin |
| max_upload_size | |
| id | 2051571 |
| size | 803,460 |
Distributed Hash Table and Routing
Core networking primitives for the MielinMesh distributed system.
Add to your Cargo.toml:
[dependencies]
mielin-mesh-core = { path = "../mielin-mesh/core" }
use mielin_mesh_core::{Node, NodeRole};
// Create a relay node
let node = Node::new(NodeRole::Relay);
println!("Node ID: {}", node.id());
println!("Role: {:?}", node.role());
use mielin_mesh_core::dht::{Dht, PeerInfo};
use uuid::Uuid;
// Create DHT
let local_id = Uuid::new_v4();
let mut dht = Dht::new(local_id);
// Add peers
let peer = PeerInfo::new(Uuid::new_v4());
dht.insert_peer(peer);
// Find closest peers to a target
let target = Uuid::new_v4();
let closest = dht.find_closest(&target, 3);
// Find peers by latency
dht.update_peer_latency(&peer_id, 50); // 50ms
let low_latency_peers = dht.find_closest_by_latency(5);
Nodepub struct Node {
id: NodeId,
role: NodeRole,
}
pub enum NodeRole {
Edge, // Sensor/actuator nodes
Relay, // Intermediate routing nodes
Core, // High-capacity compute nodes
}
impl Node {
pub fn new(role: NodeRole) -> Self;
pub fn id(&self) -> &NodeId;
pub fn role(&self) -> NodeRole;
}
Dhtpub struct Dht {
local_id: NodeId,
routing_table: HashMap<NodeId, PeerInfo>,
}
impl Dht {
pub fn new(local_id: NodeId) -> Self;
pub fn insert_peer(&mut self, peer: PeerInfo);
pub fn find_closest(&self, target: &NodeId, k: usize) -> Vec<NodeId>;
pub fn find_closest_by_latency(&self, k: usize) -> Vec<NodeId>;
pub fn update_peer_latency(&mut self, node_id: &NodeId, latency_ms: u32);
pub fn peer_count(&self) -> usize;
}
PeerInfopub struct PeerInfo {
pub node_id: NodeId,
pub last_seen: Instant,
pub latency_ms: Option<u32>,
pub reliability_score: u8,
}
impl PeerInfo {
pub fn new(node_id: NodeId) -> Self;
pub fn is_alive(&self) -> bool; // Checks if peer is still responsive
pub fn update_seen(&mut self);
pub fn set_latency(&mut self, latency_ms: u32);
}
Peers are organized by XOR distance:
distance(A, B) = A XOR B
Closer distance = more similar IDs = preferred routing path.
use mielin_mesh_core::dht::{Dht, PeerInfo};
use uuid::Uuid;
let mut dht = Dht::new(Uuid::new_v4());
// Add 10 peers
for _ in 0..10 {
dht.insert_peer(PeerInfo::new(Uuid::new_v4()));
}
// Find 3 closest to target
let target = Uuid::new_v4();
let closest = dht.find_closest(&target, 3);
println!("Found {} closest peers", closest.len());
let mut dht = Dht::new(local_id);
// Add peers with latency information
let peer1 = Uuid::new_v4();
let peer2 = Uuid::new_v4();
dht.insert_peer(PeerInfo::new(peer1));
dht.insert_peer(PeerInfo::new(peer2));
dht.update_peer_latency(&peer1, 100); // 100ms
dht.update_peer_latency(&peer2, 10); // 10ms
// Get lowest latency peers
let fast_peers = dht.find_closest_by_latency(1);
assert_eq!(fast_peers[0], peer2); // Lowest latency first
use mielin_mesh_core::{Node, NodeRole};
// Create different node types
let edge = Node::new(NodeRole::Edge);
let relay = Node::new(NodeRole::Relay);
let core = Node::new(NodeRole::Core);
// Routing decisions based on role
match node.role() {
NodeRole::Edge => {
// Connect to nearby relay
}
NodeRole::Relay => {
// Bridge edge and core
}
NodeRole::Core => {
// Handle heavy computation
}
}
| Operation | Time Complexity | Notes |
|---|---|---|
| Insert peer | O(1) | HashMap insertion |
| Find closest | O(n log n) | Sorting by distance |
| Find by latency | O(n log n) | Filter + sort |
| Peer eviction | O(n) | Full table scan |
Typical performance (1000 peers):
const K_BUCKET_SIZE: usize = 20; // Peers per bucket
const PEER_TIMEOUT: Duration = Duration::from_secs(300); // 5 minutes
cargo test -p mielin-mesh-core
Tests include:
MIT OR Apache-2.0