Skip to content

Commit e3bd773

Browse files
committed
chore: address review
1 parent fa45a6b commit e3bd773

File tree

3 files changed

+47
-114
lines changed

3 files changed

+47
-114
lines changed

src/identify/consts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ module.exports.MULTICODEC_IDENTIFY = '/p2p/id/1.1.0'
66
module.exports.MULTICODEC_IDENTIFY_PUSH = '/p2p/id/push/1.1.0'
77

88
// Legacy
9-
module.exports.MULTICODEC_IDENTIFY_LEGACY = '/ipfs/id/1.0.0'
10-
module.exports.MULTICODEC_IDENTIFY_PUSH_LEGACY = '/ipfs/id/push/1.0.0'
9+
module.exports.MULTICODEC_IDENTIFY_1_0_0 = '/ipfs/id/1.0.0'
10+
module.exports.MULTICODEC_IDENTIFY_PUSH_1_0_0 = '/ipfs/id/push/1.0.0'

src/identify/index.js

Lines changed: 34 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const PeerRecord = require('../record/peer-record')
2222

2323
const {
2424
MULTICODEC_IDENTIFY,
25-
MULTICODEC_IDENTIFY_LEGACY,
25+
MULTICODEC_IDENTIFY_1_0_0,
2626
MULTICODEC_IDENTIFY_PUSH,
27-
MULTICODEC_IDENTIFY_PUSH_LEGACY,
27+
MULTICODEC_IDENTIFY_PUSH_1_0_0,
2828
AGENT_VERSION,
2929
PROTOCOL_VERSION
3030
} = require('./consts')
@@ -97,26 +97,12 @@ class IdentifyService {
9797
push (connections) {
9898
const pushes = connections.map(async connection => {
9999
try {
100-
const { protocol, stream } = await connection.newStream([MULTICODEC_IDENTIFY_PUSH, MULTICODEC_IDENTIFY_PUSH_LEGACY])
101-
102-
// Handle Legacy
103-
if (protocol === MULTICODEC_IDENTIFY_PUSH_LEGACY) {
104-
return pipe(
105-
[{
106-
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.buffer),
107-
protocols: Array.from(this._protocols.keys())
108-
}],
109-
pb.encode(Message),
110-
stream,
111-
consume
112-
)
113-
}
114-
115-
const envelope = await this._getSelfPeerRecord()
116-
const signedPeerRecord = envelope.marshal()
100+
const { stream } = await connection.newStream([MULTICODEC_IDENTIFY_PUSH, MULTICODEC_IDENTIFY_PUSH_1_0_0])
101+
const signedPeerRecord = await this._getSelfPeerRecord()
117102

118103
await pipe(
119104
[{
105+
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.buffer),
120106
signedPeerRecord,
121107
protocols: Array.from(this._protocols.keys())
122108
}],
@@ -159,7 +145,7 @@ class IdentifyService {
159145
* @returns {Promise<void>}
160146
*/
161147
async identify (connection) {
162-
const { protocol, stream } = await connection.newStream([MULTICODEC_IDENTIFY, MULTICODEC_IDENTIFY_LEGACY])
148+
const { protocol, stream } = await connection.newStream([MULTICODEC_IDENTIFY, MULTICODEC_IDENTIFY_1_0_0])
163149
const [data] = await pipe(
164150
[],
165151
stream,
@@ -198,7 +184,7 @@ class IdentifyService {
198184
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
199185

200186
// LEGACY: differentiate message with SignedPeerRecord
201-
if (protocol === MULTICODEC_IDENTIFY_LEGACY) {
187+
if (protocol === MULTICODEC_IDENTIFY_1_0_0) {
202188
// Update peers data in PeerStore
203189
this.peerStore.addressBook.set(id, listenAddrs.map((addr) => multiaddr(addr)))
204190
this.peerStore.protoBook.set(id, protocols)
@@ -249,13 +235,11 @@ class IdentifyService {
249235
handleMessage ({ connection, stream, protocol }) {
250236
switch (protocol) {
251237
case MULTICODEC_IDENTIFY:
238+
case MULTICODEC_IDENTIFY_1_0_0:
252239
return this._handleIdentify({ connection, stream })
253-
case MULTICODEC_IDENTIFY_LEGACY:
254-
return this._handleIdentifyLegacy({ connection, stream })
255240
case MULTICODEC_IDENTIFY_PUSH:
241+
case MULTICODEC_IDENTIFY_PUSH_1_0_0:
256242
return this._handlePush({ connection, stream })
257-
case MULTICODEC_IDENTIFY_PUSH_LEGACY:
258-
return this._handlePushLegacy({ connection, stream })
259243
default:
260244
log.error('cannot handle unknown protocol %s', protocol)
261245
}
@@ -275,45 +259,14 @@ class IdentifyService {
275259
publicKey = this.peerId.pubKey.bytes
276260
}
277261

278-
const envelope = await this._getSelfPeerRecord()
279-
const signedPeerRecord = envelope.marshal()
280-
281-
const message = Message.encode({
282-
protocolVersion: PROTOCOL_VERSION,
283-
agentVersion: AGENT_VERSION,
284-
publicKey,
285-
signedPeerRecord,
286-
observedAddr: connection.remoteAddr.buffer,
287-
protocols: Array.from(this._protocols.keys())
288-
})
289-
290-
pipe(
291-
[message],
292-
lp.encode(),
293-
stream,
294-
consume
295-
)
296-
}
297-
298-
/**
299-
* Sends the `Identify` response with listen addresses (LEGACY)
300-
* to the requesting peer over the given `connection`
301-
* @private
302-
* @param {object} options
303-
* @param {*} options.stream
304-
* @param {Connection} options.connection
305-
*/
306-
_handleIdentifyLegacy ({ connection, stream }) {
307-
let publicKey = Buffer.alloc(0)
308-
if (this.peerId.pubKey) {
309-
publicKey = this.peerId.pubKey.bytes
310-
}
262+
const signedPeerRecord = await this._getSelfPeerRecord()
311263

312264
const message = Message.encode({
313265
protocolVersion: PROTOCOL_VERSION,
314266
agentVersion: AGENT_VERSION,
315267
publicKey,
316268
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.buffer),
269+
signedPeerRecord,
317270
observedAddr: connection.remoteAddr.buffer,
318271
protocols: Array.from(this._protocols.keys())
319272
})
@@ -353,6 +306,22 @@ class IdentifyService {
353306
return log.error('received invalid message', err)
354307
}
355308

309+
const id = connection.remotePeer
310+
311+
// Legacy
312+
if (!message.signedPeerRecord) {
313+
try {
314+
this.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr)))
315+
} catch (err) {
316+
return log.error('received invalid listen addrs', err)
317+
}
318+
319+
// Update the protocols
320+
this.peerStore.protoBook.set(id, message.protocols)
321+
322+
return
323+
}
324+
356325
// Open envelope and verify if is authenticated
357326
let envelope
358327
try {
@@ -372,7 +341,6 @@ class IdentifyService {
372341
}
373342

374343
// Update peers data in PeerStore
375-
const id = connection.remotePeer
376344
try {
377345
// TODO: Store as certified record
378346

@@ -386,45 +354,8 @@ class IdentifyService {
386354
}
387355

388356
/**
389-
* Reads the Identify Push message from the given `connection`
390-
* with listen addresses (LEGACY)
391-
* @private
392-
* @param {object} options
393-
* @param {*} options.stream
394-
* @param {Connection} options.connection
395-
*/
396-
async _handlePushLegacy ({ connection, stream }) {
397-
const [data] = await pipe(
398-
[],
399-
stream,
400-
lp.decode(),
401-
take(1),
402-
toBuffer,
403-
collect
404-
)
405-
406-
let message
407-
try {
408-
message = Message.decode(data)
409-
} catch (err) {
410-
return log.error('received invalid message', err)
411-
}
412-
413-
// Update peers data in PeerStore
414-
const id = connection.remotePeer
415-
try {
416-
this.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr)))
417-
} catch (err) {
418-
return log.error('received invalid listen addrs', err)
419-
}
420-
421-
// Update the protocols
422-
this.peerStore.protoBook.set(id, message.protocols)
423-
}
424-
425-
/**
426-
* Get self signed peer record envelope.
427-
* @return {Envelope}
357+
* Get self signed peer record raw envelope.
358+
* @return {Buffer}
428359
*/
429360
async _getSelfPeerRecord () {
430361
// TODO: Verify if updated
@@ -436,7 +367,9 @@ class IdentifyService {
436367
peerId: this.peerId,
437368
multiaddrs: this._libp2p.multiaddrs
438369
})
439-
this._selfRecord = await Envelope.seal(peerRecord, this.peerId)
370+
const envelope = await Envelope.seal(peerRecord, this.peerId)
371+
372+
this._selfRecord = envelope.marshal()
440373

441374
return this._selfRecord
442375
}
@@ -449,8 +382,8 @@ module.exports.IdentifyService = IdentifyService
449382
*/
450383
module.exports.multicodecs = {
451384
IDENTIFY: MULTICODEC_IDENTIFY,
452-
IDENTIFY_LEGACY: MULTICODEC_IDENTIFY_LEGACY,
385+
IDENTIFY_1_0_0: MULTICODEC_IDENTIFY_1_0_0,
453386
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH,
454-
IDENTIFY_PUSH_LEGACY: MULTICODEC_IDENTIFY_PUSH_LEGACY
387+
IDENTIFY_PUSH_1_0_0: MULTICODEC_IDENTIFY_PUSH_1_0_0
455388
}
456389
module.exports.Message = Message

test/identify/index.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const protocols = new Map([
3030
])
3131

3232
const protocolsLegacy = new Map([
33-
[multicodecs.IDENTIFY_LEGACY, () => { }],
34-
[multicodecs.IDENTIFY_PUSH_LEGACY, () => { }]
33+
[multicodecs.IDENTIFY_1_0_0, () => { }],
34+
[multicodecs.IDENTIFY_PUSH_1_0_0, () => { }]
3535
])
3636

3737
describe('Identify', () => {
@@ -49,7 +49,7 @@ describe('Identify', () => {
4949
sinon.restore()
5050
})
5151

52-
it('should be able to identify another peer with legacy protocol', async () => {
52+
it('should be able to identify another peer with 1.0.0 legacy protocol', async () => {
5353
const localIdentify = new IdentifyService({
5454
libp2p: {
5555
peerId: localPeer,
@@ -81,7 +81,7 @@ describe('Identify', () => {
8181
const remoteConnectionMock = { remoteAddr: observedAddr }
8282

8383
const [local, remote] = duplexPair()
84-
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_LEGACY })
84+
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_1_0_0 })
8585

8686
sinon.spy(localIdentify.peerStore.addressBook, 'set')
8787
sinon.spy(localIdentify.peerStore.protoBook, 'set')
@@ -92,7 +92,7 @@ describe('Identify', () => {
9292
remoteIdentify.handleMessage({
9393
connection: remoteConnectionMock,
9494
stream: remote,
95-
protocol: multicodecs.IDENTIFY_LEGACY
95+
protocol: multicodecs.IDENTIFY_1_0_0
9696
})
9797
])
9898

@@ -214,7 +214,7 @@ describe('Identify', () => {
214214
})
215215

216216
describe('push', () => {
217-
it('should be able to push identify updates to another peer with legacy protocol', async () => {
217+
it('should be able to push identify updates to another peer with 1.0.0 legacy protocols', async () => {
218218
const connectionManager = new EventEmitter()
219219
connectionManager.getConnection = () => {}
220220

@@ -225,8 +225,8 @@ describe('Identify', () => {
225225
multiaddrs: listenMaddrs
226226
},
227227
protocols: new Map([
228-
[multicodecs.IDENTIFY_LEGACY],
229-
[multicodecs.IDENTIFY_PUSH_LEGACY],
228+
[multicodecs.IDENTIFY_1_0_0],
229+
[multicodecs.IDENTIFY_PUSH_1_0_0],
230230
['/echo/1.0.0']
231231
])
232232
})
@@ -247,12 +247,12 @@ describe('Identify', () => {
247247
})
248248

249249
// Setup peer protocols and multiaddrs
250-
const localProtocols = new Set([multicodecs.IDENTIFY_LEGACY, multicodecs.IDENTIFY_PUSH_LEGACY, '/echo/1.0.0'])
250+
const localProtocols = new Set([multicodecs.IDENTIFY_1_0_0, multicodecs.IDENTIFY_PUSH_1_0_0, '/echo/1.0.0'])
251251
const localConnectionMock = { newStream: () => {} }
252252
const remoteConnectionMock = { remotePeer: localPeer }
253253

254254
const [local, remote] = duplexPair()
255-
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_PUSH_LEGACY })
255+
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_PUSH_1_0_0 })
256256

257257
sinon.spy(remoteIdentify.peerStore.addressBook, 'set')
258258
sinon.spy(remoteIdentify.peerStore.protoBook, 'set')
@@ -263,7 +263,7 @@ describe('Identify', () => {
263263
remoteIdentify.handleMessage({
264264
connection: remoteConnectionMock,
265265
stream: remote,
266-
protocol: multicodecs.IDENTIFY_PUSH_LEGACY
266+
protocol: multicodecs.IDENTIFY_PUSH_1_0_0
267267
})
268268
])
269269

0 commit comments

Comments
 (0)