2019-11-07 12:11:50 +01:00
|
|
|
'use strict'
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
|
|
|
const chai = require('chai')
|
|
|
|
chai.use(require('dirty-chai'))
|
2019-11-29 16:41:08 +01:00
|
|
|
chai.use(require('chai-as-promised'))
|
2019-11-07 12:11:50 +01:00
|
|
|
const { expect } = chai
|
|
|
|
const sinon = require('sinon')
|
|
|
|
|
2020-04-18 17:06:56 +02:00
|
|
|
const { EventEmitter } = require('events')
|
2019-11-07 12:11:50 +01:00
|
|
|
const delay = require('delay')
|
|
|
|
const PeerId = require('peer-id')
|
|
|
|
const duplexPair = require('it-pair/duplex')
|
|
|
|
const multiaddr = require('multiaddr')
|
2020-02-05 17:35:27 +01:00
|
|
|
const pWaitFor = require('p-wait-for')
|
2019-11-07 12:11:50 +01:00
|
|
|
|
|
|
|
const { codes: Errors } = require('../../src/errors')
|
|
|
|
const { IdentifyService, multicodecs } = require('../../src/identify')
|
|
|
|
const Peers = require('../fixtures/peers')
|
|
|
|
const Libp2p = require('../../src')
|
|
|
|
const baseOptions = require('../utils/base-options.browser')
|
|
|
|
|
|
|
|
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
|
|
|
|
const remoteAddr = MULTIADDRS_WEBSOCKETS[0]
|
|
|
|
|
|
|
|
describe('Identify', () => {
|
|
|
|
let localPeer
|
|
|
|
let remotePeer
|
|
|
|
const protocols = new Map([
|
|
|
|
[multicodecs.IDENTIFY, () => {}],
|
|
|
|
[multicodecs.IDENTIFY_PUSH, () => {}]
|
|
|
|
])
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
[localPeer, remotePeer] = (await Promise.all([
|
|
|
|
PeerId.createFromJSON(Peers[0]),
|
|
|
|
PeerId.createFromJSON(Peers[1])
|
2020-04-14 14:05:30 +02:00
|
|
|
]))
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sinon.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to identify another peer', async () => {
|
|
|
|
const localIdentify = new IdentifyService({
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId: localPeer,
|
|
|
|
addresses: {
|
|
|
|
listen: []
|
|
|
|
},
|
2019-11-07 12:11:50 +01:00
|
|
|
protocols,
|
2020-04-18 17:06:56 +02:00
|
|
|
connectionManager: new EventEmitter(),
|
|
|
|
peerStore: {
|
|
|
|
addressBook: {
|
|
|
|
set: () => { }
|
|
|
|
},
|
|
|
|
protoBook: {
|
|
|
|
set: () => { }
|
2019-11-07 12:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const remoteIdentify = new IdentifyService({
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId: remotePeer,
|
|
|
|
addresses: {
|
|
|
|
listen: []
|
|
|
|
},
|
2020-04-18 17:06:56 +02:00
|
|
|
protocols,
|
|
|
|
connectionManager: new EventEmitter()
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
2020-04-14 14:05:30 +02:00
|
|
|
const localConnectionMock = { newStream: () => {}, remotePeer }
|
2019-11-07 12:11:50 +01:00
|
|
|
const remoteConnectionMock = { remoteAddr: observedAddr }
|
|
|
|
|
|
|
|
const [local, remote] = duplexPair()
|
|
|
|
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })
|
|
|
|
|
2020-04-18 17:06:56 +02:00
|
|
|
sinon.spy(localIdentify.peerStore.addressBook, 'set')
|
|
|
|
sinon.spy(localIdentify.peerStore.protoBook, 'set')
|
2019-11-07 12:11:50 +01:00
|
|
|
|
|
|
|
// Run identify
|
|
|
|
await Promise.all([
|
2019-12-03 20:14:15 +01:00
|
|
|
localIdentify.identify(localConnectionMock),
|
2019-11-07 12:11:50 +01:00
|
|
|
remoteIdentify.handleMessage({
|
|
|
|
connection: remoteConnectionMock,
|
|
|
|
stream: remote,
|
|
|
|
protocol: multicodecs.IDENTIFY
|
|
|
|
})
|
|
|
|
])
|
|
|
|
|
2020-04-18 17:06:56 +02:00
|
|
|
expect(localIdentify.peerStore.addressBook.set.callCount).to.equal(1)
|
|
|
|
expect(localIdentify.peerStore.protoBook.set.callCount).to.equal(1)
|
2019-11-07 12:11:50 +01:00
|
|
|
// Validate the remote peer gets updated in the peer store
|
2020-04-18 17:06:56 +02:00
|
|
|
const call = localIdentify.peerStore.addressBook.set.firstCall
|
2020-04-14 14:05:30 +02:00
|
|
|
expect(call.args[0].id.bytes).to.equal(remotePeer.bytes)
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should throw if identified peer is the wrong peer', async () => {
|
|
|
|
const localIdentify = new IdentifyService({
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId: localPeer,
|
|
|
|
addresses: {
|
|
|
|
listen: []
|
|
|
|
},
|
2020-01-22 11:47:30 +01:00
|
|
|
protocols,
|
2020-04-18 17:06:56 +02:00
|
|
|
connectionManager: new EventEmitter(),
|
|
|
|
peerStore: {
|
|
|
|
addressBook: {
|
|
|
|
set: () => { }
|
|
|
|
},
|
|
|
|
protoBook: {
|
|
|
|
set: () => { }
|
2020-01-22 11:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
const remoteIdentify = new IdentifyService({
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId: remotePeer,
|
|
|
|
addresses: {
|
|
|
|
listen: []
|
|
|
|
},
|
2020-04-18 17:06:56 +02:00
|
|
|
protocols,
|
|
|
|
connectionManager: new EventEmitter()
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
2020-04-14 14:05:30 +02:00
|
|
|
const localConnectionMock = { newStream: () => {}, remotePeer: localPeer }
|
2019-11-07 12:11:50 +01:00
|
|
|
const remoteConnectionMock = { remoteAddr: observedAddr }
|
|
|
|
|
|
|
|
const [local, remote] = duplexPair()
|
|
|
|
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })
|
|
|
|
|
|
|
|
// Run identify
|
2019-11-29 16:41:08 +01:00
|
|
|
const identifyPromise = Promise.all([
|
2020-04-14 14:05:30 +02:00
|
|
|
localIdentify.identify(localConnectionMock, localPeer),
|
2019-11-29 16:41:08 +01:00
|
|
|
remoteIdentify.handleMessage({
|
|
|
|
connection: remoteConnectionMock,
|
|
|
|
stream: remote,
|
|
|
|
protocol: multicodecs.IDENTIFY
|
|
|
|
})
|
|
|
|
])
|
|
|
|
|
|
|
|
await expect(identifyPromise)
|
|
|
|
.to.eventually.be.rejected()
|
|
|
|
.and.to.have.property('code', Errors.ERR_INVALID_PEER)
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('push', () => {
|
|
|
|
it('should be able to push identify updates to another peer', async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
const listeningAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
2020-04-18 17:06:56 +02:00
|
|
|
const connectionManager = new EventEmitter()
|
|
|
|
connectionManager.getConnection = () => {}
|
|
|
|
|
2019-11-07 12:11:50 +01:00
|
|
|
const localIdentify = new IdentifyService({
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId: localPeer,
|
|
|
|
addresses: {
|
|
|
|
listen: [listeningAddr]
|
|
|
|
},
|
2020-04-18 17:06:56 +02:00
|
|
|
connectionManager,
|
2019-11-07 12:11:50 +01:00
|
|
|
protocols: new Map([
|
|
|
|
[multicodecs.IDENTIFY],
|
|
|
|
[multicodecs.IDENTIFY_PUSH],
|
|
|
|
['/echo/1.0.0']
|
|
|
|
])
|
|
|
|
})
|
|
|
|
const remoteIdentify = new IdentifyService({
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId: remotePeer,
|
|
|
|
addresses: {
|
|
|
|
listen: []
|
|
|
|
},
|
2020-04-18 17:06:56 +02:00
|
|
|
connectionManager,
|
|
|
|
peerStore: {
|
|
|
|
addressBook: {
|
|
|
|
set: () => { }
|
|
|
|
},
|
|
|
|
protoBook: {
|
|
|
|
set: () => { }
|
2019-11-07 12:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Setup peer protocols and multiaddrs
|
|
|
|
const localProtocols = new Set([multicodecs.IDENTIFY, multicodecs.IDENTIFY_PUSH, '/echo/1.0.0'])
|
|
|
|
const localConnectionMock = { newStream: () => {} }
|
2020-04-14 14:05:30 +02:00
|
|
|
const remoteConnectionMock = { remotePeer: localPeer }
|
2019-11-07 12:11:50 +01:00
|
|
|
|
|
|
|
const [local, remote] = duplexPair()
|
|
|
|
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_PUSH })
|
|
|
|
|
2020-04-18 17:06:56 +02:00
|
|
|
sinon.spy(remoteIdentify.peerStore.addressBook, 'set')
|
|
|
|
sinon.spy(remoteIdentify.peerStore.protoBook, 'set')
|
2019-11-07 12:11:50 +01:00
|
|
|
|
|
|
|
// Run identify
|
|
|
|
await Promise.all([
|
|
|
|
localIdentify.push([localConnectionMock]),
|
|
|
|
remoteIdentify.handleMessage({
|
|
|
|
connection: remoteConnectionMock,
|
|
|
|
stream: remote,
|
|
|
|
protocol: multicodecs.IDENTIFY_PUSH
|
|
|
|
})
|
|
|
|
])
|
|
|
|
|
2020-04-18 17:06:56 +02:00
|
|
|
expect(remoteIdentify.peerStore.addressBook.set.callCount).to.equal(1)
|
|
|
|
expect(remoteIdentify.peerStore.protoBook.set.callCount).to.equal(1)
|
|
|
|
const [peerId, multiaddrs] = remoteIdentify.peerStore.addressBook.set.firstCall.args
|
2020-04-14 14:05:30 +02:00
|
|
|
expect(peerId.bytes).to.eql(localPeer.bytes)
|
2020-04-09 16:07:18 +02:00
|
|
|
expect(multiaddrs).to.eql([listeningAddr])
|
2020-04-18 17:06:56 +02:00
|
|
|
const [peerId2, protocols] = remoteIdentify.peerStore.protoBook.set.firstCall.args
|
2020-04-14 14:05:30 +02:00
|
|
|
expect(peerId2.bytes).to.eql(localPeer.bytes)
|
2020-04-09 16:07:18 +02:00
|
|
|
expect(protocols).to.eql(Array.from(localProtocols))
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('libp2p.dialer.identifyService', () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
let peerId
|
2019-11-07 12:11:50 +01:00
|
|
|
let libp2p
|
|
|
|
let remoteLibp2p
|
|
|
|
|
|
|
|
before(async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId = await PeerId.createFromJSON(Peers[0])
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
sinon.restore()
|
|
|
|
libp2p && await libp2p.stop()
|
|
|
|
libp2p = null
|
|
|
|
})
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
remoteLibp2p && await remoteLibp2p.stop()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should run identify automatically after connecting', async () => {
|
|
|
|
libp2p = new Libp2p({
|
|
|
|
...baseOptions,
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
|
2019-12-03 10:28:52 +01:00
|
|
|
sinon.spy(libp2p.identifyService, 'identify')
|
2020-04-09 16:07:18 +02:00
|
|
|
const peerStoreSpySet = sinon.spy(libp2p.peerStore.addressBook, 'set')
|
|
|
|
const peerStoreSpyAdd = sinon.spy(libp2p.peerStore.addressBook, 'add')
|
2019-11-07 12:11:50 +01:00
|
|
|
|
2019-12-15 17:33:16 +01:00
|
|
|
const connection = await libp2p.dialer.connectToPeer(remoteAddr)
|
2019-11-07 12:11:50 +01:00
|
|
|
expect(connection).to.exist()
|
2020-02-05 17:35:27 +01:00
|
|
|
|
|
|
|
// Wait for peer store to be updated
|
2020-04-09 16:07:18 +02:00
|
|
|
// Dialer._createDialTarget (add), Identify (replace)
|
|
|
|
await pWaitFor(() => peerStoreSpySet.callCount === 1 && peerStoreSpyAdd.callCount === 1)
|
2019-12-03 10:28:52 +01:00
|
|
|
expect(libp2p.identifyService.identify.callCount).to.equal(1)
|
2019-11-07 12:11:50 +01:00
|
|
|
|
2020-02-05 17:35:27 +01:00
|
|
|
// The connection should have no open streams
|
|
|
|
expect(connection.streams).to.have.length(0)
|
2019-11-07 12:11:50 +01:00
|
|
|
await connection.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push protocol updates to an already connected peer', async () => {
|
|
|
|
libp2p = new Libp2p({
|
|
|
|
...baseOptions,
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
|
2019-12-03 10:28:52 +01:00
|
|
|
sinon.spy(libp2p.identifyService, 'identify')
|
|
|
|
sinon.spy(libp2p.identifyService, 'push')
|
2019-11-07 12:11:50 +01:00
|
|
|
|
2019-12-15 17:33:16 +01:00
|
|
|
const connection = await libp2p.dialer.connectToPeer(remoteAddr)
|
2019-11-07 12:11:50 +01:00
|
|
|
expect(connection).to.exist()
|
|
|
|
// Wait for nextTick to trigger the identify call
|
|
|
|
await delay(1)
|
|
|
|
|
|
|
|
// Wait for identify to finish
|
2019-12-03 10:28:52 +01:00
|
|
|
await libp2p.identifyService.identify.firstCall.returnValue
|
2019-11-29 16:41:08 +01:00
|
|
|
sinon.stub(libp2p, 'isStarted').returns(true)
|
2019-11-07 12:11:50 +01:00
|
|
|
|
|
|
|
libp2p.handle('/echo/2.0.0', () => {})
|
|
|
|
libp2p.unhandle('/echo/2.0.0')
|
|
|
|
|
|
|
|
// Verify the remote peer is notified of both changes
|
2019-12-03 10:28:52 +01:00
|
|
|
expect(libp2p.identifyService.push.callCount).to.equal(2)
|
|
|
|
for (const call of libp2p.identifyService.push.getCalls()) {
|
2019-11-07 12:11:50 +01:00
|
|
|
const [connections] = call.args
|
|
|
|
expect(connections.length).to.equal(1)
|
|
|
|
expect(connections[0].remotePeer.toB58String()).to.equal(remoteAddr.getPeerId())
|
|
|
|
const results = await call.returnValue
|
|
|
|
expect(results.length).to.equal(1)
|
|
|
|
}
|
2020-02-05 17:35:27 +01:00
|
|
|
|
|
|
|
// Verify the streams close
|
|
|
|
await pWaitFor(() => connection.streams.length === 0)
|
2019-11-07 12:11:50 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|