-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
431 lines (372 loc) · 15.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
index.js - Kademlia DHT K-bucket implementation as a binary tree.
The MIT License (MIT)
Copyright (c) 2013-2016 Tristan Slominski
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
"use strict";
var bufferEquals = require('buffer-equals');
var EventEmitter = require('events').EventEmitter;
var inherits = require('inherits');
var randomBytes = require('randombytes');
/*
* `options`:
* `arbiter`: _Function_ _(Default: vectorClock arbiter)_
`function (incumbent, candidate) { return contact; }` An optional
`arbiter` function that givent two `contact` objects with the same `id`
returns the desired object to be used for updating the k-bucket. For
more details, see [arbiter function](#arbiter-function).
* `localNodeId`: _Buffer_ An optional Buffer representing the local node id.
If not provided, a local node id will be created via
`crypto.randomBytes(20)`.
* `numberOfNodesPerKBucket`: _Integer_ _(Default: 20)_ The number of nodes
that a k-bucket can contain before being full or split.
* `numberOfNodesToPing`: _Integer_ _(Default: 3)_ The number of nodes to
ping when a bucket that should not be split becomes full. KBucket will
emit a `ping` event that contains `numberOfNodesToPing` nodes that have
not been contacted the longest.
* `root`: _Object_ _**CAUTION: reserved for internal use**_ Provides a
reference to the root of the tree data structure as the k-bucket splits
when new contacts are added.
*/
var KBucket = module.exports = function KBucket (options) {
var self = this;
EventEmitter.call(self);
options = options || {};
// use an arbiter from options or vectorClock arbiter by default
self.arbiter = options.arbiter || function arbiter(incumbent, candidate) {
if (incumbent.vectorClock > candidate.vectorClock) {
return incumbent;
}
return candidate;
};
// the bucket array has least-recently-contacted at the "front/left" side
// and the most-recently-contaced at the "back/right" side
self.bucket = [];
self.localNodeId = options.localNodeId || randomBytes(20);
if (!Buffer.isBuffer(self.localNodeId)) {
throw new TypeError("localNodeId is not a Buffer");
}
self.numberOfNodesPerKBucket = options.numberOfNodesPerKBucket || 20;
self.numberOfNodesToPing = options.numberOfNodesToPing || 3;
self.root = options.root || self;
// V8 hints
self.dontSplit = null;
self.low = null;
self.high = null;
};
inherits(KBucket, EventEmitter);
KBucket.distance = function distance (firstId, secondId) {
var distance = 0;
var min = Math.min(firstId.length, secondId.length);
var max = Math.max(firstId.length, secondId.length);
for (var i = 0; i < min; ++i) {
distance = distance * 256 + (firstId[i] ^ secondId[i]);
}
for (; i < max; ++i) {
distance = distance * 256 + 255;
}
return distance;
};
// contact: *required* the contact object to add
// bitIndex: the bitIndex to which bit to check in the Buffer for navigating
// the binary tree
KBucket.prototype._add = function (contact, bitIndex) {
var self = this;
// first check whether we are an inner node or a leaf (with bucket contents)
if (!self.bucket) {
// this is not a leaf node but an inner node with 'low' and 'high'
// branches; we will check the appropriate bit of the identifier and
// delegate to the appropriate node for further processing
if (self._determineBucket(contact.id, bitIndex++) < 0) {
return self.low._add(contact, bitIndex);
} else {
return self.high._add(contact, bitIndex);
}
}
// check if the contact already exists
var index = self._indexOf(contact.id);
if (index >= 0) {
self._update(contact, index);
return self;
}
if (self.bucket.length < self.numberOfNodesPerKBucket) {
self.bucket.push(contact);
self.emit('added', contact);
return self;
}
// the bucket is full
if (self.dontSplit) {
// we are not allowed to split the bucket
// we need to ping the first self.numberOfNodesToPing
// in order to determine if they are alive
// only if one of the pinged nodes does not respond, can the new contact
// be added (this prevents DoS flodding with new invalid contacts)
self.root.emit('ping', self.bucket.slice(0, self.numberOfNodesToPing), contact);
return self;
}
return self._splitAndAdd(contact, bitIndex);
};
// contact: *required* the contact object to add
KBucket.prototype.add = function add (contact) {
if (!Buffer.isBuffer(contact.id)) {
throw new TypeError("contact.id is not a Buffer");
}
return this._add(contact, 0);
};
// id: Buffer *required* node id
// n: Integer *required* maximum number of closest contacts to return
// bitIndex: Integer (Default: 0)
// Return: Array of maximum of `n` closest contacts to the node id
KBucket.prototype._closest = function (id, n, bitIndex) {
var self = this;
var contacts;
if (!self.bucket) {
if (self._determineBucket(id, bitIndex++) < 0) {
contacts = self.low._closest(id, n, bitIndex);
if (contacts.length < n) {
contacts = contacts.concat(self.high._closest(id, n, bitIndex));
}
} else {
contacts = self.high._closest(id, n, bitIndex);
if (contacts.length < n) {
contacts = contacts.concat(self.low._closest(id, n, bitIndex));
}
}
return contacts.slice(0, n);
}
contacts = self.bucket.slice();
contacts.forEach(function (storedContact) {
storedContact.distance = KBucket.distance(storedContact.id, id);
});
contacts.sort(function (a, b) {return a.distance - b.distance;});
return contacts.slice(0, n);
};
// id: Buffer *required* node id
// n: Integer *required* maximum number of closest contacts to return
// Return: Array of maximum of `n` closest contacts to the node id
KBucket.prototype.closest = function (id, n) {
if (!Buffer.isBuffer(id)) {
throw new TypeError("id is not a Buffer");
}
return this._closest(id, n, 0);
};
// Counts the number of contacts recursively.
// If this is a leaf, just return the number of contacts contained. Otherwise,
// return the length of the high and low branches combined.
KBucket.prototype.count = function count () {
var self = this;
if (self.bucket) {
return self.bucket.length;
} else {
return self.high.count() + self.low.count();
}
};
// Determines whether the id at the bitIndex is 0 or 1. If 0, returns -1, else 1
// id: a Buffer to compare localNodeId with
// bitIndex: the bitIndex to which bit to check in the id Buffer
KBucket.prototype._determineBucket = function (id, bitIndex) {
var self = this;
bitIndex = bitIndex || 0;
// **NOTE** remember that id is a Buffer and has granularity of
// bytes (8 bits), whereas the bitIndex is the _bit_ index (not byte)
// id's that are too short are put in low bucket (1 byte = 8 bits)
// parseInt(bitIndex / 8) finds how many bytes the bitIndex describes
// bitIndex % 8 checks if we have extra bits beyond byte multiples
// if number of bytes is <= no. of bytes described by bitIndex and there
// are extra bits to consider, this means id has less bits than what
// bitIndex describes, id therefore is too short, and will be put in low
// bucket
var bytesDescribedByBitIndex = parseInt(bitIndex / 8, 10);
var bitIndexWithinByte = bitIndex % 8;
if ((id.length <= bytesDescribedByBitIndex)
&& (bitIndexWithinByte != 0))
return -1;
var byteUnderConsideration = id[bytesDescribedByBitIndex];
// byteUnderConsideration is an integer from 0 to 255 represented by 8 bits
// where 255 is 11111111 and 0 is 00000000
// in order to find out whether the bit at bitIndexWithinByte is set
// we construct Math.pow(2, (7 - bitIndexWithinByte)) which will consist
// of all bits being 0, with only one bit set to 1
// for example, if bitIndexWithinByte is 3, we will construct 00010000 by
// Math.pow(2, (7 - 3)) -> Math.pow(2, 4) -> 16
if (byteUnderConsideration & Math.pow(2, (7 - bitIndexWithinByte))) {
return 1;
}
return -1;
};
// Get a contact by its exact ID.
// If this is a leaf, loop through the bucket contents and return the correct
// contact if we have it or null if not. If this is an inner node, determine
// which branch of the tree to traverse and repeat.
// id: Buffer *required* The ID of the contact to fetch.
// bitIndex: the bitIndex to which bit to check in the Buffer for navigating
// the binary tree
KBucket.prototype._get = function (id, bitIndex) {
var self = this;
if (!self.bucket) {
if (self._determineBucket(id, bitIndex++) < 0) {
return self.low._get(id, bitIndex);
} else {
return self.high._get(id, bitIndex);
}
}
var index = self._indexOf(id); // index of uses contact id for matching
if (index < 0) {
return null; // contact not found
}
return self.bucket[index];
};
// Get a contact by its exact ID.
// If this is a leaf, loop through the bucket contents and return the correct
// contact if we have it or null if not. If this is an inner node, determine
// which branch of the tree to traverse and repeat.
// id: Buffer *required* The ID of the contact to fetch.
KBucket.prototype.get = function get (id) {
if (!Buffer.isBuffer(id)) {
throw new TypeError("id is not a Buffer");
}
return this._get(id, 0);
};
// id: Buffer Contact node id.
// Returns the index of the contact with the given id if it exists
KBucket.prototype._indexOf = function indexOf (id) {
var self = this;
for (var i = 0; i < self.bucket.length; i++) {
if (bufferEquals(self.bucket[i].id, id)) return i;
}
return -1;
};
// id: Buffer *required* The ID of the contact to remove.
// bitIndex: the bitIndex to which bit to check in the Buffer for navigating
// the binary tree
KBucket.prototype._remove = function (id, bitIndex) {
var self = this;
// first check whether we are an inner node or a leaf (with bucket contents)
if (!self.bucket) {
// this is not a leaf node but an inner node with 'low' and 'high'
// branches; we will check the appropriate bit of the identifier and
// delegate to the appropriate node for further processing
if (self._determineBucket(id, bitIndex++) < 0) {
return self.low._remove(id, bitIndex);
} else {
return self.high._remove(id, bitIndex);
}
}
var index = self._indexOf(id);
if (index >= 0) {
var contact = self.bucket.splice(index, 1)[0];
self.emit('removed', contact);
}
return self;
};
// id: Buffer *required* he ID of the contact to remove.
KBucket.prototype.remove = function remove (id) {
if (!Buffer.isBuffer(id)) {
throw new TypeError("id is not a Buffer");
}
return this._remove(id, 0);
};
// Splits the bucket, redistributes contacts to the new buckets, and marks the
// bucket that was split as an inner node of the binary tree of buckets by
// setting self.bucket = undefined;
// contact: *required* the contact object to add
// bitIndex: the bitIndex to which byte to check in the Buffer for navigating the
// binary tree
KBucket.prototype._splitAndAdd = function (contact, bitIndex) {
var self = this;
self.low = new KBucket({
arbiter: self.arbiter,
localNodeId: self.localNodeId,
numberOfNodesPerKBucket: self.numberOfNodesPerKBucket,
numberOfNodesToPing: self.numberOfNodesToPing,
root: self.root
});
self.high = new KBucket({
arbiter: self.arbiter,
localNodeId: self.localNodeId,
numberOfNodesPerKBucket: self.numberOfNodesPerKBucket,
numberOfNodesToPing: self.numberOfNodesToPing,
root: self.root
});
bitIndex = bitIndex || 0;
// redistribute existing contacts amongst the two newly created buckets
self.bucket.forEach(function (storedContact) {
if (self._determineBucket(storedContact.id, bitIndex) < 0) {
self.low.add(storedContact);
} else {
self.high.add(storedContact);
}
});
self.bucket = undefined; // mark as inner tree node
// don't split the "far away" bucket
// we check where the local node would end up and mark the other one as
// "dontSplit" (i.e. "far away")
if (self._determineBucket(self.localNodeId, bitIndex) < 0) {
// local node belongs to "low" bucket, so mark the other one
self.high.dontSplit = true;
} else {
self.low.dontSplit = true;
}
// add the contact being added
self._add(contact, bitIndex);
return self;
};
// Returns all the contacts contained in the tree as an array.
// If self is a leaf, return a copy of the bucket. `slice` is used so that we
// don't accidentally leak an internal reference out that might be accidentally
// misused. If self is not a leaf, return the union of the low and high
// branches (themselves also as arrays).
KBucket.prototype.toArray = function toArray () {
var self = this;
if (self.bucket) {
return self.bucket.slice(0);
} else {
return self.low.toArray().concat(self.high.toArray());
}
};
// Updates the contact selected by the arbiter.
// If the selection is our old contact and the candidate is some new contact
// then the new contact is abandoned (not added).
// If the selection is our old contact and the candidate is our old contact
// then we are refreshing the contact and it is marked as most recently
// contacted (by being moved to the right/end of the bucket array).
// If the selection is our new contact, the old contact is removed and the new
// contact is marked as most recently contacted.
// contact: *required* the contact to update
// index: *required* the index in the bucket where contact exists
// (index has already been computed in a previous calculation)
KBucket.prototype._update = function (contact, index) {
var self = this;
// sanity check
if (!bufferEquals(self.bucket[index].id, contact.id)) {
throw new Error("indexOf() calculation resulted in wrong index")
}
var incumbent = self.bucket[index];
var selection = self.arbiter(incumbent, contact);
if (selection === incumbent && incumbent !== contact) {
// if the selection is our old contact and the candidate is some new
// contact, then there is nothing to do
return;
}
self.bucket.splice(index, 1); // remove old contact
self.bucket.push(selection); // add more recent contact version
self.emit('updated', incumbent, selection);
};