Skip to content

Commit 9b68696

Browse files
committed
chore: use topology interface
1 parent 0de3426 commit 9b68696

File tree

4 files changed

+18
-181
lines changed

4 files changed

+18
-181
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"it-pipe": "^1.0.1",
5555
"latency-monitor": "~0.2.1",
5656
"libp2p-crypto": "^0.16.2",
57-
"libp2p-interfaces": "^0.1.3",
57+
"libp2p-interfaces": "~0.1.4",
5858
"mafmt": "^7.0.0",
5959
"merge-options": "^1.0.1",
6060
"moving-average": "^1.0.0",

src/connection-manager/topology.js

Lines changed: 0 additions & 108 deletions
This file was deleted.

src/registrar.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ log.error = debug('libp2p:peer-store:error')
77

88
const { Connection } = require('libp2p-interfaces/src/connection')
99
const PeerInfo = require('peer-info')
10-
const Toplogy = require('./connection-manager/topology')
1110

1211
/**
1312
* Responsible for notifying registered protocols of events in the network.
@@ -106,17 +105,12 @@ class Registrar {
106105

107106
/**
108107
* Register handlers for a set of multicodecs given
109-
* @param {Object} topologyProps properties for topology
110-
* @param {Array<string>|string} topologyProps.multicodecs
111-
* @param {Object} topologyProps.handlers
112-
* @param {function} topologyProps.handlers.onConnect
113-
* @param {function} topologyProps.handlers.onDisconnect
108+
* @param {Topology} topology protocol topology
114109
* @return {string} registrar identifier
115110
*/
116-
register (topologyProps) {
117-
// Create multicodec topology
111+
register (topology) {
112+
// Create topology
118113
const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()
119-
const topology = new Toplogy(topologyProps)
120114

121115
this.topologies.set(id, topology)
122116

test/registrar/registrar.spec.js

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { expect } = chai
77
const pDefer = require('p-defer')
88

99
const PeerInfo = require('peer-info')
10+
const Topology = require('libp2p-interfaces/src/topology')
1011
const PeerStore = require('../../src/peer-store')
1112
const Registrar = require('../../src/registrar')
1213
const { createMockConnection } = require('./utils')
@@ -32,53 +33,7 @@ describe('registrar', () => {
3233
throw new Error('should fail to register a protocol if no multicodec is provided')
3334
})
3435

35-
it('should fail to register a protocol if no handlers are provided', () => {
36-
const topologyProps = {
37-
multicodecs: multicodec
38-
}
39-
40-
try {
41-
registrar.register(topologyProps)
42-
} catch (err) {
43-
expect(err).to.exist()
44-
return
45-
}
46-
throw new Error('should fail to register a protocol if no handlers are provided')
47-
})
48-
49-
it('should fail to register a protocol if the onConnect handler is not provided', () => {
50-
const topologyProps = {
51-
multicodecs: multicodec,
52-
handlers: {
53-
onDisconnect: () => { }
54-
}
55-
}
56-
57-
try {
58-
registrar.register(topologyProps)
59-
} catch (err) {
60-
expect(err).to.exist()
61-
return
62-
}
63-
throw new Error('should fail to register a protocol if the onConnect handler is not provided')
64-
})
65-
66-
it('should fail to register a protocol if the onDisconnect handler is not provided', () => {
67-
const topologyProps = {
68-
multicodecs: multicodec,
69-
handlers: {
70-
onConnect: () => { }
71-
}
72-
}
73-
74-
try {
75-
registrar.register(topologyProps)
76-
} catch (err) {
77-
expect(err).to.exist()
78-
return
79-
}
80-
throw new Error('should fail to register a protocol if the onDisconnect handler is not provided')
81-
})
36+
// TODO: not valid topology
8237
})
8338

8439
describe('registration', () => {
@@ -88,27 +43,27 @@ describe('registrar', () => {
8843
})
8944

9045
it('should be able to register a protocol', () => {
91-
const topologyProps = {
46+
const topologyProps = new Topology({
47+
multicodecs: multicodec,
9248
handlers: {
9349
onConnect: () => { },
9450
onDisconnect: () => { }
95-
},
96-
multicodecs: multicodec
97-
}
51+
}
52+
})
9853

9954
const identifier = registrar.register(topologyProps)
10055

10156
expect(identifier).to.exist()
10257
})
10358

10459
it('should be able to unregister a protocol', () => {
105-
const topologyProps = {
60+
const topologyProps = new Topology({
61+
multicodecs: multicodec,
10662
handlers: {
10763
onConnect: () => { },
10864
onDisconnect: () => { }
109-
},
110-
multicodecs: multicodec
111-
}
65+
}
66+
})
11267

11368
const identifier = registrar.register(topologyProps)
11469
const success = registrar.unregister(identifier)
@@ -138,7 +93,7 @@ describe('registrar', () => {
13893
registrar.onConnect(remotePeerInfo, conn)
13994
expect(registrar.connections.size).to.eql(1)
14095

141-
const topologyProps = {
96+
const topologyProps = new Topology({
14297
multicodecs: multicodec,
14398
handlers: {
14499
onConnect: (peerInfo, connection) => {
@@ -153,19 +108,17 @@ describe('registrar', () => {
153108
onDisconnectDefer.resolve()
154109
}
155110
}
156-
}
111+
})
157112

158113
// Register protocol
159114
const identifier = registrar.register(topologyProps)
160115
const topology = registrar.topologies.get(identifier)
161116

162117
// Topology created
163118
expect(topology).to.exist()
164-
expect(topology.peers.size).to.eql(1)
165119

166120
registrar.onDisconnect(remotePeerInfo)
167121
expect(registrar.connections.size).to.eql(0)
168-
expect(topology.peers.size).to.eql(1) // topology should keep the peer
169122

170123
// Wait for handlers to be called
171124
return Promise.all([
@@ -178,7 +131,7 @@ describe('registrar', () => {
178131
const onConnectDefer = pDefer()
179132
const onDisconnectDefer = pDefer()
180133

181-
const topologyProps = {
134+
const topologyProps = new Topology({
182135
multicodecs: multicodec,
183136
handlers: {
184137
onConnect: () => {
@@ -188,15 +141,14 @@ describe('registrar', () => {
188141
onDisconnectDefer.resolve()
189142
}
190143
}
191-
}
144+
})
192145

193146
// Register protocol
194147
const identifier = registrar.register(topologyProps)
195148
const topology = registrar.topologies.get(identifier)
196149

197150
// Topology created
198151
expect(topology).to.exist()
199-
expect(topology.peers.size).to.eql(0)
200152
expect(registrar.connections.size).to.eql(0)
201153

202154
// Setup connections before registrar
@@ -212,7 +164,6 @@ describe('registrar', () => {
212164
peerStore.put(peerInfo)
213165

214166
await onConnectDefer.promise
215-
expect(topology.peers.size).to.eql(1)
216167

217168
// Remove protocol to peer and update it
218169
peerInfo.protocols.delete(multicodec)

0 commit comments

Comments
 (0)