kademlia-routing-table 1.0.5 → 1.0.7
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.
- package/LICENSE +201 -21
- package/README.md +21 -18
- package/index.js +85 -35
- package/package.json +16 -9
- package/.github/workflows/test-node.yml +0 -23
- package/example.js +0 -26
- package/test.js +0 -32
package/LICENSE
CHANGED
|
@@ -1,21 +1,201 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -6,27 +6,23 @@ XOR distance based routing table used for P2P networks such as a Kademlia DHT.
|
|
|
6
6
|
npm install kademlia-routing-table
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
Similar to k-buckets, but implemented using the simplifications described in https://github.com/ethereum/wiki/wiki/Kademlia-Peer-Selection
|
|
9
|
+
Similar to k-buckets, but implemented using the simplifications described in https://web.archive.org/web/20181102041849/https://github.com/ethereum/wiki/wiki/Kademlia-Peer-Selection
|
|
10
10
|
|
|
11
11
|
To understand the concept behind peer routing, DHTs, and the terms used here,
|
|
12
12
|
I recommend reading the [Kademlia DHT paper](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf) as well.
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
|
-
```
|
|
16
|
+
```js
|
|
17
17
|
const RoutingTable = require('kademlia-routing-table')
|
|
18
18
|
const { randomBytes } = require('crypto')
|
|
19
19
|
|
|
20
20
|
// Create a new table that stores nodes "close" to the passed in id.
|
|
21
|
-
// The id should be
|
|
21
|
+
// The id should be uniformly distributed, ie a hash, random bytes etc.
|
|
22
22
|
const table = new RoutingTable(randomBytes(32))
|
|
23
23
|
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
id: randomBytes(32), // this field is required
|
|
27
|
-
// populate with any other data you want to store
|
|
28
|
-
})
|
|
29
|
-
|
|
24
|
+
// Listen before adding nodes: 'row' is only emitted when a row is created,
|
|
25
|
+
// so rows created before this listener is attached never get a 'full' handler.
|
|
30
26
|
table.on('row', function (row) {
|
|
31
27
|
// A new row has been added to the routing table
|
|
32
28
|
// This row represents row.index similar bits to the table.id
|
|
@@ -39,6 +35,12 @@ table.on('row', function (row) {
|
|
|
39
35
|
})
|
|
40
36
|
})
|
|
41
37
|
|
|
38
|
+
// Add a node to the routing table
|
|
39
|
+
table.add({
|
|
40
|
+
id: randomBytes(32) // this field is required
|
|
41
|
+
// populate with any other data you want to store
|
|
42
|
+
})
|
|
43
|
+
|
|
42
44
|
// Get the 20 nodes "closest" to a passed in id
|
|
43
45
|
const closest = table.closest(randomBytes(32), 20)
|
|
44
46
|
```
|
|
@@ -49,9 +51,9 @@ const closest = table.closest(randomBytes(32), 20)
|
|
|
49
51
|
|
|
50
52
|
Create a new routing table.
|
|
51
53
|
|
|
52
|
-
`id` should be a Buffer that is
|
|
54
|
+
`id` should be a Buffer that is uniformly distributed. `options` include:
|
|
53
55
|
|
|
54
|
-
```
|
|
56
|
+
```js
|
|
55
57
|
{
|
|
56
58
|
k: 20 // The max row size
|
|
57
59
|
}
|
|
@@ -64,7 +66,7 @@ When inserting a node the XOR distance between the node and the table.id is
|
|
|
64
66
|
calculated and used to figure which table row this node should be inserted into.
|
|
65
67
|
|
|
66
68
|
Returns `true` if the node could be added to the corresponding row or `false` if not.
|
|
67
|
-
If `false` is returned the
|
|
69
|
+
If `false` is returned the corresponding row emits `'full'` with the node.
|
|
68
70
|
|
|
69
71
|
#### `node = table.get(id)`
|
|
70
72
|
|
|
@@ -110,7 +112,7 @@ about accessing rows directly outside the row event.
|
|
|
110
112
|
|
|
111
113
|
## Row API
|
|
112
114
|
|
|
113
|
-
For the row passed in the
|
|
115
|
+
For the row passed in the `'row'` event the following API exists.
|
|
114
116
|
|
|
115
117
|
#### `row.index`
|
|
116
118
|
|
|
@@ -127,13 +129,14 @@ Property set to null initially you can use if you want to store optional data on
|
|
|
127
129
|
|
|
128
130
|
#### `bool = row.add(node)`
|
|
129
131
|
|
|
130
|
-
Same as `table.add` but for a specific row. Only use this to add the
|
|
131
|
-
passed
|
|
132
|
+
Same as `table.add` but for a specific row. Only use this to add the node
|
|
133
|
+
passed to the `'full'` event.
|
|
132
134
|
|
|
133
|
-
#### `bool = row.remove(
|
|
135
|
+
#### `bool = row.remove(id)`
|
|
134
136
|
|
|
135
|
-
Same as `table.remove` but for a specific row.
|
|
136
|
-
|
|
137
|
+
Same as `table.remove` but for a specific row. Takes the node's id, not the
|
|
138
|
+
node, e.g. `row.remove(worst.id)`. Only use this to remove the "worst" node
|
|
139
|
+
from the row when wanting to add the node passed to the `'full'` event.
|
|
137
140
|
|
|
138
141
|
#### `row.on('add', node)`
|
|
139
142
|
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { EventEmitter } = require('events')
|
|
2
2
|
|
|
3
3
|
module.exports = class RoutingTable extends EventEmitter {
|
|
4
|
-
constructor
|
|
4
|
+
constructor(id, opts) {
|
|
5
5
|
if (!opts) opts = {}
|
|
6
6
|
|
|
7
7
|
super()
|
|
@@ -10,46 +10,42 @@ module.exports = class RoutingTable extends EventEmitter {
|
|
|
10
10
|
this.k = opts.k || 20
|
|
11
11
|
this.size = 0
|
|
12
12
|
this.rows = new Array(id.length * 8)
|
|
13
|
+
this._top = -1 // highest row index in use, so closest() can skip the empty tail
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
add
|
|
16
|
+
add(node) {
|
|
16
17
|
const i = this._diff(node.id)
|
|
17
18
|
|
|
18
19
|
let row = this.rows[i]
|
|
19
20
|
|
|
20
21
|
if (!row) {
|
|
21
22
|
row = this.rows[i] = new Row(this, i)
|
|
23
|
+
if (i > this._top) this._top = i
|
|
22
24
|
this.emit('row', row)
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
if (!row.add(node, this.k)) return false
|
|
27
|
-
|
|
28
|
-
this.size += row.nodes.length - len
|
|
29
|
-
return true
|
|
27
|
+
return row.add(node)
|
|
30
28
|
}
|
|
31
29
|
|
|
32
|
-
remove
|
|
30
|
+
remove(id) {
|
|
33
31
|
const i = this._diff(id)
|
|
34
32
|
const row = this.rows[i]
|
|
35
33
|
if (!row) return false
|
|
36
|
-
|
|
37
|
-
this.size--
|
|
38
|
-
return true
|
|
34
|
+
return row.remove(id)
|
|
39
35
|
}
|
|
40
36
|
|
|
41
|
-
get
|
|
37
|
+
get(id) {
|
|
42
38
|
const i = this._diff(id)
|
|
43
39
|
const row = this.rows[i]
|
|
44
40
|
if (!row) return null
|
|
45
41
|
return row.get(id)
|
|
46
42
|
}
|
|
47
43
|
|
|
48
|
-
has
|
|
44
|
+
has(id) {
|
|
49
45
|
return this.get(id) !== null
|
|
50
46
|
}
|
|
51
47
|
|
|
52
|
-
random
|
|
48
|
+
random() {
|
|
53
49
|
let n = (Math.random() * this.size) | 0
|
|
54
50
|
|
|
55
51
|
for (let i = 0; i < this.rows.length; i++) {
|
|
@@ -62,34 +58,66 @@ module.exports = class RoutingTable extends EventEmitter {
|
|
|
62
58
|
return null
|
|
63
59
|
}
|
|
64
60
|
|
|
65
|
-
closest
|
|
61
|
+
closest(id, k) {
|
|
66
62
|
if (!k) k = this.k
|
|
67
63
|
|
|
68
64
|
const result = []
|
|
69
65
|
const d = this._diff(id)
|
|
70
66
|
|
|
71
|
-
//
|
|
72
|
-
|
|
67
|
+
// Row i holds nodes that match this.id before bit i and differ from it at bit i.
|
|
68
|
+
// If id also differs from this.id at bit i, those nodes are closer to id than every
|
|
69
|
+
// node in a later row; if id matches this.id there, they are farther. So we take
|
|
70
|
+
// the rows where id differs going up from d (all closer to id than we are), then
|
|
71
|
+
// the rows where id matches going down from the top (all farther).
|
|
72
|
+
|
|
73
|
+
// rows where id differs from this.id, closest first
|
|
74
|
+
for (let i = d; i <= this._top && result.length < k; i++) {
|
|
75
|
+
const row = this.rows[i]
|
|
76
|
+
if (row && bitDiffers(id, this.id, i)) this._pushNodes(row, id, k, result)
|
|
77
|
+
}
|
|
73
78
|
|
|
74
|
-
//
|
|
75
|
-
for (let i =
|
|
79
|
+
// rows where id matches this.id, closest first
|
|
80
|
+
for (let i = this._top; i >= 0 && result.length < k; i--) {
|
|
81
|
+
const row = this.rows[i]
|
|
82
|
+
if (row && !bitDiffers(id, this.id, i)) this._pushNodes(row, id, k, result)
|
|
83
|
+
}
|
|
76
84
|
|
|
77
85
|
return result
|
|
78
86
|
}
|
|
79
87
|
|
|
80
|
-
_pushNodes
|
|
81
|
-
const
|
|
82
|
-
|
|
88
|
+
_pushNodes(row, id, k, result) {
|
|
89
|
+
const nodes = row.nodes
|
|
90
|
+
const start = result.length
|
|
91
|
+
|
|
92
|
+
if (k - start >= nodes.length) {
|
|
93
|
+
for (let j = 0; j < nodes.length; j++) result.push(nodes[j])
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// only some fit, and rows are sorted by id, not by distance to id, so insertion sort
|
|
98
|
+
// the row into the tail of the result, dropping whatever falls off the end
|
|
99
|
+
const offset = row.byteOffset
|
|
83
100
|
|
|
84
|
-
|
|
85
|
-
|
|
101
|
+
for (let n = 0; n < nodes.length; n++) {
|
|
102
|
+
const node = nodes[n]
|
|
103
|
+
let j = result.length
|
|
104
|
+
|
|
105
|
+
if (j < k) result.push(node)
|
|
106
|
+
else if (compareDistance(node.id, result[--j].id, id, offset) >= 0) continue
|
|
107
|
+
|
|
108
|
+
for (; j > start && compareDistance(node.id, result[j - 1].id, id, offset) < 0; j--) {
|
|
109
|
+
result[j] = result[j - 1]
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
result[j] = node
|
|
113
|
+
}
|
|
86
114
|
}
|
|
87
115
|
|
|
88
|
-
toArray
|
|
116
|
+
toArray() {
|
|
89
117
|
return this.closest(this.id, Infinity)
|
|
90
118
|
}
|
|
91
119
|
|
|
92
|
-
_diff
|
|
120
|
+
_diff(id) {
|
|
93
121
|
for (let i = 0; i < id.length; i++) {
|
|
94
122
|
const a = id[i]
|
|
95
123
|
const b = this.id[i]
|
|
@@ -102,16 +130,17 @@ module.exports = class RoutingTable extends EventEmitter {
|
|
|
102
130
|
}
|
|
103
131
|
|
|
104
132
|
class Row extends EventEmitter {
|
|
105
|
-
constructor
|
|
133
|
+
constructor(table, index) {
|
|
106
134
|
super()
|
|
107
135
|
|
|
108
136
|
this.data = null // can be used be upstream for whatevs
|
|
137
|
+
this.byteOffset = index >> 3
|
|
109
138
|
this.index = index
|
|
110
139
|
this.table = table
|
|
111
140
|
this.nodes = []
|
|
112
141
|
}
|
|
113
142
|
|
|
114
|
-
add
|
|
143
|
+
add(node) {
|
|
115
144
|
const id = node.id
|
|
116
145
|
|
|
117
146
|
let l = 0
|
|
@@ -136,10 +165,11 @@ class Row extends EventEmitter {
|
|
|
136
165
|
}
|
|
137
166
|
|
|
138
167
|
this.insert(l, node)
|
|
168
|
+
this.table.size++
|
|
139
169
|
return true
|
|
140
170
|
}
|
|
141
171
|
|
|
142
|
-
remove
|
|
172
|
+
remove(id) {
|
|
143
173
|
let l = 0
|
|
144
174
|
let r = this.nodes.length - 1
|
|
145
175
|
|
|
@@ -149,6 +179,7 @@ class Row extends EventEmitter {
|
|
|
149
179
|
|
|
150
180
|
if (c === 0) {
|
|
151
181
|
this.splice(m)
|
|
182
|
+
this.table.size--
|
|
152
183
|
return true
|
|
153
184
|
}
|
|
154
185
|
|
|
@@ -159,7 +190,7 @@ class Row extends EventEmitter {
|
|
|
159
190
|
return false
|
|
160
191
|
}
|
|
161
192
|
|
|
162
|
-
get
|
|
193
|
+
get(id) {
|
|
163
194
|
let l = 0
|
|
164
195
|
let r = this.nodes.length - 1
|
|
165
196
|
|
|
@@ -176,20 +207,23 @@ class Row extends EventEmitter {
|
|
|
176
207
|
return null
|
|
177
208
|
}
|
|
178
209
|
|
|
179
|
-
insert
|
|
210
|
+
insert(i, node) {
|
|
180
211
|
this.nodes.push(node) // push node or null or whatevs, just trying to not be polymorphic
|
|
181
212
|
for (let j = this.nodes.length - 1; j > i; j--) this.nodes[j] = this.nodes[j - 1]
|
|
182
213
|
this.nodes[i] = node
|
|
183
214
|
this.emit('add', node)
|
|
184
215
|
}
|
|
185
216
|
|
|
186
|
-
splice
|
|
217
|
+
splice(i) {
|
|
218
|
+
const node = this.nodes[i]
|
|
187
219
|
for (; i < this.nodes.length - 1; i++) this.nodes[i] = this.nodes[i + 1]
|
|
188
|
-
this.
|
|
220
|
+
this.nodes.pop()
|
|
221
|
+
this.emit('remove', node)
|
|
189
222
|
}
|
|
190
223
|
|
|
191
|
-
|
|
192
|
-
|
|
224
|
+
// very likely they diverge after a couple of bytes so a simple impl, like this is prop fastest vs Buffer.compare
|
|
225
|
+
compare(a, b) {
|
|
226
|
+
for (let i = this.byteOffset; i < a.length; i++) {
|
|
193
227
|
const ai = a[i]
|
|
194
228
|
const bi = b[i]
|
|
195
229
|
if (ai === bi) continue
|
|
@@ -198,3 +232,19 @@ class Row extends EventEmitter {
|
|
|
198
232
|
return 0
|
|
199
233
|
}
|
|
200
234
|
}
|
|
235
|
+
|
|
236
|
+
function bitDiffers(a, b, i) {
|
|
237
|
+
const j = i >> 3
|
|
238
|
+
return ((a[j] ^ b[j]) & (0x80 >> (i & 7))) !== 0
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// nodes in the same row share their first `offset` bytes, so those bytes can't decide the order
|
|
242
|
+
function compareDistance(a, b, target, offset) {
|
|
243
|
+
for (let i = offset; i < a.length; i++) {
|
|
244
|
+
const ai = a[i] ^ target[i]
|
|
245
|
+
const bi = b[i] ^ target[i]
|
|
246
|
+
if (ai === bi) continue
|
|
247
|
+
return ai < bi ? -1 : 1
|
|
248
|
+
}
|
|
249
|
+
return 0
|
|
250
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kademlia-routing-table",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "XOR based routing table used for P2P networks such as a Kademlia DHT.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js"
|
|
8
|
+
],
|
|
6
9
|
"imports": {
|
|
7
10
|
"events": {
|
|
8
11
|
"bare": "bare-events",
|
|
@@ -13,11 +16,15 @@
|
|
|
13
16
|
"bare-events": "^2.2.0"
|
|
14
17
|
},
|
|
15
18
|
"devDependencies": {
|
|
16
|
-
"brittle": "^
|
|
17
|
-
"
|
|
19
|
+
"brittle": "^4.6.1",
|
|
20
|
+
"lunte": "^1.8.6",
|
|
21
|
+
"prettier": "^3.10.1",
|
|
22
|
+
"prettier-config-holepunch": "^2.0.0"
|
|
18
23
|
},
|
|
19
24
|
"scripts": {
|
|
20
|
-
"
|
|
25
|
+
"format": "prettier --write .",
|
|
26
|
+
"lint": "prettier --check . && lunte",
|
|
27
|
+
"test": "brittle-node test.js"
|
|
21
28
|
},
|
|
22
29
|
"keywords": [
|
|
23
30
|
"kademlia",
|
|
@@ -31,12 +38,12 @@
|
|
|
31
38
|
],
|
|
32
39
|
"repository": {
|
|
33
40
|
"type": "git",
|
|
34
|
-
"url": "https://github.com/
|
|
41
|
+
"url": "https://github.com/holepunchto/kademlia-routing-table.git"
|
|
35
42
|
},
|
|
36
|
-
"author": "
|
|
37
|
-
"license": "
|
|
43
|
+
"author": "Holepunch Inc.",
|
|
44
|
+
"license": "Apache-2.0",
|
|
38
45
|
"bugs": {
|
|
39
|
-
"url": "https://github.com/
|
|
46
|
+
"url": "https://github.com/holepunchto/kademlia-routing-table/issues"
|
|
40
47
|
},
|
|
41
|
-
"homepage": "https://github.com/
|
|
48
|
+
"homepage": "https://github.com/holepunchto/kademlia-routing-table"
|
|
42
49
|
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
name: Build Status
|
|
2
|
-
on:
|
|
3
|
-
push:
|
|
4
|
-
branches:
|
|
5
|
-
- master
|
|
6
|
-
pull_request:
|
|
7
|
-
branches:
|
|
8
|
-
- master
|
|
9
|
-
jobs:
|
|
10
|
-
build:
|
|
11
|
-
strategy:
|
|
12
|
-
matrix:
|
|
13
|
-
node-version: [lts/*]
|
|
14
|
-
os: [ubuntu-latest, macos-latest, windows-latest]
|
|
15
|
-
runs-on: ${{ matrix.os }}
|
|
16
|
-
steps:
|
|
17
|
-
- uses: actions/checkout@v3
|
|
18
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
19
|
-
uses: actions/setup-node@v3
|
|
20
|
-
with:
|
|
21
|
-
node-version: ${{ matrix.node-version }}
|
|
22
|
-
- run: npm install
|
|
23
|
-
- run: npm test
|
package/example.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const RoutingTable = require('./')
|
|
2
|
-
const { randomBytes } = require('crypto')
|
|
3
|
-
|
|
4
|
-
const table = new RoutingTable(randomBytes(32))
|
|
5
|
-
|
|
6
|
-
table.on('row', function (row) {
|
|
7
|
-
row.on('remove', function (node) {
|
|
8
|
-
console.log('remove node', node, 'from row', row.index)
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
row.on('add', function (node) {
|
|
12
|
-
console.log('add node', node, 'to row', row.index)
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
row.on('full', function (node) {
|
|
16
|
-
console.log('row', row.index, 'is full, cannot add', node)
|
|
17
|
-
})
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
for (let i = 0; i < 40; i++) {
|
|
21
|
-
table.add({
|
|
22
|
-
id: randomBytes(32)
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
console.log('The 20 closest nodes to', table.id, 'are:', table.closest(20))
|
package/test.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const test = require('brittle')
|
|
2
|
-
const { randomBytes } = require('crypto')
|
|
3
|
-
const RoutingTable = require('./')
|
|
4
|
-
|
|
5
|
-
test('basic', function (t) {
|
|
6
|
-
const table = new RoutingTable(id())
|
|
7
|
-
const node = { id: id() }
|
|
8
|
-
|
|
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
|
-
}
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
function id () {
|
|
31
|
-
return randomBytes(32)
|
|
32
|
-
}
|