@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.
@@ -0,0 +1,523 @@
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
+
30
+ import { EventEmitter } from '@libp2p/interfaces/events'
31
+ import type { PeerId } from '@libp2p/interface-peer-id'
32
+
33
+ function arrayEquals (array1: Uint8Array, array2: Uint8Array): boolean {
34
+ if (array1 === array2) {
35
+ return true
36
+ }
37
+ if (array1.length !== array2.length) {
38
+ return false
39
+ }
40
+ for (let i = 0, length = array1.length; i < length; ++i) {
41
+ if (array1[i] !== array2[i]) {
42
+ return false
43
+ }
44
+ }
45
+ return true
46
+ }
47
+
48
+ function createNode (): Bucket {
49
+ // @ts-expect-error loose types
50
+ return { contacts: [], dontSplit: false, left: null, right: null }
51
+ }
52
+
53
+ function ensureInt8 (name: string, val?: Uint8Array): void {
54
+ if (!(val instanceof Uint8Array)) {
55
+ throw new TypeError(name + ' is not a Uint8Array')
56
+ }
57
+ }
58
+
59
+ export interface PingEventDetails {
60
+ oldContacts: Contact[]
61
+ newContact: Contact
62
+ }
63
+
64
+ export interface UpdatedEventDetails {
65
+ incumbent: Contact
66
+ selection: Contact
67
+ }
68
+
69
+ export interface KBucketEvents {
70
+ 'ping': CustomEvent<PingEventDetails>
71
+ 'added': CustomEvent<Contact>
72
+ 'removed': CustomEvent<Contact>
73
+ 'updated': CustomEvent<UpdatedEventDetails>
74
+ }
75
+
76
+ export interface KBucketOptions {
77
+ /**
78
+ * A Uint8Array representing the local node id
79
+ */
80
+ localNodeId: Uint8Array
81
+
82
+ /**
83
+ * The number of nodes that a k-bucket can contain before being full or split.
84
+ */
85
+ numberOfNodesPerKBucket?: number
86
+
87
+ /**
88
+ * The number of nodes to ping when a bucket that should not be split becomes
89
+ * full. KBucket will emit a `ping` event that contains `numberOfNodesToPing`
90
+ * nodes that have not been contacted the longest.
91
+ */
92
+ numberOfNodesToPing?: number
93
+
94
+ /**
95
+ * An optional `distance` function that gets two `id` Uint8Arrays and return
96
+ * distance (as number) between them.
97
+ */
98
+ distance?: (a: Uint8Array, b: Uint8Array) => number
99
+
100
+ /**
101
+ * An optional `arbiter` function that given two `contact` objects with the
102
+ * same `id` returns the desired object to be used for updating the k-bucket.
103
+ * For more details, see [arbiter function](#arbiter-function).
104
+ */
105
+ arbiter?: (incumbent: Contact, candidate: Contact) => Contact
106
+ }
107
+
108
+ export interface Contact {
109
+ id: Uint8Array
110
+ peer: PeerId
111
+ vectorClock?: number
112
+ }
113
+
114
+ export interface Bucket {
115
+ id: Uint8Array
116
+ contacts: Contact[]
117
+ dontSplit: boolean
118
+ left: Bucket
119
+ right: Bucket
120
+ }
121
+
122
+ /**
123
+ * Implementation of a Kademlia DHT k-bucket used for storing
124
+ * contact (peer node) information.
125
+ *
126
+ * @extends EventEmitter
127
+ */
128
+ export class KBucket extends EventEmitter<KBucketEvents> {
129
+ public localNodeId: Uint8Array
130
+ public root: Bucket
131
+ private readonly numberOfNodesPerKBucket: number
132
+ private readonly numberOfNodesToPing: number
133
+ private readonly distance: (a: Uint8Array, b: Uint8Array) => number
134
+ private readonly arbiter: (incumbent: Contact, candidate: Contact) => Contact
135
+
136
+ constructor (options: KBucketOptions) {
137
+ super()
138
+
139
+ this.localNodeId = options.localNodeId
140
+ this.numberOfNodesPerKBucket = options.numberOfNodesPerKBucket ?? 20
141
+ this.numberOfNodesToPing = options.numberOfNodesToPing ?? 3
142
+ this.distance = options.distance ?? KBucket.distance
143
+ // use an arbiter from options or vectorClock arbiter by default
144
+ this.arbiter = options.arbiter ?? KBucket.arbiter
145
+
146
+ ensureInt8('option.localNodeId as parameter 1', this.localNodeId)
147
+
148
+ this.root = createNode()
149
+ }
150
+
151
+ /**
152
+ * Default arbiter function for contacts with the same id. Uses
153
+ * contact.vectorClock to select which contact to update the k-bucket with.
154
+ * Contact with larger vectorClock field will be selected. If vectorClock is
155
+ * the same, candidate will be selected.
156
+ *
157
+ * @param {object} incumbent - Contact currently stored in the k-bucket.
158
+ * @param {object} candidate - Contact being added to the k-bucket.
159
+ * @returns {object} Contact to updated the k-bucket with.
160
+ */
161
+ static arbiter (incumbent: Contact, candidate: Contact): Contact {
162
+ return (incumbent.vectorClock ?? 0) > (candidate.vectorClock ?? 0) ? incumbent : candidate
163
+ }
164
+
165
+ /**
166
+ * Default distance function. Finds the XOR
167
+ * distance between firstId and secondId.
168
+ *
169
+ * @param {Uint8Array} firstId - Uint8Array containing first id.
170
+ * @param {Uint8Array} secondId - Uint8Array containing second id.
171
+ * @returns {number} Integer The XOR distance between firstId and secondId.
172
+ */
173
+ static distance (firstId: Uint8Array, secondId: Uint8Array): number {
174
+ let distance = 0
175
+ let i = 0
176
+ const min = Math.min(firstId.length, secondId.length)
177
+ const max = Math.max(firstId.length, secondId.length)
178
+ for (; i < min; ++i) {
179
+ distance = distance * 256 + (firstId[i] ^ secondId[i])
180
+ }
181
+ for (; i < max; ++i) distance = distance * 256 + 255
182
+ return distance
183
+ }
184
+
185
+ /**
186
+ * Adds a contact to the k-bucket.
187
+ *
188
+ * @param {object} contact - the contact object to add
189
+ */
190
+ add (contact: Contact): KBucket {
191
+ ensureInt8('contact.id', contact?.id)
192
+
193
+ let bitIndex = 0
194
+ let node = this.root
195
+
196
+ while (node.contacts === null) {
197
+ // this is not a leaf node but an inner node with 'low' and 'high'
198
+ // branches; we will check the appropriate bit of the identifier and
199
+ // delegate to the appropriate node for further processing
200
+ node = this._determineNode(node, contact.id, bitIndex++)
201
+ }
202
+
203
+ // check if the contact already exists
204
+ const index = this._indexOf(node, contact.id)
205
+ if (index >= 0) {
206
+ this._update(node, index, contact)
207
+ return this
208
+ }
209
+
210
+ if (node.contacts.length < this.numberOfNodesPerKBucket) {
211
+ node.contacts.push(contact)
212
+ this.safeDispatchEvent('added', { detail: contact })
213
+ return this
214
+ }
215
+
216
+ // the bucket is full
217
+ if (node.dontSplit) {
218
+ // we are not allowed to split the bucket
219
+ // we need to ping the first this.numberOfNodesToPing
220
+ // in order to determine if they are alive
221
+ // only if one of the pinged nodes does not respond, can the new contact
222
+ // be added (this prevents DoS flodding with new invalid contacts)
223
+ this.safeDispatchEvent('ping', {
224
+ detail: {
225
+ oldContacts: node.contacts.slice(0, this.numberOfNodesToPing),
226
+ newContact: contact
227
+ }
228
+ })
229
+ return this
230
+ }
231
+
232
+ this._split(node, bitIndex)
233
+ return this.add(contact)
234
+ }
235
+
236
+ /**
237
+ * Get the n closest contacts to the provided node id. "Closest" here means:
238
+ * closest according to the XOR metric of the contact node id.
239
+ *
240
+ * @param {Uint8Array} id - Contact node id
241
+ * @param {number} n - Integer (Default: Infinity) The maximum number of closest contacts to return
242
+ * @returns {Array} Array Maximum of n closest contacts to the node id
243
+ */
244
+ closest (id: Uint8Array, n = Infinity): Contact[] {
245
+ ensureInt8('id', id)
246
+
247
+ if ((!Number.isInteger(n) && n !== Infinity) || n <= 0) {
248
+ throw new TypeError('n is not positive number')
249
+ }
250
+
251
+ let contacts: Contact[] = []
252
+
253
+ for (let nodes = [this.root], bitIndex = 0; nodes.length > 0 && contacts.length < n;) {
254
+ const node = nodes.pop()
255
+
256
+ if (node == null) {
257
+ continue
258
+ }
259
+
260
+ if (node.contacts === null) {
261
+ const detNode = this._determineNode(node, id, bitIndex++)
262
+ nodes.push(node.left === detNode ? node.right : node.left)
263
+ nodes.push(detNode)
264
+ } else {
265
+ contacts = contacts.concat(node.contacts)
266
+ }
267
+ }
268
+
269
+ return contacts
270
+ .map(a => ({
271
+ distance: this.distance(a.id, id),
272
+ contact: a
273
+ }))
274
+ .sort((a, b) => a.distance - b.distance)
275
+ .slice(0, n)
276
+ .map(a => a.contact)
277
+ }
278
+
279
+ /**
280
+ * Counts the total number of contacts in the tree.
281
+ *
282
+ * @returns {number} The number of contacts held in the tree
283
+ */
284
+ count (): number {
285
+ // return this.toArray().length
286
+ let count = 0
287
+ for (const nodes = [this.root]; nodes.length > 0;) {
288
+ const node = nodes.pop()
289
+
290
+ if (node == null) {
291
+ continue
292
+ }
293
+
294
+ if (node.contacts === null) {
295
+ nodes.push(node.right, node.left)
296
+ } else {
297
+ count += node.contacts.length
298
+ }
299
+ }
300
+
301
+ return count
302
+ }
303
+
304
+ /**
305
+ * Determines whether the id at the bitIndex is 0 or 1.
306
+ * Return left leaf if `id` at `bitIndex` is 0, right leaf otherwise
307
+ *
308
+ * @param {object} node - internal object that has 2 leafs: left and right
309
+ * @param {Uint8Array} id - Id to compare localNodeId with.
310
+ * @param {number} bitIndex - Integer (Default: 0) The bit index to which bit to check in the id Uint8Array.
311
+ * @returns {object} left leaf if id at bitIndex is 0, right leaf otherwise.
312
+ */
313
+ _determineNode (node: any, id: Uint8Array, bitIndex: number): Bucket {
314
+ // **NOTE** remember that id is a Uint8Array and has granularity of
315
+ // bytes (8 bits), whereas the bitIndex is the _bit_ index (not byte)
316
+
317
+ // id's that are too short are put in low bucket (1 byte = 8 bits)
318
+ // (bitIndex >> 3) finds how many bytes the bitIndex describes
319
+ // bitIndex % 8 checks if we have extra bits beyond byte multiples
320
+ // if number of bytes is <= no. of bytes described by bitIndex and there
321
+ // are extra bits to consider, this means id has less bits than what
322
+ // bitIndex describes, id therefore is too short, and will be put in low
323
+ // bucket
324
+ const bytesDescribedByBitIndex = bitIndex >> 3
325
+ const bitIndexWithinByte = bitIndex % 8
326
+ if ((id.length <= bytesDescribedByBitIndex) && (bitIndexWithinByte !== 0)) {
327
+ return node.left
328
+ }
329
+
330
+ const byteUnderConsideration = id[bytesDescribedByBitIndex]
331
+
332
+ // byteUnderConsideration is an integer from 0 to 255 represented by 8 bits
333
+ // where 255 is 11111111 and 0 is 00000000
334
+ // in order to find out whether the bit at bitIndexWithinByte is set
335
+ // we construct (1 << (7 - bitIndexWithinByte)) which will consist
336
+ // of all bits being 0, with only one bit set to 1
337
+ // for example, if bitIndexWithinByte is 3, we will construct 00010000 by
338
+ // (1 << (7 - 3)) -> (1 << 4) -> 16
339
+ if ((byteUnderConsideration & (1 << (7 - bitIndexWithinByte))) !== 0) {
340
+ return node.right
341
+ }
342
+
343
+ return node.left
344
+ }
345
+
346
+ /**
347
+ * Get a contact by its exact ID.
348
+ * If this is a leaf, loop through the bucket contents and return the correct
349
+ * contact if we have it or null if not. If this is an inner node, determine
350
+ * which branch of the tree to traverse and repeat.
351
+ *
352
+ * @param {Uint8Array} id - The ID of the contact to fetch.
353
+ * @returns {object | null} The contact if available, otherwise null
354
+ */
355
+ get (id: Uint8Array): Contact | undefined {
356
+ ensureInt8('id', id)
357
+
358
+ let bitIndex = 0
359
+
360
+ let node: Bucket = this.root
361
+ while (node.contacts === null) {
362
+ node = this._determineNode(node, id, bitIndex++)
363
+ }
364
+
365
+ // index of uses contact id for matching
366
+ const index = this._indexOf(node, id)
367
+ return index >= 0 ? node.contacts[index] : undefined
368
+ }
369
+
370
+ /**
371
+ * Returns the index of the contact with provided
372
+ * id if it exists, returns -1 otherwise.
373
+ *
374
+ * @param {object} node - internal object that has 2 leafs: left and right
375
+ * @param {Uint8Array} id - Contact node id.
376
+ * @returns {number} Integer Index of contact with provided id if it exists, -1 otherwise.
377
+ */
378
+ _indexOf (node: Bucket, id: Uint8Array): number {
379
+ for (let i = 0; i < node.contacts.length; ++i) {
380
+ if (arrayEquals(node.contacts[i].id, id)) return i
381
+ }
382
+
383
+ return -1
384
+ }
385
+
386
+ /**
387
+ * Removes contact with the provided id.
388
+ *
389
+ * @param {Uint8Array} id - The ID of the contact to remove
390
+ * @returns {object} The k-bucket itself
391
+ */
392
+ remove (id: Uint8Array): KBucket {
393
+ ensureInt8('the id as parameter 1', id)
394
+
395
+ let bitIndex = 0
396
+ let node = this.root
397
+
398
+ while (node.contacts === null) {
399
+ node = this._determineNode(node, id, bitIndex++)
400
+ }
401
+
402
+ const index = this._indexOf(node, id)
403
+ if (index >= 0) {
404
+ const contact = node.contacts.splice(index, 1)[0]
405
+ this.safeDispatchEvent('removed', {
406
+ detail: contact
407
+ })
408
+ }
409
+
410
+ return this
411
+ }
412
+
413
+ /**
414
+ * Splits the node, redistributes contacts to the new nodes, and marks the
415
+ * node that was split as an inner node of the binary tree of nodes by
416
+ * setting this.root.contacts = null
417
+ *
418
+ * @param {object} node - node for splitting
419
+ * @param {number} bitIndex - the bitIndex to which byte to check in the Uint8Array for navigating the binary tree
420
+ */
421
+ _split (node: Bucket, bitIndex: number): void {
422
+ node.left = createNode()
423
+ node.right = createNode()
424
+
425
+ // redistribute existing contacts amongst the two newly created nodes
426
+ for (const contact of node.contacts) {
427
+ this._determineNode(node, contact.id, bitIndex).contacts.push(contact)
428
+ }
429
+
430
+ // @ts-expect-error loose types
431
+ node.contacts = null // mark as inner tree node
432
+
433
+ // don't split the "far away" node
434
+ // we check where the local node would end up and mark the other one as
435
+ // "dontSplit" (i.e. "far away")
436
+ const detNode = this._determineNode(node, this.localNodeId, bitIndex)
437
+ const otherNode = node.left === detNode ? node.right : node.left
438
+ otherNode.dontSplit = true
439
+ }
440
+
441
+ /**
442
+ * Returns all the contacts contained in the tree as an array.
443
+ * If this is a leaf, return a copy of the bucket. If this is not a leaf,
444
+ * return the union of the low and high branches (themselves also as arrays).
445
+ *
446
+ * @returns {Array} All of the contacts in the tree, as an array
447
+ */
448
+ toArray (): Contact[] {
449
+ let result: Contact[] = []
450
+ for (const nodes = [this.root]; nodes.length > 0;) {
451
+ const node = nodes.pop()
452
+
453
+ if (node == null) {
454
+ continue
455
+ }
456
+
457
+ if (node.contacts === null) {
458
+ nodes.push(node.right, node.left)
459
+ } else {
460
+ result = result.concat(node.contacts)
461
+ }
462
+ }
463
+ return result
464
+ }
465
+
466
+ /**
467
+ * Similar to `toArray()` but instead of buffering everything up into an
468
+ * array before returning it, yields contacts as they are encountered while
469
+ * walking the tree.
470
+ *
471
+ * @returns {Iterable} All of the contacts in the tree, as an iterable
472
+ */
473
+ * toIterable (): Iterable<Contact> {
474
+ for (const nodes = [this.root]; nodes.length > 0;) {
475
+ const node = nodes.pop()
476
+
477
+ if (node == null) {
478
+ continue
479
+ }
480
+
481
+ if (node.contacts === null) {
482
+ nodes.push(node.right, node.left)
483
+ } else {
484
+ yield * node.contacts
485
+ }
486
+ }
487
+ }
488
+
489
+ /**
490
+ * Updates the contact selected by the arbiter.
491
+ * If the selection is our old contact and the candidate is some new contact
492
+ * then the new contact is abandoned (not added).
493
+ * If the selection is our old contact and the candidate is our old contact
494
+ * then we are refreshing the contact and it is marked as most recently
495
+ * contacted (by being moved to the right/end of the bucket array).
496
+ * If the selection is our new contact, the old contact is removed and the new
497
+ * contact is marked as most recently contacted.
498
+ *
499
+ * @param {object} node - internal object that has 2 leafs: left and right
500
+ * @param {number} index - the index in the bucket where contact exists (index has already been computed in a previous calculation)
501
+ * @param {object} contact - The contact object to update
502
+ */
503
+ _update (node: Bucket, index: number, contact: Contact): void {
504
+ // sanity check
505
+ if (!arrayEquals(node.contacts[index].id, contact.id)) {
506
+ throw new Error('wrong index for _update')
507
+ }
508
+
509
+ const incumbent = node.contacts[index]
510
+ const selection = this.arbiter(incumbent, contact)
511
+ // if the selection is our old contact and the candidate is some new
512
+ // contact, then there is nothing to do
513
+ if (selection === incumbent && incumbent !== contact) return
514
+
515
+ node.contacts.splice(index, 1) // remove old contact
516
+ node.contacts.push(selection) // add more recent contact version
517
+ this.safeDispatchEvent('updated', {
518
+ detail: {
519
+ incumbent, selection
520
+ }
521
+ })
522
+ }
523
+ }