kademlia-routing-table 1.0.2 → 1.0.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.
Files changed (3) hide show
  1. package/index.js +15 -9
  2. package/package.json +12 -4
  3. package/test.js +20 -3
package/index.js CHANGED
@@ -8,6 +8,7 @@ module.exports = class RoutingTable extends EventEmitter {
8
8
 
9
9
  this.id = id
10
10
  this.k = opts.k || 20
11
+ this.size = 0
11
12
  this.rows = new Array(id.length * 8)
12
13
  }
13
14
 
@@ -21,14 +22,21 @@ module.exports = class RoutingTable extends EventEmitter {
21
22
  this.emit('row', row)
22
23
  }
23
24
 
24
- return row.add(node, this.k)
25
+ if (row.add(node, this.k)) {
26
+ this.size++
27
+ return true
28
+ }
29
+
30
+ return false
25
31
  }
26
32
 
27
33
  remove (id) {
28
34
  const i = this._diff(id)
29
35
  const row = this.rows[i]
30
36
  if (!row) return false
31
- return row.remove(id)
37
+ if (!row.remove(id)) return false
38
+ this.size--
39
+ return true
32
40
  }
33
41
 
34
42
  get (id) {
@@ -43,11 +51,13 @@ module.exports = class RoutingTable extends EventEmitter {
43
51
  }
44
52
 
45
53
  random () {
46
- const offset = (Math.random() * this.rows.length) | 0
54
+ let n = (Math.random() * this.size) | 0
47
55
 
48
56
  for (let i = 0; i < this.rows.length; i++) {
49
- const r = this.rows[(i + offset) % this.rows.length]
50
- if (r && r.nodes.length) return r.random()
57
+ const r = this.rows[i]
58
+ if (!r) continue
59
+ if (n < r.nodes.length) return r.nodes[n]
60
+ n -= r.nodes.length
51
61
  }
52
62
 
53
63
  return null
@@ -167,10 +177,6 @@ class Row extends EventEmitter {
167
177
  return null
168
178
  }
169
179
 
170
- random () {
171
- return this.nodes.length ? this.nodes[(Math.random() * this.nodes.length) | 0] : null
172
- }
173
-
174
180
  insert (i, node) {
175
181
  this.nodes.push(node) // push node or null or whatevs, just trying to not be polymorphic
176
182
  for (let j = this.nodes.length - 1; j > i; j--) this.nodes[j] = this.nodes[j - 1]
package/package.json CHANGED
@@ -1,12 +1,20 @@
1
1
  {
2
2
  "name": "kademlia-routing-table",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "XOR based routing table used for P2P networks such as a Kademlia DHT.",
5
5
  "main": "index.js",
6
- "dependencies": {},
6
+ "imports": {
7
+ "events": {
8
+ "bare": "bare-events",
9
+ "default": "events"
10
+ }
11
+ },
12
+ "dependencies": {
13
+ "bare-events": "^2.2.0"
14
+ },
7
15
  "devDependencies": {
8
- "standard": "^17.1.0",
9
- "brittle": "^3.3.2"
16
+ "brittle": "^3.3.2",
17
+ "standard": "^17.1.0"
10
18
  },
11
19
  "scripts": {
12
20
  "test": "standard && brittle test.js"
package/test.js CHANGED
@@ -2,12 +2,29 @@ const test = require('brittle')
2
2
  const { randomBytes } = require('crypto')
3
3
  const RoutingTable = require('./')
4
4
 
5
- test('basic', function (assert) {
5
+ test('basic', function (t) {
6
6
  const table = new RoutingTable(id())
7
7
  const node = { id: id() }
8
8
 
9
- assert.ok(table.add(node))
10
- assert.alike(table.closest(id()), [node])
9
+ t.ok(table.add(node))
10
+ t.alike(table.closest(id()), [node])
11
+ })
12
+
13
+ test('random', function (t) {
14
+ const table = new RoutingTable(id())
15
+
16
+ for (let i = 0; i < 1000; i++) {
17
+ table.add({ id: id() })
18
+ }
19
+
20
+ t.is(table.size, table.toArray().length)
21
+
22
+ for (let i = 0; i < 1000; i++) {
23
+ if (!table.random()) {
24
+ t.fail('should always be a node in there')
25
+ break
26
+ }
27
+ }
11
28
  })
12
29
 
13
30
  function id () {