kademlia-routing-table 1.0.3 → 1.0.5

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 +14 -9
  2. package/package.json +1 -1
  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,20 @@ module.exports = class RoutingTable extends EventEmitter {
21
22
  this.emit('row', row)
22
23
  }
23
24
 
24
- return row.add(node, this.k)
25
+ const len = row.nodes.length
26
+ if (!row.add(node, this.k)) return false
27
+
28
+ this.size += row.nodes.length - len
29
+ return true
25
30
  }
26
31
 
27
32
  remove (id) {
28
33
  const i = this._diff(id)
29
34
  const row = this.rows[i]
30
35
  if (!row) return false
31
- return row.remove(id)
36
+ if (!row.remove(id)) return false
37
+ this.size--
38
+ return true
32
39
  }
33
40
 
34
41
  get (id) {
@@ -43,11 +50,13 @@ module.exports = class RoutingTable extends EventEmitter {
43
50
  }
44
51
 
45
52
  random () {
46
- const offset = (Math.random() * this.rows.length) | 0
53
+ let n = (Math.random() * this.size) | 0
47
54
 
48
55
  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()
56
+ const r = this.rows[i]
57
+ if (!r) continue
58
+ if (n < r.nodes.length) return r.nodes[n]
59
+ n -= r.nodes.length
51
60
  }
52
61
 
53
62
  return null
@@ -167,10 +176,6 @@ class Row extends EventEmitter {
167
176
  return null
168
177
  }
169
178
 
170
- random () {
171
- return this.nodes.length ? this.nodes[(Math.random() * this.nodes.length) | 0] : null
172
- }
173
-
174
179
  insert (i, node) {
175
180
  this.nodes.push(node) // push node or null or whatevs, just trying to not be polymorphic
176
181
  for (let j = this.nodes.length - 1; j > i; j--) this.nodes[j] = this.nodes[j - 1]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kademlia-routing-table",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "XOR based routing table used for P2P networks such as a Kademlia DHT.",
5
5
  "main": "index.js",
6
6
  "imports": {
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 () {