Skip to content

Commit 46e2509

Browse files
authored
feat: update identify to include supported protocols (libp2p#311)
1 parent 6a94d9a commit 46e2509

File tree

6 files changed

+111
-23
lines changed

6 files changed

+111
-23
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@
6868
"fsm-event": "^2.1.0",
6969
"hashlru": "^2.3.0",
7070
"interface-connection": "~0.3.3",
71-
"libp2p-circuit": "~0.3.4",
72-
"libp2p-identify": "~0.7.5",
71+
"libp2p-circuit": "~0.3.5",
72+
"libp2p-identify": "~0.7.6",
7373
"moving-average": "^1.0.0",
74-
"multiaddr": "^6.0.4",
74+
"multiaddr": "^6.0.6",
7575
"multistream-select": "~0.14.4",
7676
"once": "^1.4.0",
7777
"peer-id": "~0.12.2",

src/connection/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ class ConnectionFSM extends BaseConnection {
389389
})
390390

391391
this.switch.emit('peer-mux-established', this.theirPeerInfo)
392-
393392
this._didUpgrade(null)
394393
})
395394
}

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const EventEmitter = require('events').EventEmitter
55
const each = require('async/each')
66
const eachSeries = require('async/eachSeries')
77
const series = require('async/series')
8+
const Circuit = require('libp2p-circuit')
89
const TransportManager = require('./transport')
910
const ConnectionManager = require('./connection/manager')
1011
const getPeerInfo = require('./get-peer-info')
@@ -128,7 +129,7 @@ class Switch extends EventEmitter {
128129
return myTransports.filter((ts) => this.transports[ts].filter(myAddrs).length > 0)
129130
// push Circuit to be the last proto to be dialed
130131
.sort((a) => {
131-
return a === 'Circuit' ? 1 : 0
132+
return a === Circuit.tag ? 1 : 0
132133
})
133134
}
134135

@@ -147,6 +148,7 @@ class Switch extends EventEmitter {
147148
handlerFunc: handlerFunc,
148149
matchFunc: matchFunc
149150
}
151+
this._peerInfo.protocols.add(protocol)
150152
}
151153

152154
/**
@@ -159,6 +161,7 @@ class Switch extends EventEmitter {
159161
if (this.protocols[protocol]) {
160162
delete this.protocols[protocol]
161163
}
164+
this._peerInfo.protocols.delete(protocol)
162165
}
163166

164167
/**
@@ -185,7 +188,7 @@ class Switch extends EventEmitter {
185188
* @returns {boolean}
186189
*/
187190
hasTransports () {
188-
const transports = Object.keys(this.transports).filter((t) => t !== 'Circuit')
191+
const transports = Object.keys(this.transports).filter((t) => t !== Circuit.tag)
189192
return transports && transports.length > 0
190193
}
191194

test/circuit-relay.node.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ describe(`circuit`, function () {
252252
], (err) => {
253253
if (err) return done(err)
254254

255+
if (bootstrapSwitch._peerBook.getAllArray().length === 4) {
256+
return done()
257+
}
258+
255259
done = once(done)
256260
// Wait for everyone to connect, before we try relaying
257261
bootstrapSwitch.on('peer-mux-established', () => {
@@ -263,6 +267,10 @@ describe(`circuit`, function () {
263267
})
264268
}))
265269

270+
before('wait so hop status can be negotiated', function (done) {
271+
setTimeout(done, 1000)
272+
})
273+
266274
after(function (done) {
267275
parallel([
268276
(cb) => bootstrapSwitch.stop(cb),
@@ -294,6 +302,7 @@ describe(`circuit`, function () {
294302
done()
295303
}
296304
})
305+
297306
tcpSwitch1.dial(wsPeer1, (err, connection) => {
298307
expect(err).to.not.exist()
299308
// We're not dialing a protocol, so we won't get a connection back
@@ -323,6 +332,7 @@ describe(`circuit`, function () {
323332
done()
324333
}
325334
})
335+
326336
wsSwitch2.dial(tcpPeer1, (err, connection) => {
327337
expect(err).to.not.exist()
328338
// We're not dialing a protocol, so we won't get a connection back

test/dial-fsm.node.js

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const TCP = require('libp2p-tcp')
1313
const secio = require('libp2p-secio')
1414
const multiplex = require('pull-mplex')
1515
const pull = require('pull-stream')
16+
const identify = require('libp2p-identify')
1617

1718
const utils = require('./utils')
1819
const createInfos = utils.createInfos
@@ -24,6 +25,7 @@ describe('dialFSM', () => {
2425
let switchC
2526
let peerAId
2627
let peerBId
28+
let protocol
2729

2830
before((done) => createInfos(3, (err, infos) => {
2931
expect(err).to.not.exist()
@@ -76,10 +78,18 @@ describe('dialFSM', () => {
7678
], done)
7779
})
7880

81+
afterEach(() => {
82+
switchA.unhandle(protocol)
83+
switchB.unhandle(protocol)
84+
switchC.unhandle(protocol)
85+
protocol = null
86+
})
87+
7988
it('should emit `error:connection_attempt_failed` when a transport fails to dial', (done) => {
80-
switchC.handle('/warn/1.0.0', () => { })
89+
protocol = '/warn/1.0.0'
90+
switchC.handle(protocol, () => { })
8191

82-
switchA.dialFSM(switchC._peerInfo, '/warn/1.0.0', (err, connFSM) => {
92+
switchA.dialFSM(switchC._peerInfo, protocol, (err, connFSM) => {
8393
expect(err).to.not.exist()
8494
connFSM.once('error:connection_attempt_failed', (errors) => {
8595
expect(errors).to.be.an('array')
@@ -90,9 +100,10 @@ describe('dialFSM', () => {
90100
})
91101

92102
it('should emit an `error` event when a it cannot dial a peer', (done) => {
93-
switchC.handle('/error/1.0.0', () => { })
103+
protocol = '/error/1.0.0'
104+
switchC.handle(protocol, () => { })
94105

95-
switchA.dialFSM(switchC._peerInfo, '/error/1.0.0', (err, connFSM) => {
106+
switchA.dialFSM(switchC._peerInfo, protocol, (err, connFSM) => {
96107
expect(err).to.not.exist()
97108
connFSM.once('error', (err) => {
98109
expect(err).to.be.exist()
@@ -103,9 +114,10 @@ describe('dialFSM', () => {
103114
})
104115

105116
it('should emit a `closed` event when closed', (done) => {
106-
switchB.handle('/closed/1.0.0', () => { })
117+
protocol = '/closed/1.0.0'
118+
switchB.handle(protocol, () => { })
107119

108-
switchA.dialFSM(switchB._peerInfo, '/closed/1.0.0', (err, connFSM) => {
120+
switchA.dialFSM(switchB._peerInfo, protocol, (err, connFSM) => {
109121
expect(err).to.not.exist()
110122

111123
connFSM.once('close', () => {
@@ -120,11 +132,49 @@ describe('dialFSM', () => {
120132
})
121133
})
122134

135+
it('should have the peers protocols once connected', (done) => {
136+
protocol = '/lscheck/1.0.0'
137+
switchB.handle(protocol, () => { })
138+
139+
expect(4).checks(done)
140+
141+
switchB.once('peer-mux-established', (peerInfo) => {
142+
const peerB = switchA._peerBook.get(switchB._peerInfo.id.toB58String())
143+
const peerA = switchB._peerBook.get(switchA._peerInfo.id.toB58String())
144+
// Verify the dialer knows the receiver's protocols
145+
expect(Array.from(peerB.protocols)).to.eql([
146+
multiplex.multicodec,
147+
identify.multicodec,
148+
protocol
149+
]).mark()
150+
// Verify the receiver knows the dialer's protocols
151+
expect(Array.from(peerA.protocols)).to.eql([
152+
multiplex.multicodec,
153+
identify.multicodec
154+
]).mark()
155+
156+
switchA.hangUp(switchB._peerInfo)
157+
})
158+
159+
switchA.dialFSM(switchB._peerInfo, protocol, (err, connFSM) => {
160+
expect(err).to.not.exist().mark()
161+
162+
connFSM.once('close', () => {
163+
// Just mark that close was called
164+
expect(true).to.eql(true).mark()
165+
})
166+
})
167+
})
168+
123169
it('should close when the receiver closes', (done) => {
170+
protocol = '/closed/1.0.0'
171+
switchB.handle(protocol, () => { })
172+
124173
// wait for the expects to happen
125-
expect(2).checks(done)
174+
expect(2).checks(() => {
175+
done()
176+
})
126177

127-
switchB.handle('/closed/1.0.0', () => { })
128178
switchB.on('peer-mux-established', (peerInfo) => {
129179
if (peerInfo.id.toB58String() === peerAId) {
130180
switchB.removeAllListeners('peer-mux-established')
@@ -133,7 +183,7 @@ describe('dialFSM', () => {
133183
}
134184
})
135185

136-
switchA.dialFSM(switchB._peerInfo, '/closed/1.0.0', (err, connFSM) => {
186+
switchA.dialFSM(switchB._peerInfo, protocol, (err, connFSM) => {
137187
expect(err).to.not.exist()
138188

139189
connFSM.once('close', () => {
@@ -161,8 +211,11 @@ describe('dialFSM', () => {
161211
})
162212

163213
it('parallel dials to one another should disconnect on hangup', function (done) {
164-
switchA.handle('/parallel/1.0.0', (_, conn) => { pull(conn, conn) })
165-
switchB.handle('/parallel/1.0.0', (_, conn) => { pull(conn, conn) })
214+
this.timeout(10e3)
215+
protocol = '/parallel/1.0.0'
216+
217+
switchA.handle(protocol, (_, conn) => { pull(conn, conn) })
218+
switchB.handle(protocol, (_, conn) => { pull(conn, conn) })
166219

167220
expect(switchA.connection.getAllById(peerBId)).to.have.length(0)
168221

@@ -180,12 +233,12 @@ describe('dialFSM', () => {
180233
expect(peerInfo.id.toB58String()).to.eql(peerAId).mark()
181234
})
182235

183-
switchA.dialFSM(switchB._peerInfo, '/parallel/1.0.0', (err, connFSM) => {
236+
switchA.dialFSM(switchB._peerInfo, protocol, (err, connFSM) => {
184237
expect(err).to.not.exist()
185238
// Hold the dial from A, until switch B is done dialing to ensure
186239
// we have both incoming and outgoing connections
187240
connFSM._state.on('DIALING:leave', (cb) => {
188-
switchB.dialFSM(switchA._peerInfo, '/parallel/1.0.0', (err, connB) => {
241+
switchB.dialFSM(switchA._peerInfo, protocol, (err, connB) => {
189242
expect(err).to.not.exist()
190243
connB.on('muxed', cb)
191244
})
@@ -201,8 +254,9 @@ describe('dialFSM', () => {
201254
})
202255

203256
it('parallel dials to one another should disconnect on stop', (done) => {
204-
switchA.handle('/parallel/1.0.0', (_, conn) => { pull(conn, conn) })
205-
switchB.handle('/parallel/1.0.0', (_, conn) => { pull(conn, conn) })
257+
protocol = '/parallel/1.0.0'
258+
switchA.handle(protocol, (_, conn) => { pull(conn, conn) })
259+
switchB.handle(protocol, (_, conn) => { pull(conn, conn) })
206260

207261
// 4 close checks and 1 hangup check
208262
expect(5).checks(() => {

test/identify.node.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ describe('Identify', () => {
8787
switchA.handle('/id-test/1.0.0', (protocol, conn) => pull(conn, conn))
8888
switchB.dial(switchA._peerInfo, '/id-test/1.0.0', (err, conn) => {
8989
expect(err).to.not.exist()
90-
let data = Buffer.from('data that cant be had')
90+
91+
let data = Buffer.from('data that can be had')
9192
pull(
9293
pull.values([data]),
9394
conn,
@@ -100,6 +101,27 @@ describe('Identify', () => {
100101
})
101102
})
102103

104+
it('should get protocols for one another', (done) => {
105+
switchA.handle('/id-test/1.0.0', (protocol, conn) => pull(conn, conn))
106+
switchB.dial(switchA._peerInfo, '/id-test/1.0.0', (err, conn) => {
107+
expect(err).to.not.exist()
108+
109+
const peerB = switchA._peerBook.get(switchB._peerInfo.id.toB58String())
110+
const peerA = switchB._peerBook.get(switchA._peerInfo.id.toB58String())
111+
expect(Array.from(peerB.protocols)).to.eql([
112+
multiplex.multicodec,
113+
identify.multicodec
114+
])
115+
expect(Array.from(peerA.protocols)).to.eql([
116+
multiplex.multicodec,
117+
identify.multicodec,
118+
'/id-test/1.0.0'
119+
])
120+
121+
done()
122+
})
123+
})
124+
103125
it('should close connection when identify fails', (done) => {
104126
const stub = sinon.stub(identify, 'listener').callsFake((conn) => {
105127
conn.getObservedAddrs((err, observedAddrs) => {
@@ -125,7 +147,7 @@ describe('Identify', () => {
125147
})
126148
})
127149

128-
expect(2).check(done)
150+
expect(2).checks(done)
129151

130152
switchA.handle('/id-test/1.0.0', (protocol, conn) => pull(conn, conn))
131153
switchB.dialFSM(switchA._peerInfo, '/id-test/1.0.0', (err, connFSM) => {

0 commit comments

Comments
 (0)