Comparing version 0.4.0 to 0.4.1
34
index.js
@@ -34,3 +34,3 @@ /* | ||
var assert = require('assert'), | ||
bufferEqual = require('./lib/bufferEqual.js'), | ||
bufferEqual = require('buffer-equal'), | ||
constants = require('./lib/constants.js'), | ||
@@ -86,3 +86,3 @@ crypto = require('crypto'), | ||
// contact: *required* the contact object to add | ||
// bitIndex: the bitIndex to which bit to check in the Buffer for navigating | ||
// bitIndex: the bitIndex to which bit to check in the Buffer for navigating | ||
// the binary tree | ||
@@ -125,3 +125,3 @@ KBucket.prototype.add = function add (contact, bitIndex) { | ||
// be added (this prevents DoS flodding with new invalid contacts) | ||
self.root.emit('ping', | ||
self.root.emit('ping', | ||
self.bucket.slice(0, constants.DEFAULT_NUMBER_OF_NODES_TO_PING), | ||
@@ -131,3 +131,3 @@ contact); | ||
} | ||
return self.splitAndAdd(contact, bitIndex); | ||
@@ -139,3 +139,3 @@ }; | ||
// n: Integer *required* maximum number of closest contacts to return | ||
// bitIndex: Integer (Default: 0) | ||
// bitIndex: Integer (Default: 0) | ||
// Return: Array of maximum of `n` closest contacts to the `contact` | ||
@@ -149,3 +149,3 @@ KBucket.prototype.closest = function closest (contact, n, bitIndex) { | ||
bitIndex = bitIndex || 0; | ||
if (self.determineBucket(contact.id, bitIndex++) < 0) { | ||
@@ -196,3 +196,3 @@ contacts = self.low.closest(contact, n, bitIndex); | ||
// **NOTE** remember that id is a Buffer and has granularity of | ||
// **NOTE** remember that id is a Buffer and has granularity of | ||
// bytes (8 bits), whereas the bitIndex is the _bit_ index (not byte) | ||
@@ -204,4 +204,4 @@ | ||
// 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 | ||
// 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 | ||
@@ -212,10 +212,10 @@ var bytesDescribedByBitIndex = parseInt(bitIndex / 8, 10); | ||
&& (bitIndexWithinByte != 0)) | ||
return -1; | ||
return -1; | ||
var byteUnderConsideration = id[bytesDescribedByBitIndex]; | ||
// byteUnderConsideration is an integer from 0 to 255 represented by 8 bits | ||
// 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 | ||
// 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 | ||
@@ -270,3 +270,3 @@ // for example, if bitIndexWithinByte is 3, we will construct 00010000 by | ||
// contact: *required* the contact object to remove | ||
// bitIndex: the bitIndex to which bit to check in the Buffer for navigating | ||
// bitIndex: the bitIndex to which bit to check in the Buffer for navigating | ||
// the binary tree | ||
@@ -328,3 +328,3 @@ KBucket.prototype.remove = function remove (contact, bitIndex) { | ||
} | ||
// add the contact being added | ||
@@ -343,3 +343,3 @@ self.add(contact, bitIndex); | ||
var self = this; | ||
if (self.bucket) { | ||
@@ -366,3 +366,3 @@ return self.bucket.slice(0); | ||
// sanity check | ||
assert.ok(bufferEqual(self.bucket[index].id, contact.id), | ||
assert.ok(bufferEqual(self.bucket[index].id, contact.id), | ||
"indexOf() calculation resulted in wrong index"); | ||
@@ -369,0 +369,0 @@ |
{ | ||
"name": "k-bucket", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Kademlia DHT K-bucket implementation as a binary tree", | ||
@@ -10,4 +10,7 @@ "scripts": { | ||
"devDependencies": { | ||
"nodeunit": "0.8.x" | ||
"nodeunit": "0.9.x" | ||
}, | ||
"dependencies": { | ||
"buffer-equal": "0.0.1" | ||
}, | ||
"repository": { | ||
@@ -25,5 +28,6 @@ "type": "git", | ||
"Mike de Boer <info@mikedeboer.nl>", | ||
"Conrad Pankoff <deoxxa@fknsrs.biz>" | ||
"Conrad Pankoff <deoxxa@fknsrs.biz>", | ||
"Feross Aboukhadijeh <feross@feross.org>" | ||
], | ||
"license": "MIT" | ||
} |
@@ -11,3 +11,3 @@ # k-bucket | ||
[@tristanls](https://github.com/tristanls), [@mikedeboer](https://github.com/mikedeboer), [@deoxxa](https://github.com/deoxxa) | ||
[@tristanls](https://github.com/tristanls), [@mikedeboer](https://github.com/mikedeboer), [@deoxxa](https://github.com/deoxxa), [@feross](https://github.com/feross) | ||
@@ -34,3 +34,3 @@ ## Installation | ||
A [*Distributed Hash Table (DHT)*](http://en.wikipedia.org/wiki/Distributed_hash_table) is a decentralized distributed system that provides a lookup table similar to a hash table. | ||
A [*Distributed Hash Table (DHT)*](http://en.wikipedia.org/wiki/Distributed_hash_table) is a decentralized distributed system that provides a lookup table similar to a hash table. | ||
@@ -98,4 +98,6 @@ *k-bucket* is an implementation of a storage mechanism for keys within a DHT. It stores `contact` objects which represent locations and addresses of nodes in the decentralized distributed system. `contact` objects are typically identified by a SHA-1 hash, however this restriction is lifted in this implementation. Additionally, node ids of different lengths can be compared. | ||
KBucket starts off as a single k-bucket with capacity of _k_. As contacts are added, once the _k+1_ contact is added, the k-bucket is split into two k-buckets. The split happens according to the first bit of the contact node id. The k-bucket that would contain the local node id is the "near" k-bucket, and the other one is the "far" k-bucket. The "far" k-bucket is marked as _don't split_ in order to prevent further splitting. The contact nodes that existed are then redistributed along the two new k-buckets and the old k-bucket becomes an inner node within a tree data structure. | ||
For a step by step example of k-bucket operation you may find the following slideshow useful: [Distribute All The Things](https://docs.google.com/presentation/d/11qGZlPWu6vEAhA7p3qsQaQtWH7KofEC9dMeBFZ1gYeA/edit#slide=id.g1718cc2bc_0661). | ||
KBucket starts off as a single k-bucket with capacity of _k_. As contacts are added, once the _k+1_ contact is added, the k-bucket is split into two k-buckets. The split happens according to the first bit of the contact node id. The k-bucket that would contain the local node id is the "near" k-bucket, and the other one is the "far" k-bucket. The "far" k-bucket is marked as _don't split_ in order to prevent further splitting. The contact nodes that existed are then redistributed along the two new k-buckets and the old k-bucket becomes an inner node within a tree data structure. | ||
As even more contacts are added to the "near" k-bucket, the "near" k-bucket will split again as it becomes full. However, this time it is split along the second bit of the contact node id. Again, the two newly created k-buckets are marked "near" and "far" and the "far" k-bucket is marked as _don't split_. Again, the contact nodes that existed in the old bucket are redistributed. This continues as long as nodes are being added to the "near" k-bucket, until the number of splits reaches the length of the local node id. | ||
@@ -242,2 +244,2 @@ | ||
- [A formal specification of the Kademlia distributed hash table](http://maude.sip.ucm.es/kademlia/files/pita_kademlia.pdf) | ||
- [Distributed Hash Tables (part 2)](http://offthelip.org/?p=157) | ||
- [Distributed Hash Tables (part 2)](http://offthelip.org/?p=157) |
"use strict"; | ||
var bufferEqual = require('../lib/bufferEqual.js'), | ||
var bufferEqual = require('buffer-equal'), | ||
constants = require('../lib/constants.js'), | ||
@@ -35,3 +35,3 @@ KBucket = require('../index.js'); | ||
test.strictEqual(kBucket.get(new Buffer("a")).bar, undefined); | ||
test.done(); | ||
test.done(); | ||
}; | ||
@@ -50,3 +50,3 @@ | ||
kBucket.add({id: new Buffer(iString, 'hex')}); | ||
} | ||
} | ||
// cause a split to happen | ||
@@ -53,0 +53,0 @@ kBucket.add({id: new Buffer('00' + iString, 'hex'), find: 'me'}); |
@@ -87,3 +87,3 @@ "use strict"; | ||
} | ||
// above algorithm will split low bucket 4 times and put 0x00 through 0x0f | ||
// above algorithm will split low bucket 4 times and put 0x00 through 0x0f | ||
// in the low bucket, and put 0x10 through 0x14 in high bucket | ||
@@ -90,0 +90,0 @@ // since localNodeId is 0x00, we expect every high bucket to be "far" and |
@@ -28,5 +28,5 @@ "use strict"; | ||
kBucket.add({id: new Buffer(iString, 'hex')}); | ||
} | ||
} | ||
// cause a split to happen | ||
kBucket.add({id: new Buffer('00' + iString, 'hex')}); | ||
kBucket.add({id: new Buffer('00' + iString, 'hex')}); | ||
// console.log(require('util').inspect(kBucket, {depth: null})); | ||
@@ -33,0 +33,0 @@ var contacts = kBucket.toArray(); |
"use strict"; | ||
var KBucket = require('../index.js'), | ||
bufferEqual = require('../lib/bufferEqual.js'); | ||
bufferEqual = require('buffer-equal'); | ||
@@ -6,0 +6,0 @@ var test = module.exports = {}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
242
52839
1
19
903
+ Addedbuffer-equal@0.0.1
+ Addedbuffer-equal@0.0.1(transitive)