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