KademliaNode

Extends AbstractNode with Kademlia-specific rules

Constructor

new KademliaNode()

Extends

Methods

expire(callbackopt) → {Promise}

Items expire T_EXPIRE seconds after the original publication. All items are assigned an expiration time which is "exponentially inversely proportional to the number of nodes between the current node and the node whose ID is closest to the key", where this number is "inferred from the bucket structure of the current node".

Parameters:
NameTypeAttributesDescription
callbackKademliaNode~expireCallback<optional>
Returns:
Type: 
Promise

iterativeFindNode(key, callbackopt) → {Promise.<Array.<Bucket~contact>>}

Basic kademlia lookup operation that builds a set of K contacts closest to the given key

Parameters:
NameTypeAttributesDescription
keybuffer | string

Reference key for node lookup

callbackKademliaNode~iterativeFindNodeCallback<optional>
Returns:
Type: 
Promise.<Array.<Bucket~contact>>

iterativeFindValue(key, callbackopt) → {Promise.<object>}

Kademlia search operation that is conducted as a node lookup and builds a list of K closest contacts. If at any time during the lookup the value is returned, the search is abandoned. If no value is found, the K closest contacts are returned. Upon success, we must store the value at the nearest node seen during the search that did not return the value.

Parameters:
NameTypeAttributesDescription
keybuffer | string

Key for value lookup

callbackKademliaNode~iterativeFindValueCallback<optional>
Returns:
Type: 
Promise.<object>

iterativeStore(key, value, callback) → {Promise.<number>}

Performs a KademliaNode#iterativeFindNode to collect K contacts nearest to the given key, sending a STORE message to each of them.

Parameters:
NameTypeDescription
keybuffer | string

Key to store data under

valuebuffer | string | object

Value to store by key

callbackKademliaNode~iterativeStoreCallback
Returns:
Type: 
Promise.<number>

join(peer, joinListeneropt) → {Promise}

Inserts the given contact into the routing table and uses it to perform a KademliaNode#iterativeFindNode for this node's identity, then refreshes all buckets further than it's closest neighbor, which will be in the occupied bucket with the lowest index

Parameters:
NameTypeAttributesDescription
peerBucket~contact

Peer to bootstrap from

joinListenerfunction<optional>

Function to set as join listener

Returns:
Type: 
Promise

listen()

Adds the kademlia rule handlers before calling super#listen()

ping(peer, callbackopt) → {Promise.<number>}

Sends a PING message to the supplied contact, resolves with latency

Parameters:
NameTypeAttributesDescription
peerBucket~contact
callbackKademliaNode~pingCallback<optional>
Returns:
Type: 
Promise.<number>

plugin(plugin)

Accepts an arbitrary function that receives this node as context for mounting protocol handlers and extending the node with other methods

Parameters:
NameTypeDescription
pluginfunction

Using and Authoring Plugins

receive(request, response)

Processes a the given arguments by sending them through the appropriate middleware stack

Parameters:
NameTypeDescription
requestAbstractNode~request
responseAbstractNode~response

refresh(startIndex, callbackopt) → {Promise}

If no node lookups have been performed in any given bucket's range for T_REFRESH, the node selects a random number in that range and does a refresh, an iterativeFindNode using that number as key.

Parameters:
NameTypeAttributesDefaultDescription
startIndexnumber0

bucket index to start refresh from

callbackKademliaNode~refreshCallback<optional>
Returns:
Type: 
Promise

replicate(callbackopt) → {Promise}

Performs a scan of the storage adapter and performs republishing/replication of items stored. Items that we did not publish ourselves get republished every T_REPLICATE. Items we did publish get republished every T_REPUBLISH.

Parameters:
NameTypeAttributesDescription
callbackKademliaNode~replicateCallback<optional>
Returns:
Type: 
Promise

send(method, params, contact, callbackopt) → {Promise.<(object|array), Error>}

Sends the [method, params] to the contact and executes the handler on response or timeout

Parameters:
NameTypeAttributesDescription
methodstring

RPC method name

paramsobject | array

RPC parameters

contactBucket~contact

Destination address information

callbackAbstractNode~sendCallback<optional>
Returns:
Type: 
Promise.<(object|array), Error>

use(methodopt, middleware)

Mounts a message handler route for processing incoming RPC messages

Parameters:
NameTypeAttributesDescription
methodstring<optional>

RPC method name to route through

middlewareAbstractNode~middleware

Type Definitions

entry

Type:
  • object
Properties
NameTypeDescription
valuestring | object | array

The primary entry value

publisherstring

Node identity of the original publisher

timestampnumber

Last update/replicate time

expireCallback(error)

Parameters:
NameTypeDescription
errorerror | null

iterativeFindNodeCallback(error, contacts)

Parameters:
NameTypeDescription
errorerror | null
contactsArray.<Bucket~contact>

Result of the lookup operation

iterativeFindValueCallback(error, value, contact)

Parameters:
NameTypeDescription
errorerror | null
valueKademliaNode~entry
contactnull | Bucket~contact

Contact responded with entry

iterativeStoreCallback(error, stored)

Note that if there is a protocol/validation error, you will not receive it as an error in the callback. Be sure to also check that stored > 0 as part of error handling here.

Parameters:
NameTypeDescription
errorerror | null
storednumber

Total nodes who stored the pair

pingCallback(error, latency)

Parameters:
NameTypeDescription
errorerror | null
latencynumber

Milliseconds before response received

refreshCallback(error, bucketsRefreshed)

Parameters:
NameTypeDescription
errorerror | null
bucketsRefreshedarray

replicateCallback(error)

Parameters:
NameTypeDescription
errorerror | null

Events

error

Error event fires when a critical failure has occurred; if no handler is specified, then it will throw

Type:
  • Error

join

Join event is triggered when the routing table is no longer empty