Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

Commit 6437139

Browse files
authored
fix: identify on dial (#313)
1 parent b318e3f commit 6437139

File tree

5 files changed

+89
-27
lines changed

5 files changed

+89
-27
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"libp2p-mplex": "~0.8.4",
4848
"libp2p-pnet": "~0.1.0",
4949
"libp2p-secio": "~0.11.1",
50-
"libp2p-spdy": "~0.13.1",
50+
"libp2p-spdy": "~0.13.3",
5151
"libp2p-tcp": "~0.13.0",
5252
"libp2p-webrtc-star": "~0.15.8",
5353
"libp2p-websockets": "~0.12.2",

src/connection/incoming.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class IncomingConnectionFSM extends BaseConnection {
5454
}
5555
})
5656

57+
this._state.on('DISCONNECTED', () => this._onDisconnected())
5758
this._state.on('PRIVATIZING', () => this._onPrivatizing())
5859
this._state.on('PRIVATIZED', () => this._onPrivatized())
5960
this._state.on('ENCRYPTING', () => this._onEncrypting())

src/connection/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const multistream = require('multistream-select')
66
const withIs = require('class-is')
77
const BaseConnection = require('./base')
88
const parallel = require('async/parallel')
9+
const nextTick = require('async/nextTick')
10+
const identify = require('libp2p-identify')
11+
const errCode = require('err-code')
12+
const { msHandle, msSelect, identifyDialer } = require('../utils')
913

1014
const observeConnection = require('../observe-connection')
1115
const {
@@ -390,13 +394,48 @@ class ConnectionFSM extends BaseConnection {
390394

391395
this.switch.emit('peer-mux-established', this.theirPeerInfo)
392396
this._didUpgrade(null)
397+
398+
// Run identify on the connection
399+
if (this.switch.identify) {
400+
this._identify((err, results) => {
401+
if (err) {
402+
return this.close(err)
403+
}
404+
this.theirPeerInfo = this.switch._peerBook.put(results.peerInfo)
405+
})
406+
}
393407
})
394408
}
395409

396410
nextMuxer(muxers.shift())
397411
})
398412
}
399413

414+
/**
415+
* Runs the identify protocol on the connection
416+
* @private
417+
* @param {function(error, { PeerInfo })} callback
418+
* @returns {void}
419+
*/
420+
_identify (callback) {
421+
if (!this.muxer) {
422+
return nextTick(callback, errCode('The connection was already closed', 'ERR_CONNECTION_CLOSED'))
423+
}
424+
this.muxer.newStream(async (err, conn) => {
425+
if (err) return callback(err)
426+
const ms = new multistream.Dialer()
427+
let results
428+
try {
429+
await msHandle(ms, conn)
430+
const msConn = await msSelect(ms, identify.multicodec)
431+
results = await identifyDialer(msConn, this.theirPeerInfo)
432+
} catch (err) {
433+
return callback(err)
434+
}
435+
callback(null, results)
436+
})
437+
}
438+
400439
/**
401440
* Analyses the given error, if it exists, to determine where the state machine
402441
* needs to go.

test/dial-fsm.node.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,27 @@ describe('dialFSM', () => {
219219

220220
expect(switchA.connection.getAllById(peerBId)).to.have.length(0)
221221

222-
// 4 close checks (1 inc and 1 out for each node) and 1 hangup check
223-
expect(5).checks(() => {
224-
switchA.removeAllListeners('peer-mux-closed')
225-
switchB.removeAllListeners('peer-mux-closed')
226-
done()
222+
// Expect 4 `peer-mux-established` events
223+
expect(4).checks(() => {
224+
// Expect 4 `peer-mux-closed`, plus 1 hangup
225+
expect(5).checks(() => {
226+
switchA.removeAllListeners('peer-mux-closed')
227+
switchB.removeAllListeners('peer-mux-closed')
228+
switchA.removeAllListeners('peer-mux-established')
229+
switchB.removeAllListeners('peer-mux-established')
230+
done()
231+
})
232+
233+
switchA.hangUp(switchB._peerInfo, (err) => {
234+
expect(err).to.not.exist().mark()
235+
})
236+
})
237+
238+
switchA.on('peer-mux-established', (peerInfo) => {
239+
expect(peerInfo.id.toB58String()).to.eql(peerBId).mark()
240+
})
241+
switchB.on('peer-mux-established', (peerInfo) => {
242+
expect(peerInfo.id.toB58String()).to.eql(peerAId).mark()
227243
})
228244

229245
switchA.on('peer-mux-closed', (peerInfo) => {
@@ -243,13 +259,6 @@ describe('dialFSM', () => {
243259
connB.on('muxed', cb)
244260
})
245261
})
246-
247-
connFSM.on('connection', () => {
248-
// Hangup and verify the connections are closed
249-
switchA.hangUp(switchB._peerInfo, (err) => {
250-
expect(err).to.not.exist().mark()
251-
})
252-
})
253262
})
254263
})
255264

test/identify.node.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const TCP = require('libp2p-tcp')
1010
const multiplex = require('libp2p-mplex')
1111
const pull = require('pull-stream')
1212
const secio = require('libp2p-secio')
13+
const PeerInfo = require('peer-info')
1314
const PeerBook = require('peer-book')
1415
const identify = require('libp2p-identify')
1516
const lp = require('pull-length-prefixed')
@@ -102,23 +103,35 @@ describe('Identify', () => {
102103
})
103104

104105
it('should get protocols for one another', (done) => {
106+
// We need to reset the PeerInfo objects we use,
107+
// since we share memory we can receive a false positive if not
108+
let peerA = new PeerInfo(switchA._peerInfo.id)
109+
switchA._peerInfo.multiaddrs.toArray().forEach((m) => {
110+
peerA.multiaddrs.add(m)
111+
})
112+
switchB._peerBook.remove(switchA._peerInfo.id.toB58String())
113+
switchA._peerBook.remove(switchB._peerInfo.id.toB58String())
114+
105115
switchA.handle('/id-test/1.0.0', (protocol, conn) => pull(conn, conn))
106-
switchB.dial(switchA._peerInfo, '/id-test/1.0.0', (err, conn) => {
116+
switchB.dial(peerA, '/id-test/1.0.0', (err) => {
107117
expect(err).to.not.exist()
108118

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()
119+
// Give identify a moment to run
120+
setTimeout(() => {
121+
const peerB = switchA._peerBook.get(switchB._peerInfo.id.toB58String())
122+
const peerA = switchB._peerBook.get(switchA._peerInfo.id.toB58String())
123+
expect(Array.from(peerB.protocols)).to.eql([
124+
multiplex.multicodec,
125+
identify.multicodec
126+
])
127+
expect(Array.from(peerA.protocols)).to.eql([
128+
multiplex.multicodec,
129+
identify.multicodec,
130+
'/id-test/1.0.0'
131+
])
132+
133+
done()
134+
}, 500)
122135
})
123136
})
124137

0 commit comments

Comments
 (0)