fix: reconnect should trigger topology on connect if protocol stored (#54)

* fix: reconnect should trigger topology on connect if protocol stored

* chore: address review
This commit is contained in:
Vasco Santos 2020-07-03 15:48:12 +02:00 committed by GitHub
parent 9fbf9d0331
commit e10a1545c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 0 deletions

View File

@ -43,11 +43,13 @@ class MulticodecTopology extends Topology {
this._registrar = undefined this._registrar = undefined
this._onProtocolChange = this._onProtocolChange.bind(this) this._onProtocolChange = this._onProtocolChange.bind(this)
this._onPeerConnect = this._onPeerConnect.bind(this)
} }
set registrar (registrar) { set registrar (registrar) {
this._registrar = registrar this._registrar = registrar
this._registrar.peerStore.on('change:protocols', this._onProtocolChange) this._registrar.peerStore.on('change:protocols', this._onProtocolChange)
this._registrar.connectionManager.on('peer:connect', this._onPeerConnect)
// Update topology peers // Update topology peers
this._updatePeers(this._registrar.peerStore.peers.values()) this._updatePeers(this._registrar.peerStore.peers.values())
@ -97,6 +99,25 @@ class MulticodecTopology extends Topology {
} }
} }
} }
/**
* Verify if a new connected peer has a topology multicodec and call _onConnect.
* @param {Connection} connection
* @returns {void}
*/
_onPeerConnect (connection) {
const peerId = connection.remotePeer
const protocols = this._registrar.peerStore.protoBook.get(peerId)
if (!protocols) {
return
}
if (this.multicodecs.find(multicodec => protocols.includes(multicodec))) {
this.peers.add(peerId.toB58String())
this._onConnect(peerId, connection)
}
}
} }
module.exports = withIs(MulticodecTopology, { className: 'MulticodecTopology', symbolName: '@libp2p/js-interfaces/topology/multicodec-topology' }) module.exports = withIs(MulticodecTopology, { className: 'MulticodecTopology', symbolName: '@libp2p/js-interfaces/topology/multicodec-topology' })

View File

@ -96,5 +96,38 @@ module.exports = (test) => {
expect(topology._onDisconnect.callCount).to.equal(1) expect(topology._onDisconnect.callCount).to.equal(1)
expect(topology._onDisconnect.calledWith(id2)).to.equal(true) expect(topology._onDisconnect.calledWith(id2)).to.equal(true)
}) })
it('should trigger "onConnect" when a peer connects and has one of the topology multicodecs in its known protocols', () => {
sinon.spy(topology, '_onConnect')
sinon.stub(topology._registrar.peerStore.protoBook, 'get').returns(topology.multicodecs)
topology._registrar.connectionManager.emit('peer:connect', {
remotePeer: id
})
expect(topology._onConnect.callCount).to.equal(1)
})
it('should not trigger "onConnect" when a peer connects and has none of the topology multicodecs in its known protocols', () => {
sinon.spy(topology, '_onConnect')
sinon.stub(topology._registrar.peerStore.protoBook, 'get').returns([])
topology._registrar.connectionManager.emit('peer:connect', {
remotePeer: id
})
expect(topology._onConnect.callCount).to.equal(0)
})
it('should not trigger "onConnect" when a peer connects and its protocols are not known', () => {
sinon.spy(topology, '_onConnect')
sinon.stub(topology._registrar.peerStore.protoBook, 'get').returns(undefined)
topology._registrar.connectionManager.emit('peer:connect', {
remotePeer: id
})
expect(topology._onConnect.callCount).to.equal(0)
})
}) })
} }

View File

@ -6,6 +6,9 @@ class MockPeerStore extends EventEmitter {
constructor (peers) { constructor (peers) {
super() super()
this.peers = peers this.peers = peers
this.protoBook = {
get: () => {}
}
} }
get (peerId) { get (peerId) {

View File

@ -1,6 +1,8 @@
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'
const { EventEmitter } = require('events')
const tests = require('../../src/topology/tests/multicodec-topology') const tests = require('../../src/topology/tests/multicodec-topology')
const MulticodecTopology = require('../../src/topology/multicodec-topology') const MulticodecTopology = require('../../src/topology/multicodec-topology')
const MockPeerStore = require('./mock-peer-store') const MockPeerStore = require('./mock-peer-store')
@ -23,9 +25,11 @@ describe('multicodec topology compliance tests', () => {
if (!registrar) { if (!registrar) {
const peers = new Map() const peers = new Map()
const peerStore = new MockPeerStore(peers) const peerStore = new MockPeerStore(peers)
const connectionManager = new EventEmitter()
registrar = { registrar = {
peerStore, peerStore,
connectionManager,
getConnection: () => { } getConnection: () => { }
} }
} }