@libp2p/interface-compliance-tests 1.1.6 → 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.
Files changed (92) hide show
  1. package/dist/src/mocks/connection.d.ts +13 -2
  2. package/dist/src/mocks/connection.d.ts.map +1 -1
  3. package/dist/src/mocks/connection.js +56 -24
  4. package/dist/src/mocks/connection.js.map +1 -1
  5. package/dist/src/mocks/multiaddr-connection.d.ts +2 -1
  6. package/dist/src/mocks/multiaddr-connection.d.ts.map +1 -1
  7. package/dist/src/mocks/multiaddr-connection.js +2 -2
  8. package/dist/src/mocks/multiaddr-connection.js.map +1 -1
  9. package/dist/src/mocks/muxer.d.ts +2 -2
  10. package/dist/src/mocks/muxer.d.ts.map +1 -1
  11. package/dist/src/mocks/muxer.js +175 -26
  12. package/dist/src/mocks/muxer.js.map +1 -1
  13. package/dist/src/mocks/registrar.d.ts +11 -2
  14. package/dist/src/mocks/registrar.d.ts.map +1 -1
  15. package/dist/src/mocks/registrar.js +47 -5
  16. package/dist/src/mocks/registrar.js.map +1 -1
  17. package/dist/src/mocks/upgrader.d.ts.map +1 -1
  18. package/dist/src/mocks/upgrader.js +8 -4
  19. package/dist/src/mocks/upgrader.js.map +1 -1
  20. package/dist/src/pubsub/api.d.ts +3 -2
  21. package/dist/src/pubsub/api.d.ts.map +1 -1
  22. package/dist/src/pubsub/api.js +23 -13
  23. package/dist/src/pubsub/api.js.map +1 -1
  24. package/dist/src/pubsub/connection-handlers.d.ts +3 -2
  25. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  26. package/dist/src/pubsub/connection-handlers.js +165 -69
  27. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  28. package/dist/src/pubsub/emit-self.d.ts +3 -2
  29. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  30. package/dist/src/pubsub/emit-self.js +15 -4
  31. package/dist/src/pubsub/emit-self.js.map +1 -1
  32. package/dist/src/pubsub/index.d.ts +6 -3
  33. package/dist/src/pubsub/index.d.ts.map +1 -1
  34. package/dist/src/pubsub/index.js.map +1 -1
  35. package/dist/src/pubsub/messages.d.ts +3 -2
  36. package/dist/src/pubsub/messages.d.ts.map +1 -1
  37. package/dist/src/pubsub/messages.js +42 -39
  38. package/dist/src/pubsub/messages.js.map +1 -1
  39. package/dist/src/pubsub/multiple-nodes.d.ts +3 -2
  40. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  41. package/dist/src/pubsub/multiple-nodes.js +163 -65
  42. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  43. package/dist/src/pubsub/two-nodes.d.ts +3 -2
  44. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  45. package/dist/src/pubsub/two-nodes.js +80 -38
  46. package/dist/src/pubsub/two-nodes.js.map +1 -1
  47. package/dist/src/stream-muxer/base-test.js +1 -1
  48. package/dist/src/stream-muxer/base-test.js.map +1 -1
  49. package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
  50. package/dist/src/stream-muxer/close-test.js +12 -10
  51. package/dist/src/stream-muxer/close-test.js.map +1 -1
  52. package/dist/src/transport/dial-test.d.ts.map +1 -1
  53. package/dist/src/transport/dial-test.js +2 -1
  54. package/dist/src/transport/dial-test.js.map +1 -1
  55. package/dist/src/transport/filter-test.js +1 -1
  56. package/dist/src/transport/filter-test.js.map +1 -1
  57. package/dist/src/transport/listen-test.d.ts.map +1 -1
  58. package/dist/src/transport/listen-test.js +2 -1
  59. package/dist/src/transport/listen-test.js.map +1 -1
  60. package/dist/src/utils/is-valid-tick.d.ts +6 -0
  61. package/dist/src/utils/is-valid-tick.d.ts.map +1 -0
  62. package/dist/src/utils/is-valid-tick.js +15 -0
  63. package/dist/src/utils/is-valid-tick.js.map +1 -0
  64. package/package.json +7 -5
  65. package/src/mocks/connection.ts +80 -24
  66. package/src/mocks/multiaddr-connection.ts +3 -2
  67. package/src/mocks/muxer.ts +230 -28
  68. package/src/mocks/registrar.ts +65 -7
  69. package/src/mocks/upgrader.ts +8 -5
  70. package/src/pubsub/api.ts +29 -15
  71. package/src/pubsub/connection-handlers.ts +186 -75
  72. package/src/pubsub/emit-self.ts +19 -7
  73. package/src/pubsub/index.ts +6 -3
  74. package/src/pubsub/messages.ts +59 -44
  75. package/src/pubsub/multiple-nodes.ts +189 -76
  76. package/src/pubsub/two-nodes.ts +94 -46
  77. package/src/stream-muxer/base-test.ts +1 -1
  78. package/src/stream-muxer/close-test.ts +13 -12
  79. package/src/transport/dial-test.ts +2 -1
  80. package/src/transport/filter-test.ts +1 -1
  81. package/src/transport/listen-test.ts +2 -1
  82. package/src/utils/is-valid-tick.ts +18 -0
  83. package/dist/src/pubsub/utils.d.ts +0 -3
  84. package/dist/src/pubsub/utils.d.ts.map +0 -1
  85. package/dist/src/pubsub/utils.js +0 -11
  86. package/dist/src/pubsub/utils.js.map +0 -1
  87. package/dist/src/transport/utils/index.d.ts +0 -15
  88. package/dist/src/transport/utils/index.d.ts.map +0 -1
  89. package/dist/src/transport/utils/index.js +0 -137
  90. package/dist/src/transport/utils/index.js.map +0 -1
  91. package/src/pubsub/utils.ts +0 -13
  92. package/src/transport/utils/index.ts +0 -172
@@ -1,23 +1,33 @@
1
1
  import { expect } from 'aegir/utils/chai.js'
2
2
  import sinon from 'sinon'
3
- import * as PeerIdFactory from '@libp2p/peer-id-factory'
3
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
4
4
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
5
- import * as utils from '@libp2p/pubsub/utils'
5
+ import { noSignMsgId } from '@libp2p/pubsub/utils'
6
6
  import { PeerStreams } from '@libp2p/pubsub/peer-streams'
7
+ import { mockRegistrar } from '../mocks/registrar.js'
8
+ import pDefer from 'p-defer'
9
+ import delay from 'delay'
10
+ import pWaitFor from 'p-wait-for'
11
+ import { CustomEvent } from '@libp2p/interfaces'
7
12
  import type { TestSetup } from '../index.js'
8
- import type { PubSub } from '@libp2p/interfaces/pubsub'
13
+ import type { PubSubOptions, RPC } from '@libp2p/interfaces/pubsub'
9
14
  import type { EventMap } from './index.js'
15
+ import type { PubsubBaseProtocol } from '@libp2p/pubsub'
10
16
 
11
17
  const topic = 'foo'
12
18
  const data = uint8ArrayFromString('bar')
13
19
 
14
- export default (common: TestSetup<PubSub<EventMap>>) => {
20
+ export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
15
21
  describe('messages', () => {
16
- let pubsub: PubSub<EventMap>
22
+ let pubsub: PubsubBaseProtocol<EventMap>
17
23
 
18
24
  // Create pubsub router
19
25
  beforeEach(async () => {
20
- pubsub = await common.setup()
26
+ pubsub = await common.setup({
27
+ peerId: await createEd25519PeerId(),
28
+ registrar: mockRegistrar(),
29
+ emitSelf: true
30
+ })
21
31
  await pubsub.start()
22
32
  })
23
33
 
@@ -29,83 +39,88 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
29
39
 
30
40
  it('should emit normalized signed messages on publish', async () => {
31
41
  pubsub.globalSignaturePolicy = 'StrictSign'
32
- // @ts-expect-error protected field
33
- sinon.spy(pubsub, '_emitMessage')
34
42
 
35
- await pubsub.publish(topic, data)
36
- // @ts-expect-error protected field
37
- expect(pubsub._emitMessage.callCount).to.eql(1)
38
- // @ts-expect-error protected field
39
- const [messageToEmit] = pubsub._emitMessage.getCall(0).args
43
+ const spy = sinon.spy(pubsub, 'publishMessage')
44
+
45
+ await pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
46
+
47
+ await pWaitFor(async () => {
48
+ return spy.callCount === 1
49
+ })
50
+
51
+ expect(spy).to.have.property('callCount', 1)
52
+
53
+ const [from, messageToEmit] = spy.getCall(0).args
40
54
 
55
+ expect(from.toString()).to.equal(pubsub.peerId.toString())
41
56
  expect(messageToEmit.seqno).to.not.eql(undefined)
42
57
  expect(messageToEmit.key).to.not.eql(undefined)
43
58
  expect(messageToEmit.signature).to.not.eql(undefined)
44
59
  })
45
60
 
46
61
  it('should drop unsigned messages', async () => {
47
- // @ts-expect-error protected field
48
- sinon.spy(pubsub, '_emitMessage')
49
- // @ts-expect-error protected field
50
- sinon.spy(pubsub, '_publish')
62
+ const publishSpy = sinon.spy(pubsub, 'publishMessage')
51
63
  sinon.spy(pubsub, 'validate')
52
64
 
53
65
  const peerStream = new PeerStreams({
54
- id: await PeerIdFactory.createEd25519PeerId(),
66
+ id: await createEd25519PeerId(),
55
67
  protocol: 'test'
56
68
  })
57
- const rpc = {
69
+ const rpc: RPC = {
58
70
  subscriptions: [],
59
- msgs: [{
60
- receivedFrom: peerStream.id.toString(),
71
+ messages: [{
61
72
  from: peerStream.id.toBytes(),
62
73
  data,
63
- seqno: utils.randomSeqno(),
64
- topicIDs: [topic]
74
+ seqno: await noSignMsgId(data),
75
+ topic: topic
65
76
  }]
66
77
  }
67
78
 
68
79
  pubsub.subscribe(topic)
69
- // @ts-expect-error protected field
70
- await pubsub._processRpc(peerStream.id.toString(), peerStream, rpc)
71
80
 
72
- expect(pubsub.validate).to.have.property('callCount', 1)
73
- // @ts-expect-error protected field
74
- expect(pubsub._emitMessage).to.have.property('called', false)
75
- // @ts-expect-error protected field
76
- expect(pubsub._publish).to.have.property('called', false)
81
+ await pubsub.processRpc(peerStream.id, peerStream, rpc)
82
+
83
+ // message should not be delivered
84
+ await delay(1000)
85
+
86
+ expect(publishSpy).to.have.property('called', false)
77
87
  })
78
88
 
79
89
  it('should not drop unsigned messages if strict signing is disabled', async () => {
80
90
  pubsub.globalSignaturePolicy = 'StrictNoSign'
81
- // @ts-expect-error protected field
82
- sinon.spy(pubsub, '_emitMessage')
83
- // @ts-expect-error protected field
84
- sinon.spy(pubsub, '_publish')
91
+
92
+ const publishSpy = sinon.spy(pubsub, 'publishMessage')
85
93
  sinon.spy(pubsub, 'validate')
86
94
 
87
95
  const peerStream = new PeerStreams({
88
- id: await PeerIdFactory.createEd25519PeerId(),
96
+ id: await createEd25519PeerId(),
89
97
  protocol: 'test'
90
98
  })
91
99
 
92
- const rpc = {
100
+ const rpc: RPC = {
93
101
  subscriptions: [],
94
- msgs: [{
102
+ messages: [{
103
+ from: peerStream.id.toBytes(),
95
104
  data,
96
- topicIDs: [topic]
105
+ topic
97
106
  }]
98
107
  }
99
108
 
100
109
  pubsub.subscribe(topic)
101
- // @ts-expect-error protected field
102
- await pubsub._processRpc(peerStream.id.toString(), peerStream, rpc)
110
+
111
+ const deferred = pDefer()
112
+
113
+ pubsub.addEventListener(topic, () => {
114
+ deferred.resolve()
115
+ })
116
+
117
+ await pubsub.processRpc(peerStream.id, peerStream, rpc)
118
+
119
+ // await message delivery
120
+ await deferred.promise
103
121
 
104
122
  expect(pubsub.validate).to.have.property('callCount', 1)
105
- // @ts-expect-error protected field
106
- expect(pubsub._emitMessage).to.have.property('called', 1)
107
- // @ts-expect-error protected field
108
- expect(pubsub._publish).to.have.property('called', 1)
123
+ expect(publishSpy).to.have.property('callCount', 1)
109
124
  })
110
125
  })
111
126
  }
@@ -6,51 +6,97 @@ import pDefer from 'p-defer'
6
6
  import pWaitFor from 'p-wait-for'
7
7
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
8
8
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9
- import { expectSet } from './utils.js'
9
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
10
+ import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
11
+ import { CustomEvent } from '@libp2p/interfaces'
10
12
  import type { TestSetup } from '../index.js'
11
- import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
13
+ import type { Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
12
14
  import type { EventMap } from './index.js'
15
+ import type { PeerId } from '@libp2p/interfaces/src/peer-id'
16
+ import type { Registrar } from '@libp2p/interfaces/src/registrar'
17
+ import type { PubsubBaseProtocol } from '@libp2p/pubsub'
13
18
 
14
- export default (common: TestSetup<PubSub<EventMap>>) => {
19
+ export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
15
20
  describe('pubsub with multiple nodes', function () {
16
21
  describe('every peer subscribes to the topic', () => {
17
22
  describe('line', () => {
18
23
  // line
19
24
  // ◉────◉────◉
20
25
  // a b c
21
- let psA: PubSub<EventMap>
22
- let psB: PubSub<EventMap>
23
- let psC: PubSub<EventMap>
26
+ let psA: PubsubBaseProtocol<EventMap>
27
+ let psB: PubsubBaseProtocol<EventMap>
28
+ let psC: PubsubBaseProtocol<EventMap>
29
+ let peerIdA: PeerId
30
+ let peerIdB: PeerId
31
+ let peerIdC: PeerId
32
+ let registrarA: Registrar
33
+ let registrarB: Registrar
34
+ let registrarC: Registrar
24
35
 
25
36
  // Create and start pubsub nodes
26
37
  beforeEach(async () => {
27
- psA = await common.setup()
28
- psB = await common.setup()
29
- psC = await common.setup()
38
+ peerIdA = await createEd25519PeerId()
39
+ peerIdB = await createEd25519PeerId()
40
+ peerIdC = await createEd25519PeerId()
41
+
42
+ registrarA = mockRegistrar()
43
+ registrarB = mockRegistrar()
44
+ registrarC = mockRegistrar()
45
+
46
+ psA = await common.setup({
47
+ peerId: peerIdA,
48
+ registrar: registrarA,
49
+ emitSelf: true
50
+ })
51
+ psB = await common.setup({
52
+ peerId: peerIdB,
53
+ registrar: registrarB,
54
+ emitSelf: true
55
+ })
56
+ psC = await common.setup({
57
+ peerId: peerIdC,
58
+ registrar: registrarC,
59
+ emitSelf: true
60
+ })
30
61
 
31
62
  // Start pubsub modes
32
- ;[psA, psB, psC].map((p) => p.start())
63
+ await Promise.all(
64
+ [psA, psB, psC].map(async (p) => await p.start())
65
+ )
33
66
  })
34
67
 
35
68
  // Connect nodes
36
69
  beforeEach(async () => {
37
- // @ts-expect-error protected field
38
- await psA._libp2p.dial(psB.peerId)
39
- // @ts-expect-error protected field
40
- await psB._libp2p.dial(psC.peerId)
70
+ await connectPeers(psA.multicodecs[0], {
71
+ peerId: peerIdA,
72
+ registrar: registrarA
73
+ }, {
74
+ peerId: peerIdB,
75
+ registrar: registrarB
76
+ })
77
+ await connectPeers(psA.multicodecs[0], {
78
+ peerId: peerIdB,
79
+ registrar: registrarB
80
+ }, {
81
+ peerId: peerIdC,
82
+ registrar: registrarC
83
+ })
41
84
 
42
85
  // Wait for peers to be ready in pubsub
43
86
  await pWaitFor(() =>
44
- psA.peers.size === 1 &&
45
- psC.peers.size === 1 &&
46
- psA.peers.size === 1
87
+ psA.getPeers().length === 1 &&
88
+ psC.getPeers().length === 1 &&
89
+ psA.getPeers().length === 1
47
90
  )
48
91
  })
49
92
 
50
93
  afterEach(async () => {
51
94
  sinon.restore()
52
95
 
53
- ;[psA, psB, psC].map((p) => p.stop())
96
+ await Promise.all(
97
+ [psA, psB, psC].map(async (p) => await p.stop())
98
+ )
99
+
54
100
  await common.teardown()
55
101
  })
56
102
 
@@ -58,24 +104,23 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
58
104
  const topic = 'Z'
59
105
 
60
106
  psA.subscribe(topic)
61
- expectSet(psA.subscriptions, [topic])
107
+ expect(psA.getTopics()).to.deep.equal([topic])
62
108
 
63
109
  await new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve, {
64
110
  once: true
65
111
  }))
66
- expect(psB.peers.size).to.equal(2)
112
+ expect(psB.getPeers().length).to.equal(2)
67
113
 
68
- const aPeerId = psA.peerId.toString()
69
- expectSet(psB.topics.get(topic), [aPeerId])
114
+ expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
70
115
 
71
- expect(psC.peers.size).to.equal(1)
72
- expect(psC.topics.get(topic)).to.eql(undefined)
116
+ expect(psC.getPeers().length).to.equal(1)
117
+ expect(psC.getSubscribers(topic)).to.be.empty()
73
118
  })
74
119
 
75
120
  it('subscribe to the topic on node b', async () => {
76
121
  const topic = 'Z'
77
122
  psB.subscribe(topic)
78
- expectSet(psB.subscriptions, [topic])
123
+ expect(psB.getTopics()).to.deep.equal([topic])
79
124
 
80
125
  await Promise.all([
81
126
  new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
@@ -86,11 +131,11 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
86
131
  }))
87
132
  ])
88
133
 
89
- expect(psA.peers.size).to.equal(1)
90
- expectSet(psA.topics.get(topic), [psB.peerId.toString()])
134
+ expect(psA.getPeers().length).to.equal(1)
135
+ expect(psA.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
91
136
 
92
- expect(psC.peers.size).to.equal(1)
93
- expectSet(psC.topics.get(topic), [psB.peerId.toString()])
137
+ expect(psC.getPeers().length).to.equal(1)
138
+ expect(psC.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
94
139
  })
95
140
 
96
141
  it('subscribe to the topic on node c', async () => {
@@ -98,12 +143,12 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
98
143
  const defer = pDefer()
99
144
 
100
145
  psC.subscribe(topic)
101
- expectSet(psC.subscriptions, [topic])
146
+ expect(psC.getTopics()).to.deep.equal([topic])
102
147
 
103
148
  psB.addEventListener('pubsub:subscription-change', () => {
104
- expect(psA.peers.size).to.equal(1)
105
- expect(psB.peers.size).to.equal(2)
106
- expectSet(psB.topics.get(topic), [psC.peerId.toString()])
149
+ expect(psA.getPeers().length).to.equal(1)
150
+ expect(psB.getPeers().length).to.equal(2)
151
+ expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdC.toString()])
107
152
 
108
153
  defer.resolve()
109
154
  }, {
@@ -117,25 +162,24 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
117
162
  const topic = 'Z'
118
163
  const defer = pDefer()
119
164
 
120
- psA.subscribe(topic)
121
- psB.subscribe(topic)
122
- psC.subscribe(topic)
123
-
124
165
  // await subscription change
125
- await Promise.all([
126
- new Promise(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(null), {
166
+ const p = Promise.all([
167
+ new Promise<void>(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(), {
127
168
  once: true
128
169
  })),
129
- new Promise(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(null), {
170
+ new Promise<void>(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(), {
130
171
  once: true
131
172
  })),
132
- new Promise(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(null), {
173
+ new Promise<void>(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(), {
133
174
  once: true
134
175
  }))
135
176
  ])
136
177
 
137
- // await a cycle
138
- await delay(1000)
178
+ psA.subscribe(topic)
179
+ psB.subscribe(topic)
180
+ psC.subscribe(topic)
181
+
182
+ await p
139
183
 
140
184
  let counter = 0
141
185
 
@@ -143,7 +187,10 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
143
187
  psB.addEventListener(topic, incMsg)
144
188
  psC.addEventListener(topic, incMsg)
145
189
 
146
- void psA.publish(topic, uint8ArrayFromString('hey'))
190
+ // await a cycle
191
+ await delay(1000)
192
+
193
+ void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
147
194
 
148
195
  function incMsg (evt: CustomEvent<Message>) {
149
196
  const msg = evt.detail
@@ -201,7 +248,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
201
248
  // await a cycle
202
249
  await delay(1000)
203
250
 
204
- void psB.publish(topic, uint8ArrayFromString('hey'))
251
+ void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
205
252
 
206
253
  function incMsg (evt: CustomEvent<Message>) {
207
254
  const msg = evt.detail
@@ -231,61 +278,127 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
231
278
  // │b d│
232
279
  // ◉─┘ └─◉
233
280
  // a
234
- let psA: PubSub<EventMap>
235
- let psB: PubSub<EventMap>
236
- let psC: PubSub<EventMap>
237
- let psD: PubSub<EventMap>
238
- let psE: PubSub<EventMap>
281
+ let psA: PubsubBaseProtocol<EventMap>
282
+ let psB: PubsubBaseProtocol<EventMap>
283
+ let psC: PubsubBaseProtocol<EventMap>
284
+ let psD: PubsubBaseProtocol<EventMap>
285
+ let psE: PubsubBaseProtocol<EventMap>
286
+ let peerIdA: PeerId
287
+ let peerIdB: PeerId
288
+ let peerIdC: PeerId
289
+ let peerIdD: PeerId
290
+ let peerIdE: PeerId
291
+ let registrarA: Registrar
292
+ let registrarB: Registrar
293
+ let registrarC: Registrar
294
+ let registrarD: Registrar
295
+ let registrarE: Registrar
239
296
 
240
297
  // Create and start pubsub nodes
241
298
  beforeEach(async () => {
242
- psA = await common.setup()
243
- psB = await common.setup()
244
- psC = await common.setup()
245
- psD = await common.setup()
246
- psE = await common.setup()
299
+ peerIdA = await createEd25519PeerId()
300
+ peerIdB = await createEd25519PeerId()
301
+ peerIdC = await createEd25519PeerId()
302
+ peerIdD = await createEd25519PeerId()
303
+ peerIdE = await createEd25519PeerId()
304
+
305
+ registrarA = mockRegistrar()
306
+ registrarB = mockRegistrar()
307
+ registrarC = mockRegistrar()
308
+ registrarD = mockRegistrar()
309
+ registrarE = mockRegistrar()
310
+
311
+ psA = await common.setup({
312
+ peerId: peerIdA,
313
+ registrar: registrarA,
314
+ emitSelf: true
315
+ })
316
+ psB = await common.setup({
317
+ peerId: peerIdB,
318
+ registrar: registrarB,
319
+ emitSelf: true
320
+ })
321
+ psC = await common.setup({
322
+ peerId: peerIdC,
323
+ registrar: registrarC,
324
+ emitSelf: true
325
+ })
326
+ psD = await common.setup({
327
+ peerId: peerIdD,
328
+ registrar: registrarD,
329
+ emitSelf: true
330
+ })
331
+ psE = await common.setup({
332
+ peerId: peerIdE,
333
+ registrar: registrarE,
334
+ emitSelf: true
335
+ })
247
336
 
248
337
  // Start pubsub nodes
249
- ;[psA, psB, psC, psD, psE].map((p) => p.start())
338
+ await Promise.all(
339
+ [psA, psB, psC, psD, psE].map(async (p) => await p.start())
340
+ )
250
341
  })
251
342
 
252
343
  // connect nodes
253
344
  beforeEach(async () => {
254
- // @ts-expect-error protected field
255
- await psA._libp2p.dial(psB.peerId)
256
- // @ts-expect-error protected field
257
- await psB._libp2p.dial(psC.peerId)
258
- // @ts-expect-error protected field
259
- await psC._libp2p.dial(psD.peerId)
260
- // @ts-expect-error protected field
261
- await psD._libp2p.dial(psE.peerId)
345
+ await connectPeers(psA.multicodecs[0], {
346
+ peerId: peerIdA,
347
+ registrar: registrarA
348
+ }, {
349
+ peerId: peerIdB,
350
+ registrar: registrarB
351
+ })
352
+ await connectPeers(psA.multicodecs[0], {
353
+ peerId: peerIdB,
354
+ registrar: registrarB
355
+ }, {
356
+ peerId: peerIdC,
357
+ registrar: registrarC
358
+ })
359
+ await connectPeers(psA.multicodecs[0], {
360
+ peerId: peerIdC,
361
+ registrar: registrarC
362
+ }, {
363
+ peerId: peerIdD,
364
+ registrar: registrarD
365
+ })
366
+ await connectPeers(psA.multicodecs[0], {
367
+ peerId: peerIdD,
368
+ registrar: registrarD
369
+ }, {
370
+ peerId: peerIdE,
371
+ registrar: registrarE
372
+ })
262
373
 
263
374
  // Wait for peers to be ready in pubsub
264
375
  await pWaitFor(() =>
265
- psA.peers.size === 1 &&
266
- psB.peers.size === 2 &&
267
- psC.peers.size === 2 &&
268
- psD.peers.size === 2 &&
269
- psE.peers.size === 1
376
+ psA.getPeers().length === 1 &&
377
+ psB.getPeers().length === 2 &&
378
+ psC.getPeers().length === 2 &&
379
+ psD.getPeers().length === 2 &&
380
+ psE.getPeers().length === 1
270
381
  )
271
382
  })
272
383
 
273
384
  afterEach(async () => {
274
- [psA, psB, psC, psD, psE].map((p) => p.stop())
385
+ await Promise.all(
386
+ [psA, psB, psC, psD, psE].map(async (p) => await p.stop())
387
+ )
275
388
  await common.teardown()
276
389
  })
277
390
 
278
391
  it('subscribes', () => {
279
392
  psA.subscribe('Z')
280
- expectSet(psA.subscriptions, ['Z'])
393
+ expect(psA.getTopics()).to.deep.equal(['Z'])
281
394
  psB.subscribe('Z')
282
- expectSet(psB.subscriptions, ['Z'])
395
+ expect(psB.getTopics()).to.deep.equal(['Z'])
283
396
  psC.subscribe('Z')
284
- expectSet(psC.subscriptions, ['Z'])
397
+ expect(psC.getTopics()).to.deep.equal(['Z'])
285
398
  psD.subscribe('Z')
286
- expectSet(psD.subscriptions, ['Z'])
399
+ expect(psD.getTopics()).to.deep.equal(['Z'])
287
400
  psE.subscribe('Z')
288
- expectSet(psE.subscriptions, ['Z'])
401
+ expect(psE.getTopics()).to.deep.equal(['Z'])
289
402
  })
290
403
 
291
404
  it('publishes from c', async function () {
@@ -314,7 +427,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
314
427
  // await a cycle
315
428
  await delay(1000)
316
429
 
317
- void psC.publish('Z', uint8ArrayFromString('hey from c'))
430
+ void psC.dispatchEvent(new CustomEvent('Z', { detail: uint8ArrayFromString('hey from c') }))
318
431
 
319
432
  function incMsg (evt: CustomEvent<Message>) {
320
433
  const msg = evt.detail