Skip to content

Commit 340edf5

Browse files
committed
chore: address review
1 parent 4cc9736 commit 340edf5

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"it-protocol-buffers": "^0.2.0",
5656
"latency-monitor": "~0.2.1",
5757
"libp2p-crypto": "^0.17.1",
58-
"libp2p-interfaces": "^0.1.4",
58+
"libp2p-interfaces": "^0.1.5",
5959
"mafmt": "^7.0.0",
6060
"merge-options": "^1.0.1",
6161
"moving-average": "^1.0.0",

src/registrar.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const debug = require('debug')
55
const log = debug('libp2p:peer-store')
66
log.error = debug('libp2p:peer-store:error')
77

8+
const Topology = require('libp2p-interfaces/src/topology')
9+
const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology')
810
const { Connection } = require('libp2p-interfaces/src/connection')
911
const PeerInfo = require('peer-info')
1012

@@ -109,6 +111,11 @@ class Registrar {
109111
* @return {string} registrar identifier
110112
*/
111113
register (topology) {
114+
assert(
115+
Topology.isTopology(topology) ||
116+
MulticodecTopology.isMulticodecTopology(topology),
117+
'topology must be an instance of interfaces/topology')
118+
112119
// Create topology
113120
const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()
114121

src/upgrader.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class Upgrader {
186186
const { stream, protocol } = await mss.handle(Array.from(this.protocols.keys()))
187187
log('%s: incoming stream opened on %s', direction, protocol)
188188
connection.addStream(stream, protocol)
189-
this._onStream({ connection, stream, protocol, remotePeer })
189+
this._onStream({ connection, stream, protocol })
190190
} catch (err) {
191191
log.error(err)
192192
}
@@ -254,9 +254,9 @@ class Upgrader {
254254
* @param {Stream} options.stream
255255
* @param {string} options.protocol
256256
*/
257-
_onStream ({ connection, stream, protocol, remotePeer }) {
257+
_onStream ({ connection, stream, protocol }) {
258258
const handler = this.protocols.get(protocol)
259-
handler({ connection, stream, protocol, remotePeer })
259+
handler({ connection, stream, protocol })
260260
}
261261

262262
/**

test/pubsub/operation.node.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ describe('Pubsub subsystem operates correctly', () => {
101101
})
102102
})
103103

104-
// TODO: Needs identify push
105-
describe.skip('pubsub started after connect', () => {
104+
describe('pubsub started after connect', () => {
106105
beforeEach(async () => {
107106
libp2p = await create(mergeOptions(subsystemOptions, {
108107
peerInfo
@@ -132,7 +131,7 @@ describe('Pubsub subsystem operates correctly', () => {
132131
sinon.restore()
133132
})
134133

135-
it.skip('should get notified of connected peers after starting', async () => {
134+
it('should get notified of connected peers after starting', async () => {
136135
const connection = await libp2p.dial(remAddr)
137136

138137
expect(connection).to.exist()
@@ -141,30 +140,43 @@ describe('Pubsub subsystem operates correctly', () => {
141140

142141
remoteLibp2p.pubsub.start()
143142

144-
// Wait for
145-
// Validate
143+
await pWaitFor(() => libp2p.pubsub._pubsub.peers.size === 1)
144+
146145
expect(libp2p.pubsub._pubsub.peers.size).to.be.eql(1)
147146
expect(remoteLibp2p.pubsub._pubsub.peers.size).to.be.eql(1)
148147
})
149148

150-
it.skip('should receive pubsub messages', async () => {
149+
it('should receive pubsub messages', async function () {
150+
this.timeout(10e3)
151151
const defer = pDefer()
152+
const libp2pId = libp2p.peerInfo.id.toB58String()
152153
const topic = 'test-topic'
153154
const data = 'hey!'
154155

155156
await libp2p.dial(remAddr)
156157

157158
remoteLibp2p.pubsub.start()
158159

159-
// TODO: wait for
160+
await pWaitFor(() => libp2p.pubsub._pubsub.peers.size === 1)
161+
162+
let subscribedTopics = libp2p.pubsub.getTopics()
163+
expect(subscribedTopics).to.not.include(topic)
160164

161-
libp2p.pubsub.subscribe(topic)
162-
libp2p.pubsub.once(topic, (msg) => {
165+
libp2p.pubsub.subscribe(topic, (msg) => {
163166
expect(msg.data.toString()).to.equal(data)
164167
defer.resolve()
165168
})
166169

167-
libp2p.pubsub.publish(topic, data)
170+
subscribedTopics = libp2p.pubsub.getTopics()
171+
expect(subscribedTopics).to.include(topic)
172+
173+
// wait for remoteLibp2p to know about libp2p subscription
174+
await pWaitFor(() => {
175+
const subscribedPeers = remoteLibp2p.pubsub.getPeersSubscribed(topic)
176+
return subscribedPeers.includes(libp2pId)
177+
})
178+
179+
remoteLibp2p.pubsub.publish(topic, data)
168180

169181
await defer.promise
170182
})

test/registrar/registrar.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ describe('registrar', () => {
3333
throw new Error('should fail to register a protocol if no multicodec is provided')
3434
})
3535

36-
// TODO: not valid topology
36+
it('should fail to register a protocol if an invalid topology is provided', () => {
37+
const fakeTopology = {
38+
random: 1
39+
}
40+
try {
41+
registrar.register()
42+
} catch (err) {
43+
expect(err).to.exist(fakeTopology)
44+
return
45+
}
46+
throw new Error('should fail to register a protocol if an invalid topology is provided')
47+
})
3748
})
3849

3950
describe('registration', () => {

0 commit comments

Comments
 (0)