Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

k-bucket

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

k-bucket - npm Package Compare versions

Comparing version 1.0.3 to 2.0.0

30

index.js

@@ -35,4 +35,2 @@ /*

var randomBytes = require('randombytes');
var events = require('events');
var inherits = require('inherits');

@@ -53,7 +51,10 @@ /*

ping when a bucket that should not be split becomes full. KBucket will
emit a `ping` event that contains `numberOfNodesToPing` nodes that have
call the `ping` callback 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.
* `ping`: _Function_ _(Default: no-op)_
`function (oldContacts, newContact) {}` Callback called every time a
contact is added that would exceed the capacity of a don't split k-bucket it belongs to.
* `oldContacts`: _Array_ The array of contacts to ping.
* `newContact`: _Object_ The new contact to be added if one of old contacts
does not respond.
*/

@@ -63,3 +64,2 @@ var KBucket = module.exports = function KBucket (options) {

options = options || {};
events.EventEmitter.call(self);

@@ -83,3 +83,3 @@ // use an arbiter from options or vectorClock arbiter by default

self.numberOfNodesToPing = options.numberOfNodesToPing || 3;
self.root = options.root || self;
self.ping = options.ping || function() {};

@@ -92,4 +92,2 @@ // V8 hints

inherits(KBucket, events.EventEmitter);
KBucket.distance = function distance (firstId, secondId) {

@@ -148,5 +146,3 @@ var max = Math.max(firstId.length, secondId.length);

// be added (this prevents DoS flodding with new invalid contacts)
self.root.emit('ping',
self.bucket.slice(0, self.numberOfNodesToPing),
contact);
self.ping(self.bucket.slice(0, self.numberOfNodesToPing), contact);
return self;

@@ -322,5 +318,5 @@ }

localNodeId: self.localNodeId,
root: self.root,
numberOfNodesPerKBucket: self.numberOfNodesPerKBucket,
numberOfNodesToPing: self.numberOfNodesToPing
numberOfNodesToPing: self.numberOfNodesToPing,
ping: self.ping
});

@@ -330,5 +326,5 @@ self.high = new KBucket({

localNodeId: self.localNodeId,
root: self.root,
numberOfNodesPerKBucket: self.numberOfNodesPerKBucket,
numberOfNodesToPing: self.numberOfNodesToPing
numberOfNodesToPing: self.numberOfNodesToPing,
ping: self.ping
});

@@ -335,0 +331,0 @@

{
"name": "k-bucket",
"version": "1.0.3",
"version": "2.0.0",
"description": "Kademlia DHT K-bucket implementation as a binary tree",

@@ -14,3 +14,2 @@ "scripts": {

"buffer-equal": "0.0.1",
"inherits": "^2.0.1",
"randombytes": "^2.0.3"

@@ -17,0 +16,0 @@ },

@@ -102,3 +102,3 @@ # k-bucket

As more contacts are added to the "far" k-bucket and it reaches its capacity, it does not split. Instead, the k-bucket emits a "ping" event (register a listener: `kBucket.on('ping', function (oldContacts, newContact) {...});` and includes an array of old contact nodes that it hasn't heard from in a while and requires you to confirm that those contact nodes still respond (literally respond to a PING RPC). If an old contact node still responds, it should be re-added (`kBucket.add(oldContact)`) back to the k-bucket. This puts the old contact on the "recently heard from" end of the list of nodes in the k-bucket. If the old contact does not respond, it should be removed (`kBucket.remove(oldContact)`) and the new contact being added now has room to be stored (`kBucket.add(newContact)`).
As more contacts are added to the "far" k-bucket and it reaches its capacity, it does not split. Instead, the k-bucket calls the "ping" callback (`function (oldContacts, newContact) {...});` and includes an array of old contact nodes that it hasn't heard from in a while and requires you to confirm that those contact nodes still respond (literally respond to a PING RPC). If an old contact node still responds, it should be re-added (`kBucket.add(oldContact)`) back to the k-bucket. This puts the old contact on the "recently heard from" end of the list of nodes in the k-bucket. If the old contact does not respond, it should be removed (`kBucket.remove(oldContact)`) and the new contact being added now has room to be stored (`kBucket.add(newContact)`).

@@ -114,3 +114,2 @@ **Public API**

* [kBucket.toArray()](#kbuckettoarray)
* [Event 'ping'](#event-ping)

@@ -128,7 +127,11 @@ #### KBucket.distance(firstId, secondId)

* `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).
* `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.
* `numberOfNodesToPing`: _Integer_ _(Default: 3)_ The number of nodes to ping when a bucket that should not be split becomes full. KBucket will call the `ping` callback that contains `numberOfNodesToPing` nodes that have not been contacted the longest.
* `ping`: _Function_ _(Default: no-op)_
`function (oldContacts, newContact) {}` Callback called every time a contact is added that would exceed the capacity of a don't split k-bucket it belongs to.
* `oldContacts`: _Array_ The array of contacts to ping.
* `newContact`: _Object_ The new contact to be added if one of old contacts does not respond.

@@ -234,9 +237,2 @@ Creates a new KBucket.

#### Event: 'ping'
* `oldContacts`: _Array_ The array of contacts to ping.
* `newContact`: _Object_ The new contact to be added if one of old contacts does not respond.
Emitted every time a contact is added that would exceed the capacity of a _don't split_ k-bucket it belongs to.
## Sources

@@ -243,0 +239,0 @@

@@ -43,8 +43,7 @@ "use strict";

test['adding contact to bucket that can\'t be split results in emitting' +
' "ping" event'] = function (test) {
test['adding contact to bucket that can\'t be split results in calling' +
' "ping" callback'] = function (test) {
var i, iString, j;
test.expect(3 /*numberOfNodesToPing*/ + 2);
var kBucket = new KBucket({localNodeId: new Buffer('0000', 'hex')});
kBucket.on('ping', function (contacts, replacement) {
var ping = function (contacts, replacement) {
test.equal(contacts.length, kBucket.numberOfNodesToPing);

@@ -58,2 +57,6 @@ // console.dir(kBucket.high.bucket[0]);

test.done();
};
var kBucket = new KBucket({
localNodeId: new Buffer('0000', 'hex'),
ping: ping
});

@@ -68,2 +71,2 @@ for (var j = 0; j < kBucket.numberOfNodesPerKBucket + 1; j++) {

}
};
};
"use strict";
var bufferEqual = require('buffer-equal');
var events = require('events');

@@ -36,14 +35,7 @@ var KBucket = require('../index.js');

test['root is \'self\' if not provided'] = function (test) {
test['ping is \'Function\' if not provided'] = function (test) {
test.expect(1);
var kBucket = new KBucket();
test.strictEqual(kBucket.root, kBucket);
test.ok(kBucket.ping instanceof Function);
test.done();
};
test['inherits from EventEmitter'] = function (test) {
test.expect(1);
var kBucket = new KBucket();
test.ok(kBucket instanceof events.EventEmitter);
test.done();
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc