Gathering detailed insights and metrics for kademlia-tables
Gathering detailed insights and metrics for kademlia-tables
An extension of the kademlia-table library for managing nodes between many tiered kademlia tables.
npm install kademlia-tables
Typescript
Module System
Node Version
NPM Version
70
Supply Chain
98.6
Quality
77.1
Maintenance
100
Vulnerability
100
License
TypeScript (98.62%)
JavaScript (1.38%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
310
Last Day
1
Last Week
1
Last Month
8
Last Year
310
MIT License
3 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Apr 15, 2024
Minified
Minified + Gzipped
Latest Version
1.0.1
Package Id
kademlia-tables@1.0.1
Unpacked Size
17.24 kB
Size
4.86 kB
File Count
7
NPM Version
10.5.0
Node Version
18.20.1
Published on
Apr 16, 2024
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
-50%
1
Compared to previous week
Last Month
-66.7%
8
Compared to previous month
Last Year
0%
310
Compared to previous year
An extension of the kademlia-table library for managing nodes between many kademlia tables.
npm install kademlia-tables
kademlia-table package is an extendable implementation of Kademlia and K-Buckets closely following details set out in the Kademlia DHT paper.
The default implementation of Kademlia has drawbacks such as long node traversal time since round trip time and locality are not taken into account. When traversing nodes, the local node may contact nodes on the other side of the world just to find it needs to contact a node in the next building.
The Sloppy hashing and self-organizing clusters paper suggests clustering as a remedy to this. Multiple tiers of Kademlia Table with nodes sorted by round trip time gives rise to node clustering around locality and connectivity. Nodes with lower table index and thus lower round trip response times are preffered when building routes, drastically reducing traversal time.
The KademliaTables
class is extendable to prefer metrics other than round trip time to build clustering around that metric.
1import { KademliaTables } from "kademlia-tables"; 2import { randomBytes } from "node:crypto"; 3 4interface Node { 5 id: string; 6 pingMs: number; 7 // ...properties of Node 8} 9 10// The base KademliaTables class is abstract and must be extended with a custom method to assign nodes to a table. 11class CustomTables extends KademliaTables<Node> { 12 // Required table assignment method 13 getTableIndex(node: Node): number { 14 return node.pingMs < 30 ? 0 : node.pingMs < 100 ? 1 : 2; // Assign a node to table 0, 1, or 2 depending on pingMs 15 16 // Tables are indexed by number with 0 being the most preferred table. 17 } 18 19 // Optional customization of the add method 20 add(node: Node): boolean { 21 const result = super.add(node); 22 23 if (!result) { 24 // Do something if bucket is full and retry 25 return this.add(node); 26 } 27 28 return true; 29 } 30} 31 32// Create new tables that store nodes "close" to the passed in id. 33// The id should be uniformily distributed, ie a hash, random bytes etc. 34const tables = new KademliaTables<Node>(id(), { tableCount: 3 }); 35 36// Add a node to the routing tables 37tables.add({ id: id() }); 38 39// Get the 20 nodes "closest" to a passed in id 40const closest = table.closest(id(), 20);
table = new KademliaTable(id, [configuration])
Create a new routing table.
id
should be a string that is uniformily distributed. configuration
includes:
1{ 2 bucketSize?: 20 // Max number of nodes in a bucket 3 encoding?: "utf8" // Encoding of id strings 4 tableCount?: 3 // Number of tables, indexed 0 - length-1 5 preferenceFactor?: 2 // Range of closeness is multiplied by this every step closer to the most preferred table 6 7 // Eg. Table 2 returns 3 nodes in a 2 bucket range. Table 1 can return nodes in 4 bucket range and Table 0 can return nodes in a 8 bucket range. 8 // The reason for this is that it is preferrable to route through a node that is further away in the binary tree but has a 10ms round trip time than a closer node with a 200ms round trip time. 9 10 KademliaTable?: KademliaTable // Allows for using your own extended KademliaTables. 11}
bool = tables.add(node)
Insert a new node. node.id
must be a string of same or shorter length as tables.id
.
When inserting a node the XOR distance between the node and the tables.id
is
calculated and used to figure which bucket this node should be inserted into.
Returns true
if the node could be added or already exists.
Returns false
if the bucket is full.
bool = tables.has(id)
Returns true
if a node exists for the passed in id
and false
otherwise.
node = tables.get(id)
Returns a node or undefined
if not found.
ti = tables.getTableIndex(node)
Returns a node's corresponding table index.
i = tables.getBucketIndex(id)
Returns a node's corresponding bucket index.
nodes = tables.closest(id, [maxNodes])
Returns an array of the closest (in XOR distance) nodes to the passed in id.
This method is normally used in a routing context, i.e. figuring out which nodes in a DHT should store a value based on its id.
Nodes are preferred from lower index tables even though they may not be as close. preferenceFactor
in the tables configuration sets the size of this effect.
Returns an exact match first regardless of table index.
true = tables.remove(id)
Remove a node using its id.
tables.nodes
Returns all nodes from tables as an array. Ordered from closest to furthest buckets.
tables.buckets
A fixed size array of all buckets in the tables.
tables.tables
A fixed size array of all tables.
number = KademliaTables.getDistance(idA, idB, encoding)
Gets the XOR distance between two id strings.
1 | -1 | 0 = compare(idA, idB) = KademliaTables.createCompare(targetId, encoding)
Creates a function for sorting ids based on distance from a target id going from closest to furthest
MIT
No vulnerabilities found.
No security vulnerabilities found.