2020-05-06 16:55:43 +02:00
|
|
|
'use strict'
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
2020-10-15 15:31:33 +01:00
|
|
|
const { chai, expect } = require('aegir/utils/chai')
|
2020-05-07 10:46:00 +02:00
|
|
|
chai.use(require('chai-bytes'))
|
2020-05-06 16:55:43 +02:00
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
|
|
const baseOptions = require('../utils/base-options')
|
|
|
|
const peerUtils = require('../utils/creators/peer')
|
|
|
|
|
|
|
|
describe('libp2p.peerStore', () => {
|
|
|
|
let libp2p, remoteLibp2p
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
[libp2p, remoteLibp2p] = await peerUtils.createPeer({
|
|
|
|
number: 2,
|
|
|
|
populateAddressBooks: false,
|
|
|
|
config: {
|
|
|
|
...baseOptions
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-07 10:46:00 +02:00
|
|
|
it('adds peer address to AddressBook and keys to the keybook when establishing connection', async () => {
|
|
|
|
const remoteIdStr = remoteLibp2p.peerId.toB58String()
|
|
|
|
|
2020-05-06 16:55:43 +02:00
|
|
|
const spyAddressBook = sinon.spy(libp2p.peerStore.addressBook, 'add')
|
2020-05-07 10:46:00 +02:00
|
|
|
const spyKeyBook = sinon.spy(libp2p.peerStore.keyBook, 'set')
|
|
|
|
|
|
|
|
const remoteMultiaddr = `${remoteLibp2p.multiaddrs[0]}/p2p/${remoteIdStr}`
|
2020-05-06 16:55:43 +02:00
|
|
|
const conn = await libp2p.dial(remoteMultiaddr)
|
|
|
|
|
|
|
|
expect(conn).to.exist()
|
2020-05-07 10:46:00 +02:00
|
|
|
expect(spyAddressBook).to.have.property('called', true)
|
|
|
|
expect(spyKeyBook).to.have.property('called', true)
|
|
|
|
|
|
|
|
const localPeers = libp2p.peerStore.peers
|
|
|
|
expect(localPeers.size).to.equal(1)
|
2020-05-07 15:49:03 +02:00
|
|
|
|
2020-05-13 10:37:07 +02:00
|
|
|
const publicKeyInLocalPeer = localPeers.get(remoteIdStr).id.pubKey
|
|
|
|
expect(publicKeyInLocalPeer.bytes).to.equalBytes(remoteLibp2p.peerId.pubKey.bytes)
|
2020-05-07 10:46:00 +02:00
|
|
|
|
2020-05-13 10:30:57 +02:00
|
|
|
const publicKeyInRemotePeer = remoteLibp2p.peerStore.keyBook.get(libp2p.peerId)
|
2020-05-07 10:46:00 +02:00
|
|
|
expect(publicKeyInRemotePeer).to.exist()
|
|
|
|
expect(publicKeyInRemotePeer.bytes).to.equalBytes(libp2p.peerId.pubKey.bytes)
|
2020-05-06 16:55:43 +02:00
|
|
|
})
|
|
|
|
})
|