Skip to main content

Module routing_table_manager

Module routing_table_manager 

Source
Expand description

Kademlia-style XOR-metric routing table manager for P2P overlay networks.

Implements a full 256-bucket Kademlia routing table where each bucket stores at most k entries (default 20) sorted by last-seen time. A replacement cache holds candidates that were not admitted because the bucket was full.

§Design highlights

  • NodeId([u8; 32]) — 256-bit node identifier (SHA-256 hash of public key).
  • BucketEntry — information about a known remote node.
  • KBucket — a single k-bucket with a replacement cache (VecDeque).
  • RoutingTableManager — 256 k-buckets indexed by the leading-zero-bit count of XOR(local_id, target_id).

§Thread safety

RoutingTableManager is not Sync by design; callers that need shared access should wrap it in parking_lot::RwLock or tokio::sync::RwLock.

§Example

use ipfrs_network::routing_table_manager::{NodeId, RoutingTableManager};

let local = NodeId([0u8; 32]);
let mut rtm = RoutingTableManager::new(local, 20, 3);

let mut target_bytes = [0u8; 32];
target_bytes[0] = 0x80; // distance bucket 0
let target = NodeId(target_bytes);

rtm.add_node(target, "127.0.0.1:4001".to_string(), 5, 1000);
assert_eq!(rtm.total_nodes(), 1);

Structs§

BucketEntry
Metadata stored for every known remote node.
KBucket
A single Kademlia k-bucket.
NodeId
256-bit node identifier, typically the SHA-256 hash of a public key.
RoutingTableManager
Kademlia-style XOR-metric routing table manager.
RoutingTableStats
Aggregate statistics for the routing table.

Constants§

DEFAULT_ALPHA
Default alpha concurrency parameter.
DEFAULT_K
Default k-bucket size (standard Kademlia value).
DEFAULT_REPLACEMENT_CACHE_SIZE
Maximum number of entries kept in a bucket’s replacement cache.

Functions§

bucket_index
Return the index of the k-bucket for target relative to local.
xor_distance
Compute the 256-bit XOR distance between two node IDs.