@libp2p/interface-compliance-tests 1.1.24 → 1.1.27
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/dist/src/mocks/connection-manager.d.ts +0 -8
- package/dist/src/mocks/connection-manager.d.ts.map +1 -1
- package/dist/src/mocks/connection-manager.js +4 -16
- package/dist/src/mocks/connection-manager.js.map +1 -1
- package/dist/src/mocks/connection.d.ts +2 -1
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +4 -4
- package/dist/src/mocks/connection.js.map +1 -1
- package/dist/src/mocks/registrar.d.ts +2 -5
- package/dist/src/mocks/registrar.d.ts.map +1 -1
- package/dist/src/mocks/registrar.js +4 -4
- package/dist/src/mocks/registrar.js.map +1 -1
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +10 -15
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +58 -148
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +8 -7
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +9 -12
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +88 -217
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +20 -38
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/pubsub/utils.d.ts +2 -0
- package/dist/src/pubsub/utils.d.ts.map +1 -1
- package/dist/src/pubsub/utils.js +10 -0
- package/dist/src/pubsub/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/mocks/connection-manager.ts +4 -20
- package/src/mocks/connection.ts +5 -5
- package/src/mocks/registrar.ts +6 -10
- package/src/pubsub/api.ts +11 -16
- package/src/pubsub/connection-handlers.ts +59 -159
- package/src/pubsub/emit-self.ts +10 -7
- package/src/pubsub/messages.ts +11 -13
- package/src/pubsub/multiple-nodes.ts +101 -223
- package/src/pubsub/two-nodes.ts +23 -41
- package/src/pubsub/utils.ts +11 -0
|
@@ -5,16 +5,14 @@ import pDefer from 'p-defer'
|
|
|
5
5
|
import pWaitFor from 'p-wait-for'
|
|
6
6
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
7
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { waitForSubscriptionUpdate } from './utils.js'
|
|
8
|
+
import { connectPeers } from '../mocks/registrar.js'
|
|
9
|
+
import { createComponents, waitForSubscriptionUpdate } from './utils.js'
|
|
11
10
|
import type { TestSetup } from '../index.js'
|
|
12
11
|
import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
|
|
13
12
|
import type { PubSubArgs } from './index.js'
|
|
14
|
-
import type {
|
|
15
|
-
import type { Registrar } from '@libp2p/interfaces/registrar'
|
|
16
|
-
import { Components } from '@libp2p/interfaces/components'
|
|
13
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
17
14
|
import { start, stop } from '../index.js'
|
|
15
|
+
import delay from 'delay'
|
|
18
16
|
|
|
19
17
|
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
20
18
|
describe('pubsub with multiple nodes', function () {
|
|
@@ -26,46 +24,30 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
26
24
|
let psA: PubSub
|
|
27
25
|
let psB: PubSub
|
|
28
26
|
let psC: PubSub
|
|
29
|
-
let
|
|
30
|
-
let
|
|
31
|
-
let
|
|
32
|
-
let registrarA: Registrar
|
|
33
|
-
let registrarB: Registrar
|
|
34
|
-
let registrarC: Registrar
|
|
27
|
+
let componentsA: Components
|
|
28
|
+
let componentsB: Components
|
|
29
|
+
let componentsC: Components
|
|
35
30
|
|
|
36
31
|
// Create and start pubsub nodes
|
|
37
32
|
beforeEach(async () => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
registrarA = mockRegistrar()
|
|
43
|
-
registrarB = mockRegistrar()
|
|
44
|
-
registrarC = mockRegistrar()
|
|
33
|
+
componentsA = await createComponents()
|
|
34
|
+
componentsB = await createComponents()
|
|
35
|
+
componentsC = await createComponents()
|
|
45
36
|
|
|
46
37
|
psA = await common.setup({
|
|
47
|
-
components:
|
|
48
|
-
peerId: peerIdA,
|
|
49
|
-
registrar: registrarA
|
|
50
|
-
}),
|
|
38
|
+
components: componentsA,
|
|
51
39
|
init: {
|
|
52
40
|
emitSelf: true
|
|
53
41
|
}
|
|
54
42
|
})
|
|
55
43
|
psB = await common.setup({
|
|
56
|
-
components:
|
|
57
|
-
peerId: peerIdB,
|
|
58
|
-
registrar: registrarB
|
|
59
|
-
}),
|
|
44
|
+
components: componentsB,
|
|
60
45
|
init: {
|
|
61
46
|
emitSelf: true
|
|
62
47
|
}
|
|
63
48
|
})
|
|
64
49
|
psC = await common.setup({
|
|
65
|
-
components:
|
|
66
|
-
peerId: peerIdC,
|
|
67
|
-
registrar: registrarC
|
|
68
|
-
}),
|
|
50
|
+
components: componentsC,
|
|
69
51
|
init: {
|
|
70
52
|
emitSelf: true
|
|
71
53
|
}
|
|
@@ -77,20 +59,8 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
77
59
|
|
|
78
60
|
// Connect nodes
|
|
79
61
|
beforeEach(async () => {
|
|
80
|
-
await connectPeers(psA.multicodecs[0],
|
|
81
|
-
|
|
82
|
-
registrar: registrarA
|
|
83
|
-
}, {
|
|
84
|
-
peerId: peerIdB,
|
|
85
|
-
registrar: registrarB
|
|
86
|
-
})
|
|
87
|
-
await connectPeers(psA.multicodecs[0], {
|
|
88
|
-
peerId: peerIdB,
|
|
89
|
-
registrar: registrarB
|
|
90
|
-
}, {
|
|
91
|
-
peerId: peerIdC,
|
|
92
|
-
registrar: registrarC
|
|
93
|
-
})
|
|
62
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
63
|
+
await connectPeers(psB.multicodecs[0], componentsB, componentsC)
|
|
94
64
|
|
|
95
65
|
// Wait for peers to be ready in pubsub
|
|
96
66
|
await pWaitFor(() =>
|
|
@@ -114,10 +84,10 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
114
84
|
psA.subscribe(topic)
|
|
115
85
|
expect(psA.getTopics()).to.deep.equal([topic])
|
|
116
86
|
|
|
117
|
-
await waitForSubscriptionUpdate(psB,
|
|
87
|
+
await waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
118
88
|
|
|
119
89
|
expect(psB.getPeers().length).to.equal(2)
|
|
120
|
-
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([
|
|
90
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
|
|
121
91
|
|
|
122
92
|
expect(psC.getPeers().length).to.equal(1)
|
|
123
93
|
expect(psC.getSubscribers(topic)).to.be.empty()
|
|
@@ -129,15 +99,15 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
129
99
|
expect(psB.getTopics()).to.deep.equal([topic])
|
|
130
100
|
|
|
131
101
|
await Promise.all([
|
|
132
|
-
waitForSubscriptionUpdate(psA,
|
|
133
|
-
waitForSubscriptionUpdate(psC,
|
|
102
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
103
|
+
waitForSubscriptionUpdate(psC, componentsB.getPeerId())
|
|
134
104
|
])
|
|
135
105
|
|
|
136
106
|
expect(psA.getPeers().length).to.equal(1)
|
|
137
|
-
expect(psA.getSubscribers(topic).map(p => p.toString())).to.deep.equal([
|
|
107
|
+
expect(psA.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsB.getPeerId().toString()])
|
|
138
108
|
|
|
139
109
|
expect(psC.getPeers().length).to.equal(1)
|
|
140
|
-
expect(psC.getSubscribers(topic).map(p => p.toString())).to.deep.equal([
|
|
110
|
+
expect(psC.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsB.getPeerId().toString()])
|
|
141
111
|
})
|
|
142
112
|
|
|
143
113
|
it('subscribe to the topic on node c', async () => {
|
|
@@ -150,7 +120,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
150
120
|
psB.addEventListener('subscription-change', () => {
|
|
151
121
|
expect(psA.getPeers().length).to.equal(1)
|
|
152
122
|
expect(psB.getPeers().length).to.equal(2)
|
|
153
|
-
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([
|
|
123
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsC.getPeerId().toString()])
|
|
154
124
|
|
|
155
125
|
defer.resolve()
|
|
156
126
|
}, {
|
|
@@ -168,55 +138,41 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
168
138
|
psB.subscribe(topic)
|
|
169
139
|
psC.subscribe(topic)
|
|
170
140
|
|
|
141
|
+
await Promise.all([
|
|
142
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
143
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId()),
|
|
144
|
+
waitForSubscriptionUpdate(psC, componentsB.getPeerId())
|
|
145
|
+
])
|
|
146
|
+
|
|
147
|
+
// GossipSub needs time to build the mesh overlay
|
|
148
|
+
await delay(1000)
|
|
149
|
+
|
|
171
150
|
let counter = 0
|
|
172
151
|
|
|
173
|
-
psA.addEventListener('message',
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
})
|
|
178
|
-
psB.addEventListener('message', (evt) => {
|
|
179
|
-
if (evt.detail.topic === topic) {
|
|
180
|
-
incMsg(evt)
|
|
181
|
-
}
|
|
182
|
-
})
|
|
183
|
-
psC.addEventListener('message', (evt) => {
|
|
184
|
-
if (evt.detail.topic === topic) {
|
|
185
|
-
incMsg(evt)
|
|
186
|
-
}
|
|
187
|
-
})
|
|
152
|
+
psA.addEventListener('message', incMsg)
|
|
153
|
+
psB.addEventListener('message', incMsg)
|
|
154
|
+
psC.addEventListener('message', incMsg)
|
|
188
155
|
|
|
189
|
-
await
|
|
190
|
-
waitForSubscriptionUpdate(psA, peerIdB),
|
|
191
|
-
waitForSubscriptionUpdate(psB, peerIdA),
|
|
192
|
-
waitForSubscriptionUpdate(psC, peerIdB)
|
|
193
|
-
])
|
|
156
|
+
const result = await psA.publish(topic, uint8ArrayFromString('hey'))
|
|
194
157
|
|
|
195
|
-
|
|
158
|
+
expect(result).to.have.property('recipients').with.property('length').greaterThanOrEqual(1)
|
|
196
159
|
|
|
197
160
|
function incMsg (evt: CustomEvent<Message>) {
|
|
198
161
|
const msg = evt.detail
|
|
162
|
+
|
|
163
|
+
if (msg.topic !== topic) {
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
|
|
199
167
|
expect(uint8ArrayToString(msg.data)).to.equal('hey')
|
|
200
168
|
check()
|
|
201
169
|
}
|
|
202
170
|
|
|
203
171
|
function check () {
|
|
204
172
|
if (++counter === 3) {
|
|
205
|
-
psA.removeEventListener('message',
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
})
|
|
210
|
-
psB.removeEventListener('message', (evt) => {
|
|
211
|
-
if (evt.detail.topic === topic) {
|
|
212
|
-
incMsg(evt)
|
|
213
|
-
}
|
|
214
|
-
})
|
|
215
|
-
psC.removeEventListener('message', (evt) => {
|
|
216
|
-
if (evt.detail.topic === topic) {
|
|
217
|
-
incMsg(evt)
|
|
218
|
-
}
|
|
219
|
-
})
|
|
173
|
+
psA.removeEventListener('message', incMsg)
|
|
174
|
+
psB.removeEventListener('message', incMsg)
|
|
175
|
+
psC.removeEventListener('message', incMsg)
|
|
220
176
|
defer.resolve()
|
|
221
177
|
}
|
|
222
178
|
}
|
|
@@ -238,57 +194,41 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
238
194
|
const defer = pDefer()
|
|
239
195
|
let counter = 0
|
|
240
196
|
|
|
241
|
-
psA.addEventListener('message', (evt) => {
|
|
242
|
-
if (evt.detail.topic === topic) {
|
|
243
|
-
incMsg(evt)
|
|
244
|
-
}
|
|
245
|
-
})
|
|
246
|
-
psB.addEventListener('message', (evt) => {
|
|
247
|
-
if (evt.detail.topic === topic) {
|
|
248
|
-
incMsg(evt)
|
|
249
|
-
}
|
|
250
|
-
})
|
|
251
|
-
psC.addEventListener('message', (evt) => {
|
|
252
|
-
if (evt.detail.topic === topic) {
|
|
253
|
-
incMsg(evt)
|
|
254
|
-
}
|
|
255
|
-
})
|
|
256
|
-
|
|
257
197
|
psA.subscribe(topic)
|
|
258
198
|
psB.subscribe(topic)
|
|
259
199
|
psC.subscribe(topic)
|
|
260
200
|
|
|
261
201
|
await Promise.all([
|
|
262
|
-
waitForSubscriptionUpdate(psA,
|
|
263
|
-
waitForSubscriptionUpdate(psB,
|
|
264
|
-
waitForSubscriptionUpdate(psC,
|
|
202
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
203
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId()),
|
|
204
|
+
waitForSubscriptionUpdate(psC, componentsB.getPeerId())
|
|
265
205
|
])
|
|
266
206
|
|
|
267
|
-
|
|
207
|
+
// GossipSub needs time to build the mesh overlay
|
|
208
|
+
await delay(1000)
|
|
209
|
+
|
|
210
|
+
psA.addEventListener('message', incMsg)
|
|
211
|
+
psB.addEventListener('message', incMsg)
|
|
212
|
+
psC.addEventListener('message', incMsg)
|
|
213
|
+
|
|
214
|
+
await psB.publish(topic, uint8ArrayFromString('hey'))
|
|
268
215
|
|
|
269
216
|
function incMsg (evt: CustomEvent<Message>) {
|
|
270
217
|
const msg = evt.detail
|
|
218
|
+
|
|
219
|
+
if (msg.topic !== topic) {
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
|
|
271
223
|
expect(uint8ArrayToString(msg.data)).to.equal('hey')
|
|
272
224
|
check()
|
|
273
225
|
}
|
|
274
226
|
|
|
275
227
|
function check () {
|
|
276
228
|
if (++counter === 3) {
|
|
277
|
-
psA.removeEventListener('message',
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
}
|
|
281
|
-
})
|
|
282
|
-
psB.removeEventListener('message', (evt) => {
|
|
283
|
-
if (evt.detail.topic === topic) {
|
|
284
|
-
incMsg(evt)
|
|
285
|
-
}
|
|
286
|
-
})
|
|
287
|
-
psC.removeEventListener('message', (evt) => {
|
|
288
|
-
if (evt.detail.topic === topic) {
|
|
289
|
-
incMsg(evt)
|
|
290
|
-
}
|
|
291
|
-
})
|
|
229
|
+
psA.removeEventListener('message', incMsg)
|
|
230
|
+
psB.removeEventListener('message', incMsg)
|
|
231
|
+
psC.removeEventListener('message', incMsg)
|
|
292
232
|
defer.resolve()
|
|
293
233
|
}
|
|
294
234
|
}
|
|
@@ -311,72 +251,46 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
311
251
|
let psC: PubSub
|
|
312
252
|
let psD: PubSub
|
|
313
253
|
let psE: PubSub
|
|
314
|
-
let
|
|
315
|
-
let
|
|
316
|
-
let
|
|
317
|
-
let
|
|
318
|
-
let
|
|
319
|
-
let registrarA: Registrar
|
|
320
|
-
let registrarB: Registrar
|
|
321
|
-
let registrarC: Registrar
|
|
322
|
-
let registrarD: Registrar
|
|
323
|
-
let registrarE: Registrar
|
|
254
|
+
let componentsA: Components
|
|
255
|
+
let componentsB: Components
|
|
256
|
+
let componentsC: Components
|
|
257
|
+
let componentsD: Components
|
|
258
|
+
let componentsE: Components
|
|
324
259
|
|
|
325
260
|
// Create and start pubsub nodes
|
|
326
261
|
beforeEach(async () => {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
registrarA = mockRegistrar()
|
|
334
|
-
registrarB = mockRegistrar()
|
|
335
|
-
registrarC = mockRegistrar()
|
|
336
|
-
registrarD = mockRegistrar()
|
|
337
|
-
registrarE = mockRegistrar()
|
|
262
|
+
componentsA = await createComponents()
|
|
263
|
+
componentsB = await createComponents()
|
|
264
|
+
componentsC = await createComponents()
|
|
265
|
+
componentsD = await createComponents()
|
|
266
|
+
componentsE = await createComponents()
|
|
338
267
|
|
|
339
268
|
psA = await common.setup({
|
|
340
|
-
components:
|
|
341
|
-
peerId: peerIdA,
|
|
342
|
-
registrar: registrarA
|
|
343
|
-
}),
|
|
269
|
+
components: componentsA,
|
|
344
270
|
init: {
|
|
345
271
|
emitSelf: true
|
|
346
272
|
}
|
|
347
273
|
})
|
|
348
274
|
psB = await common.setup({
|
|
349
|
-
components:
|
|
350
|
-
peerId: peerIdB,
|
|
351
|
-
registrar: registrarB
|
|
352
|
-
}),
|
|
275
|
+
components: componentsB,
|
|
353
276
|
init: {
|
|
354
277
|
emitSelf: true
|
|
355
278
|
}
|
|
356
279
|
})
|
|
357
280
|
psC = await common.setup({
|
|
358
|
-
components:
|
|
359
|
-
peerId: peerIdC,
|
|
360
|
-
registrar: registrarC
|
|
361
|
-
}),
|
|
281
|
+
components: componentsC,
|
|
362
282
|
init: {
|
|
363
283
|
emitSelf: true
|
|
364
284
|
}
|
|
365
285
|
})
|
|
366
286
|
psD = await common.setup({
|
|
367
|
-
components:
|
|
368
|
-
peerId: peerIdD,
|
|
369
|
-
registrar: registrarD
|
|
370
|
-
}),
|
|
287
|
+
components: componentsD,
|
|
371
288
|
init: {
|
|
372
289
|
emitSelf: true
|
|
373
290
|
}
|
|
374
291
|
})
|
|
375
292
|
psE = await common.setup({
|
|
376
|
-
components:
|
|
377
|
-
peerId: peerIdE,
|
|
378
|
-
registrar: registrarE
|
|
379
|
-
}),
|
|
293
|
+
components: componentsE,
|
|
380
294
|
init: {
|
|
381
295
|
emitSelf: true
|
|
382
296
|
}
|
|
@@ -388,34 +302,10 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
388
302
|
|
|
389
303
|
// connect nodes
|
|
390
304
|
beforeEach(async () => {
|
|
391
|
-
await connectPeers(psA.multicodecs[0],
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
peerId: peerIdB,
|
|
396
|
-
registrar: registrarB
|
|
397
|
-
})
|
|
398
|
-
await connectPeers(psA.multicodecs[0], {
|
|
399
|
-
peerId: peerIdB,
|
|
400
|
-
registrar: registrarB
|
|
401
|
-
}, {
|
|
402
|
-
peerId: peerIdC,
|
|
403
|
-
registrar: registrarC
|
|
404
|
-
})
|
|
405
|
-
await connectPeers(psA.multicodecs[0], {
|
|
406
|
-
peerId: peerIdC,
|
|
407
|
-
registrar: registrarC
|
|
408
|
-
}, {
|
|
409
|
-
peerId: peerIdD,
|
|
410
|
-
registrar: registrarD
|
|
411
|
-
})
|
|
412
|
-
await connectPeers(psA.multicodecs[0], {
|
|
413
|
-
peerId: peerIdD,
|
|
414
|
-
registrar: registrarD
|
|
415
|
-
}, {
|
|
416
|
-
peerId: peerIdE,
|
|
417
|
-
registrar: registrarE
|
|
418
|
-
})
|
|
305
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
306
|
+
await connectPeers(psA.multicodecs[0], componentsB, componentsC)
|
|
307
|
+
await connectPeers(psA.multicodecs[0], componentsC, componentsD)
|
|
308
|
+
await connectPeers(psA.multicodecs[0], componentsD, componentsE)
|
|
419
309
|
|
|
420
310
|
// Wait for peers to be ready in pubsub
|
|
421
311
|
await pWaitFor(() =>
|
|
@@ -451,48 +341,36 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
451
341
|
const topic = 'Z'
|
|
452
342
|
|
|
453
343
|
psA.subscribe(topic)
|
|
454
|
-
psA.addEventListener('message',
|
|
455
|
-
if (evt.detail.topic === topic) {
|
|
456
|
-
incMsg(evt)
|
|
457
|
-
}
|
|
458
|
-
})
|
|
344
|
+
psA.addEventListener('message', incMsg)
|
|
459
345
|
psB.subscribe(topic)
|
|
460
|
-
psB.addEventListener('message',
|
|
461
|
-
if (evt.detail.topic === topic) {
|
|
462
|
-
incMsg(evt)
|
|
463
|
-
}
|
|
464
|
-
})
|
|
346
|
+
psB.addEventListener('message', incMsg)
|
|
465
347
|
psC.subscribe(topic)
|
|
466
|
-
psC.addEventListener('message',
|
|
467
|
-
if (evt.detail.topic === topic) {
|
|
468
|
-
incMsg(evt)
|
|
469
|
-
}
|
|
470
|
-
})
|
|
348
|
+
psC.addEventListener('message', incMsg)
|
|
471
349
|
psD.subscribe(topic)
|
|
472
|
-
psD.addEventListener('message',
|
|
473
|
-
if (evt.detail.topic === topic) {
|
|
474
|
-
incMsg(evt)
|
|
475
|
-
}
|
|
476
|
-
})
|
|
350
|
+
psD.addEventListener('message', incMsg)
|
|
477
351
|
psE.subscribe(topic)
|
|
478
|
-
psE.addEventListener('message',
|
|
479
|
-
if (evt.detail.topic === topic) {
|
|
480
|
-
incMsg(evt)
|
|
481
|
-
}
|
|
482
|
-
})
|
|
352
|
+
psE.addEventListener('message', incMsg)
|
|
483
353
|
|
|
484
354
|
await Promise.all([
|
|
485
|
-
waitForSubscriptionUpdate(psA,
|
|
486
|
-
waitForSubscriptionUpdate(psB,
|
|
487
|
-
waitForSubscriptionUpdate(psC,
|
|
488
|
-
waitForSubscriptionUpdate(psD,
|
|
489
|
-
waitForSubscriptionUpdate(psE,
|
|
355
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
356
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId()),
|
|
357
|
+
waitForSubscriptionUpdate(psC, componentsB.getPeerId()),
|
|
358
|
+
waitForSubscriptionUpdate(psD, componentsC.getPeerId()),
|
|
359
|
+
waitForSubscriptionUpdate(psE, componentsD.getPeerId())
|
|
490
360
|
])
|
|
491
361
|
|
|
492
|
-
|
|
362
|
+
// GossipSub needs time to build the mesh overlay
|
|
363
|
+
await delay(1000)
|
|
364
|
+
|
|
365
|
+
await psC.publish('Z', uint8ArrayFromString('hey from c'))
|
|
493
366
|
|
|
494
367
|
function incMsg (evt: CustomEvent<Message>) {
|
|
495
368
|
const msg = evt.detail
|
|
369
|
+
|
|
370
|
+
if (msg.topic !== topic) {
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
|
|
496
374
|
expect(uint8ArrayToString(msg.data)).to.equal('hey from c')
|
|
497
375
|
check()
|
|
498
376
|
}
|
package/src/pubsub/two-nodes.ts
CHANGED
|
@@ -5,15 +5,12 @@ import pDefer from 'p-defer'
|
|
|
5
5
|
import pWaitFor from 'p-wait-for'
|
|
6
6
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
7
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
|
-
import { connectPeers
|
|
9
|
-
import {
|
|
10
|
-
import { waitForSubscriptionUpdate } from './utils.js'
|
|
8
|
+
import { connectPeers } from '../mocks/registrar.js'
|
|
9
|
+
import { createComponents, waitForSubscriptionUpdate } from './utils.js'
|
|
11
10
|
import type { TestSetup } from '../index.js'
|
|
12
11
|
import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
|
|
13
12
|
import type { PubSubArgs } from './index.js'
|
|
14
|
-
import type {
|
|
15
|
-
import type { Registrar } from '@libp2p/interfaces/registrar'
|
|
16
|
-
import { Components } from '@libp2p/interfaces/components'
|
|
13
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
17
14
|
import { start, stop } from '../index.js'
|
|
18
15
|
|
|
19
16
|
const topic = 'foo'
|
|
@@ -26,33 +23,22 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
26
23
|
describe('pubsub with two nodes', () => {
|
|
27
24
|
let psA: PubSub
|
|
28
25
|
let psB: PubSub
|
|
29
|
-
let
|
|
30
|
-
let
|
|
31
|
-
let registrarA: Registrar
|
|
32
|
-
let registrarB: Registrar
|
|
26
|
+
let componentsA: Components
|
|
27
|
+
let componentsB: Components
|
|
33
28
|
|
|
34
29
|
// Create pubsub nodes and connect them
|
|
35
30
|
beforeEach(async () => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
registrarA = mockRegistrar()
|
|
40
|
-
registrarB = mockRegistrar()
|
|
31
|
+
componentsA = await createComponents()
|
|
32
|
+
componentsB = await createComponents()
|
|
41
33
|
|
|
42
34
|
psA = await common.setup({
|
|
43
|
-
components:
|
|
44
|
-
peerId: peerIdA,
|
|
45
|
-
registrar: registrarA
|
|
46
|
-
}),
|
|
35
|
+
components: componentsA,
|
|
47
36
|
init: {
|
|
48
37
|
emitSelf: true
|
|
49
38
|
}
|
|
50
39
|
})
|
|
51
40
|
psB = await common.setup({
|
|
52
|
-
components:
|
|
53
|
-
peerId: peerIdB,
|
|
54
|
-
registrar: registrarB
|
|
55
|
-
}),
|
|
41
|
+
components: componentsB,
|
|
56
42
|
init: {
|
|
57
43
|
emitSelf: false
|
|
58
44
|
}
|
|
@@ -64,13 +50,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
64
50
|
expect(psA.getPeers()).to.be.empty()
|
|
65
51
|
expect(psB.getPeers()).to.be.empty()
|
|
66
52
|
|
|
67
|
-
await connectPeers(psA.multicodecs[0],
|
|
68
|
-
peerId: peerIdA,
|
|
69
|
-
registrar: registrarA
|
|
70
|
-
}, {
|
|
71
|
-
peerId: peerIdB,
|
|
72
|
-
registrar: registrarB
|
|
73
|
-
})
|
|
53
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
74
54
|
|
|
75
55
|
// Wait for peers to be ready in pubsub
|
|
76
56
|
await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
|
|
@@ -91,7 +71,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
91
71
|
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
92
72
|
expect(psA.getTopics()).to.deep.equal([topic])
|
|
93
73
|
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
94
|
-
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([
|
|
74
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
|
|
95
75
|
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
96
76
|
expect(changedSubs).to.have.lengthOf(1)
|
|
97
77
|
expect(changedSubs[0].topic).to.equal(topic)
|
|
@@ -123,11 +103,11 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
123
103
|
psB.subscribe(topic)
|
|
124
104
|
|
|
125
105
|
await Promise.all([
|
|
126
|
-
waitForSubscriptionUpdate(psA,
|
|
127
|
-
waitForSubscriptionUpdate(psB,
|
|
106
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
107
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
128
108
|
])
|
|
129
109
|
|
|
130
|
-
psA.publish(topic, uint8ArrayFromString('hey'))
|
|
110
|
+
await psA.publish(topic, uint8ArrayFromString('hey'))
|
|
131
111
|
|
|
132
112
|
return await defer.promise
|
|
133
113
|
})
|
|
@@ -166,11 +146,11 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
166
146
|
psB.subscribe(topic)
|
|
167
147
|
|
|
168
148
|
await Promise.all([
|
|
169
|
-
waitForSubscriptionUpdate(psA,
|
|
170
|
-
waitForSubscriptionUpdate(psB,
|
|
149
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
150
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
171
151
|
])
|
|
172
152
|
|
|
173
|
-
psB.publish(topic, uint8ArrayFromString('banana'))
|
|
153
|
+
await psB.publish(topic, uint8ArrayFromString('banana'))
|
|
174
154
|
|
|
175
155
|
return await defer.promise
|
|
176
156
|
})
|
|
@@ -185,7 +165,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
185
165
|
function receivedMsg (evt: CustomEvent<Message>) {
|
|
186
166
|
const msg = evt.detail
|
|
187
167
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
188
|
-
expect(msg.from.toString()).to.equal(
|
|
168
|
+
expect(msg.from.toString()).to.equal(componentsB.getPeerId().toString())
|
|
189
169
|
expect(msg.sequenceNumber).to.be.a('BigInt')
|
|
190
170
|
expect(msg.topic).to.be.equal(topic)
|
|
191
171
|
|
|
@@ -201,11 +181,13 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
201
181
|
psB.subscribe(topic)
|
|
202
182
|
|
|
203
183
|
await Promise.all([
|
|
204
|
-
waitForSubscriptionUpdate(psA,
|
|
205
|
-
waitForSubscriptionUpdate(psB,
|
|
184
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
185
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
206
186
|
])
|
|
207
187
|
|
|
208
|
-
|
|
188
|
+
await Promise.all(
|
|
189
|
+
Array.from({ length: 10 }, async (_, i) => await psB.publish(topic, uint8ArrayFromString('banana')))
|
|
190
|
+
)
|
|
209
191
|
|
|
210
192
|
return await defer.promise
|
|
211
193
|
})
|
package/src/pubsub/utils.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { pEvent } from 'p-event'
|
|
2
2
|
import pWaitFor from 'p-wait-for'
|
|
3
|
+
import { Components } from '@libp2p/interfaces/components'
|
|
3
4
|
import type { PubSub, SubscriptionChangeData } from '@libp2p/interfaces/pubsub'
|
|
4
5
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
6
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
7
|
+
import { mockConnectionManager, mockRegistrar } from '../mocks/index.js'
|
|
5
8
|
|
|
6
9
|
export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
|
|
7
10
|
await pWaitFor(async () => {
|
|
@@ -10,3 +13,11 @@ export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
|
|
|
10
13
|
return event.detail.peerId.equals(b)
|
|
11
14
|
})
|
|
12
15
|
}
|
|
16
|
+
|
|
17
|
+
export async function createComponents (): Promise<Components> {
|
|
18
|
+
return new Components({
|
|
19
|
+
peerId: await createEd25519PeerId(),
|
|
20
|
+
registrar: mockRegistrar(),
|
|
21
|
+
connectionManager: mockConnectionManager()
|
|
22
|
+
})
|
|
23
|
+
}
|