libp2p-kad-dht
Advanced tools
Comparing version 0.14.13 to 0.14.14
@@ -0,1 +1,11 @@ | ||
<a name="0.14.14"></a> | ||
## [0.14.14](https://github.com/libp2p/js-libp2p-kad-dht/compare/v0.14.13...v0.14.14) (2019-05-08) | ||
### Bug Fixes | ||
* performance improvements ([#107](https://github.com/libp2p/js-libp2p-kad-dht/issues/107)) ([ddf80fe](https://github.com/libp2p/js-libp2p-kad-dht/commit/ddf80fe)) | ||
<a name="0.14.13"></a> | ||
@@ -2,0 +12,0 @@ ## [0.14.13](https://github.com/libp2p/js-libp2p-kad-dht/compare/v0.14.12...v0.14.13) (2019-04-22) |
{ | ||
"name": "libp2p-kad-dht", | ||
"version": "0.14.13", | ||
"version": "0.14.14", | ||
"description": "JavaScript implementation of the Kad-DHT for libp2p", | ||
@@ -17,3 +17,4 @@ "leadMaintainer": "Vasco Santos <vasco.santos@moxy.studio>", | ||
"coverage": "aegir coverage", | ||
"coverage-publish": "aegir-coverage publish" | ||
"coverage-publish": "aegir-coverage publish", | ||
"sim": "node test/simulation/index.js" | ||
}, | ||
@@ -93,4 +94,4 @@ "pre-push": [ | ||
"Thomas Eizinger <thomas@eizinger.io>", | ||
"Vasco Santos <vasco.santos@moxy.studio>", | ||
"Vasco Santos <vasco.santos@ua.pt>", | ||
"Vasco Santos <vasco.santos@moxy.studio>", | ||
"dirkmc <dirkmdev@gmail.com>", | ||
@@ -97,0 +98,0 @@ "dirkmc <dirk@mccormick.cx>", |
@@ -26,3 +26,3 @@ 'use strict' | ||
exports.READ_MESSAGE_TIMEOUT = minute | ||
exports.READ_MESSAGE_TIMEOUT = 10 * second | ||
@@ -38,6 +38,2 @@ // The number of records that will be retrieved on a call to getMany() | ||
// Number of disjoint query paths to use | ||
// This is set to K/2 per the S/Kademlia paper | ||
exports.DISJOINT_PATHS = 10 | ||
exports.maxMessageSize = 2 << 22 // 4MB | ||
@@ -49,4 +45,4 @@ | ||
interval: 5 * minute, | ||
timeout: 30 * second, | ||
timeout: 10 * second, | ||
delay: 10 * second | ||
} |
@@ -51,2 +51,3 @@ 'use strict' | ||
* @param {number} options.kBucketSize k-bucket size (default 20) | ||
* @param {number} options.concurrency alpha concurrency of queries (default 3) | ||
* @param {Datastore} options.datastore datastore (default MemoryDatastore) | ||
@@ -79,9 +80,15 @@ * @param {object} options.validators validators object with namespace as keys and function(key, record, callback) | ||
/** | ||
* Number of closest peers to return on kBucket search, default 20 | ||
* | ||
* ALPHA concurrency at which each query path with run, defaults to 3 | ||
* @type {number} | ||
*/ | ||
this.ncp = options.ncp || c.K | ||
this.concurrency = options.concurrency || c.ALPHA | ||
/** | ||
* Number of disjoint query paths to use | ||
* This is set to `kBucketSize`/2 per the S/Kademlia paper | ||
* @type {number} | ||
*/ | ||
this.disjointPaths = Math.ceil(this.kBucketSize / 2) | ||
/** | ||
* The routing table. | ||
@@ -326,3 +333,3 @@ * | ||
(id, cb) => { | ||
const rtp = this.routingTable.closestPeers(id, c.ALPHA) | ||
const rtp = this.routingTable.closestPeers(id, this.kBucketSize) | ||
@@ -418,3 +425,3 @@ this._log('peers in rt: %d', rtp.length) | ||
const tablePeers = this.routingTable.closestPeers(id, c.ALPHA) | ||
const tablePeers = this.routingTable.closestPeers(id, this.kBucketSize) | ||
@@ -449,3 +456,3 @@ const q = new Query(this, key, () => { | ||
(cb) => utils.sortClosestPeers(Array.from(res.finalSet), id, cb), | ||
(sorted, cb) => cb(null, sorted.slice(0, c.K)) | ||
(sorted, cb) => cb(null, sorted.slice(0, this.kBucketSize)) | ||
], callback) | ||
@@ -535,2 +542,3 @@ }) | ||
const errors = [] | ||
waterfall([ | ||
@@ -545,6 +553,17 @@ (cb) => this.providers.addProvider(key, this.peerInfo.id, cb), | ||
this._log('putProvider %s to %s', key.toBaseEncodedString(), peer.toB58String()) | ||
this.network.sendMessage(peer, msg, cb) | ||
this.network.sendMessage(peer, msg, (err) => { | ||
if (err) errors.push(err) | ||
cb() | ||
}) | ||
}, cb) | ||
} | ||
], (err) => callback(err)) | ||
], (err) => { | ||
if (errors.length) { | ||
// This should be infrequent. This means a peer we previously connected | ||
// to failed to exchange the provide message. If getClosestPeers was an | ||
// iterator, we could continue to pull until we announce to kBucketSize peers. | ||
err = errcode(`Failed to provide to ${errors.length} of ${this.kBucketSize} peers`, 'ERR_SOME_PROVIDES_FAILED', { errors }) | ||
} | ||
callback(err) | ||
}) | ||
} | ||
@@ -623,3 +642,3 @@ | ||
(key, cb) => { | ||
const peers = this.routingTable.closestPeers(key, c.ALPHA) | ||
const peers = this.routingTable.closestPeers(key, this.kBucketSize) | ||
@@ -626,0 +645,0 @@ if (peers.length === 0) { |
@@ -37,3 +37,3 @@ 'use strict' | ||
try { | ||
ids = dht.routingTable.closestPeers(key, dht.ncp) | ||
ids = dht.routingTable.closestPeers(key, dht.kBucketSize) | ||
} catch (err) { | ||
@@ -86,3 +86,3 @@ return callback(err) | ||
* - it was either authored by this node, or | ||
* - it was receceived less than `MAX_RECORD_AGE` ago. | ||
* - it was received less than `MAX_RECORD_AGE` ago. | ||
* | ||
@@ -176,3 +176,3 @@ * @param {Buffer} key | ||
* @param {PeerId} peer | ||
* @param {function(Error)} callback | ||
* @param {function(Error, Array<PeerInfo>)} callback | ||
* @returns {void} | ||
@@ -191,3 +191,2 @@ * | ||
.filter((pInfo) => !dht._isSelf(pInfo.id)) | ||
.map((pInfo) => dht.peerBook.put(pInfo)) | ||
@@ -198,3 +197,3 @@ callback(null, out) | ||
/** | ||
* Is the given peer id the peer id? | ||
* Is the given peer id our PeerId? | ||
* | ||
@@ -214,3 +213,3 @@ * @param {PeerId} other | ||
* @param {PeerId} target | ||
* @param {function(Error)} callback | ||
* @param {function(Error, Message)} callback | ||
* @returns {void} | ||
@@ -531,3 +530,3 @@ * | ||
const peers = dht.routingTable.closestPeers(key.buffer, c.ALPHA) | ||
const peers = dht.routingTable.closestPeers(key.buffer, dht.kBucketSize) | ||
@@ -534,0 +533,0 @@ timeout((cb) => query.run(peers, cb), providerTimeout)((err) => { |
@@ -97,7 +97,7 @@ 'use strict' | ||
/** | ||
* Sort peers by distance to the given `id`. | ||
* Sort peers by distance to the given `target`. | ||
* | ||
* @param {Array<PeerId>} peers | ||
* @param {Buffer} target | ||
* @param {function(Error, )} callback | ||
* @param {function(Error, Array<PeerId>)} callback | ||
* @returns {void} | ||
@@ -104,0 +104,0 @@ */ |
@@ -740,3 +740,3 @@ /* eslint-env mocha */ | ||
const rtablePeers = guy.routingTable.closestPeers(rtval, c.ALPHA) | ||
expect(rtablePeers).to.have.length(3) | ||
expect(rtablePeers).to.have.length(c.ALPHA) | ||
@@ -772,9 +772,9 @@ // The set of peers used to initiate the query (the closest alpha | ||
// Expect that there were 20 peers found | ||
expect(out).to.have.length(20) | ||
// Expect that there were kValue peers found | ||
expect(out).to.have.length(c.K) | ||
// The expected closest 20 peers to the key | ||
const exp = actualClosest.slice(0, 20) | ||
// The expected closest kValue peers to the key | ||
const exp = actualClosest.slice(0, c.K) | ||
// Expect the 20 peers found to be the 20 closest connected peers | ||
// Expect the kValue peers found to be the kValue closest connected peers | ||
// to the key | ||
@@ -781,0 +781,0 @@ expect(countDiffPeers(exp, out)).to.eql(0) |
'use strict' | ||
const multihashing = require('multihashing-async') | ||
const distance = require('xor-distance') | ||
const waterfall = require('async/waterfall') | ||
const map = require('async/map') | ||
const { | ||
convertPeerId, | ||
sortClosestPeers | ||
} = require('../../src/utils') | ||
function convertPeerId (peer, callback) { | ||
multihashing.digest(peer.id, 'sha2-256', callback) | ||
} | ||
function sortClosestPeers (peers, target, callback) { | ||
map(peers, (peer, cb) => { | ||
convertPeerId(peer, (err, id) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
cb(null, { | ||
peer: peer, | ||
distance: distance(id, target) | ||
}) | ||
}) | ||
}, (err, distances) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
callback(null, distances.sort(xorCompare).map((d) => d.peer)) | ||
}) | ||
} | ||
function xorCompare (a, b) { | ||
return distance.compare(a.distance, b.distance) | ||
} | ||
/* | ||
@@ -38,0 +10,0 @@ * Given an array of peerInfos, decide on a target, start peers, and |
@@ -7,3 +7,7 @@ 'use strict' | ||
// Creates multiple PeerInfos | ||
/** | ||
* Creates multiple PeerInfos | ||
* @param {number} n The number of `PeerInfo` to create | ||
* @param {function(Error, Array<PeerInfo>)} callback | ||
*/ | ||
function createPeerInfo (n, callback) { | ||
@@ -10,0 +14,0 @@ times(n, (i, cb) => PeerId.create({ bits: 512 }, cb), (err, ids) => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
244594
69
7261