Skip to content

Commit 83922a7

Browse files
vasco-santosjacobheun
authored andcommitted
feat: create self peer record in identify
1 parent f835457 commit 83922a7

File tree

3 files changed

+39
-49
lines changed

3 files changed

+39
-49
lines changed

src/identify/index.js

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const debug = require('debug')
4+
const log = debug('libp2p:identify')
5+
log.error = debug('libp2p:identify:error')
6+
7+
const errCode = require('err-code')
8+
const { Buffer } = require('buffer')
59
const pb = require('it-protocol-buffers')
610
const lp = require('it-length-prefixed')
711
const pipe = require('it-pipe')
@@ -13,8 +17,8 @@ const { toBuffer } = require('it-buffer')
1317

1418
const Message = require('./message')
1519

16-
const log = debug('libp2p:identify')
17-
log.error = debug('libp2p:identify:error')
20+
const Envelope = require('../record/envelope')
21+
const PeerRecord = require('../record/peer-record')
1822

1923
const {
2024
MULTICODEC_IDENTIFY,
@@ -25,10 +29,7 @@ const {
2529
PROTOCOL_VERSION
2630
} = require('./consts')
2731

28-
const errCode = require('err-code')
2932
const { messages, codes } = require('../errors')
30-
const Envelope = require('../record-manager/envelope')
31-
const PeerRecord = require('../record-manager/peer-record')
3233

3334
class IdentifyService {
3435
/**
@@ -83,6 +84,9 @@ class IdentifyService {
8384
this._protocols = protocols
8485

8586
this.handleMessage = this.handleMessage.bind(this)
87+
88+
// TODO: this should be stored in the certified AddressBook in follow up PR
89+
this._selfRecord = undefined
8690
}
8791

8892
/**
@@ -108,7 +112,7 @@ class IdentifyService {
108112
)
109113
}
110114

111-
const envelope = this._libp2p.recordManager.getPeerRecord()
115+
const envelope = await this._getSelfPeerRecord()
112116
const signedPeerRecord = envelope.marshal()
113117

114118
await pipe(
@@ -272,7 +276,7 @@ class IdentifyService {
272276
publicKey = this.peerId.pubKey.bytes
273277
}
274278

275-
const envelope = this._libp2p.recordManager.getPeerRecord()
279+
const envelope = await this._getSelfPeerRecord()
276280
const signedPeerRecord = envelope.marshal()
277281

278282
const message = Message.encode({
@@ -418,6 +422,25 @@ class IdentifyService {
418422
// Update the protocols
419423
this.peerStore.protoBook.set(id, message.protocols)
420424
}
425+
426+
/**
427+
* Get self signed peer record envelope.
428+
* @return {Envelope}
429+
*/
430+
async _getSelfPeerRecord () {
431+
// TODO: Verify if updated
432+
if (this._selfRecord) {
433+
return this._selfRecord
434+
}
435+
436+
const peerRecord = new PeerRecord({
437+
peerId: this.peerId,
438+
multiaddrs: this._libp2p.multiaddrs
439+
})
440+
this._selfRecord = await Envelope.seal(peerRecord, this.peerId)
441+
442+
return this._selfRecord
443+
}
421444
}
422445

423446
module.exports.IdentifyService = IdentifyService

src/index.js

-3
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,6 @@ class Libp2p extends EventEmitter {
446446
// Listen on the provided transports
447447
await this.transportManager.listen()
448448

449-
// Start record Manager
450-
await this.recordManager.start()
451-
452449
// Start PeerStore
453450
await this.peerStore.start()
454451

test/identify/index.spec.js

+8-38
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ const duplexPair = require('it-pair/duplex')
1414
const multiaddr = require('multiaddr')
1515
const pWaitFor = require('p-wait-for')
1616

17-
const Envelope = require('../../src/record-manager/envelope')
18-
const PeerRecord = require('../../src/record-manager/peer-record')
19-
2017
const { codes: Errors } = require('../../src/errors')
2118
const { IdentifyService, multicodecs } = require('../../src/identify')
2219
const Peers = require('../fixtures/peers')
@@ -39,8 +36,8 @@ const protocolsLegacy = new Map([
3936
])
4037

4138
describe('Identify', () => {
42-
let localPeer, localPeerRecord
43-
let remotePeer, remotePeerRecord
39+
let localPeer
40+
let remotePeer
4441

4542
before(async () => {
4643
[localPeer, remotePeer] = (await Promise.all([
@@ -49,15 +46,6 @@ describe('Identify', () => {
4946
]))
5047
})
5148

52-
// Compute peer records
53-
before(async () => {
54-
// Compute PeerRecords
55-
const localRecord = new PeerRecord({ peerId: localPeer, multiaddrs: listenMaddrs })
56-
localPeerRecord = await Envelope.seal(localRecord, localPeer)
57-
const remoteRecord = new PeerRecord({ peerId: remotePeer, multiaddrs: listenMaddrs })
58-
remotePeerRecord = await Envelope.seal(remoteRecord, remotePeer)
59-
})
60-
6149
afterEach(() => {
6250
sinon.restore()
6351
})
@@ -136,10 +124,7 @@ describe('Identify', () => {
136124
set: () => { }
137125
}
138126
},
139-
multiaddrs: [],
140-
recordManager: {
141-
getPeerRecord: () => localPeerRecord
142-
}
127+
multiaddrs: listenMaddrs
143128
},
144129
protocols
145130
})
@@ -148,10 +133,7 @@ describe('Identify', () => {
148133
libp2p: {
149134
peerId: remotePeer,
150135
connectionManager: new EventEmitter(),
151-
multiaddrs: [],
152-
recordManager: {
153-
getPeerRecord: () => remotePeerRecord
154-
}
136+
multiaddrs: listenMaddrs
155137
},
156138
protocols
157139
})
@@ -206,21 +188,15 @@ describe('Identify', () => {
206188
set: () => { }
207189
}
208190
},
209-
multiaddrs: [],
210-
recordManager: {
211-
getPeerRecord: () => localPeerRecord
212-
}
191+
multiaddrs: []
213192
},
214193
protocols
215194
})
216195
const remoteIdentify = new IdentifyService({
217196
libp2p: {
218197
peerId: remotePeer,
219198
connectionManager: new EventEmitter(),
220-
multiaddrs: [],
221-
recordManager: {
222-
getPeerRecord: () => remotePeerRecord
223-
}
199+
multiaddrs: []
224200
},
225201
protocols
226202
})
@@ -319,10 +295,7 @@ describe('Identify', () => {
319295
libp2p: {
320296
peerId: localPeer,
321297
connectionManager: new EventEmitter(),
322-
multiaddrs: listenMaddrs,
323-
recordManager: {
324-
getPeerRecord: () => localPeerRecord
325-
}
298+
multiaddrs: listenMaddrs
326299
},
327300
protocols: new Map([
328301
[multicodecs.IDENTIFY],
@@ -342,10 +315,7 @@ describe('Identify', () => {
342315
set: () => { }
343316
}
344317
},
345-
multiaddrs: [],
346-
recordManager: {
347-
getPeerRecord: () => remotePeerRecord
348-
}
318+
multiaddrs: []
349319
}
350320
})
351321

0 commit comments

Comments
 (0)