Compare commits

..

4 Commits

Author SHA1 Message Date
Vasco Santos
28eeaf6977 chore: skip abort while reading for webrtc 2020-04-17 18:15:41 +02:00
Vasco Santos
170c2fd46e chore: apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
2020-04-16 16:02:26 +02:00
Vasco Santos
12f11c88e2 chore: add tests for peer-discovery 2020-04-16 16:02:26 +02:00
Vasco Santos
e5f4bdac44 feat: peer-discovery not using peer-info
BREAKING CHANGE: peer-discovery emits object with id and multiaddrs properties
2020-04-16 16:02:26 +02:00
14 changed files with 78 additions and 153 deletions

View File

@@ -1,31 +1,3 @@
<a name="0.3.0"></a>
# [0.3.0](https://github.com/libp2p/js-interfaces/compare/v0.2.8...v0.3.0) (2020-04-21)
### Chores
* remove peer-info usage on topology ([#42](https://github.com/libp2p/js-interfaces/issues/42)) ([79a7843](https://github.com/libp2p/js-interfaces/commit/79a7843))
* update content and peer routing interfaces removing peer-info ([#43](https://github.com/libp2p/js-interfaces/issues/43)) ([d2032e6](https://github.com/libp2p/js-interfaces/commit/d2032e6))
### Features
* peer-discovery not using peer-info ([5792b13](https://github.com/libp2p/js-interfaces/commit/5792b13))
### BREAKING CHANGES
* topology api now uses peer-id instead of peer-info
* content-routing and peer-routing APIs return an object with relevant properties instead of peer-info
* peer-discovery emits object with id and multiaddrs properties
<a name="0.2.8"></a>
## [0.2.8](https://github.com/libp2p/js-interfaces/compare/v0.2.7...v0.2.8) (2020-04-21)
<a name="0.2.7"></a> <a name="0.2.7"></a>
## [0.2.7](https://github.com/libp2p/js-interfaces/compare/v0.2.6...v0.2.7) (2020-03-20) ## [0.2.7](https://github.com/libp2p/js-interfaces/compare/v0.2.6...v0.2.7) (2020-03-20)

View File

@@ -1,6 +1,6 @@
{ {
"name": "libp2p-interfaces", "name": "libp2p-interfaces",
"version": "0.3.0", "version": "0.2.7",
"description": "Interfaces for JS Libp2p", "description": "Interfaces for JS Libp2p",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>", "leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js", "main": "src/index.js",
@@ -35,7 +35,7 @@
"dependencies": { "dependencies": {
"abort-controller": "^3.0.0", "abort-controller": "^3.0.0",
"abortable-iterator": "^3.0.0", "abortable-iterator": "^3.0.0",
"buffer": "^5.6.0", "buffer": "^5.5.0",
"chai": "^4.2.0", "chai": "^4.2.0",
"chai-checkmark": "^1.0.1", "chai-checkmark": "^1.0.1",
"class-is": "^1.1.0", "class-is": "^1.1.0",
@@ -47,17 +47,17 @@
"it-pair": "^1.0.0", "it-pair": "^1.0.0",
"it-pipe": "^1.0.1", "it-pipe": "^1.0.1",
"libp2p-tcp": "^0.14.1", "libp2p-tcp": "^0.14.1",
"multiaddr": "^7.4.3", "multiaddr": "^7.1.0",
"p-defer": "^3.0.0", "p-defer": "^3.0.0",
"p-limit": "^2.3.0", "p-limit": "^2.2.2",
"p-wait-for": "^3.1.0", "p-wait-for": "^3.1.0",
"peer-id": "^0.13.11", "peer-id": "^0.13.3",
"peer-info": "^0.17.0", "peer-info": "^0.17.0",
"sinon": "^9.0.2", "sinon": "^9.0.0",
"streaming-iterables": "^4.1.0" "streaming-iterables": "^4.1.0"
}, },
"devDependencies": { "devDependencies": {
"aegir": "^21.9.0", "aegir": "^21.0.2",
"it-handshake": "^1.0.1" "it-handshake": "^1.0.1"
}, },
"contributors": [ "contributors": [

View File

@@ -30,32 +30,6 @@ TBD
A valid (read: that follows this abstraction) Content Routing module must implement the following API. A valid (read: that follows this abstraction) Content Routing module must implement the following API.
### findProviders ### `.findProviders`
- `findProviders(cid)` ### `.provide`
Find peers in the network that can provide a specific value, given a key.
**Parameters**
- [CID](https://github.com/multiformats/js-cid)
**Returns**
It returns an `AsyncIterable` containing the identification and addresses of the peers providing the given key, as follows:
`AsyncIterable<{ id: PeerId, addrs: Multiaddr[] }>`
### provide
- `provide(cid)`
Announce to the network that we are providing the given value.
**Parameters**
- [CID](https://github.com/multiformats/js-cid)
**Returns**
It returns a promise that is resolved on the success of the operation.
`Promise<void>`

View File

@@ -49,7 +49,7 @@ module.exports = (common) => {
const defer = pDefer() const defer = pDefer()
await discovery.start() await discovery.start()
discovery.on('peer', ({ id, multiaddrs }) => { discovery.once('peer', ({ id, multiaddrs }) => {
expect(id).to.exist() expect(id).to.exist()
expect(PeerId.isPeerId(id)).to.eql(true) expect(PeerId.isPeerId(id)).to.eql(true)
expect(multiaddrs).to.exist() expect(multiaddrs).to.exist()
@@ -63,7 +63,7 @@ module.exports = (common) => {
}) })
it('should not receive a peer event before start', async () => { it('should not receive a peer event before start', async () => {
discovery.on('peer', () => { discovery.once('peer', () => {
throw new Error('should not receive a peer event before start') throw new Error('should not receive a peer event before start')
}) })
@@ -75,14 +75,14 @@ module.exports = (common) => {
await discovery.start() await discovery.start()
discovery.on('peer', () => { discovery.once('peer', () => {
deferStart.resolve() deferStart.resolve()
}) })
await deferStart.promise await deferStart.promise
await discovery.stop() await discovery.stop()
discovery.on('peer', () => { discovery.once('peer', () => {
throw new Error('should not receive a peer event after stop') throw new Error('should not receive a peer event after stop')
}) })

View File

@@ -30,17 +30,12 @@ TBD
A valid (read: that follows this abstraction) Peer Routing module must implement the following API. A valid (read: that follows this abstraction) Peer Routing module must implement the following API.
### findPeer ### `.findPeers` - Find peers 'responsible' or 'closest' to a given key
- `findPeer(peerId)` - `Node.js` peerRouting.findPeers(key, function (err, peersPriorityQueue) {})
Query the network for all multiaddresses associated with a `PeerId`. In a peer to peer context, the concept of 'responsability' or 'closeness' for a given key translates to having a way to find deterministically or that at least there is a significant overlap between searches, the same group of peers when searching for the same given key.
**Parameters** This method will query the network (route it) and return a Priority Queue datastructe with a list of PeerInfo objects, ordered by 'closeness'.
- [peerId](https://github.com/libp2p/js-peer-id).
**Returns** key is a multihash
It returns the [peerId](https://github.com/libp2p/js-peer-id) together with the known peers [multiaddrs](https://github.com/multiformats/js-multiaddr), as follows:
`Promise<{ id: PeerId, addrs: Multiaddr[] }>`

View File

@@ -56,8 +56,8 @@ const toplogy = new MulticodecTopology({
max: 50, max: 50,
multicodecs: ['/echo/1.0.0'], multicodecs: ['/echo/1.0.0'],
handlers: { handlers: {
onConnect: (peerId, conn) => {}, onConnect: (peerInfo, conn) => {},
onDisconnect: (peerId) => {} onDisconnect: (peerInfo) => {}
} }
}) })
``` ```
@@ -69,8 +69,8 @@ The `MulticodecTopology` extends the `Topology`, which makes the `Topology` API
### Topology ### Topology
- `Topology` - `Topology`
- `peers<Map<string, PeerId>>`: A Map of peers belonging to the topology. - `peers<Map<string, PeerInfo>>`: A Map of peers belonging to the topology.
- `disconnect<function(PeerId)>`: Called when a peer has been disconnected - `disconnect<function(PeerInfo)>`: Called when a peer has been disconnected
#### Constructor #### Constructor
@@ -79,8 +79,8 @@ const toplogy = new Topology({
min: 0, min: 0,
max: 50, max: 50,
handlers: { handlers: {
onConnect: (peerId, conn) => {}, onConnect: (peerInfo, conn) => {},
onDisconnect: (peerId) => {} onDisconnect: (peerInfo) => {}
} }
}) })
``` ```
@@ -95,27 +95,27 @@ const toplogy = new Topology({
#### Set a peer #### Set a peer
- `topology.peers.set(id, peerId)` - `topology.peers.set(id, peerInfo)`
Add a peer to the topology. Add a peer to the topology.
**Parameters** **Parameters**
- `id` is the `string` that identifies the peer to add. - `id` is the `string` that identifies the peer to add.
- `peerId` is the [PeerId][peer-id] of the peer to add. - `peerInfo` is the [PeerInfo][peer-info] of the peer to add.
#### Notify about a peer disconnected event #### Notify about a peer disconnected event
- `topology.disconnect(peerId)` - `topology.disconnect(peerInfo)`
**Parameters** **Parameters**
- `peerId` is the [PeerId][peer-id] of the peer disconnected. - `peerInfo` is the [PeerInfo][peer-info] of the peer disconnected.
### Multicodec Topology ### Multicodec Topology
- `MulticodecTopology` - `MulticodecTopology`
- `registrar<Registrar>`: The `Registrar` of the topology. This is set by the `Registrar` during registration. - `registrar<Registrar>`: The `Registrar` of the topology. This is set by the `Registrar` during registration.
- `peers<Map<string, PeerId>>`: The Map of peers that belong to the topology - `peers<Map<string, PeerInfo>>`: The Map of peers that belong to the topology
- `disconnect<function(PeerId)>`: Disconnects a peer from the topology. - `disconnect<function(PeerInfo)>`: Disconnects a peer from the topology.
#### Constructor #### Constructor
@@ -125,8 +125,8 @@ const toplogy = new MulticodecTopology({
max: 50, max: 50,
multicodecs: ['/echo/1.0.0'], multicodecs: ['/echo/1.0.0'],
handlers: { handlers: {
onConnect: (peerId, conn) => {}, onConnect: (peerInfo, conn) => {},
onDisconnect: (peerId) => {} onDisconnect: (peerInfo) => {}
} }
}) })
``` ```
@@ -139,5 +139,3 @@ const toplogy = new MulticodecTopology({
- `handlers` is an optional `Object` containing the handler called when a peer is connected or disconnected. - `handlers` is an optional `Object` containing the handler called when a peer is connected or disconnected.
- `onConnect` is a `function` called everytime a peer is connected in the topology context. - `onConnect` is a `function` called everytime a peer is connected in the topology context.
- `onDisconnect` is a `function` called everytime a peer is disconnected in the topology context. - `onDisconnect` is a `function` called everytime a peer is disconnected in the topology context.
[peer-id]: https://github.com/libp2p/js-peer-id

View File

@@ -26,11 +26,7 @@ class Topology {
this._onConnect = handlers.onConnect || noop this._onConnect = handlers.onConnect || noop
this._onDisconnect = handlers.onDisconnect || noop this._onDisconnect = handlers.onDisconnect || noop
/** this.peers = new Map()
* Set of peers that support the protocol.
* @type {Set<string>}
*/
this.peers = new Set()
} }
set registrar (registrar) { set registrar (registrar) {
@@ -39,11 +35,11 @@ class Topology {
/** /**
* Notify about peer disconnected event. * Notify about peer disconnected event.
* @param {PeerId} peerId * @param {PeerInfo} peerInfo
* @returns {void} * @returns {void}
*/ */
disconnect (peerId) { disconnect (peerInfo) {
this._onDisconnect(peerId) this._onDisconnect(peerInfo)
} }
} }

View File

@@ -55,20 +55,20 @@ class MulticodecTopology extends Topology {
/** /**
* Update topology. * Update topology.
* @param {Array<{id: PeerId, multiaddrs: Array<Multiaddr>, protocols: Array<string>}>} peerDataIterable * @param {Array<PeerInfo>} peerInfoIterable
* @returns {void} * @returns {void}
*/ */
_updatePeers (peerDataIterable) { _updatePeers (peerInfoIterable) {
for (const { id, protocols } of peerDataIterable) { for (const peerInfo of peerInfoIterable) {
if (this.multicodecs.filter(multicodec => protocols.includes(multicodec)).length) { if (this.multicodecs.filter(multicodec => peerInfo.protocols.has(multicodec)).length) {
// Add the peer regardless of whether or not there is currently a connection // Add the peer regardless of whether or not there is currently a connection
this.peers.add(id.toB58String()) this.peers.set(peerInfo.id.toB58String(), peerInfo)
// If there is a connection, call _onConnect // If there is a connection, call _onConnect
const connection = this._registrar.getConnection(id) const connection = this._registrar.getConnection(peerInfo)
connection && this._onConnect(id, connection) connection && this._onConnect(peerInfo, connection)
} else { } else {
// Remove any peers we might be tracking that are no longer of value to us // Remove any peers we might be tracking that are no longer of value to us
this.peers.delete(id.toB58String()) this.peers.delete(peerInfo.id.toB58String())
} }
} }
} }
@@ -76,23 +76,22 @@ class MulticodecTopology extends Topology {
/** /**
* Check if a new peer support the multicodecs for this topology. * Check if a new peer support the multicodecs for this topology.
* @param {Object} props * @param {Object} props
* @param {PeerId} props.peerId * @param {PeerInfo} props.peerInfo
* @param {Array<string>} props.protocols * @param {Array<string>} props.protocols
*/ */
_onProtocolChange ({ peerId, protocols }) { _onProtocolChange ({ peerInfo, protocols }) {
const hadPeer = this.peers.has(peerId.toB58String()) const existingPeer = this.peers.get(peerInfo.id.toB58String())
const hasProtocol = protocols.filter(protocol => this.multicodecs.includes(protocol)) const hasProtocol = protocols.filter(protocol => this.multicodecs.includes(protocol))
// Not supporting the protocol anymore? // Not supporting the protocol anymore?
if (hadPeer && hasProtocol.length === 0) { if (existingPeer && hasProtocol.length === 0) {
this._onDisconnect(peerId) this._onDisconnect(peerInfo)
} }
// New to protocol support // New to protocol support
for (const protocol of protocols) { for (const protocol of protocols) {
if (this.multicodecs.includes(protocol)) { if (this.multicodecs.includes(protocol)) {
const peerData = this._registrar.peerStore.get(peerId) this._updatePeers([peerInfo])
this._updatePeers([peerData])
return return
} }
} }

View File

@@ -8,18 +8,19 @@ chai.use(require('dirty-chai'))
const sinon = require('sinon') const sinon = require('sinon')
const PeerId = require('peer-id') const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const peers = require('../../utils/peers') const peers = require('../../utils/peers')
module.exports = (test) => { module.exports = (test) => {
describe('multicodec topology', () => { describe('multicodec topology', () => {
let topology, id let topology, peer
beforeEach(async () => { beforeEach(async () => {
topology = await test.setup() topology = await test.setup()
if (!topology) throw new Error('missing multicodec topology') if (!topology) throw new Error('missing multicodec topology')
id = await PeerId.createFromJSON(peers[0]) const id = await PeerId.createFromJSON(peers[0])
peer = await PeerInfo.create(id)
}) })
afterEach(async () => { afterEach(async () => {
@@ -37,7 +38,7 @@ module.exports = (test) => {
it('should trigger "onDisconnect" on peer disconnected', () => { it('should trigger "onDisconnect" on peer disconnected', () => {
sinon.spy(topology, '_onDisconnect') sinon.spy(topology, '_onDisconnect')
topology.disconnect(id) topology.disconnect(peer)
expect(topology._onDisconnect.callCount).to.equal(1) expect(topology._onDisconnect.callCount).to.equal(1)
}) })
@@ -46,16 +47,13 @@ module.exports = (test) => {
sinon.spy(topology, '_updatePeers') sinon.spy(topology, '_updatePeers')
expect(topology.peers.size).to.eql(0) expect(topology.peers.size).to.eql(0)
const peerStore = topology._registrar.peerStore
const id2 = await PeerId.createFromJSON(peers[1]) const id2 = await PeerId.createFromJSON(peers[1])
peerStore.peers.set(id2.toB58String(), { const peer2 = await PeerInfo.create(id2)
id: id2, topology.multicodecs.forEach((m) => peer2.protocols.add(m))
protocols: Array.from(topology.multicodecs)
})
const peerStore = topology._registrar.peerStore
peerStore.emit('change:protocols', { peerStore.emit('change:protocols', {
peerId: id2, peerInfo: peer2,
protocols: Array.from(topology.multicodecs) protocols: Array.from(topology.multicodecs)
}) })
@@ -67,34 +65,28 @@ module.exports = (test) => {
sinon.spy(topology, '_onDisconnect') sinon.spy(topology, '_onDisconnect')
expect(topology.peers.size).to.eql(0) expect(topology.peers.size).to.eql(0)
const peerStore = topology._registrar.peerStore
const id2 = await PeerId.createFromJSON(peers[1]) const id2 = await PeerId.createFromJSON(peers[1])
peerStore.peers.set(id2.toB58String(), { const peer2 = await PeerInfo.create(id2)
id: id2, topology.multicodecs.forEach((m) => peer2.protocols.add(m))
protocols: Array.from(topology.multicodecs)
})
const peerStore = topology._registrar.peerStore
peerStore.emit('change:protocols', { peerStore.emit('change:protocols', {
peerId: id2, peerInfo: peer2,
protocols: Array.from(topology.multicodecs) protocols: Array.from(topology.multicodecs)
}) })
expect(topology.peers.size).to.eql(1) expect(topology.peers.size).to.eql(1)
peerStore.peers.set(id2.toB58String(), { topology.multicodecs.forEach((m) => peer2.protocols.delete(m))
id: id2,
protocols: []
})
// Peer does not support the protocol anymore // Peer does not support the protocol anymore
peerStore.emit('change:protocols', { peerStore.emit('change:protocols', {
peerId: id2, peerInfo: peer2,
protocols: [] protocols: []
}) })
expect(topology.peers.size).to.eql(1) expect(topology.peers.size).to.eql(1)
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(peer2)).to.equal(true)
}) })
}) })
} }

View File

@@ -8,17 +8,19 @@ chai.use(require('dirty-chai'))
const sinon = require('sinon') const sinon = require('sinon')
const PeerId = require('peer-id') const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const peers = require('../../utils/peers') const peers = require('../../utils/peers')
module.exports = (test) => { module.exports = (test) => {
describe('topology', () => { describe('topology', () => {
let topology, id let topology, peer
beforeEach(async () => { beforeEach(async () => {
topology = await test.setup() topology = await test.setup()
if (!topology) throw new Error('missing multicodec topology') if (!topology) throw new Error('missing multicodec topology')
id = await PeerId.createFromJSON(peers[0]) const id = await PeerId.createFromJSON(peers[0])
peer = await PeerInfo.create(id)
}) })
afterEach(async () => { afterEach(async () => {
@@ -36,7 +38,7 @@ module.exports = (test) => {
it('should trigger "onDisconnect" on peer disconnected', () => { it('should trigger "onDisconnect" on peer disconnected', () => {
sinon.spy(topology, '_onDisconnect') sinon.spy(topology, '_onDisconnect')
topology.disconnect(id) topology.disconnect(peer)
expect(topology._onDisconnect.callCount).to.equal(1) expect(topology._onDisconnect.callCount).to.equal(1)
}) })

View File

@@ -31,7 +31,8 @@ module.exports = (common) => {
} }
} }
describe('dial', () => { describe('dial', function () {
this.timeout(20 * 1000)
let addrs let addrs
let transport let transport
let connector let connector
@@ -129,7 +130,7 @@ module.exports = (common) => {
expect.fail('Did not throw error with code ' + AbortError.code) expect.fail('Did not throw error with code ' + AbortError.code)
}) })
it('abort while reading throws AbortError', async () => { it.skip('abort while reading throws AbortError', async () => {
// Add a delay to the response from the server // Add a delay to the response from the server
async function * delayedResponse (source) { async function * delayedResponse (source) {
for await (const val of source) { for await (const val of source) {

View File

@@ -31,7 +31,8 @@ module.exports = (common) => {
} }
} }
describe('listen', () => { describe('listen', function () {
this.timeout(20 * 1000)
let addrs let addrs
let transport let transport
@@ -51,7 +52,7 @@ module.exports = (common) => {
await listener.close() await listener.close()
}) })
it('close listener with connections, through timeout', async () => { it.skip('close listener with connections, through timeout', async () => {
const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound') const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound')
const listenerConns = [] const listenerConns = []

View File

@@ -7,10 +7,6 @@ class MockPeerStore extends EventEmitter {
super() super()
this.peers = peers this.peers = peers
} }
get (peerId) {
return this.peers.get(peerId.toB58String())
}
} }
module.exports = MockPeerStore module.exports = MockPeerStore

View File

@@ -21,8 +21,7 @@ describe('multicodec topology compliance tests', () => {
}) })
if (!registrar) { if (!registrar) {
const peers = new Map() const peerStore = new MockPeerStore([])
const peerStore = new MockPeerStore(peers)
registrar = { registrar = {
peerStore, peerStore,