0.16.1
Installable via npm install --save libp2p-kad-dht
, it can also be used directly in the browser.
The source is available for download from GitHub. Alternatively, you can install using npm:
$ npm install --save libp2p-kad-dht
You can then require()
libp2p-kad-dht as normal:
const libp2PKadDht = require('libp2p-kad-dht')
Libp2p-kad-dht should work in any ES2015 environment out of the box.
Usage:
<script type="text/javascript" src="index.js"></script>
The portable versions of libp2p-kad-dht, including index.js
and index.min.js
, are included in the /dist
folder. Libp2p-kad-dht can also be found on unpkg.com under
A DHT implementation modeled after Kademlia with S/Kademlia modifications.
Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
Extends EventEmitter
(Switch)
libp2p-switch instance
(object)
DHT options
Local reference to the libp2p-switch instance
Type: Switch
ALPHA concurrency at which each query path with run, defaults to 3
Type: number
Number of disjoint query paths to use
This is set to kBucketSize
/2 per the S/Kademlia paper
Type: number
The routing table.
Type: RoutingTable
Reference to the datastore, uses an in-memory store if none given.
Type: Datastore
Random walk management
Type: RandomWalk
Keeps track of running queries
Type: QueryManager
Is this DHT running.
Type: bool
Local peer (yourself)
Type: PeerInfo
Get the n
values to the given key without sorting.
(Buffer)
(number)
void
:
Search the dht for up to K
providers of the given CID.
(CID)
void
:
A wrapper around k-bucket
, to provide easy store and
retrival for peers.
(PeerId)
(number)
Creates a DHT ID by hashing a given buffer.
void
:
Creates a DHT ID by hashing a Peer ID
void
:
Convert a buffer to their SHA2-256 hash.
(Buffer)
Key
:
Generate the key for a public key.
(PeerId)
Buffer
:
Get the current time as timestamp.
number
:
Encode a given buffer into a base32 string.
(Buffer)
string
:
Decode a given base32 string into a buffer.
(string)
Buffer
:
Sort peers by distance to the given target
.
void
:
Compare function to sort an array of elements which have a distance property which is the xor distance to a given element.
number
:
Computes how many results to collect on each disjoint path, rounding up. This ensures that we look for at least one result per path.
number
:
Create a new put record, encodes and signs it if enabled.
void
:
Divide peers up into disjoint paths (subqueries). Any peer can only be used once over all paths. Within each path, query peers from closest to farthest away.
(DHT)
DHT instance
(Buffer)
(makePath)
Called to set up each disjoint path. Must return the query function.
Manages a single run of the query.
Extends EventEmitter
(Query)
Stop all the workers
Maintains a list of peerIds sorted by distance from a DHT key.
(Buffer)
the DHT key from which distance is calculated
(number)
the maximum size of the list
The length of the list
The peerIds in the list, in order of distance from the origin key
Indicates whether any of the peerIds passed as a parameter are closer to the origin key than the furthest peerId in the PeerDistanceList.
void
:
Manages a single Path through the DHT.
(Run)
(queryFunc)
Add a peer to the set of peers that are used to intialize the path.
(PeerId)
PeerQueue is a heap that sorts its entries (PeerIds) by their xor distance to the inital provided key.
(Buffer)
The sha2-256 encoded peer id
Creates a new WorkerQueue.
Create the underlying async queue.
Object
:
Stop the worker, optionally providing an error to pass to the worker's callback.
(Error)
Use the queue from async to keep concurrency
amount items running
per path.
Promise<void>
:
Add peers to the worker queue until there are enough to satisfy the worker queue concurrency. Note that we don't want to take any more than those required to satisfy concurrency from the peers-to-query queue, because we always want to query the closest peers to the key first, and new peers are continously being added to the peers-to-query queue.
Process the next peer in the queue
(PeerId)
Promise<void>
:
Handle network operations for the dht
(KadDHT)
Is the network online?
Type: bool
Are all network components there?
Type: bool
Handle incoming streams from the Switch, on the dht protocol.
(string)
(Connection)
undefined
:
Process GetValue
DHT messages.
undefined
:
Process PutValue
DHT messages.
undefined
:
Process FindNode
DHT messages.
undefined
:
Process AddProvider
DHT messages.
undefined
:
Process GetProviders
DHT messages.
(PeerInfo)
(Message)
Promise<Message>
:
Resolves a
Message
response
Process GetProviders
DHT messages.
undefined
:
Process Ping
DHT messages.
undefined
:
Like PeerList but with a length restriction.
Extends PeerList
(number)
Add a PeerInfo if it fits in the list
(PeerInfo)
bool
:
A list of unique peer infos.
Add a new info. Returns true
if it was a new one
(PeerInfo)
bool
:
Check if this PeerInfo is already in here.
(PeerInfo)
bool
:
Remove the last element
PeerInfo
:
This class manages known providers. A provider is a peer that we know to have the content for a given CID.
Every cleanupInterval
providers are checked if they
are still valid, i.e. younger than the provideValidity
.
If they are not, they are deleted.
To ensure the list survives restarts of the daemon, providers are stored in the datastore, but to ensure access is fast there is an LRU cache in front of that.
Represents a single DHT control message.
(DHT)
(object)
Name | Description |
---|---|
options.enabled randomWalkOptions.enabled
|
|
options.queriesPerPeriod randomWalkOptions.queriesPerPeriod
|
|
options.interval randomWalkOptions.interval
|
|
options.timeout randomWalkOptions.timeout
|
|
options.delay randomWalkOptions.delay
|
|
options.dht DHT
|
Start the Random Walk process. This means running a number of queries every interval requesting random data. This is done to keep the dht healthy over time.
void
:
Stop the random-walk process. Any active queries will be aborted.
void
:
Keeps track of all running queries.