Compare commits

..

39 Commits

Author SHA1 Message Date
4adedcc4bf chore: release version v0.3.1 2020-07-03 15:51:41 +02:00
0628d708c4 chore: update contributors 2020-07-03 15:51:41 +02:00
e10a1545c8 fix: reconnect should trigger topology on connect if protocol stored (#54)
* fix: reconnect should trigger topology on connect if protocol stored

* chore: address review
2020-07-03 15:48:12 +02:00
9fbf9d0331 fix: content and peer routing multiaddrs property (#49) 2020-06-05 16:12:33 +02:00
aa996d2054 chore: release version v0.3.0 2020-06-05 16:12:33 +02:00
507013a724 chore: update contributors 2020-06-05 16:12:33 +02:00
a55c7c454a chore: remove peer-info usage on topology (#42)
* chore: remove peer-info usage on topology

BREAKING CHANGE: topology api now uses peer-id instead of peer-info
2020-06-05 16:12:33 +02:00
87e2e89791 chore: update content and peer routing interfaces removing peer-info (#43)
* chore: update content and peer routing interfaces removing peer-info

BREAKING CHANGE: content-routing and peer-routing APIs return an object with relevant properties instead of peer-info
2020-06-05 16:12:33 +02:00
5bcfc966f7 chore: discovery should not use once (#45) 2020-06-05 16:12:33 +02:00
c8c249de6e chore: apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
2020-06-05 16:12:33 +02:00
5b138ef0a0 chore: add tests for peer-discovery 2020-06-05 16:12:33 +02:00
bdd2502ef6 feat: peer-discovery not using peer-info
BREAKING CHANGE: peer-discovery emits object with id and multiaddrs properties
2020-06-05 16:12:33 +02:00
1bef8d5d78 chore(deps-dev): bump aegir from 21.10.2 to 22.0.0 (#50)
Bumps [aegir](https://github.com/ipfs/aegir) from 21.10.2 to 22.0.0.
- [Release notes](https://github.com/ipfs/aegir/releases)
- [Changelog](https://github.com/ipfs/aegir/blob/master/CHANGELOG.md)
- [Commits](https://github.com/ipfs/aegir/compare/v21.10.2...v22.0.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-05-08 10:52:10 +02:00
9a8f375d40 fix: peer-routing typo (#47) 2020-04-22 15:30:59 +02:00
afc2aa6891 chore: release version v0.2.8 2020-04-21 11:33:14 +02:00
424a0c081e chore: update contributors 2020-04-21 11:33:14 +02:00
e824b15032 chore: update deps (#46) 2020-04-21 11:27:41 +02:00
dba3b9932f Merge pull request #44 from wemeetagain/patch-1
chore: add discv5 peer discovery module
2020-04-11 11:02:12 +02:00
c56dd3e3fa chore: add discv5 peer discovery module 2020-04-10 14:03:58 -05:00
de64a49007 chore: release version v0.2.7 2020-03-20 20:57:31 +01:00
3cfd9714bd chore: update contributors 2020-03-20 20:57:30 +01:00
78e015cef3 fix: add buffer (#39) 2020-03-20 20:53:54 +01:00
bd5a8b9337 chore(deps): bump sinon from 8.1.1 to 9.0.0
Bumps [sinon](https://github.com/sinonjs/sinon) from 8.1.1 to 9.0.0.
- [Release notes](https://github.com/sinonjs/sinon/releases)
- [Changelog](https://github.com/sinonjs/sinon/blob/master/CHANGELOG.md)
- [Commits](https://github.com/sinonjs/sinon/compare/v8.1.1...v9.0.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-19 08:20:14 -05:00
098390cd31 chore: release version v0.2.6 2020-02-17 16:21:31 -05:00
953e289e1a chore: update contributors 2020-02-17 16:21:30 -05:00
ba822856ef chore(deps-dev): bump aegir from 20.6.1 to 21.0.2
Bumps [aegir](https://github.com/ipfs/aegir) from 20.6.1 to 21.0.2.
- [Release notes](https://github.com/ipfs/aegir/releases)
- [Changelog](https://github.com/ipfs/aegir/blob/master/CHANGELOG.md)
- [Commits](https://github.com/ipfs/aegir/compare/v20.6.1...v21.0.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-17 22:19:35 +01:00
c77d8de2e7 fix: remove use of assert module (#34)
* fix: remove use of assert module

The polyfill is big, we can simulate it by throwing an Error and it doesn't work under React Native.

* chore: pr comments
2020-02-17 22:19:13 +01:00
6203109751 chore: release version v0.2.5 2020-02-04 18:38:20 +01:00
25eeedd20e chore: update contributors 2020-02-04 18:38:19 +01:00
5c88d77aaa fix(connection): tracks streams properly (#25) 2020-02-04 17:34:56 +01:00
f6871afd76 chore(deps): bump sinon from 7.5.0 to 8.1.1
Bumps [sinon](https://github.com/sinonjs/sinon) from 7.5.0 to 8.1.1.
- [Release notes](https://github.com/sinonjs/sinon/releases)
- [Changelog](https://github.com/sinonjs/sinon/blob/master/CHANGELOG.md)
- [Commits](https://github.com/sinonjs/sinon/compare/v7.5.0...v8.1.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-04 15:55:39 +01:00
734c491bb0 chore(deps): bump abortable-iterator from 2.1.0 to 3.0.0 (#24)
Bumps [abortable-iterator](https://github.com/alanshaw/abortable-iterator) from 2.1.0 to 3.0.0.
- [Release notes](https://github.com/alanshaw/abortable-iterator/releases)
- [Commits](https://github.com/alanshaw/abortable-iterator/compare/v2.1.0...v3.0.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-04 15:55:13 +01:00
c43a52d176 chore: release version v0.2.4 2020-02-04 12:29:36 +01:00
af1ba5a409 chore: update contributors 2020-02-04 12:29:35 +01:00
c5b724ab93 fix: dependencies for tests should not be needed by who requires the tests (#18)
* fix: dependencies for tests should not be needed by who requires the tests

* chore: address review
2020-02-04 10:53:05 +01:00
6fb45b775b chore: remove unused dependency (#22) 2020-02-04 10:51:38 +01:00
85c68b5df4 chore: release version v0.2.3 2020-01-21 16:10:16 +01:00
27ac0a5670 chore: update contributors 2020-01-21 16:10:16 +01:00
2de533e15b fix(transport): make close listener test more resilient (#21) 2020-01-21 16:06:13 +01:00
22 changed files with 467 additions and 113 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@ docs
node_modules
# Lock files
package-lock.json
yarn.lock

View File

@ -1,3 +1,111 @@
<a name="0.3.1"></a>
## [0.3.1](https://github.com/libp2p/js-interfaces/compare/v0.2.8...v0.3.1) (2020-07-03)
### Bug Fixes
* content and peer routing multiaddrs property ([#49](https://github.com/libp2p/js-interfaces/issues/49)) ([9fbf9d0](https://github.com/libp2p/js-interfaces/commit/9fbf9d0))
* peer-routing typo ([#47](https://github.com/libp2p/js-interfaces/issues/47)) ([9a8f375](https://github.com/libp2p/js-interfaces/commit/9a8f375))
* reconnect should trigger topology on connect if protocol stored ([#54](https://github.com/libp2p/js-interfaces/issues/54)) ([e10a154](https://github.com/libp2p/js-interfaces/commit/e10a154))
### Chores
* remove peer-info usage on topology ([#42](https://github.com/libp2p/js-interfaces/issues/42)) ([a55c7c4](https://github.com/libp2p/js-interfaces/commit/a55c7c4))
* update content and peer routing interfaces removing peer-info ([#43](https://github.com/libp2p/js-interfaces/issues/43)) ([87e2e89](https://github.com/libp2p/js-interfaces/commit/87e2e89))
### Features
* peer-discovery not using peer-info ([bdd2502](https://github.com/libp2p/js-interfaces/commit/bdd2502))
### 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.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>
## [0.2.7](https://github.com/libp2p/js-interfaces/compare/v0.2.6...v0.2.7) (2020-03-20)
### Bug Fixes
* add buffer ([#39](https://github.com/libp2p/js-interfaces/issues/39)) ([78e015c](https://github.com/libp2p/js-interfaces/commit/78e015c))
<a name="0.2.6"></a>
## [0.2.6](https://github.com/libp2p/js-interfaces/compare/v0.2.5...v0.2.6) (2020-02-17)
### Bug Fixes
* remove use of assert module ([#34](https://github.com/libp2p/js-interfaces/issues/34)) ([c77d8de](https://github.com/libp2p/js-interfaces/commit/c77d8de))
<a name="0.2.5"></a>
## [0.2.5](https://github.com/libp2p/js-interfaces/compare/v0.2.4...v0.2.5) (2020-02-04)
### Bug Fixes
* **connection:** tracks streams properly ([#25](https://github.com/libp2p/js-interfaces/issues/25)) ([5c88d77](https://github.com/libp2p/js-interfaces/commit/5c88d77))
<a name="0.2.4"></a>
## [0.2.4](https://github.com/libp2p/js-interfaces/compare/v0.2.3...v0.2.4) (2020-02-04)
### Bug Fixes
* dependencies for tests should not be needed by who requires the tests ([#18](https://github.com/libp2p/js-interfaces/issues/18)) ([c5b724a](https://github.com/libp2p/js-interfaces/commit/c5b724a))
<a name="0.2.3"></a>
## [0.2.3](https://github.com/libp2p/js-interfaces/compare/v0.2.2...v0.2.3) (2020-01-21)
### Bug Fixes
* **transport:** make close listener test more resilient ([#21](https://github.com/libp2p/js-interfaces/issues/21)) ([2de533e](https://github.com/libp2p/js-interfaces/commit/2de533e))
<a name="0.2.2"></a>
## [0.2.2](https://github.com/libp2p/js-interfaces/compare/v0.2.1...v0.2.2) (2020-01-17)

View File

@ -1,6 +1,6 @@
{
"name": "libp2p-interfaces",
"version": "0.2.2",
"version": "0.3.1",
"description": "Interfaces for JS Libp2p",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
@ -34,29 +34,31 @@
"homepage": "https://github.com/libp2p/js-interfaces#readme",
"dependencies": {
"abort-controller": "^3.0.0",
"abortable-iterator": "^2.1.0",
"async-iterator-to-pull-stream": "^1.3.0",
"abortable-iterator": "^3.0.0",
"buffer": "^5.6.0",
"chai": "^4.2.0",
"chai-checkmark": "^1.0.1",
"class-is": "^1.1.0",
"delay": "^4.3.0",
"detect-node": "^2.0.4",
"dirty-chai": "^2.0.1",
"err-code": "^2.0.0",
"it-goodbye": "^2.0.1",
"it-pair": "^1.0.0",
"it-pipe": "^1.0.1",
"libp2p-tcp": "^0.14.1",
"multiaddr": "^7.1.0",
"p-limit": "^2.2.1",
"multiaddr": "^7.4.3",
"p-defer": "^3.0.0",
"p-limit": "^2.3.0",
"p-wait-for": "^3.1.0",
"peer-id": "^0.13.3",
"sinon": "^7.5.0",
"peer-id": "^0.13.11",
"peer-info": "^0.17.0",
"sinon": "^9.0.2",
"streaming-iterables": "^4.1.0"
},
"devDependencies": {
"aegir": "^20.4.1",
"it-handshake": "^1.0.0",
"it-pair": "^1.0.0",
"it-pipe": "^1.0.1",
"peer-info": "^0.17.0"
"aegir": "^22.0.0",
"it-handshake": "^1.0.1"
},
"contributors": [
"Alan Shaw <alan.shaw@protocol.ai>",
@ -79,6 +81,7 @@
"Sathya Narrayanan <plasmashadowx@gmail.com>",
"Vasco Santos <vasco.santos@moxy.studio>",
"Vasco Santos <vasco.santos@ua.pt>",
"dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>",
"dirkmc <dirkmdev@gmail.com>",
"dmitriy ryajov <dryajov@dmitriys-MBP.HomeNET>",
"greenkeeperio-bot <support@greenkeeper.io>",

View File

@ -2,13 +2,56 @@
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const withIs = require('class-is')
const assert = require('assert')
const errCode = require('err-code')
const Status = require('./status')
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {
if (localAddr && !multiaddr.isMultiaddr(localAddr)) {
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS')
}
if (!PeerId.isPeerId(localPeer)) {
throw errCode(new Error('localPeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS')
}
if (!PeerId.isPeerId(remotePeer)) {
throw errCode(new Error('remotePeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS')
}
if (typeof newStream !== 'function') {
throw errCode(new Error('new stream must be a function'), 'ERR_INVALID_PARAMETERS')
}
if (typeof close !== 'function') {
throw errCode(new Error('close must be a function'), 'ERR_INVALID_PARAMETERS')
}
if (typeof getStreams !== 'function') {
throw errCode(new Error('getStreams must be a function'), 'ERR_INVALID_PARAMETERS')
}
if (!stat) {
throw errCode(new Error('connection metadata object must be provided'), 'ERR_INVALID_PARAMETERS')
}
if (stat.direction !== 'inbound' && stat.direction !== 'outbound') {
throw errCode(new Error('direction must be "inbound" or "outbound"'), 'ERR_INVALID_PARAMETERS')
}
if (!stat.timeline) {
throw errCode(new Error('connection timeline object must be provided in the stat object'), 'ERR_INVALID_PARAMETERS')
}
if (!stat.timeline.open) {
throw errCode(new Error('connection open timestamp must be provided'), 'ERR_INVALID_PARAMETERS')
}
if (!stat.timeline.upgraded) {
throw errCode(new Error('connection upgraded timestamp must be provided'), 'ERR_INVALID_PARAMETERS')
}
}
/**
* An implementation of the js-libp2p connection.
* Any libp2p transport should use an upgrader to return this connection.
@ -33,17 +76,7 @@ class Connection {
* @param {string} [properties.stat.encryption] connection encryption method identifier.
*/
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
assert(PeerId.isPeerId(localPeer), 'localPeer must be an instance of peer-id')
assert(PeerId.isPeerId(remotePeer), 'remotePeer must be an instance of peer-id')
assert(typeof newStream === 'function', 'new stream must be a function')
assert(typeof close === 'function', 'close must be a function')
assert(typeof getStreams === 'function', 'getStreams must be a function')
assert(stat, 'connection metadata object must be provided')
assert(stat.direction === 'inbound' || stat.direction === 'outbound', 'direction must be "inbound" or "outbound"')
assert(stat.timeline, 'connection timeline object must be provided in the stat object')
assert(stat.timeline.open, 'connection open timestamp must be provided')
assert(stat.timeline.upgraded, 'connection upgraded timestamp must be provided')
validateArgs(localAddr, localPeer, remotePeer, newStream, close, getStreams, stat)
/**
* Connection identifier.
@ -138,7 +171,7 @@ class Connection {
const { stream, protocol } = await this._newStream(protocols)
this.addStream(stream, protocol)
this.addStream(stream, { protocol })
return {
stream,

View File

@ -120,6 +120,19 @@ module.exports = (test) => {
expect(connection.stat.status).to.equal(Status.CLOSED)
})
it('should properly track streams', async () => {
// Open stream
const protocol = '/echo/0.0.1'
const { stream } = await connection.newStream(protocol)
const trackedStream = connection.registry.get(stream.id)
expect(trackedStream).to.have.property('protocol', protocol)
// Close stream
await stream.close()
expect(connection.registry.get(stream.id)).to.not.exist()
})
it('should support a proxy on the timeline', async () => {
sinon.spy(proxyHandler, 'set')
expect(connection.stat.timeline.close).to.not.exist()

View File

@ -30,6 +30,32 @@ TBD
A valid (read: that follows this abstraction) Content Routing module must implement the following API.
### `.findProviders`
### findProviders
### `.provide`
- `findProviders(cid)`
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, multiaddrs: 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

@ -1,6 +1,7 @@
/* eslint-env mocha */
'use strict'
const { Buffer } = require('buffer')
const duplexPair = require('it-pair/duplex')
const pipe = require('it-pipe')
const peers = require('../../utils/peers')

View File

@ -16,6 +16,7 @@ The API is presented with both Node.js and Go primitives, however, there is not
- [JavaScript libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
- [JavaScript libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star)
- [JavaScript libp2p-websocket-star](https://github.com/libp2p/js-libp2p-websocket-star)
- [TypeScript discv5](https://github.com/chainsafe/discv5)
Send a PR to add a new one if you happen to find or write one.
@ -69,6 +70,9 @@ It returns a `Promise`
### discoverying peers
- `discovery.on('peer', (peerInfo) => {})`
- `discovery.on('peer', (peerData) => {})`
Everytime a peer is discovered by a discovery service, it emmits a `peer` event with the discover peer's [PeerInfo](https://github.com/libp2p/js-peer-info).
Every time a peer is discovered by a discovery service, it emits a `peer` event with the discovered peer's information, which must contain the following properties:
- `<`[`PeerId`](https://github.com/libp2p/js-peer-id)`>` `peerData.id`
- `<Array<`[`Multiaddr`](https://github.com/multiformats/js-multiaddr)`>>` `peerData.multiaddrs`

View File

@ -1,17 +1,31 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const expect = chai.expect
chai.use(require('dirty-chai'))
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const delay = require('delay')
const pDefer = require('p-defer')
module.exports = (common) => {
describe('interface-peer-discovery', () => {
let discovery
before(async () => {
beforeEach(async () => {
discovery = await common.setup()
})
after(() => common.teardown && common.teardown())
afterEach('ensure discovery was stopped', async () => {
await discovery.stop()
afterEach('ensure discovery was stopped', () => discovery.stop())
discovery.removeAllListeners()
common.teardown && common.teardown()
})
it('can start the service', async () => {
await discovery.start()
@ -30,5 +44,49 @@ module.exports = (common) => {
await discovery.start()
await discovery.start()
})
it('should emit a peer event after start', async () => {
const defer = pDefer()
await discovery.start()
discovery.on('peer', ({ id, multiaddrs }) => {
expect(id).to.exist()
expect(PeerId.isPeerId(id)).to.eql(true)
expect(multiaddrs).to.exist()
multiaddrs.forEach((m) => expect(multiaddr.isMultiaddr(m)).to.eql(true))
defer.resolve()
})
await defer.promise
})
it('should not receive a peer event before start', async () => {
discovery.on('peer', () => {
throw new Error('should not receive a peer event before start')
})
await delay(2000)
})
it('should not receive a peer event after stop', async () => {
const deferStart = pDefer()
await discovery.start()
discovery.on('peer', () => {
deferStart.resolve()
})
await deferStart.promise
await discovery.stop()
discovery.on('peer', () => {
throw new Error('should not receive a peer event after stop')
})
await delay(2000)
})
})
}

View File

@ -30,12 +30,17 @@ TBD
A valid (read: that follows this abstraction) Peer Routing module must implement the following API.
### `.findPeers` - Find peers 'responsible' or 'closest' to a given key
### findPeer
- `Node.js` peerRouting.findPeers(key, function (err, peersPriorityQueue) {})
- `findPeer(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.
Query the network for all multiaddresses associated with a `PeerId`.
This method will query the network (route it) and return a Priority Queue datastructe with a list of PeerInfo objects, ordered by 'closeness'.
**Parameters**
- [peerId](https://github.com/libp2p/js-peer-id).
key is a multihash
**Returns**
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, multiaddrs: Multiaddr[] }>`

View File

@ -2,6 +2,7 @@
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'
const { Buffer } = require('buffer')
const pair = require('it-pair/duplex')
const pipe = require('it-pipe')
const { consume } = require('streaming-iterables')

View File

@ -56,8 +56,8 @@ const toplogy = new MulticodecTopology({
max: 50,
multicodecs: ['/echo/1.0.0'],
handlers: {
onConnect: (peerInfo, conn) => {},
onDisconnect: (peerInfo) => {}
onConnect: (peerId, conn) => {},
onDisconnect: (peerId) => {}
}
})
```
@ -69,8 +69,8 @@ The `MulticodecTopology` extends the `Topology`, which makes the `Topology` API
### Topology
- `Topology`
- `peers<Map<string, PeerInfo>>`: A Map of peers belonging to the topology.
- `disconnect<function(PeerInfo)>`: Called when a peer has been disconnected
- `peers<Map<string, PeerId>>`: A Map of peers belonging to the topology.
- `disconnect<function(PeerId)>`: Called when a peer has been disconnected
#### Constructor
@ -79,8 +79,8 @@ const toplogy = new Topology({
min: 0,
max: 50,
handlers: {
onConnect: (peerInfo, conn) => {},
onDisconnect: (peerInfo) => {}
onConnect: (peerId, conn) => {},
onDisconnect: (peerId) => {}
}
})
```
@ -95,27 +95,27 @@ const toplogy = new Topology({
#### Set a peer
- `topology.peers.set(id, peerInfo)`
- `topology.peers.set(id, peerId)`
Add a peer to the topology.
**Parameters**
- `id` is the `string` that identifies the peer to add.
- `peerInfo` is the [PeerInfo][peer-info] of the peer to add.
- `peerId` is the [PeerId][peer-id] of the peer to add.
#### Notify about a peer disconnected event
- `topology.disconnect(peerInfo)`
- `topology.disconnect(peerId)`
**Parameters**
- `peerInfo` is the [PeerInfo][peer-info] of the peer disconnected.
- `peerId` is the [PeerId][peer-id] of the peer disconnected.
### Multicodec Topology
- `MulticodecTopology`
- `registrar<Registrar>`: The `Registrar` of the topology. This is set by the `Registrar` during registration.
- `peers<Map<string, PeerInfo>>`: The Map of peers that belong to the topology
- `disconnect<function(PeerInfo)>`: Disconnects a peer from the topology.
- `peers<Map<string, PeerId>>`: The Map of peers that belong to the topology
- `disconnect<function(PeerId)>`: Disconnects a peer from the topology.
#### Constructor
@ -125,8 +125,8 @@ const toplogy = new MulticodecTopology({
max: 50,
multicodecs: ['/echo/1.0.0'],
handlers: {
onConnect: (peerInfo, conn) => {},
onDisconnect: (peerInfo) => {}
onConnect: (peerId, conn) => {},
onDisconnect: (peerId) => {}
}
})
```
@ -139,3 +139,5 @@ const toplogy = new MulticodecTopology({
- `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.
- `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,7 +26,11 @@ class Topology {
this._onConnect = handlers.onConnect || 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) {
@ -35,11 +39,11 @@ class Topology {
/**
* Notify about peer disconnected event.
* @param {PeerInfo} peerInfo
* @param {PeerId} peerId
* @returns {void}
*/
disconnect (peerInfo) {
this._onDisconnect(peerInfo)
disconnect (peerId) {
this._onDisconnect(peerId)
}
}

View File

@ -1,6 +1,5 @@
'use strict'
const assert = require('assert')
const withIs = require('class-is')
const Topology = require('./index')
@ -24,22 +23,33 @@ class MulticodecTopology extends Topology {
}) {
super({ min, max, handlers })
assert(multicodecs, 'one or more multicodec should be provided')
assert(handlers, 'the handlers should be provided')
assert(handlers.onConnect && typeof handlers.onConnect === 'function',
'the \'onConnect\' handler must be provided')
assert(handlers.onDisconnect && typeof handlers.onDisconnect === 'function',
'the \'onDisconnect\' handler must be provided')
if (!multicodecs) {
throw new Error('one or more multicodec should be provided')
}
if (!handlers) {
throw new Error('the handlers should be provided')
}
if (typeof handlers.onConnect !== 'function') {
throw new Error('the \'onConnect\' handler must be provided')
}
if (typeof handlers.onDisconnect !== 'function') {
throw new Error('the \'onDisconnect\' handler must be provided')
}
this.multicodecs = Array.isArray(multicodecs) ? multicodecs : [multicodecs]
this._registrar = undefined
this._onProtocolChange = this._onProtocolChange.bind(this)
this._onPeerConnect = this._onPeerConnect.bind(this)
}
set registrar (registrar) {
this._registrar = registrar
this._registrar.peerStore.on('change:protocols', this._onProtocolChange)
this._registrar.connectionManager.on('peer:connect', this._onPeerConnect)
// Update topology peers
this._updatePeers(this._registrar.peerStore.peers.values())
@ -47,20 +57,20 @@ class MulticodecTopology extends Topology {
/**
* Update topology.
* @param {Array<PeerInfo>} peerInfoIterable
* @param {Array<{id: PeerId, multiaddrs: Array<Multiaddr>, protocols: Array<string>}>} peerDataIterable
* @returns {void}
*/
_updatePeers (peerInfoIterable) {
for (const peerInfo of peerInfoIterable) {
if (this.multicodecs.filter(multicodec => peerInfo.protocols.has(multicodec)).length) {
_updatePeers (peerDataIterable) {
for (const { id, protocols } of peerDataIterable) {
if (this.multicodecs.filter(multicodec => protocols.includes(multicodec)).length) {
// Add the peer regardless of whether or not there is currently a connection
this.peers.set(peerInfo.id.toB58String(), peerInfo)
this.peers.add(id.toB58String())
// If there is a connection, call _onConnect
const connection = this._registrar.getConnection(peerInfo)
connection && this._onConnect(peerInfo, connection)
const connection = this._registrar.getConnection(id)
connection && this._onConnect(id, connection)
} else {
// Remove any peers we might be tracking that are no longer of value to us
this.peers.delete(peerInfo.id.toB58String())
this.peers.delete(id.toB58String())
}
}
}
@ -68,26 +78,46 @@ class MulticodecTopology extends Topology {
/**
* Check if a new peer support the multicodecs for this topology.
* @param {Object} props
* @param {PeerInfo} props.peerInfo
* @param {PeerId} props.peerId
* @param {Array<string>} props.protocols
*/
_onProtocolChange ({ peerInfo, protocols }) {
const existingPeer = this.peers.get(peerInfo.id.toB58String())
_onProtocolChange ({ peerId, protocols }) {
const hadPeer = this.peers.has(peerId.toB58String())
const hasProtocol = protocols.filter(protocol => this.multicodecs.includes(protocol))
// Not supporting the protocol anymore?
if (existingPeer && hasProtocol.length === 0) {
this._onDisconnect(peerInfo)
if (hadPeer && hasProtocol.length === 0) {
this._onDisconnect(peerId)
}
// New to protocol support
for (const protocol of protocols) {
if (this.multicodecs.includes(protocol)) {
this._updatePeers([peerInfo])
const peerData = this._registrar.peerStore.get(peerId)
this._updatePeers([peerData])
return
}
}
}
/**
* 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' })

View File

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

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

View File

@ -2,6 +2,7 @@
/* eslint-env mocha */
'use strict'
const { Buffer } = require('buffer')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
@ -70,7 +71,7 @@ module.exports = (common) => {
])
// Give the listener a chance to finish its upgrade
await new Promise(resolve => setTimeout(resolve, 0))
await pWaitFor(() => listenerConns.length === 2)
// Wait for the data send and close to finish
await Promise.all([

View File

@ -25,7 +25,7 @@ describe('compliance tests', () => {
const openStreams = []
let streamId = 0
return new Connection({
const connection = new Connection({
localPeer,
remotePeer,
localAddr,
@ -43,7 +43,10 @@ describe('compliance tests', () => {
const id = streamId++
const stream = pair()
stream.close = () => stream.sink([])
stream.close = async () => {
await stream.sink([])
connection.removeStream(stream.id)
}
stream.id = id
openStreams.push(stream)
@ -57,6 +60,7 @@ describe('compliance tests', () => {
getStreams: () => openStreams,
...properties
})
return connection
},
async teardown () {
// cleanup resources created by setup()

View File

@ -5,13 +5,20 @@ const tests = require('../../src/peer-discovery/tests')
const MockDiscovery = require('./mock-discovery')
describe('compliance tests', () => {
let intervalId
tests({
async setup () {
await new Promise(resolve => setTimeout(resolve, 10))
return new MockDiscovery()
setup () {
const mockDiscovery = new MockDiscovery({
discoveryDelay: 1
})
intervalId = setInterval(mockDiscovery._discoverPeer, 1000)
return mockDiscovery
},
async teardown () {
await new Promise(resolve => setTimeout(resolve, 10))
teardown () {
clearInterval(intervalId)
}
})
})

View File

@ -2,8 +2,8 @@
const { EventEmitter } = require('events')
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
/**
* Emits 'peer' events on discovery.
@ -37,10 +37,12 @@ class MockDiscovery extends EventEmitter {
if (!this._isRunning) return
const peerId = await PeerId.create({ bits: 512 })
const peerInfo = new PeerInfo(peerId)
this._timer = setTimeout(() => {
this.emit('peer', peerInfo)
this.emit('peer', {
id: peerId,
multiaddrs: [multiaddr('/ip4/127.0.0.1/tcp/8000')]
})
}, this.options.discoveryDelay || 1000)
}
}

View File

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

View File

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