@libp2p/interface-compliance-tests 1.1.9 → 1.1.10
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.d.ts +5 -1
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +16 -8
- package/dist/src/mocks/connection.js.map +1 -1
- package/dist/src/mocks/registrar.d.ts +5 -1
- package/dist/src/mocks/registrar.d.ts.map +1 -1
- package/dist/src/mocks/registrar.js +8 -10
- package/dist/src/mocks/registrar.js.map +1 -1
- package/dist/src/pubsub/api.d.ts +3 -2
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +8 -6
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts +3 -2
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +128 -34
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts +3 -2
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +3 -2
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/index.d.ts +5 -2
- package/dist/src/pubsub/index.d.ts.map +1 -1
- package/dist/src/pubsub/index.js.map +1 -1
- package/dist/src/pubsub/messages.d.ts +3 -2
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +16 -17
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +3 -2
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +80 -34
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts +3 -2
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +57 -29
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/package.json +1 -1
- package/src/mocks/connection.ts +22 -8
- package/src/mocks/registrar.ts +12 -10
- package/src/pubsub/api.ts +13 -9
- package/src/pubsub/connection-handlers.ts +148 -43
- package/src/pubsub/emit-self.ts +9 -7
- package/src/pubsub/index.ts +5 -2
- package/src/pubsub/messages.ts +24 -23
- package/src/pubsub/multiple-nodes.ts +95 -47
- package/src/pubsub/two-nodes.ts +69 -36
|
@@ -4,18 +4,20 @@ import pDefer from 'p-defer'
|
|
|
4
4
|
import pWaitFor from 'p-wait-for'
|
|
5
5
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
8
|
+
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
9
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
7
10
|
import type { TestSetup } from '../index.js'
|
|
8
|
-
import type {
|
|
11
|
+
import type { Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
9
12
|
import type { EventMap } from './index.js'
|
|
10
13
|
import type { PeerId } from '@libp2p/interfaces/src/peer-id'
|
|
11
|
-
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
12
14
|
import type { Registrar } from '@libp2p/interfaces/src/registrar'
|
|
13
|
-
import {
|
|
15
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
14
16
|
|
|
15
|
-
export default (common: TestSetup<
|
|
17
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
16
18
|
describe('pubsub connection handlers', () => {
|
|
17
|
-
let psA:
|
|
18
|
-
let psB:
|
|
19
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
20
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
19
21
|
let peerA: PeerId
|
|
20
22
|
let peerB: PeerId
|
|
21
23
|
let registrarA: Registrar
|
|
@@ -64,9 +66,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
64
66
|
})
|
|
65
67
|
|
|
66
68
|
it('existing subscriptions are sent upon peer connection', async function () {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
await Promise.all([
|
|
69
|
+
const subscriptionsChanged = Promise.all([
|
|
70
70
|
new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
|
|
71
71
|
once: true
|
|
72
72
|
})),
|
|
@@ -75,22 +75,51 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
75
75
|
}))
|
|
76
76
|
])
|
|
77
77
|
|
|
78
|
+
await connectPeers(psA.multicodecs[0], {
|
|
79
|
+
peerId: peerA,
|
|
80
|
+
registrar: registrarA
|
|
81
|
+
}, {
|
|
82
|
+
peerId: peerB,
|
|
83
|
+
registrar: registrarB
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
await subscriptionsChanged
|
|
87
|
+
|
|
78
88
|
expect(psA.getPeers()).to.have.lengthOf(1)
|
|
79
89
|
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
80
90
|
|
|
81
91
|
expect(psA.getTopics()).to.deep.equal(['Za'])
|
|
82
92
|
expect(psB.getTopics()).to.deep.equal(['Zb'])
|
|
83
93
|
|
|
84
|
-
expect(psA.getSubscribers('Zb')).to.deep.equal([peerB])
|
|
85
|
-
expect(psB.getSubscribers('Za')).to.deep.equal([peerA])
|
|
94
|
+
expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([peerB.toString()])
|
|
95
|
+
expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([peerA.toString()])
|
|
86
96
|
})
|
|
87
97
|
})
|
|
88
98
|
|
|
89
99
|
describe('pubsub started before connect', () => {
|
|
100
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
101
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
102
|
+
let peerA: PeerId
|
|
103
|
+
let peerB: PeerId
|
|
104
|
+
let registrarA: Registrar
|
|
105
|
+
let registrarB: Registrar
|
|
106
|
+
|
|
90
107
|
// Create pubsub nodes and start them
|
|
91
108
|
beforeEach(async () => {
|
|
92
|
-
|
|
93
|
-
|
|
109
|
+
peerA = await createEd25519PeerId()
|
|
110
|
+
peerB = await createEd25519PeerId()
|
|
111
|
+
|
|
112
|
+
registrarA = mockRegistrar()
|
|
113
|
+
registrarB = mockRegistrar()
|
|
114
|
+
|
|
115
|
+
psA = await common.setup({
|
|
116
|
+
peerId: peerA,
|
|
117
|
+
registrar: registrarA
|
|
118
|
+
})
|
|
119
|
+
psB = await common.setup({
|
|
120
|
+
peerId: peerB,
|
|
121
|
+
registrar: registrarB
|
|
122
|
+
})
|
|
94
123
|
|
|
95
124
|
await psA.start()
|
|
96
125
|
await psB.start()
|
|
@@ -103,7 +132,13 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
103
132
|
})
|
|
104
133
|
|
|
105
134
|
it('should get notified of connected peers on dial', async () => {
|
|
106
|
-
await connectPeers(psA.multicodecs[0],
|
|
135
|
+
await connectPeers(psA.multicodecs[0], {
|
|
136
|
+
peerId: peerA,
|
|
137
|
+
registrar: registrarA
|
|
138
|
+
}, {
|
|
139
|
+
peerId: peerB,
|
|
140
|
+
registrar: registrarB
|
|
141
|
+
})
|
|
107
142
|
|
|
108
143
|
return await Promise.all([
|
|
109
144
|
pWaitFor(() => psA.getPeers().length === 1),
|
|
@@ -116,7 +151,13 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
116
151
|
const topic = 'test-topic'
|
|
117
152
|
const data = uint8ArrayFromString('hey!')
|
|
118
153
|
|
|
119
|
-
await connectPeers(psA.multicodecs[0],
|
|
154
|
+
await connectPeers(psA.multicodecs[0], {
|
|
155
|
+
peerId: peerA,
|
|
156
|
+
registrar: registrarA
|
|
157
|
+
}, {
|
|
158
|
+
peerId: peerB,
|
|
159
|
+
registrar: registrarB
|
|
160
|
+
})
|
|
120
161
|
|
|
121
162
|
let subscribedTopics = psA.getTopics()
|
|
122
163
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -134,19 +175,38 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
134
175
|
// wait for psB to know about psA subscription
|
|
135
176
|
await pWaitFor(() => {
|
|
136
177
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
137
|
-
return subscribedPeers.includes(peerA)
|
|
178
|
+
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
138
179
|
})
|
|
139
|
-
void psB.
|
|
180
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
140
181
|
|
|
141
182
|
await defer.promise
|
|
142
183
|
})
|
|
143
184
|
})
|
|
144
185
|
|
|
145
186
|
describe('pubsub started after connect', () => {
|
|
187
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
188
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
189
|
+
let peerA: PeerId
|
|
190
|
+
let peerB: PeerId
|
|
191
|
+
let registrarA: Registrar
|
|
192
|
+
let registrarB: Registrar
|
|
193
|
+
|
|
146
194
|
// Create pubsub nodes
|
|
147
195
|
beforeEach(async () => {
|
|
148
|
-
|
|
149
|
-
|
|
196
|
+
peerA = await createEd25519PeerId()
|
|
197
|
+
peerB = await createEd25519PeerId()
|
|
198
|
+
|
|
199
|
+
registrarA = mockRegistrar()
|
|
200
|
+
registrarB = mockRegistrar()
|
|
201
|
+
|
|
202
|
+
psA = await common.setup({
|
|
203
|
+
peerId: peerA,
|
|
204
|
+
registrar: registrarA
|
|
205
|
+
})
|
|
206
|
+
psB = await common.setup({
|
|
207
|
+
peerId: peerB,
|
|
208
|
+
registrar: registrarB
|
|
209
|
+
})
|
|
150
210
|
})
|
|
151
211
|
|
|
152
212
|
afterEach(async () => {
|
|
@@ -159,15 +219,17 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
159
219
|
})
|
|
160
220
|
|
|
161
221
|
it('should get notified of connected peers after starting', async () => {
|
|
162
|
-
// @ts-expect-error protected fields
|
|
163
|
-
const connection = await psA._libp2p.dial(psB.peerId)
|
|
164
|
-
expect(connection).to.exist()
|
|
165
|
-
expect(psA.getPeers()).to.be.empty()
|
|
166
|
-
expect(psB.getPeers()).to.be.empty()
|
|
167
|
-
|
|
168
222
|
await psA.start()
|
|
169
223
|
await psB.start()
|
|
170
224
|
|
|
225
|
+
await connectPeers(psA.multicodecs[0], {
|
|
226
|
+
peerId: peerA,
|
|
227
|
+
registrar: registrarA
|
|
228
|
+
}, {
|
|
229
|
+
peerId: peerB,
|
|
230
|
+
registrar: registrarB
|
|
231
|
+
})
|
|
232
|
+
|
|
171
233
|
return await Promise.all([
|
|
172
234
|
pWaitFor(() => psA.getPeers().length === 1),
|
|
173
235
|
pWaitFor(() => psB.getPeers().length === 1)
|
|
@@ -179,11 +241,17 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
179
241
|
const topic = 'test-topic'
|
|
180
242
|
const data = uint8ArrayFromString('hey!')
|
|
181
243
|
|
|
182
|
-
await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerA, peerB)
|
|
183
|
-
|
|
184
244
|
await psA.start()
|
|
185
245
|
await psB.start()
|
|
186
246
|
|
|
247
|
+
await connectPeers(psA.multicodecs[0], {
|
|
248
|
+
peerId: peerA,
|
|
249
|
+
registrar: registrarA
|
|
250
|
+
}, {
|
|
251
|
+
peerId: peerB,
|
|
252
|
+
registrar: registrarB
|
|
253
|
+
})
|
|
254
|
+
|
|
187
255
|
await Promise.all([
|
|
188
256
|
pWaitFor(() => psA.getPeers().length === 1),
|
|
189
257
|
pWaitFor(() => psB.getPeers().length === 1)
|
|
@@ -205,19 +273,38 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
205
273
|
// wait for psB to know about psA subscription
|
|
206
274
|
await pWaitFor(() => {
|
|
207
275
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
208
|
-
return subscribedPeers.includes(peerA)
|
|
276
|
+
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
209
277
|
})
|
|
210
|
-
void psB.
|
|
278
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
211
279
|
|
|
212
280
|
await defer.promise
|
|
213
281
|
})
|
|
214
282
|
})
|
|
215
283
|
|
|
216
284
|
describe('pubsub with intermittent connections', () => {
|
|
285
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
286
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
287
|
+
let peerA: PeerId
|
|
288
|
+
let peerB: PeerId
|
|
289
|
+
let registrarA: Registrar
|
|
290
|
+
let registrarB: Registrar
|
|
291
|
+
|
|
217
292
|
// Create pubsub nodes and start them
|
|
218
293
|
beforeEach(async () => {
|
|
219
|
-
|
|
220
|
-
|
|
294
|
+
peerA = await createEd25519PeerId()
|
|
295
|
+
peerB = await createEd25519PeerId()
|
|
296
|
+
|
|
297
|
+
registrarA = mockRegistrar()
|
|
298
|
+
registrarB = mockRegistrar()
|
|
299
|
+
|
|
300
|
+
psA = await common.setup({
|
|
301
|
+
peerId: peerA,
|
|
302
|
+
registrar: registrarA
|
|
303
|
+
})
|
|
304
|
+
psB = await common.setup({
|
|
305
|
+
peerId: peerB,
|
|
306
|
+
registrar: registrarB
|
|
307
|
+
})
|
|
221
308
|
|
|
222
309
|
await psA.start()
|
|
223
310
|
await psB.start()
|
|
@@ -240,7 +327,13 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
240
327
|
const defer1 = pDefer()
|
|
241
328
|
const defer2 = pDefer()
|
|
242
329
|
|
|
243
|
-
await connectPeers(psA.multicodecs[0],
|
|
330
|
+
await connectPeers(psA.multicodecs[0], {
|
|
331
|
+
peerId: peerA,
|
|
332
|
+
registrar: registrarA
|
|
333
|
+
}, {
|
|
334
|
+
peerId: peerB,
|
|
335
|
+
registrar: registrarB
|
|
336
|
+
})
|
|
244
337
|
|
|
245
338
|
let subscribedTopics = psA.getTopics()
|
|
246
339
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -259,9 +352,9 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
259
352
|
// wait for psB to know about psA subscription
|
|
260
353
|
await pWaitFor(() => {
|
|
261
354
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
262
|
-
return subscribedPeers.includes(peerA)
|
|
355
|
+
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
263
356
|
})
|
|
264
|
-
void psB.
|
|
357
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
265
358
|
|
|
266
359
|
await defer1.promise
|
|
267
360
|
|
|
@@ -280,15 +373,21 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
280
373
|
await psB._libp2p.start()
|
|
281
374
|
await psB.start()
|
|
282
375
|
|
|
283
|
-
await connectPeers(psA.multicodecs[0],
|
|
376
|
+
await connectPeers(psA.multicodecs[0], {
|
|
377
|
+
peerId: peerA,
|
|
378
|
+
registrar: registrarA
|
|
379
|
+
}, {
|
|
380
|
+
peerId: peerB,
|
|
381
|
+
registrar: registrarB
|
|
382
|
+
})
|
|
284
383
|
|
|
285
384
|
// wait for remoteLibp2p to know about libp2p subscription
|
|
286
385
|
await pWaitFor(() => {
|
|
287
386
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
288
|
-
return subscribedPeers.includes(peerA)
|
|
387
|
+
return subscribedPeers.toString().includes(peerA.toString())
|
|
289
388
|
})
|
|
290
389
|
|
|
291
|
-
void psB.
|
|
390
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
292
391
|
|
|
293
392
|
await defer2.promise
|
|
294
393
|
})
|
|
@@ -337,17 +436,23 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
337
436
|
const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
|
|
338
437
|
|
|
339
438
|
// second connection
|
|
340
|
-
await connectPeers(psA.multicodecs[0],
|
|
439
|
+
await connectPeers(psA.multicodecs[0], {
|
|
440
|
+
peerId: peerA,
|
|
441
|
+
registrar: registrarA
|
|
442
|
+
}, {
|
|
443
|
+
peerId: peerB,
|
|
444
|
+
registrar: registrarB
|
|
445
|
+
})
|
|
341
446
|
|
|
342
447
|
// Wait for subscriptions to occur
|
|
343
448
|
await pWaitFor(() => {
|
|
344
449
|
return psA.getSubscribers(topic).includes(peerB) &&
|
|
345
|
-
psB.getSubscribers(topic).includes(peerA)
|
|
450
|
+
psB.getSubscribers(topic).map(p => p.toString()).includes(peerA.toString())
|
|
346
451
|
})
|
|
347
452
|
|
|
348
453
|
// Verify messages go both ways
|
|
349
|
-
void psA.
|
|
350
|
-
void psB.
|
|
454
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-a-1') }))
|
|
455
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-b-1') }))
|
|
351
456
|
await pWaitFor(() => {
|
|
352
457
|
return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
|
|
353
458
|
})
|
|
@@ -360,8 +465,8 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
360
465
|
await pWaitFor(() => psAConnUpdateSpy.callCount === 1)
|
|
361
466
|
|
|
362
467
|
// Verify messages go both ways after the disconnect
|
|
363
|
-
void psA.
|
|
364
|
-
void psB.
|
|
468
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-a-2') }))
|
|
469
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-b-2') }))
|
|
365
470
|
await pWaitFor(() => {
|
|
366
471
|
return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
|
|
367
472
|
})
|
package/src/pubsub/emit-self.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import { expect } from 'aegir/utils/chai.js'
|
|
2
2
|
import sinon from 'sinon'
|
|
3
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
|
-
import type { TestSetup } from '../index.js'
|
|
5
|
-
import type { PubSub, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
6
|
-
import type { EventMap } from './index.js'
|
|
7
4
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
8
5
|
import { mockRegistrar } from '../mocks/registrar.js'
|
|
6
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
7
|
+
import type { TestSetup } from '../index.js'
|
|
8
|
+
import type { PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
9
|
+
import type { EventMap } from './index.js'
|
|
10
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
9
11
|
|
|
10
12
|
const topic = 'foo'
|
|
11
13
|
const data = uint8ArrayFromString('bar')
|
|
12
14
|
const shouldNotHappen = () => expect.fail()
|
|
13
15
|
|
|
14
|
-
export default (common: TestSetup<
|
|
16
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
15
17
|
describe('emit self', () => {
|
|
16
|
-
let pubsub:
|
|
18
|
+
let pubsub: PubsubBaseProtocol<EventMap>
|
|
17
19
|
|
|
18
20
|
describe('enabled', () => {
|
|
19
21
|
before(async () => {
|
|
@@ -40,7 +42,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
40
42
|
once: true
|
|
41
43
|
}))
|
|
42
44
|
|
|
43
|
-
void pubsub.
|
|
45
|
+
void pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
44
46
|
|
|
45
47
|
return await promise
|
|
46
48
|
})
|
|
@@ -71,7 +73,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
71
73
|
once: true
|
|
72
74
|
})
|
|
73
75
|
|
|
74
|
-
void pubsub.
|
|
76
|
+
void pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
75
77
|
|
|
76
78
|
// Wait 1 second to guarantee that self is not noticed
|
|
77
79
|
return await new Promise((resolve) => setTimeout(resolve, 1000))
|
package/src/pubsub/index.ts
CHANGED
|
@@ -5,7 +5,8 @@ import connectionHandlersTest from './connection-handlers.js'
|
|
|
5
5
|
import twoNodesTest from './two-nodes.js'
|
|
6
6
|
import multipleNodesTest from './multiple-nodes.js'
|
|
7
7
|
import type { TestSetup } from '../index.js'
|
|
8
|
-
import type {
|
|
8
|
+
import type { Message, PubSubEvents, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
9
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
9
10
|
|
|
10
11
|
export interface EventMap extends PubSubEvents {
|
|
11
12
|
'topic': CustomEvent<Message>
|
|
@@ -13,9 +14,11 @@ export interface EventMap extends PubSubEvents {
|
|
|
13
14
|
'test-topic': CustomEvent<Message>
|
|
14
15
|
'reconnect-channel': CustomEvent<Message>
|
|
15
16
|
'Z': CustomEvent<Message>
|
|
17
|
+
'Za': CustomEvent<Message>
|
|
18
|
+
'Zb': CustomEvent<Message>
|
|
16
19
|
}
|
|
17
20
|
|
|
18
|
-
export default (common: TestSetup<
|
|
21
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
19
22
|
describe('interface-pubsub compliance tests', () => {
|
|
20
23
|
apiTest(common)
|
|
21
24
|
emitSelfTest(common)
|
package/src/pubsub/messages.ts
CHANGED
|
@@ -4,19 +4,22 @@ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
|
4
4
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
5
5
|
import { noSignMsgId } from '@libp2p/pubsub/utils'
|
|
6
6
|
import { PeerStreams } from '@libp2p/pubsub/peer-streams'
|
|
7
|
-
import type { TestSetup } from '../index.js'
|
|
8
|
-
import type { PubSub, PubSubOptions, RPC } from '@libp2p/interfaces/pubsub'
|
|
9
|
-
import type { EventMap } from './index.js'
|
|
10
7
|
import { mockRegistrar } from '../mocks/registrar.js'
|
|
11
8
|
import pDefer from 'p-defer'
|
|
12
9
|
import delay from 'delay'
|
|
10
|
+
import pWaitFor from 'p-wait-for'
|
|
11
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
12
|
+
import type { TestSetup } from '../index.js'
|
|
13
|
+
import type { PubSubOptions, RPC } from '@libp2p/interfaces/pubsub'
|
|
14
|
+
import type { EventMap } from './index.js'
|
|
15
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
13
16
|
|
|
14
17
|
const topic = 'foo'
|
|
15
18
|
const data = uint8ArrayFromString('bar')
|
|
16
19
|
|
|
17
|
-
export default (common: TestSetup<
|
|
20
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
18
21
|
describe('messages', () => {
|
|
19
|
-
let pubsub:
|
|
22
|
+
let pubsub: PubsubBaseProtocol<EventMap>
|
|
20
23
|
|
|
21
24
|
// Create pubsub router
|
|
22
25
|
beforeEach(async () => {
|
|
@@ -37,23 +40,26 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
37
40
|
it('should emit normalized signed messages on publish', async () => {
|
|
38
41
|
pubsub.globalSignaturePolicy = 'StrictSign'
|
|
39
42
|
|
|
40
|
-
const spy = sinon.spy(pubsub, '
|
|
43
|
+
const spy = sinon.spy(pubsub, 'publishMessage')
|
|
44
|
+
|
|
45
|
+
await pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
41
46
|
|
|
42
|
-
await
|
|
47
|
+
await pWaitFor(async () => {
|
|
48
|
+
return spy.callCount === 1
|
|
49
|
+
})
|
|
43
50
|
|
|
44
51
|
expect(spy).to.have.property('callCount', 1)
|
|
45
52
|
|
|
46
|
-
const [messageToEmit] = spy.getCall(0).args
|
|
53
|
+
const [from, messageToEmit] = spy.getCall(0).args
|
|
47
54
|
|
|
55
|
+
expect(from.toString()).to.equal(pubsub.peerId.toString())
|
|
48
56
|
expect(messageToEmit.seqno).to.not.eql(undefined)
|
|
49
57
|
expect(messageToEmit.key).to.not.eql(undefined)
|
|
50
58
|
expect(messageToEmit.signature).to.not.eql(undefined)
|
|
51
59
|
})
|
|
52
60
|
|
|
53
61
|
it('should drop unsigned messages', async () => {
|
|
54
|
-
const
|
|
55
|
-
// @ts-expect-error protected abstract field
|
|
56
|
-
const publishSpy = sinon.spy(pubsub, '_publish')
|
|
62
|
+
const publishSpy = sinon.spy(pubsub, 'publishMessage')
|
|
57
63
|
sinon.spy(pubsub, 'validate')
|
|
58
64
|
|
|
59
65
|
const peerStream = new PeerStreams({
|
|
@@ -62,11 +68,11 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
62
68
|
})
|
|
63
69
|
const rpc: RPC = {
|
|
64
70
|
subscriptions: [],
|
|
65
|
-
|
|
71
|
+
messages: [{
|
|
66
72
|
from: peerStream.id.toBytes(),
|
|
67
73
|
data,
|
|
68
74
|
seqno: await noSignMsgId(data),
|
|
69
|
-
|
|
75
|
+
topic: topic
|
|
70
76
|
}]
|
|
71
77
|
}
|
|
72
78
|
|
|
@@ -77,17 +83,13 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
77
83
|
// message should not be delivered
|
|
78
84
|
await delay(1000)
|
|
79
85
|
|
|
80
|
-
expect(pubsub.validate).to.have.property('callCount', 1)
|
|
81
|
-
expect(emitMessageSpy).to.have.property('called', false)
|
|
82
86
|
expect(publishSpy).to.have.property('called', false)
|
|
83
87
|
})
|
|
84
88
|
|
|
85
89
|
it('should not drop unsigned messages if strict signing is disabled', async () => {
|
|
86
90
|
pubsub.globalSignaturePolicy = 'StrictNoSign'
|
|
87
91
|
|
|
88
|
-
const
|
|
89
|
-
// @ts-expect-error protected field
|
|
90
|
-
const publishSpy = sinon.spy(pubsub, '_publish')
|
|
92
|
+
const publishSpy = sinon.spy(pubsub, 'publishMessage')
|
|
91
93
|
sinon.spy(pubsub, 'validate')
|
|
92
94
|
|
|
93
95
|
const peerStream = new PeerStreams({
|
|
@@ -97,10 +99,10 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
97
99
|
|
|
98
100
|
const rpc: RPC = {
|
|
99
101
|
subscriptions: [],
|
|
100
|
-
|
|
102
|
+
messages: [{
|
|
101
103
|
from: peerStream.id.toBytes(),
|
|
102
104
|
data,
|
|
103
|
-
|
|
105
|
+
topic
|
|
104
106
|
}]
|
|
105
107
|
}
|
|
106
108
|
|
|
@@ -108,17 +110,16 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
108
110
|
|
|
109
111
|
const deferred = pDefer()
|
|
110
112
|
|
|
111
|
-
await pubsub.processRpc(peerStream.id, peerStream, rpc)
|
|
112
|
-
|
|
113
113
|
pubsub.addEventListener(topic, () => {
|
|
114
114
|
deferred.resolve()
|
|
115
115
|
})
|
|
116
116
|
|
|
117
|
+
await pubsub.processRpc(peerStream.id, peerStream, rpc)
|
|
118
|
+
|
|
117
119
|
// await message delivery
|
|
118
120
|
await deferred.promise
|
|
119
121
|
|
|
120
122
|
expect(pubsub.validate).to.have.property('callCount', 1)
|
|
121
|
-
expect(emitMessageSpy).to.have.property('callCount', 1)
|
|
122
123
|
expect(publishSpy).to.have.property('callCount', 1)
|
|
123
124
|
})
|
|
124
125
|
})
|