Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit f03619e

Browse files
dirkmcvasco-santos
authored andcommitted
feat: limit scope of queries to k closest peers (#97)
* feat: limit scope of queries to k closest peers
1 parent 7b70fa7 commit f03619e

File tree

8 files changed

+909
-211
lines changed

8 files changed

+909
-211
lines changed

.aegir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
bundlesize: { maxSize: '191kB' }
2+
bundlesize: { maxSize: '192kB' }
33
}
44

src/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ exports.READ_MESSAGE_TIMEOUT = minute
2828
// The number of records that will be retrieved on a call to getMany()
2929
exports.GET_MANY_RECORD_COUNT = 16
3030

31-
// K is the maximum number of requests to perform before returning failue
31+
// K is the maximum number of requests to perform before returning failure
3232
exports.K = 20
3333

3434
// Alpha is the concurrency for asynchronous requests

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ class KadDHT extends EventEmitter {
7272
*
7373
* @type {number}
7474
*/
75-
this.kBucketSize = options.kBucketSize || 20
75+
this.kBucketSize = options.kBucketSize || c.K
7676

7777
/**
78-
* Number of closest peers to return on kBucket search, default 6
78+
* Number of closest peers to return on kBucket search, default 20
7979
*
8080
* @type {number}
8181
*/
82-
this.ncp = options.ncp || 6
82+
this.ncp = options.ncp || c.K
8383

8484
/**
8585
* The routing table.

src/peer-distance-list.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use strict'
2+
3+
const distance = require('xor-distance')
4+
const utils = require('./utils')
5+
const map = require('async/map')
6+
7+
/**
8+
* Maintains a list of peerIds sorted by distance from a DHT key.
9+
*/
10+
class PeerDistanceList {
11+
/**
12+
* Creates a new PeerDistanceList.
13+
*
14+
* @param {Buffer} originDhtKey - the DHT key from which distance is calculated
15+
* @param {number} capacity - the maximum size of the list
16+
*/
17+
constructor (originDhtKey, capacity) {
18+
this.originDhtKey = originDhtKey
19+
this.capacity = capacity
20+
this.peerDistances = []
21+
}
22+
23+
/**
24+
* The length of the list
25+
*/
26+
get length () {
27+
return this.peerDistances.length
28+
}
29+
30+
/**
31+
* The peerIds in the list, in order of distance from the origin key
32+
*/
33+
get peers () {
34+
return this.peerDistances.map(pd => pd.peerId)
35+
}
36+
37+
/**
38+
* Add a peerId to the list.
39+
*
40+
* @param {PeerId} peerId
41+
* @param {function(Error)} callback
42+
* @returns {void}
43+
*/
44+
add (peerId, callback) {
45+
if (this.peerDistances.find(pd => pd.peerId.id.equals(peerId.id))) {
46+
return callback()
47+
}
48+
49+
utils.convertPeerId(peerId, (err, dhtKey) => {
50+
if (err) {
51+
return callback(err)
52+
}
53+
54+
const el = {
55+
peerId,
56+
distance: distance(this.originDhtKey, dhtKey)
57+
}
58+
59+
this.peerDistances.push(el)
60+
this.peerDistances.sort((a, b) => distance.compare(a.distance, b.distance))
61+
this.peerDistances = this.peerDistances.slice(0, this.capacity)
62+
63+
callback()
64+
})
65+
}
66+
67+
/**
68+
* Indicates whether any of the peerIds passed as a parameter are closer
69+
* to the origin key than the furthest peerId in the PeerDistanceList.
70+
*
71+
* @param {Array<PeerId>} peerIds
72+
* @param {function(Error, Boolean)} callback
73+
* @returns {void}
74+
*/
75+
anyCloser (peerIds, callback) {
76+
if (!peerIds.length) {
77+
return callback(null, false)
78+
}
79+
80+
if (!this.length) {
81+
return callback(null, true)
82+
}
83+
84+
map(peerIds, (peerId, cb) => utils.convertPeerId(peerId, cb), (err, dhtKeys) => {
85+
if (err) {
86+
return callback(err)
87+
}
88+
89+
const furthestDistance = this.peerDistances[this.peerDistances.length - 1].distance
90+
for (const dhtKey of dhtKeys) {
91+
const keyDistance = distance(this.originDhtKey, dhtKey)
92+
if (distance.compare(keyDistance, furthestDistance) < 0) {
93+
return callback(null, true)
94+
}
95+
}
96+
return callback(null, false)
97+
})
98+
}
99+
}
100+
101+
module.exports = PeerDistanceList

0 commit comments

Comments
 (0)