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 authored and jacobheun committed Aug 27, 2020
1 parent e50f0ee commit 8a97dde
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 49 deletions.
39 changes: 31 additions & 8 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 @@ -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({
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

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 @@ -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([
Expand 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()
})
Expand Down Expand Up @@ -136,10 +124,7 @@ describe('Identify', () => {
set: () => { }
}
},
multiaddrs: [],
recordManager: {
getPeerRecord: () => localPeerRecord
}
multiaddrs: listenMaddrs
},
protocols
})
Expand All @@ -148,10 +133,7 @@ describe('Identify', () => {
libp2p: {
peerId: remotePeer,
connectionManager: new EventEmitter(),
multiaddrs: [],
recordManager: {
getPeerRecord: () => remotePeerRecord
}
multiaddrs: listenMaddrs
},
protocols
})
Expand Down Expand Up @@ -206,21 +188,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 @@ -319,10 +295,7 @@ describe('Identify', () => {
libp2p: {
peerId: localPeer,
connectionManager: new EventEmitter(),
multiaddrs: listenMaddrs,
recordManager: {
getPeerRecord: () => localPeerRecord
}
multiaddrs: listenMaddrs
},
protocols: new Map([
[multicodecs.IDENTIFY],
Expand All @@ -342,10 +315,7 @@ describe('Identify', () => {
set: () => { }
}
},
multiaddrs: [],
recordManager: {
getPeerRecord: () => remotePeerRecord
}
multiaddrs: []
}
})

Expand Down

0 comments on commit 8a97dde

Please sign in to comment.