Skip to content

Commit

Permalink
feat: create self peer record in identify
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jun 24, 2020
1 parent e21f13e commit 12022ab
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 50 deletions.
41 changes: 32 additions & 9 deletions src/identify/index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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,
Expand All @@ -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 {
/**
Expand Down Expand Up @@ -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
}

/**
Expand All @@ -108,7 +112,7 @@ class IdentifyService {
)
}

const envelope = this._libp2p.recordManager.getPeerRecord()
const envelope = await this._getSelfPeerRecord()
const signedPeerRecord = envelope.marshal()

await pipe(
Expand Down Expand Up @@ -265,13 +269,13 @@ class IdentifyService {
* @param {*} options.stream
* @param {Connection} options.connection
*/
_handleIdentify ({ connection, stream }) {
async _handleIdentify ({ connection, stream }) {
let publicKey = Buffer.alloc(0)
if (this.peerId.pubKey) {
publicKey = this.peerId.pubKey.bytes
}

const envelope = this._libp2p.recordManager.getPeerRecord()
const envelope = await this._getSelfPeerRecord()
const signedPeerRecord = envelope.marshal()

const message = Message.encode({
Expand Down Expand Up @@ -414,6 +418,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
Expand Down
3 changes: 0 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,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()

Expand Down
46 changes: 8 additions & 38 deletions test/identify/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -38,8 +35,8 @@ const protocolsLegacy = new Map([
])

describe('Identify', () => {
let localPeer, localPeerRecord
let remotePeer, remotePeerRecord
let localPeer
let remotePeer

before(async () => {
[localPeer, remotePeer] = (await Promise.all([
Expand All @@ -48,15 +45,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()
})
Expand Down Expand Up @@ -132,10 +120,7 @@ describe('Identify', () => {
set: () => { }
}
},
multiaddrs: [],
recordManager: {
getPeerRecord: () => localPeerRecord
}
multiaddrs: listenMaddrs
},
protocols
})
Expand All @@ -144,10 +129,7 @@ describe('Identify', () => {
libp2p: {
peerId: remotePeer,
connectionManager: new EventEmitter(),
multiaddrs: [],
recordManager: {
getPeerRecord: () => remotePeerRecord
}
multiaddrs: listenMaddrs
},
protocols
})
Expand Down Expand Up @@ -196,21 +178,15 @@ describe('Identify', () => {
set: () => { }
}
},
multiaddrs: [],
recordManager: {
getPeerRecord: () => localPeerRecord
}
multiaddrs: []
},
protocols
})
const remoteIdentify = new IdentifyService({
libp2p: {
peerId: remotePeer,
connectionManager: new EventEmitter(),
multiaddrs: [],
recordManager: {
getPeerRecord: () => remotePeerRecord
}
multiaddrs: []
},
protocols
})
Expand Down Expand Up @@ -309,10 +285,7 @@ describe('Identify', () => {
libp2p: {
peerId: localPeer,
connectionManager: new EventEmitter(),
multiaddrs: listenMaddrs,
recordManager: {
getPeerRecord: () => localPeerRecord
}
multiaddrs: listenMaddrs
},
protocols: new Map([
[multicodecs.IDENTIFY],
Expand All @@ -332,10 +305,7 @@ describe('Identify', () => {
set: () => { }
}
},
multiaddrs: [],
recordManager: {
getPeerRecord: () => remotePeerRecord
}
multiaddrs: []
}
})

Expand Down

0 comments on commit 12022ab

Please sign in to comment.