|
| 1 | +/* eslint max-nested-callbacks: ["error", 6] */ |
1 | 2 | /* eslint-env mocha */
|
2 | 3 |
|
3 |
| -import randomBytes from 'iso-random-stream/src/random.js' |
4 |
| -import concat from 'it-concat' |
5 | 4 | import { expect } from 'aegir/utils/chai.js'
|
6 | 5 | import { daemonFactory } from './utils/daemon-factory.js'
|
| 6 | +import delay from 'delay' |
| 7 | +import defer from 'p-defer' |
| 8 | +import { fromString as uint8ArrayFromString } from 'uint8arrays' |
| 9 | +import { isNode } from 'wherearewe' |
| 10 | +import toBuffer from 'it-to-buffer' |
7 | 11 |
|
8 |
| -describe.skip('kad-dht', () => { |
9 |
| - let factory |
| 12 | +const getConfig = (bootstrap) => ({ |
| 13 | + Bootstrap: bootstrap, |
| 14 | + Routing: { |
| 15 | + Type: 'dhtserver' |
| 16 | + } |
| 17 | +}) |
10 | 18 |
|
11 |
| - before(async () => { |
12 |
| - factory = await daemonFactory() |
| 19 | +const spawnGoDaemon = (factory, bootstrap = []) => { |
| 20 | + return factory.spawn({ |
| 21 | + type: 'go', |
| 22 | + test: true, |
| 23 | + ipfsOptions: { |
| 24 | + config: getConfig(bootstrap) |
| 25 | + } |
13 | 26 | })
|
| 27 | +} |
14 | 28 |
|
15 |
| - after(() => factory.clean()) |
| 29 | +const spawnJsDaemon = (factory, bootstrap = []) => { |
| 30 | + return factory.spawn({ |
| 31 | + type: 'js', |
| 32 | + test: true, |
| 33 | + ipfsOptions: { |
| 34 | + config: getConfig(bootstrap) |
| 35 | + } |
| 36 | + }) |
| 37 | +} |
16 | 38 |
|
17 |
| - describe('a JS node in the land of Go', () => { |
18 |
| - let jsD |
19 |
| - let goD1 |
20 |
| - let goD2 |
21 |
| - let goD3 |
| 39 | +const spawnDaemon = async function (factory, fn) { |
| 40 | + const daemon = await fn(factory) |
| 41 | + const id = await daemon.api.id() |
22 | 42 |
|
23 |
| - before(async () => { |
24 |
| - [goD1, goD2, goD3, jsD] = await Promise.all([ |
25 |
| - factory.spawn({ type: 'go' }), |
26 |
| - factory.spawn({ type: 'go' }), |
27 |
| - factory.spawn({ type: 'go' }), |
28 |
| - factory.spawn({ type: 'js' }) |
29 |
| - ]) |
| 43 | + return { |
| 44 | + api: daemon.api, id |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const getNodeAddr = async (node) => { |
| 49 | + const res = await node.api.id() |
| 50 | + expect(res.id).to.exist() |
| 51 | + |
| 52 | + return res.addresses[0] |
| 53 | +} |
| 54 | + |
| 55 | +const addFileAndCat = async (addDaemon, catDaemons, options = {}) => { |
| 56 | + const data = uint8ArrayFromString(`some-data-${Math.random()}`) |
| 57 | + const { cid } = await addDaemon.api.add(data) |
| 58 | + |
| 59 | + await Promise.all( |
| 60 | + catDaemons.map(async daemon => { |
| 61 | + const res = await toBuffer(daemon.api.cat(cid, options)) |
| 62 | + |
| 63 | + expect(res).to.equalBytes(data) |
30 | 64 | })
|
| 65 | + ) |
| 66 | +} |
31 | 67 |
|
32 |
| - after(() => factory.clean()) |
| 68 | +const createNetwork = function (name, createNodes, tests) { |
| 69 | + describe(name, function () { |
| 70 | + const nodes = defer() |
| 71 | + let factory |
33 | 72 |
|
34 |
| - it('make connections', async () => { |
35 |
| - await Promise.all([ |
36 |
| - jsD.api.swarm.connect(goD1.api.peerId.addresses[0]), |
37 |
| - goD1.api.swarm.connect(goD2.api.peerId.addresses[0]), |
38 |
| - goD2.api.swarm.connect(goD3.api.peerId.addresses[0]) |
39 |
| - ]) |
| 73 | + before(async function () { |
| 74 | + factory = await daemonFactory() |
| 75 | + nodes.resolve(await createNodes(factory)) |
40 | 76 | })
|
41 | 77 |
|
42 |
| - it('one hop', async () => { |
43 |
| - const data = randomBytes(9001) |
| 78 | + after(async function () { |
| 79 | + await factory.clean() |
| 80 | + }) |
| 81 | + |
| 82 | + tests(nodes.promise) |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +const createBootstrappedNetwork = function (name, createBootstrapper, createNodes) { |
| 87 | + createNetwork(name, async factory => { |
| 88 | + const bootstrapper = await createBootstrapper(factory) |
| 89 | + const bootstrapAddr = await getNodeAddr(bootstrapper) |
| 90 | + const nodes = await createNodes(factory, bootstrapAddr) |
| 91 | + |
| 92 | + while (true) { |
| 93 | + const peers = await bootstrapper.api.swarm.peers() |
44 | 94 |
|
45 |
| - const { cid } = await goD1.api.add(data) |
46 |
| - const file = await concat(jsD.api.cat(cid)) |
| 95 | + if (peers.length === nodes.length) { |
| 96 | + break |
| 97 | + } |
47 | 98 |
|
48 |
| - expect(file.slice()).to.be.eql(data) |
| 99 | + await delay(500) |
| 100 | + } |
| 101 | + |
| 102 | + return nodes |
| 103 | + }, (nodes) => { |
| 104 | + it('should get from the network after being added', async function () { |
| 105 | + const [add, ...cat] = await nodes |
| 106 | + await addFileAndCat(add, cat) |
49 | 107 | })
|
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +const createLinearNetwork = function (name, createNodes) { |
| 112 | + createNetwork(name, async factory => { |
| 113 | + const [node0, node1, node2, node3] = await createNodes(factory) |
| 114 | + |
| 115 | + /* |
| 116 | + * Make connections between nodes |
| 117 | + * +-+ +-+ |
| 118 | + * |0+-----> |1| |
| 119 | + * +++ +++ |
| 120 | + * ^ | |
| 121 | + * | | |
| 122 | + * | v |
| 123 | + * +++ +++ |
| 124 | + * |3| |2| |
| 125 | + * +-+ +-+ |
| 126 | + */ |
| 127 | + await node3.api.swarm.connect(node0.id.addresses[0]) |
| 128 | + await node0.api.swarm.connect(node1.id.addresses[0]) |
| 129 | + await node1.api.swarm.connect(node2.id.addresses[0]) |
50 | 130 |
|
| 131 | + return [node0, node1, node2, node3] |
| 132 | + }, (nodes) => { |
| 133 | + it('one hop', async () => { |
| 134 | + const [node0, _node1, _node2, node3] = await nodes // eslint-disable-line no-unused-vars |
| 135 | + await addFileAndCat(node0, [node3]) |
| 136 | + }) |
51 | 137 | it('two hops', async () => {
|
52 |
| - const data = randomBytes(9001) |
| 138 | + const [_node0, node1, _node2, node3] = await nodes // eslint-disable-line no-unused-vars |
| 139 | + await addFileAndCat(node1, [node3]) |
| 140 | + }) |
| 141 | + it('three hops', async () => { |
| 142 | + const [_node0, _node1, node2, node3] = await nodes // eslint-disable-line no-unused-vars |
| 143 | + await addFileAndCat(node2, [node3]) |
| 144 | + }) |
| 145 | + }) |
| 146 | +} |
| 147 | + |
| 148 | +const createDisjointNetwork = function (name, createNodes) { |
| 149 | + createNetwork(name, async factory => { |
| 150 | + const [node0, node1, node2, node3, node4, node5] = await createNodes(factory) |
| 151 | + |
| 152 | + // Make connections between nodes |
| 153 | + |
| 154 | + // 0 -> 1 -> 2 |
| 155 | + await node0.api.swarm.connect(node1.id.addresses[0]) |
| 156 | + await node1.api.swarm.connect(node2.id.addresses[0]) |
| 157 | + |
| 158 | + // 3 -> 4 -> 5 |
| 159 | + await node3.api.swarm.connect(node4.id.addresses[0]) |
| 160 | + await node4.api.swarm.connect(node5.id.addresses[0]) |
53 | 161 |
|
54 |
| - const { cid } = await goD2.api.add(data) |
55 |
| - const file = await concat(jsD.api.cat(cid)) |
| 162 | + return [node0, node1, node2, node3, node4, node5] |
| 163 | + }, (nodes) => { |
| 164 | + it('join network', async () => { |
| 165 | + const [node0, _node1, node2, node3, _node4, node5] = await nodes // eslint-disable-line no-unused-vars |
56 | 166 |
|
57 |
| - expect(file.slice()).to.be.eql(data) |
| 167 | + // nodes at opposite ends should not find content |
| 168 | + await expect(addFileAndCat(node0, [node3], { |
| 169 | + timeout: 5000 |
| 170 | + })).to.eventually.be.rejected() |
| 171 | + |
| 172 | + /* |
| 173 | + * Make connections between nodes |
| 174 | + * 0 -> 1 -> 2 -> 5 -> 4 -> 3 |
| 175 | + */ |
| 176 | + |
| 177 | + await node2.api.swarm.connect(node5.id.addresses[0]) |
| 178 | + |
| 179 | + // should now succeed |
| 180 | + await addFileAndCat(node0, [node3]) |
58 | 181 | })
|
| 182 | + }) |
| 183 | +} |
59 | 184 |
|
60 |
| - it('three hops', async () => { |
61 |
| - const data = randomBytes(9001) |
| 185 | +describe('kad-dht', function () { |
| 186 | + this.timeout(600 * 1000) |
62 | 187 |
|
63 |
| - const { cid } = await goD3.api.add(data) |
64 |
| - const file = await concat(jsD.api.cat(cid)) |
| 188 | + if (!isNode) { |
| 189 | + it.skip('DHT tests are only run on node') |
| 190 | + return |
| 191 | + } |
65 | 192 |
|
66 |
| - expect(file.slice()).to.be.eql(data) |
| 193 | + describe('kad-dht with a bootstrap node', () => { |
| 194 | + createBootstrappedNetwork('a JS network', factory => spawnJsDaemon(factory), (factory, bootstrapAddr) => { |
| 195 | + return Promise.all([ |
| 196 | + spawnJsDaemon(factory, [bootstrapAddr]), |
| 197 | + spawnJsDaemon(factory, [bootstrapAddr]), |
| 198 | + spawnJsDaemon(factory, [bootstrapAddr]) |
| 199 | + ]) |
| 200 | + }) |
| 201 | + |
| 202 | + createBootstrappedNetwork('a GO network', factory => spawnGoDaemon(factory), (factory, bootstrapAddr) => { |
| 203 | + return Promise.all([ |
| 204 | + spawnGoDaemon(factory, [bootstrapAddr]), |
| 205 | + spawnGoDaemon(factory, [bootstrapAddr]), |
| 206 | + spawnGoDaemon(factory, [bootstrapAddr]) |
| 207 | + ]) |
| 208 | + }) |
| 209 | + |
| 210 | + createBootstrappedNetwork('a JS bootstrap node in the land of Go', factory => spawnJsDaemon(factory), (factory, bootstrapAddr) => { |
| 211 | + return Promise.all([ |
| 212 | + spawnGoDaemon(factory, [bootstrapAddr]), |
| 213 | + spawnGoDaemon(factory, [bootstrapAddr]), |
| 214 | + spawnGoDaemon(factory, [bootstrapAddr]) |
| 215 | + ]) |
| 216 | + }) |
| 217 | + |
| 218 | + createBootstrappedNetwork('a Go bootstrap node in the land of JS', factory => spawnGoDaemon(factory), (factory, bootstrapAddr) => { |
| 219 | + return Promise.all([ |
| 220 | + spawnJsDaemon(factory, [bootstrapAddr]), |
| 221 | + spawnJsDaemon(factory, [bootstrapAddr]), |
| 222 | + spawnJsDaemon(factory, [bootstrapAddr]) |
| 223 | + ]) |
| 224 | + }) |
| 225 | + |
| 226 | + createBootstrappedNetwork('a JS bootstrap node in a hybrid land', factory => spawnJsDaemon(factory), (factory, bootstrapAddr) => { |
| 227 | + return Promise.all([ |
| 228 | + spawnGoDaemon(factory, [bootstrapAddr]), |
| 229 | + spawnJsDaemon(factory, [bootstrapAddr]), |
| 230 | + spawnGoDaemon(factory, [bootstrapAddr]) |
| 231 | + ]) |
| 232 | + }) |
| 233 | + |
| 234 | + createBootstrappedNetwork('a Go bootstrap node in a hybrid land', factory => spawnGoDaemon(factory), (factory, bootstrapAddr) => { |
| 235 | + return Promise.all([ |
| 236 | + spawnJsDaemon(factory, [bootstrapAddr]), |
| 237 | + spawnGoDaemon(factory, [bootstrapAddr]), |
| 238 | + spawnJsDaemon(factory, [bootstrapAddr]) |
| 239 | + ]) |
67 | 240 | })
|
68 | 241 | })
|
69 | 242 |
|
70 |
| - describe('a Go node in the land of JS', () => {}) |
71 |
| - describe('hybrid', () => {}) |
| 243 | + describe('kad-dht with multiple hops', () => { |
| 244 | + createLinearNetwork('a JS node in the land of Go', (factory) => { |
| 245 | + return Promise.all([ |
| 246 | + spawnDaemon(factory, spawnGoDaemon), |
| 247 | + spawnDaemon(factory, spawnGoDaemon), |
| 248 | + spawnDaemon(factory, spawnGoDaemon), |
| 249 | + spawnDaemon(factory, spawnJsDaemon) |
| 250 | + ]) |
| 251 | + }) |
| 252 | + |
| 253 | + createLinearNetwork('a Go node in the land of JS', (factory) => { |
| 254 | + return Promise.all([ |
| 255 | + spawnDaemon(factory, spawnJsDaemon), |
| 256 | + spawnDaemon(factory, spawnJsDaemon), |
| 257 | + spawnDaemon(factory, spawnJsDaemon), |
| 258 | + spawnDaemon(factory, spawnGoDaemon) |
| 259 | + ]) |
| 260 | + }) |
| 261 | + |
| 262 | + createLinearNetwork('a hybrid network, cat from GO', (factory) => { |
| 263 | + return Promise.all([ |
| 264 | + spawnDaemon(factory, spawnJsDaemon), |
| 265 | + spawnDaemon(factory, spawnGoDaemon), |
| 266 | + spawnDaemon(factory, spawnJsDaemon), |
| 267 | + spawnDaemon(factory, spawnGoDaemon) |
| 268 | + ]) |
| 269 | + }) |
| 270 | + |
| 271 | + createLinearNetwork('a hybrid network, cat from JS', (factory) => { |
| 272 | + return Promise.all([ |
| 273 | + spawnDaemon(factory, spawnJsDaemon), |
| 274 | + spawnDaemon(factory, spawnGoDaemon), |
| 275 | + spawnDaemon(factory, spawnJsDaemon), |
| 276 | + spawnDaemon(factory, spawnGoDaemon) |
| 277 | + ]) |
| 278 | + }) |
| 279 | + }) |
| 280 | + |
| 281 | + describe('kad-dht across disjoint networks that become joint', () => { |
| 282 | + createDisjointNetwork('a GO network', (factory) => { |
| 283 | + return Promise.all([ |
| 284 | + spawnDaemon(factory, spawnGoDaemon), |
| 285 | + spawnDaemon(factory, spawnGoDaemon), |
| 286 | + spawnDaemon(factory, spawnGoDaemon), |
| 287 | + spawnDaemon(factory, spawnGoDaemon), |
| 288 | + spawnDaemon(factory, spawnGoDaemon), |
| 289 | + spawnDaemon(factory, spawnGoDaemon) |
| 290 | + ]) |
| 291 | + }) |
| 292 | + |
| 293 | + createDisjointNetwork('a JS network', (factory) => { |
| 294 | + return Promise.all([ |
| 295 | + spawnDaemon(factory, spawnJsDaemon), |
| 296 | + spawnDaemon(factory, spawnJsDaemon), |
| 297 | + spawnDaemon(factory, spawnJsDaemon), |
| 298 | + spawnDaemon(factory, spawnJsDaemon), |
| 299 | + spawnDaemon(factory, spawnJsDaemon), |
| 300 | + spawnDaemon(factory, spawnJsDaemon) |
| 301 | + ]) |
| 302 | + }) |
| 303 | + |
| 304 | + createDisjointNetwork('a hybrid network, cat from GO', (factory) => { |
| 305 | + return Promise.all([ |
| 306 | + spawnDaemon(factory, spawnGoDaemon), |
| 307 | + spawnDaemon(factory, spawnJsDaemon), |
| 308 | + spawnDaemon(factory, spawnGoDaemon), |
| 309 | + spawnDaemon(factory, spawnJsDaemon), |
| 310 | + spawnDaemon(factory, spawnGoDaemon), |
| 311 | + spawnDaemon(factory, spawnJsDaemon) |
| 312 | + ]) |
| 313 | + }) |
| 314 | + |
| 315 | + createDisjointNetwork('a hybrid network, cat from JS', (factory) => { |
| 316 | + return Promise.all([ |
| 317 | + spawnDaemon(factory, spawnJsDaemon), |
| 318 | + spawnDaemon(factory, spawnGoDaemon), |
| 319 | + spawnDaemon(factory, spawnJsDaemon), |
| 320 | + spawnDaemon(factory, spawnGoDaemon), |
| 321 | + spawnDaemon(factory, spawnJsDaemon), |
| 322 | + spawnDaemon(factory, spawnGoDaemon) |
| 323 | + ]) |
| 324 | + }) |
| 325 | + }) |
72 | 326 | })
|
0 commit comments