mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-19 05:51:20 +00:00
feat: create self peer record in identify
This commit is contained in:
parent
f8354577a2
commit
83922a7fad
@ -1,7 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:identify')
|
||||
log.error = debug('libp2p:identify:error')
|
||||
|
||||
const errCode = require('err-code')
|
||||
const { Buffer } = require('buffer')
|
||||
const pb = require('it-protocol-buffers')
|
||||
const lp = require('it-length-prefixed')
|
||||
const pipe = require('it-pipe')
|
||||
@ -13,8 +17,8 @@ const { toBuffer } = require('it-buffer')
|
||||
|
||||
const Message = require('./message')
|
||||
|
||||
const log = debug('libp2p:identify')
|
||||
log.error = debug('libp2p:identify:error')
|
||||
const Envelope = require('../record/envelope')
|
||||
const PeerRecord = require('../record/peer-record')
|
||||
|
||||
const {
|
||||
MULTICODEC_IDENTIFY,
|
||||
@ -25,10 +29,7 @@ const {
|
||||
PROTOCOL_VERSION
|
||||
} = require('./consts')
|
||||
|
||||
const errCode = require('err-code')
|
||||
const { messages, codes } = require('../errors')
|
||||
const Envelope = require('../record-manager/envelope')
|
||||
const PeerRecord = require('../record-manager/peer-record')
|
||||
|
||||
class IdentifyService {
|
||||
/**
|
||||
@ -83,6 +84,9 @@ class IdentifyService {
|
||||
this._protocols = protocols
|
||||
|
||||
this.handleMessage = this.handleMessage.bind(this)
|
||||
|
||||
// TODO: this should be stored in the certified AddressBook in follow up PR
|
||||
this._selfRecord = undefined
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,7 +112,7 @@ class IdentifyService {
|
||||
)
|
||||
}
|
||||
|
||||
const envelope = this._libp2p.recordManager.getPeerRecord()
|
||||
const envelope = await this._getSelfPeerRecord()
|
||||
const signedPeerRecord = envelope.marshal()
|
||||
|
||||
await pipe(
|
||||
@ -272,7 +276,7 @@ class IdentifyService {
|
||||
publicKey = this.peerId.pubKey.bytes
|
||||
}
|
||||
|
||||
const envelope = this._libp2p.recordManager.getPeerRecord()
|
||||
const envelope = await this._getSelfPeerRecord()
|
||||
const signedPeerRecord = envelope.marshal()
|
||||
|
||||
const message = Message.encode({
|
||||
@ -418,6 +422,25 @@ class IdentifyService {
|
||||
// Update the protocols
|
||||
this.peerStore.protoBook.set(id, message.protocols)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get self signed peer record envelope.
|
||||
* @return {Envelope}
|
||||
*/
|
||||
async _getSelfPeerRecord () {
|
||||
// TODO: Verify if updated
|
||||
if (this._selfRecord) {
|
||||
return this._selfRecord
|
||||
}
|
||||
|
||||
const peerRecord = new PeerRecord({
|
||||
peerId: this.peerId,
|
||||
multiaddrs: this._libp2p.multiaddrs
|
||||
})
|
||||
this._selfRecord = await Envelope.seal(peerRecord, this.peerId)
|
||||
|
||||
return this._selfRecord
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.IdentifyService = IdentifyService
|
||||
|
@ -446,9 +446,6 @@ class Libp2p extends EventEmitter {
|
||||
// Listen on the provided transports
|
||||
await this.transportManager.listen()
|
||||
|
||||
// Start record Manager
|
||||
await this.recordManager.start()
|
||||
|
||||
// Start PeerStore
|
||||
await this.peerStore.start()
|
||||
|
||||
|
@ -14,9 +14,6 @@ const duplexPair = require('it-pair/duplex')
|
||||
const multiaddr = require('multiaddr')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
|
||||
const Envelope = require('../../src/record-manager/envelope')
|
||||
const PeerRecord = require('../../src/record-manager/peer-record')
|
||||
|
||||
const { codes: Errors } = require('../../src/errors')
|
||||
const { IdentifyService, multicodecs } = require('../../src/identify')
|
||||
const Peers = require('../fixtures/peers')
|
||||
@ -39,8 +36,8 @@ const protocolsLegacy = new Map([
|
||||
])
|
||||
|
||||
describe('Identify', () => {
|
||||
let localPeer, localPeerRecord
|
||||
let remotePeer, remotePeerRecord
|
||||
let localPeer
|
||||
let remotePeer
|
||||
|
||||
before(async () => {
|
||||
[localPeer, remotePeer] = (await Promise.all([
|
||||
@ -49,15 +46,6 @@ describe('Identify', () => {
|
||||
]))
|
||||
})
|
||||
|
||||
// Compute peer records
|
||||
before(async () => {
|
||||
// Compute PeerRecords
|
||||
const localRecord = new PeerRecord({ peerId: localPeer, multiaddrs: listenMaddrs })
|
||||
localPeerRecord = await Envelope.seal(localRecord, localPeer)
|
||||
const remoteRecord = new PeerRecord({ peerId: remotePeer, multiaddrs: listenMaddrs })
|
||||
remotePeerRecord = await Envelope.seal(remoteRecord, remotePeer)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
@ -136,10 +124,7 @@ describe('Identify', () => {
|
||||
set: () => { }
|
||||
}
|
||||
},
|
||||
multiaddrs: [],
|
||||
recordManager: {
|
||||
getPeerRecord: () => localPeerRecord
|
||||
}
|
||||
multiaddrs: listenMaddrs
|
||||
},
|
||||
protocols
|
||||
})
|
||||
@ -148,10 +133,7 @@ describe('Identify', () => {
|
||||
libp2p: {
|
||||
peerId: remotePeer,
|
||||
connectionManager: new EventEmitter(),
|
||||
multiaddrs: [],
|
||||
recordManager: {
|
||||
getPeerRecord: () => remotePeerRecord
|
||||
}
|
||||
multiaddrs: listenMaddrs
|
||||
},
|
||||
protocols
|
||||
})
|
||||
@ -206,10 +188,7 @@ describe('Identify', () => {
|
||||
set: () => { }
|
||||
}
|
||||
},
|
||||
multiaddrs: [],
|
||||
recordManager: {
|
||||
getPeerRecord: () => localPeerRecord
|
||||
}
|
||||
multiaddrs: []
|
||||
},
|
||||
protocols
|
||||
})
|
||||
@ -217,10 +196,7 @@ describe('Identify', () => {
|
||||
libp2p: {
|
||||
peerId: remotePeer,
|
||||
connectionManager: new EventEmitter(),
|
||||
multiaddrs: [],
|
||||
recordManager: {
|
||||
getPeerRecord: () => remotePeerRecord
|
||||
}
|
||||
multiaddrs: []
|
||||
},
|
||||
protocols
|
||||
})
|
||||
@ -319,10 +295,7 @@ describe('Identify', () => {
|
||||
libp2p: {
|
||||
peerId: localPeer,
|
||||
connectionManager: new EventEmitter(),
|
||||
multiaddrs: listenMaddrs,
|
||||
recordManager: {
|
||||
getPeerRecord: () => localPeerRecord
|
||||
}
|
||||
multiaddrs: listenMaddrs
|
||||
},
|
||||
protocols: new Map([
|
||||
[multicodecs.IDENTIFY],
|
||||
@ -342,10 +315,7 @@ describe('Identify', () => {
|
||||
set: () => { }
|
||||
}
|
||||
},
|
||||
multiaddrs: [],
|
||||
recordManager: {
|
||||
getPeerRecord: () => remotePeerRecord
|
||||
}
|
||||
multiaddrs: []
|
||||
}
|
||||
})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user