@libp2p/kad-dht 9.3.2 → 9.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +19 -34
- package/dist/src/routing-table/index.d.ts +4 -32
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +7 -7
- package/dist/src/routing-table/index.js.map +1 -1
- package/dist/src/routing-table/k-bucket.d.ts +188 -0
- package/dist/src/routing-table/k-bucket.d.ts.map +1 -0
- package/dist/src/routing-table/k-bucket.js +409 -0
- package/dist/src/routing-table/k-bucket.js.map +1 -0
- package/package.json +3 -3
- package/src/routing-table/index.ts +13 -45
- package/src/routing-table/k-bucket.ts +523 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/*
|
|
2
|
+
index.js - Kademlia DHT K-bucket implementation as a binary tree.
|
|
3
|
+
|
|
4
|
+
The MIT License (MIT)
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2013-2021 Tristan Slominski
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person
|
|
9
|
+
obtaining a copy of this software and associated documentation
|
|
10
|
+
files (the "Software"), to deal in the Software without
|
|
11
|
+
restriction, including without limitation the rights to use,
|
|
12
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the
|
|
14
|
+
Software is furnished to do so, subject to the following
|
|
15
|
+
conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be
|
|
18
|
+
included in all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
21
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
22
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
23
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
24
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
25
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
26
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
27
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
28
|
+
*/
|
|
29
|
+
import { EventEmitter } from '@libp2p/interfaces/events';
|
|
30
|
+
function arrayEquals(array1, array2) {
|
|
31
|
+
if (array1 === array2) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if (array1.length !== array2.length) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
for (let i = 0, length = array1.length; i < length; ++i) {
|
|
38
|
+
if (array1[i] !== array2[i]) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
function createNode() {
|
|
45
|
+
// @ts-expect-error loose types
|
|
46
|
+
return { contacts: [], dontSplit: false, left: null, right: null };
|
|
47
|
+
}
|
|
48
|
+
function ensureInt8(name, val) {
|
|
49
|
+
if (!(val instanceof Uint8Array)) {
|
|
50
|
+
throw new TypeError(name + ' is not a Uint8Array');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Implementation of a Kademlia DHT k-bucket used for storing
|
|
55
|
+
* contact (peer node) information.
|
|
56
|
+
*
|
|
57
|
+
* @extends EventEmitter
|
|
58
|
+
*/
|
|
59
|
+
export class KBucket extends EventEmitter {
|
|
60
|
+
localNodeId;
|
|
61
|
+
root;
|
|
62
|
+
numberOfNodesPerKBucket;
|
|
63
|
+
numberOfNodesToPing;
|
|
64
|
+
distance;
|
|
65
|
+
arbiter;
|
|
66
|
+
constructor(options) {
|
|
67
|
+
super();
|
|
68
|
+
this.localNodeId = options.localNodeId;
|
|
69
|
+
this.numberOfNodesPerKBucket = options.numberOfNodesPerKBucket ?? 20;
|
|
70
|
+
this.numberOfNodesToPing = options.numberOfNodesToPing ?? 3;
|
|
71
|
+
this.distance = options.distance ?? KBucket.distance;
|
|
72
|
+
// use an arbiter from options or vectorClock arbiter by default
|
|
73
|
+
this.arbiter = options.arbiter ?? KBucket.arbiter;
|
|
74
|
+
ensureInt8('option.localNodeId as parameter 1', this.localNodeId);
|
|
75
|
+
this.root = createNode();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Default arbiter function for contacts with the same id. Uses
|
|
79
|
+
* contact.vectorClock to select which contact to update the k-bucket with.
|
|
80
|
+
* Contact with larger vectorClock field will be selected. If vectorClock is
|
|
81
|
+
* the same, candidate will be selected.
|
|
82
|
+
*
|
|
83
|
+
* @param {object} incumbent - Contact currently stored in the k-bucket.
|
|
84
|
+
* @param {object} candidate - Contact being added to the k-bucket.
|
|
85
|
+
* @returns {object} Contact to updated the k-bucket with.
|
|
86
|
+
*/
|
|
87
|
+
static arbiter(incumbent, candidate) {
|
|
88
|
+
return (incumbent.vectorClock ?? 0) > (candidate.vectorClock ?? 0) ? incumbent : candidate;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Default distance function. Finds the XOR
|
|
92
|
+
* distance between firstId and secondId.
|
|
93
|
+
*
|
|
94
|
+
* @param {Uint8Array} firstId - Uint8Array containing first id.
|
|
95
|
+
* @param {Uint8Array} secondId - Uint8Array containing second id.
|
|
96
|
+
* @returns {number} Integer The XOR distance between firstId and secondId.
|
|
97
|
+
*/
|
|
98
|
+
static distance(firstId, secondId) {
|
|
99
|
+
let distance = 0;
|
|
100
|
+
let i = 0;
|
|
101
|
+
const min = Math.min(firstId.length, secondId.length);
|
|
102
|
+
const max = Math.max(firstId.length, secondId.length);
|
|
103
|
+
for (; i < min; ++i) {
|
|
104
|
+
distance = distance * 256 + (firstId[i] ^ secondId[i]);
|
|
105
|
+
}
|
|
106
|
+
for (; i < max; ++i)
|
|
107
|
+
distance = distance * 256 + 255;
|
|
108
|
+
return distance;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Adds a contact to the k-bucket.
|
|
112
|
+
*
|
|
113
|
+
* @param {object} contact - the contact object to add
|
|
114
|
+
*/
|
|
115
|
+
add(contact) {
|
|
116
|
+
ensureInt8('contact.id', contact?.id);
|
|
117
|
+
let bitIndex = 0;
|
|
118
|
+
let node = this.root;
|
|
119
|
+
while (node.contacts === null) {
|
|
120
|
+
// this is not a leaf node but an inner node with 'low' and 'high'
|
|
121
|
+
// branches; we will check the appropriate bit of the identifier and
|
|
122
|
+
// delegate to the appropriate node for further processing
|
|
123
|
+
node = this._determineNode(node, contact.id, bitIndex++);
|
|
124
|
+
}
|
|
125
|
+
// check if the contact already exists
|
|
126
|
+
const index = this._indexOf(node, contact.id);
|
|
127
|
+
if (index >= 0) {
|
|
128
|
+
this._update(node, index, contact);
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
if (node.contacts.length < this.numberOfNodesPerKBucket) {
|
|
132
|
+
node.contacts.push(contact);
|
|
133
|
+
this.safeDispatchEvent('added', { detail: contact });
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
// the bucket is full
|
|
137
|
+
if (node.dontSplit) {
|
|
138
|
+
// we are not allowed to split the bucket
|
|
139
|
+
// we need to ping the first this.numberOfNodesToPing
|
|
140
|
+
// in order to determine if they are alive
|
|
141
|
+
// only if one of the pinged nodes does not respond, can the new contact
|
|
142
|
+
// be added (this prevents DoS flodding with new invalid contacts)
|
|
143
|
+
this.safeDispatchEvent('ping', {
|
|
144
|
+
detail: {
|
|
145
|
+
oldContacts: node.contacts.slice(0, this.numberOfNodesToPing),
|
|
146
|
+
newContact: contact
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
this._split(node, bitIndex);
|
|
152
|
+
return this.add(contact);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Get the n closest contacts to the provided node id. "Closest" here means:
|
|
156
|
+
* closest according to the XOR metric of the contact node id.
|
|
157
|
+
*
|
|
158
|
+
* @param {Uint8Array} id - Contact node id
|
|
159
|
+
* @param {number} n - Integer (Default: Infinity) The maximum number of closest contacts to return
|
|
160
|
+
* @returns {Array} Array Maximum of n closest contacts to the node id
|
|
161
|
+
*/
|
|
162
|
+
closest(id, n = Infinity) {
|
|
163
|
+
ensureInt8('id', id);
|
|
164
|
+
if ((!Number.isInteger(n) && n !== Infinity) || n <= 0) {
|
|
165
|
+
throw new TypeError('n is not positive number');
|
|
166
|
+
}
|
|
167
|
+
let contacts = [];
|
|
168
|
+
for (let nodes = [this.root], bitIndex = 0; nodes.length > 0 && contacts.length < n;) {
|
|
169
|
+
const node = nodes.pop();
|
|
170
|
+
if (node == null) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (node.contacts === null) {
|
|
174
|
+
const detNode = this._determineNode(node, id, bitIndex++);
|
|
175
|
+
nodes.push(node.left === detNode ? node.right : node.left);
|
|
176
|
+
nodes.push(detNode);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
contacts = contacts.concat(node.contacts);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return contacts
|
|
183
|
+
.map(a => ({
|
|
184
|
+
distance: this.distance(a.id, id),
|
|
185
|
+
contact: a
|
|
186
|
+
}))
|
|
187
|
+
.sort((a, b) => a.distance - b.distance)
|
|
188
|
+
.slice(0, n)
|
|
189
|
+
.map(a => a.contact);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Counts the total number of contacts in the tree.
|
|
193
|
+
*
|
|
194
|
+
* @returns {number} The number of contacts held in the tree
|
|
195
|
+
*/
|
|
196
|
+
count() {
|
|
197
|
+
// return this.toArray().length
|
|
198
|
+
let count = 0;
|
|
199
|
+
for (const nodes = [this.root]; nodes.length > 0;) {
|
|
200
|
+
const node = nodes.pop();
|
|
201
|
+
if (node == null) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (node.contacts === null) {
|
|
205
|
+
nodes.push(node.right, node.left);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
count += node.contacts.length;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return count;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Determines whether the id at the bitIndex is 0 or 1.
|
|
215
|
+
* Return left leaf if `id` at `bitIndex` is 0, right leaf otherwise
|
|
216
|
+
*
|
|
217
|
+
* @param {object} node - internal object that has 2 leafs: left and right
|
|
218
|
+
* @param {Uint8Array} id - Id to compare localNodeId with.
|
|
219
|
+
* @param {number} bitIndex - Integer (Default: 0) The bit index to which bit to check in the id Uint8Array.
|
|
220
|
+
* @returns {object} left leaf if id at bitIndex is 0, right leaf otherwise.
|
|
221
|
+
*/
|
|
222
|
+
_determineNode(node, id, bitIndex) {
|
|
223
|
+
// **NOTE** remember that id is a Uint8Array and has granularity of
|
|
224
|
+
// bytes (8 bits), whereas the bitIndex is the _bit_ index (not byte)
|
|
225
|
+
// id's that are too short are put in low bucket (1 byte = 8 bits)
|
|
226
|
+
// (bitIndex >> 3) finds how many bytes the bitIndex describes
|
|
227
|
+
// bitIndex % 8 checks if we have extra bits beyond byte multiples
|
|
228
|
+
// if number of bytes is <= no. of bytes described by bitIndex and there
|
|
229
|
+
// are extra bits to consider, this means id has less bits than what
|
|
230
|
+
// bitIndex describes, id therefore is too short, and will be put in low
|
|
231
|
+
// bucket
|
|
232
|
+
const bytesDescribedByBitIndex = bitIndex >> 3;
|
|
233
|
+
const bitIndexWithinByte = bitIndex % 8;
|
|
234
|
+
if ((id.length <= bytesDescribedByBitIndex) && (bitIndexWithinByte !== 0)) {
|
|
235
|
+
return node.left;
|
|
236
|
+
}
|
|
237
|
+
const byteUnderConsideration = id[bytesDescribedByBitIndex];
|
|
238
|
+
// byteUnderConsideration is an integer from 0 to 255 represented by 8 bits
|
|
239
|
+
// where 255 is 11111111 and 0 is 00000000
|
|
240
|
+
// in order to find out whether the bit at bitIndexWithinByte is set
|
|
241
|
+
// we construct (1 << (7 - bitIndexWithinByte)) which will consist
|
|
242
|
+
// of all bits being 0, with only one bit set to 1
|
|
243
|
+
// for example, if bitIndexWithinByte is 3, we will construct 00010000 by
|
|
244
|
+
// (1 << (7 - 3)) -> (1 << 4) -> 16
|
|
245
|
+
if ((byteUnderConsideration & (1 << (7 - bitIndexWithinByte))) !== 0) {
|
|
246
|
+
return node.right;
|
|
247
|
+
}
|
|
248
|
+
return node.left;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Get a contact by its exact ID.
|
|
252
|
+
* If this is a leaf, loop through the bucket contents and return the correct
|
|
253
|
+
* contact if we have it or null if not. If this is an inner node, determine
|
|
254
|
+
* which branch of the tree to traverse and repeat.
|
|
255
|
+
*
|
|
256
|
+
* @param {Uint8Array} id - The ID of the contact to fetch.
|
|
257
|
+
* @returns {object | null} The contact if available, otherwise null
|
|
258
|
+
*/
|
|
259
|
+
get(id) {
|
|
260
|
+
ensureInt8('id', id);
|
|
261
|
+
let bitIndex = 0;
|
|
262
|
+
let node = this.root;
|
|
263
|
+
while (node.contacts === null) {
|
|
264
|
+
node = this._determineNode(node, id, bitIndex++);
|
|
265
|
+
}
|
|
266
|
+
// index of uses contact id for matching
|
|
267
|
+
const index = this._indexOf(node, id);
|
|
268
|
+
return index >= 0 ? node.contacts[index] : undefined;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Returns the index of the contact with provided
|
|
272
|
+
* id if it exists, returns -1 otherwise.
|
|
273
|
+
*
|
|
274
|
+
* @param {object} node - internal object that has 2 leafs: left and right
|
|
275
|
+
* @param {Uint8Array} id - Contact node id.
|
|
276
|
+
* @returns {number} Integer Index of contact with provided id if it exists, -1 otherwise.
|
|
277
|
+
*/
|
|
278
|
+
_indexOf(node, id) {
|
|
279
|
+
for (let i = 0; i < node.contacts.length; ++i) {
|
|
280
|
+
if (arrayEquals(node.contacts[i].id, id))
|
|
281
|
+
return i;
|
|
282
|
+
}
|
|
283
|
+
return -1;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Removes contact with the provided id.
|
|
287
|
+
*
|
|
288
|
+
* @param {Uint8Array} id - The ID of the contact to remove
|
|
289
|
+
* @returns {object} The k-bucket itself
|
|
290
|
+
*/
|
|
291
|
+
remove(id) {
|
|
292
|
+
ensureInt8('the id as parameter 1', id);
|
|
293
|
+
let bitIndex = 0;
|
|
294
|
+
let node = this.root;
|
|
295
|
+
while (node.contacts === null) {
|
|
296
|
+
node = this._determineNode(node, id, bitIndex++);
|
|
297
|
+
}
|
|
298
|
+
const index = this._indexOf(node, id);
|
|
299
|
+
if (index >= 0) {
|
|
300
|
+
const contact = node.contacts.splice(index, 1)[0];
|
|
301
|
+
this.safeDispatchEvent('removed', {
|
|
302
|
+
detail: contact
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
return this;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Splits the node, redistributes contacts to the new nodes, and marks the
|
|
309
|
+
* node that was split as an inner node of the binary tree of nodes by
|
|
310
|
+
* setting this.root.contacts = null
|
|
311
|
+
*
|
|
312
|
+
* @param {object} node - node for splitting
|
|
313
|
+
* @param {number} bitIndex - the bitIndex to which byte to check in the Uint8Array for navigating the binary tree
|
|
314
|
+
*/
|
|
315
|
+
_split(node, bitIndex) {
|
|
316
|
+
node.left = createNode();
|
|
317
|
+
node.right = createNode();
|
|
318
|
+
// redistribute existing contacts amongst the two newly created nodes
|
|
319
|
+
for (const contact of node.contacts) {
|
|
320
|
+
this._determineNode(node, contact.id, bitIndex).contacts.push(contact);
|
|
321
|
+
}
|
|
322
|
+
// @ts-expect-error loose types
|
|
323
|
+
node.contacts = null; // mark as inner tree node
|
|
324
|
+
// don't split the "far away" node
|
|
325
|
+
// we check where the local node would end up and mark the other one as
|
|
326
|
+
// "dontSplit" (i.e. "far away")
|
|
327
|
+
const detNode = this._determineNode(node, this.localNodeId, bitIndex);
|
|
328
|
+
const otherNode = node.left === detNode ? node.right : node.left;
|
|
329
|
+
otherNode.dontSplit = true;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Returns all the contacts contained in the tree as an array.
|
|
333
|
+
* If this is a leaf, return a copy of the bucket. If this is not a leaf,
|
|
334
|
+
* return the union of the low and high branches (themselves also as arrays).
|
|
335
|
+
*
|
|
336
|
+
* @returns {Array} All of the contacts in the tree, as an array
|
|
337
|
+
*/
|
|
338
|
+
toArray() {
|
|
339
|
+
let result = [];
|
|
340
|
+
for (const nodes = [this.root]; nodes.length > 0;) {
|
|
341
|
+
const node = nodes.pop();
|
|
342
|
+
if (node == null) {
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
if (node.contacts === null) {
|
|
346
|
+
nodes.push(node.right, node.left);
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
result = result.concat(node.contacts);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return result;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Similar to `toArray()` but instead of buffering everything up into an
|
|
356
|
+
* array before returning it, yields contacts as they are encountered while
|
|
357
|
+
* walking the tree.
|
|
358
|
+
*
|
|
359
|
+
* @returns {Iterable} All of the contacts in the tree, as an iterable
|
|
360
|
+
*/
|
|
361
|
+
*toIterable() {
|
|
362
|
+
for (const nodes = [this.root]; nodes.length > 0;) {
|
|
363
|
+
const node = nodes.pop();
|
|
364
|
+
if (node == null) {
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if (node.contacts === null) {
|
|
368
|
+
nodes.push(node.right, node.left);
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
yield* node.contacts;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Updates the contact selected by the arbiter.
|
|
377
|
+
* If the selection is our old contact and the candidate is some new contact
|
|
378
|
+
* then the new contact is abandoned (not added).
|
|
379
|
+
* If the selection is our old contact and the candidate is our old contact
|
|
380
|
+
* then we are refreshing the contact and it is marked as most recently
|
|
381
|
+
* contacted (by being moved to the right/end of the bucket array).
|
|
382
|
+
* If the selection is our new contact, the old contact is removed and the new
|
|
383
|
+
* contact is marked as most recently contacted.
|
|
384
|
+
*
|
|
385
|
+
* @param {object} node - internal object that has 2 leafs: left and right
|
|
386
|
+
* @param {number} index - the index in the bucket where contact exists (index has already been computed in a previous calculation)
|
|
387
|
+
* @param {object} contact - The contact object to update
|
|
388
|
+
*/
|
|
389
|
+
_update(node, index, contact) {
|
|
390
|
+
// sanity check
|
|
391
|
+
if (!arrayEquals(node.contacts[index].id, contact.id)) {
|
|
392
|
+
throw new Error('wrong index for _update');
|
|
393
|
+
}
|
|
394
|
+
const incumbent = node.contacts[index];
|
|
395
|
+
const selection = this.arbiter(incumbent, contact);
|
|
396
|
+
// if the selection is our old contact and the candidate is some new
|
|
397
|
+
// contact, then there is nothing to do
|
|
398
|
+
if (selection === incumbent && incumbent !== contact)
|
|
399
|
+
return;
|
|
400
|
+
node.contacts.splice(index, 1); // remove old contact
|
|
401
|
+
node.contacts.push(selection); // add more recent contact version
|
|
402
|
+
this.safeDispatchEvent('updated', {
|
|
403
|
+
detail: {
|
|
404
|
+
incumbent, selection
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
//# sourceMappingURL=k-bucket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"k-bucket.js","sourceRoot":"","sources":["../../../src/routing-table/k-bucket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;EA2BE;AAEF,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAGxD,SAAS,WAAW,CAAE,MAAkB,EAAE,MAAkB;IAC1D,IAAI,MAAM,KAAK,MAAM,EAAE;QACrB,OAAO,IAAI,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aG;IACH,OAAO,CAAE,IAAY,EAAE,KAAa,EAAE,OAAgB;QACpD,eAAe;QACf,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,CAAC,EAAE;YACrD,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;SAC3C;QAED,MAAM,SAAS,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;QACtC,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;QAClD,oEAAoE;QACpE,uCAAuC;QACvC,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,OAAO;YAAE,OAAM;QAE5D,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA,CAAC,qBAAqB;QACpD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA,CAAC,kCAAkC;QAChE,IAAI,CAAC,iBAAiB,CAAC,SAAS,EAAE;YAChC,MAAM,EAAE;gBACN,SAAS,EAAE,SAAS;aACrB;SACF,CAAC,CAAA;IACJ,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/kad-dht",
|
|
3
|
-
"version": "9.3.
|
|
3
|
+
"version": "9.3.4",
|
|
4
4
|
"description": "JavaScript implementation of the Kad-DHT for libp2p",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p-kad-dht#readme",
|
|
@@ -137,7 +137,7 @@
|
|
|
137
137
|
"test:chrome-webworker": "aegir test -t webworker",
|
|
138
138
|
"test:firefox": "aegir test -t browser -- --browser firefox",
|
|
139
139
|
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
|
|
140
|
-
"dep-check": "aegir dep-check -i protons",
|
|
140
|
+
"dep-check": "aegir dep-check -i protons -i events",
|
|
141
141
|
"release": "aegir release",
|
|
142
142
|
"docs": "aegir docs"
|
|
143
143
|
},
|
|
@@ -165,6 +165,7 @@
|
|
|
165
165
|
"abortable-iterator": "^5.0.1",
|
|
166
166
|
"any-signal": "^4.1.1",
|
|
167
167
|
"datastore-core": "^9.0.1",
|
|
168
|
+
"events": "^3.3.0",
|
|
168
169
|
"hashlru": "^2.3.0",
|
|
169
170
|
"interface-datastore": "^8.0.0",
|
|
170
171
|
"it-all": "^3.0.1",
|
|
@@ -178,7 +179,6 @@
|
|
|
178
179
|
"it-pipe": "^3.0.0",
|
|
179
180
|
"it-stream-types": "^2.0.1",
|
|
180
181
|
"it-take": "^3.0.1",
|
|
181
|
-
"k-bucket": "^5.1.0",
|
|
182
182
|
"multiformats": "^11.0.0",
|
|
183
183
|
"p-defer": "^4.0.0",
|
|
184
184
|
"p-queue": "^7.3.4",
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
3
|
-
// @ts-expect-error no types
|
|
4
|
-
import KBuck from 'k-bucket'
|
|
5
3
|
import Queue from 'p-queue'
|
|
6
4
|
import * as utils from '../utils.js'
|
|
5
|
+
import { KBucket, type PingEventDetails } from './k-bucket.js'
|
|
7
6
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
8
7
|
import type { Metric, Metrics } from '@libp2p/interface-metrics'
|
|
9
8
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
@@ -17,42 +16,6 @@ export const KBUCKET_SIZE = 20
|
|
|
17
16
|
export const PING_TIMEOUT = 10000
|
|
18
17
|
export const PING_CONCURRENCY = 10
|
|
19
18
|
|
|
20
|
-
export interface KBucketPeer {
|
|
21
|
-
id: Uint8Array
|
|
22
|
-
peer: PeerId
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface KBucket {
|
|
26
|
-
id: Uint8Array
|
|
27
|
-
contacts: KBucketPeer[]
|
|
28
|
-
dontSplit: boolean
|
|
29
|
-
left: KBucket
|
|
30
|
-
right: KBucket
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface KBucketTreeEvents {
|
|
34
|
-
'ping': (oldContacts: KBucketPeer[], newContact: KBucketPeer) => void
|
|
35
|
-
'added': (contact: KBucketPeer) => void
|
|
36
|
-
'removed': (contact: KBucketPeer) => void
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface KBucketTree {
|
|
40
|
-
root: KBucket
|
|
41
|
-
localNodeId: Uint8Array
|
|
42
|
-
|
|
43
|
-
on: <U extends keyof KBucketTreeEvents>(
|
|
44
|
-
event: U, listener: KBucketTreeEvents[U]
|
|
45
|
-
) => this
|
|
46
|
-
|
|
47
|
-
closest: (key: Uint8Array, count: number) => KBucketPeer[]
|
|
48
|
-
closestPeer: (key: Uint8Array) => KBucketPeer
|
|
49
|
-
remove: (key: Uint8Array) => void
|
|
50
|
-
add: (peer: KBucketPeer) => void
|
|
51
|
-
get: (key: Uint8Array) => Uint8Array
|
|
52
|
-
count: () => number
|
|
53
|
-
toIterable: () => Iterable<KBucketPeer>
|
|
54
|
-
}
|
|
55
|
-
|
|
56
19
|
export interface RoutingTableInit {
|
|
57
20
|
lan: boolean
|
|
58
21
|
protocol: string
|
|
@@ -76,7 +39,7 @@ export interface RoutingTableComponents {
|
|
|
76
39
|
*/
|
|
77
40
|
export class RoutingTable implements Startable {
|
|
78
41
|
public kBucketSize: number
|
|
79
|
-
public kb?:
|
|
42
|
+
public kb?: KBucket
|
|
80
43
|
public pingQueue: Queue
|
|
81
44
|
|
|
82
45
|
private readonly log: Logger
|
|
@@ -135,7 +98,7 @@ export class RoutingTable implements Startable {
|
|
|
135
98
|
}
|
|
136
99
|
}
|
|
137
100
|
|
|
138
|
-
const kBuck
|
|
101
|
+
const kBuck = new KBucket({
|
|
139
102
|
localNodeId: await utils.convertPeerId(this.components.peerId),
|
|
140
103
|
numberOfNodesPerKBucket: this.kBucketSize,
|
|
141
104
|
numberOfNodesToPing: 1
|
|
@@ -143,7 +106,7 @@ export class RoutingTable implements Startable {
|
|
|
143
106
|
this.kb = kBuck
|
|
144
107
|
|
|
145
108
|
// test whether to evict peers
|
|
146
|
-
kBuck.
|
|
109
|
+
kBuck.addEventListener('ping', this._onPing)
|
|
147
110
|
|
|
148
111
|
// tag kad-close peers
|
|
149
112
|
this._tagPeers(kBuck)
|
|
@@ -160,7 +123,7 @@ export class RoutingTable implements Startable {
|
|
|
160
123
|
* - this will lower the chances that connections to them get closed when
|
|
161
124
|
* we reach connection limits
|
|
162
125
|
*/
|
|
163
|
-
_tagPeers (kBuck:
|
|
126
|
+
_tagPeers (kBuck: KBucket): void {
|
|
164
127
|
let kClosest = new PeerSet()
|
|
165
128
|
|
|
166
129
|
const updatePeerTags = utils.debounce(() => {
|
|
@@ -197,10 +160,10 @@ export class RoutingTable implements Startable {
|
|
|
197
160
|
kClosest = newClosest
|
|
198
161
|
})
|
|
199
162
|
|
|
200
|
-
kBuck.
|
|
163
|
+
kBuck.addEventListener('added', () => {
|
|
201
164
|
updatePeerTags()
|
|
202
165
|
})
|
|
203
|
-
kBuck.
|
|
166
|
+
kBuck.addEventListener('removed', () => {
|
|
204
167
|
updatePeerTags()
|
|
205
168
|
})
|
|
206
169
|
}
|
|
@@ -215,7 +178,12 @@ export class RoutingTable implements Startable {
|
|
|
215
178
|
* `oldContacts` will not be empty and is the list of contacts that
|
|
216
179
|
* have not been contacted for the longest.
|
|
217
180
|
*/
|
|
218
|
-
_onPing (
|
|
181
|
+
_onPing (evt: CustomEvent<PingEventDetails>): void {
|
|
182
|
+
const {
|
|
183
|
+
oldContacts,
|
|
184
|
+
newContact
|
|
185
|
+
} = evt.detail
|
|
186
|
+
|
|
219
187
|
// add to a queue so multiple ping requests do not overlap and we don't
|
|
220
188
|
// flood the network with ping requests if lots of newContact requests
|
|
221
189
|
// are received
|