Skip to content

Commit 93966b9

Browse files
committed
chore: address review
1 parent abf9869 commit 93966b9

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
})
@@ -350,6 +303,22 @@ class IdentifyService {
350303
return log.error('received invalid message', err)
351304
}
352305

306+
const id = connection.remotePeer
307+
308+
// Legacy
309+
if (!message.signedPeerRecord) {
310+
try {
311+
this.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr)))
312+
} catch (err) {
313+
return log.error('received invalid listen addrs', err)
314+
}
315+
316+
// Update the protocols
317+
this.peerStore.protoBook.set(id, message.protocols)
318+
319+
return
320+
}
321+
353322
// Open envelope and verify if is authenticated
354323
let envelope
355324
try {
@@ -369,7 +338,6 @@ class IdentifyService {
369338
}
370339

371340
// Update peers data in PeerStore
372-
const id = connection.remotePeer
373341
try {
374342
// TODO: Store as certified record
375343

@@ -383,45 +351,8 @@ class IdentifyService {
383351
}
384352

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

438371
return this._selfRecord
439372
}
@@ -446,8 +379,8 @@ module.exports.IdentifyService = IdentifyService
446379
*/
447380
module.exports.multicodecs = {
448381
IDENTIFY: MULTICODEC_IDENTIFY,
449-
IDENTIFY_LEGACY: MULTICODEC_IDENTIFY_LEGACY,
382+
IDENTIFY_1_0_0: MULTICODEC_IDENTIFY_1_0_0,
450383
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH,
451-
IDENTIFY_PUSH_LEGACY: MULTICODEC_IDENTIFY_PUSH_LEGACY
384+
IDENTIFY_PUSH_1_0_0: MULTICODEC_IDENTIFY_PUSH_1_0_0
452385
}
453386
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)