mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-07-06 23:31:43 +00:00
Compare commits
22 Commits
v0.2.x
...
skip-abort
Author | SHA1 | Date | |
---|---|---|---|
82ed140b42 | |||
13aa6cbfa0 | |||
a8ba13da4b | |||
75f6777d89 | |||
71b813ad3b | |||
46589ce3d0 | |||
f2a18818f2 | |||
1cc943e1b2 | |||
4adedcc4bf | |||
0628d708c4 | |||
e10a1545c8 | |||
9fbf9d0331 | |||
aa996d2054 | |||
507013a724 | |||
a55c7c454a | |||
87e2e89791 | |||
5bcfc966f7 | |||
c8c249de6e | |||
5b138ef0a0 | |||
bdd2502ef6 | |||
1bef8d5d78 | |||
9a8f375d40 |
80
CHANGELOG.md
80
CHANGELOG.md
@ -1,3 +1,83 @@
|
|||||||
|
<a name="0.4.0"></a>
|
||||||
|
# [0.4.0](https://github.com/libp2p/js-interfaces/compare/v0.3.2...v0.4.0) (2020-08-10)
|
||||||
|
|
||||||
|
|
||||||
|
### Chores
|
||||||
|
|
||||||
|
* update deps ([#57](https://github.com/libp2p/js-interfaces/issues/57)) ([75f6777](https://github.com/libp2p/js-interfaces/commit/75f6777))
|
||||||
|
|
||||||
|
|
||||||
|
### BREAKING CHANGES
|
||||||
|
|
||||||
|
* - The peer id dep of this module has replaced node Buffers with Uint8Arrays
|
||||||
|
|
||||||
|
* chore: update gh deps
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.3.2"></a>
|
||||||
|
## [0.3.2](https://github.com/libp2p/js-interfaces/compare/v0.3.1...v0.3.2) (2020-07-15)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* record interface ([#52](https://github.com/libp2p/js-interfaces/issues/52)) ([1cc943e](https://github.com/libp2p/js-interfaces/commit/1cc943e))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<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>
|
<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)
|
## [0.2.8](https://github.com/libp2p/js-interfaces/compare/v0.2.7...v0.2.8) (2020-04-21)
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
- [Crypto](./src/crypto)
|
- [Crypto](./src/crypto)
|
||||||
- [Peer Discovery](./src/peer-discovery)
|
- [Peer Discovery](./src/peer-discovery)
|
||||||
- [Peer Routing](./src/peer-routing)
|
- [Peer Routing](./src/peer-routing)
|
||||||
|
- [Record](./src/record)
|
||||||
- [Stream Muxer](./src/stream-muxer)
|
- [Stream Muxer](./src/stream-muxer)
|
||||||
- [Topology](./src/topology)
|
- [Topology](./src/topology)
|
||||||
- [Transport](./src/transport)
|
- [Transport](./src/transport)
|
||||||
|
17
package.json
17
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-interfaces",
|
"name": "libp2p-interfaces",
|
||||||
"version": "0.2.8",
|
"version": "0.4.0",
|
||||||
"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",
|
||||||
@ -39,23 +39,24 @@
|
|||||||
"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",
|
||||||
|
"delay": "^4.3.0",
|
||||||
"detect-node": "^2.0.4",
|
"detect-node": "^2.0.4",
|
||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1",
|
||||||
"err-code": "^2.0.0",
|
"err-code": "^2.0.0",
|
||||||
"it-goodbye": "^2.0.1",
|
"it-goodbye": "^2.0.1",
|
||||||
"it-pair": "^1.0.0",
|
"it-pair": "^1.0.0",
|
||||||
"it-pipe": "^1.0.1",
|
"it-pipe": "^1.1.0",
|
||||||
"libp2p-tcp": "^0.14.1",
|
"libp2p-tcp": "^0.14.5",
|
||||||
"multiaddr": "^7.4.3",
|
"multiaddr": "^8.0.0",
|
||||||
|
"p-defer": "^3.0.0",
|
||||||
"p-limit": "^2.3.0",
|
"p-limit": "^2.3.0",
|
||||||
"p-wait-for": "^3.1.0",
|
"p-wait-for": "^3.1.0",
|
||||||
"peer-id": "^0.13.11",
|
"peer-id": "^0.14.0",
|
||||||
"peer-info": "^0.17.0",
|
|
||||||
"sinon": "^9.0.2",
|
"sinon": "^9.0.2",
|
||||||
"streaming-iterables": "^4.1.0"
|
"streaming-iterables": "^5.0.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^21.9.0",
|
"aegir": "^25.0.0",
|
||||||
"it-handshake": "^1.0.1"
|
"it-handshake": "^1.0.1"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
|
@ -30,6 +30,32 @@ 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
|
||||||
|
|
||||||
### `.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>`
|
||||||
|
@ -70,6 +70,9 @@ It returns a `Promise`
|
|||||||
|
|
||||||
### discoverying peers
|
### 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`
|
||||||
|
@ -1,17 +1,31 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'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) => {
|
module.exports = (common) => {
|
||||||
describe('interface-peer-discovery', () => {
|
describe('interface-peer-discovery', () => {
|
||||||
let discovery
|
let discovery
|
||||||
|
|
||||||
before(async () => {
|
beforeEach(async () => {
|
||||||
discovery = await common.setup()
|
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 () => {
|
it('can start the service', async () => {
|
||||||
await discovery.start()
|
await discovery.start()
|
||||||
@ -30,5 +44,49 @@ module.exports = (common) => {
|
|||||||
await discovery.start()
|
await discovery.start()
|
||||||
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -30,12 +30,17 @@ 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.
|
||||||
|
|
||||||
### `.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[] }>`
|
||||||
|
75
src/record/README.md
Normal file
75
src/record/README.md
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
interface-record
|
||||||
|
==================
|
||||||
|
|
||||||
|
A libp2p node needs to store data in a public location (e.g. a DHT), or rely on potentially untrustworthy intermediaries to relay information. Libp2p provides an all-purpose data container called **envelope**, which includes a signature of the data, so that it its authenticity can be verified.
|
||||||
|
|
||||||
|
The record represents the data that will be stored inside the **envelope** when distributing records across the network. The `interface-record` aims to guarantee that any type of record created is compliant with the libp2p **envelope**.
|
||||||
|
|
||||||
|
Taking into account that a record might be used in different contexts, an **envelope** signature made for a specific purpose **must not** be considered valid for a different purpose. Accordingly, each record has a short and descriptive string representing the record use case, known as **domain**. The data to be signed will be prepended with the domain string, in order to create a domain signature.
|
||||||
|
|
||||||
|
A record can also contain a Buffer codec (ideally registered as a [multicodec](https://github.com/multiformats/multicodec)). This codec will prefix the record data in the **envelope** , so that it can be deserialized deterministically.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```js
|
||||||
|
const tests = require('libp2p-interfaces/src/record/tests')
|
||||||
|
describe('your record', () => {
|
||||||
|
tests({
|
||||||
|
async setup () {
|
||||||
|
return YourRecord
|
||||||
|
},
|
||||||
|
async teardown () {
|
||||||
|
// cleanup resources created by setup()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## Create Record
|
||||||
|
|
||||||
|
```js
|
||||||
|
const multicodec = require('multicodec')
|
||||||
|
const Record = require('libp2p-interfaces/src/record')
|
||||||
|
// const Protobuf = require('./record.proto')
|
||||||
|
|
||||||
|
const ENVELOPE_DOMAIN_PEER_RECORD = 'libp2p-peer-record'
|
||||||
|
const ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = Buffer.from('0301', 'hex')
|
||||||
|
|
||||||
|
class PeerRecord extends Record {
|
||||||
|
constructor (peerId, multiaddrs, seqNumber) {
|
||||||
|
super (ENVELOPE_DOMAIN_PEER_RECORD, ENVELOPE_PAYLOAD_TYPE_PEER_RECORD)
|
||||||
|
}
|
||||||
|
|
||||||
|
marshal () {
|
||||||
|
// Implement and return using Protobuf
|
||||||
|
}
|
||||||
|
|
||||||
|
equals (other) {
|
||||||
|
// Verify
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### marshal
|
||||||
|
|
||||||
|
- `record.marshal()`
|
||||||
|
|
||||||
|
Marshal a record to be used in a libp2p envelope.
|
||||||
|
|
||||||
|
**Returns**
|
||||||
|
|
||||||
|
It returns a `Protobuf` containing the record data.
|
||||||
|
|
||||||
|
### equals
|
||||||
|
|
||||||
|
- `record.equals(other)`
|
||||||
|
|
||||||
|
Verifies if the other Record is identical to this one.
|
||||||
|
|
||||||
|
**Parameters**
|
||||||
|
- other is a `Record` to compare with the current instance.
|
||||||
|
|
||||||
|
**Returns**
|
||||||
|
- `boolean`
|
35
src/record/index.js
Normal file
35
src/record/index.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const errcode = require('err-code')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Record is the base implementation of a record that can be used as the payload of a libp2p envelope.
|
||||||
|
*/
|
||||||
|
class Record {
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @param {String} domain signature domain
|
||||||
|
* @param {Buffer} codec identifier of the type of record
|
||||||
|
*/
|
||||||
|
constructor (domain, codec) {
|
||||||
|
this.domain = domain
|
||||||
|
this.codec = codec
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marshal a record to be used in an envelope.
|
||||||
|
*/
|
||||||
|
marshal () {
|
||||||
|
throw errcode(new Error('marshal must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies if the other provided Record is identical to this one.
|
||||||
|
* @param {Record} other
|
||||||
|
*/
|
||||||
|
equals (other) {
|
||||||
|
throw errcode(new Error('equals must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Record
|
35
src/record/tests/index.js
Normal file
35
src/record/tests/index.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const chai = require('chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(require('dirty-chai'))
|
||||||
|
|
||||||
|
module.exports = (test) => {
|
||||||
|
describe('record', () => {
|
||||||
|
let record
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
record = await test.setup()
|
||||||
|
if (!record) throw new Error('missing record')
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => test.teardown())
|
||||||
|
|
||||||
|
it('has domain and codec', () => {
|
||||||
|
expect(record.domain).to.exist()
|
||||||
|
expect(record.codec).to.exist()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('is able to marshal', () => {
|
||||||
|
const rawData = record.marshal()
|
||||||
|
expect(Buffer.isBuffer(rawData)).to.eql(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('is able to compare two records', () => {
|
||||||
|
const equals = record.equals(record)
|
||||||
|
expect(equals).to.eql(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
@ -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: (peerInfo, conn) => {},
|
onConnect: (peerId, conn) => {},
|
||||||
onDisconnect: (peerInfo) => {}
|
onDisconnect: (peerId) => {}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -69,8 +69,8 @@ The `MulticodecTopology` extends the `Topology`, which makes the `Topology` API
|
|||||||
### Topology
|
### Topology
|
||||||
|
|
||||||
- `Topology`
|
- `Topology`
|
||||||
- `peers<Map<string, PeerInfo>>`: A Map of peers belonging to the topology.
|
- `peers<Map<string, PeerId>>`: A Map of peers belonging to the topology.
|
||||||
- `disconnect<function(PeerInfo)>`: Called when a peer has been disconnected
|
- `disconnect<function(PeerId)>`: 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: (peerInfo, conn) => {},
|
onConnect: (peerId, conn) => {},
|
||||||
onDisconnect: (peerInfo) => {}
|
onDisconnect: (peerId) => {}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -95,27 +95,27 @@ const toplogy = new Topology({
|
|||||||
|
|
||||||
#### Set a peer
|
#### Set a peer
|
||||||
|
|
||||||
- `topology.peers.set(id, peerInfo)`
|
- `topology.peers.set(id, peerId)`
|
||||||
|
|
||||||
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.
|
||||||
- `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
|
#### Notify about a peer disconnected event
|
||||||
|
|
||||||
- `topology.disconnect(peerInfo)`
|
- `topology.disconnect(peerId)`
|
||||||
|
|
||||||
**Parameters**
|
**Parameters**
|
||||||
- `peerInfo` is the [PeerInfo][peer-info] of the peer disconnected.
|
- `peerId` is the [PeerId][peer-id] 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, PeerInfo>>`: The Map of peers that belong to the topology
|
- `peers<Map<string, PeerId>>`: The Map of peers that belong to the topology
|
||||||
- `disconnect<function(PeerInfo)>`: Disconnects a peer from the topology.
|
- `disconnect<function(PeerId)>`: 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: (peerInfo, conn) => {},
|
onConnect: (peerId, conn) => {},
|
||||||
onDisconnect: (peerInfo) => {}
|
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.
|
- `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
|
||||||
|
@ -26,7 +26,11 @@ 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) {
|
||||||
@ -35,11 +39,11 @@ class Topology {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Notify about peer disconnected event.
|
* Notify about peer disconnected event.
|
||||||
* @param {PeerInfo} peerInfo
|
* @param {PeerId} peerId
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
disconnect (peerInfo) {
|
disconnect (peerId) {
|
||||||
this._onDisconnect(peerInfo)
|
this._onDisconnect(peerId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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())
|
||||||
@ -55,20 +57,20 @@ class MulticodecTopology extends Topology {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Update topology.
|
* Update topology.
|
||||||
* @param {Array<PeerInfo>} peerInfoIterable
|
* @param {Array<{id: PeerId, multiaddrs: Array<Multiaddr>, protocols: Array<string>}>} peerDataIterable
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
_updatePeers (peerInfoIterable) {
|
_updatePeers (peerDataIterable) {
|
||||||
for (const peerInfo of peerInfoIterable) {
|
for (const { id, protocols } of peerDataIterable) {
|
||||||
if (this.multicodecs.filter(multicodec => peerInfo.protocols.has(multicodec)).length) {
|
if (this.multicodecs.filter(multicodec => protocols.includes(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.set(peerInfo.id.toB58String(), peerInfo)
|
this.peers.add(id.toB58String())
|
||||||
// If there is a connection, call _onConnect
|
// If there is a connection, call _onConnect
|
||||||
const connection = this._registrar.getConnection(peerInfo)
|
const connection = this._registrar.getConnection(id)
|
||||||
connection && this._onConnect(peerInfo, connection)
|
connection && this._onConnect(id, 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(peerInfo.id.toB58String())
|
this.peers.delete(id.toB58String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,26 +78,46 @@ 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 {PeerInfo} props.peerInfo
|
* @param {PeerId} props.peerId
|
||||||
* @param {Array<string>} props.protocols
|
* @param {Array<string>} props.protocols
|
||||||
*/
|
*/
|
||||||
_onProtocolChange ({ peerInfo, protocols }) {
|
_onProtocolChange ({ peerId, protocols }) {
|
||||||
const existingPeer = this.peers.get(peerInfo.id.toB58String())
|
const hadPeer = this.peers.has(peerId.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 (existingPeer && hasProtocol.length === 0) {
|
if (hadPeer && hasProtocol.length === 0) {
|
||||||
this._onDisconnect(peerInfo)
|
this._onDisconnect(peerId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)) {
|
||||||
this._updatePeers([peerInfo])
|
const peerData = this._registrar.peerStore.get(peerId)
|
||||||
|
this._updatePeers([peerData])
|
||||||
return
|
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' })
|
module.exports = withIs(MulticodecTopology, { className: 'MulticodecTopology', symbolName: '@libp2p/js-interfaces/topology/multicodec-topology' })
|
||||||
|
@ -8,19 +8,18 @@ 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, peer
|
let topology, id
|
||||||
|
|
||||||
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')
|
||||||
|
|
||||||
const id = await PeerId.createFromJSON(peers[0])
|
id = await PeerId.createFromJSON(peers[0])
|
||||||
peer = await PeerInfo.create(id)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
@ -38,7 +37,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(peer)
|
topology.disconnect(id)
|
||||||
|
|
||||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||||
})
|
})
|
||||||
@ -47,13 +46,16 @@ 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 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 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', {
|
peerStore.emit('change:protocols', {
|
||||||
peerInfo: peer2,
|
peerId: id2,
|
||||||
protocols: Array.from(topology.multicodecs)
|
protocols: Array.from(topology.multicodecs)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -65,28 +67,67 @@ 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 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 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', {
|
peerStore.emit('change:protocols', {
|
||||||
peerInfo: peer2,
|
peerId: id2,
|
||||||
protocols: Array.from(topology.multicodecs)
|
protocols: Array.from(topology.multicodecs)
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(topology.peers.size).to.eql(1)
|
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
|
// Peer does not support the protocol anymore
|
||||||
peerStore.emit('change:protocols', {
|
peerStore.emit('change:protocols', {
|
||||||
peerInfo: peer2,
|
peerId: id2,
|
||||||
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(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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -8,19 +8,17 @@ 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, peer
|
let topology, id
|
||||||
|
|
||||||
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')
|
||||||
|
|
||||||
const id = await PeerId.createFromJSON(peers[0])
|
id = await PeerId.createFromJSON(peers[0])
|
||||||
peer = await PeerInfo.create(id)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
@ -38,7 +36,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(peer)
|
topology.disconnect(id)
|
||||||
|
|
||||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||||
})
|
})
|
||||||
|
@ -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) {
|
||||||
|
@ -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 = []
|
||||||
|
|
||||||
@ -94,7 +95,7 @@ module.exports = (common) => {
|
|||||||
expect(upgradeSpy.callCount).to.equal(2)
|
expect(upgradeSpy.callCount).to.equal(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not handle connection if upgradeInbound throws', async () => {
|
it.skip('should not handle connection if upgradeInbound throws', async () => {
|
||||||
sinon.stub(upgrader, 'upgradeInbound').throws()
|
sinon.stub(upgrader, 'upgradeInbound').throws()
|
||||||
|
|
||||||
const listener = transport.createListener(() => {
|
const listener = transport.createListener(() => {
|
||||||
|
@ -5,13 +5,20 @@ const tests = require('../../src/peer-discovery/tests')
|
|||||||
const MockDiscovery = require('./mock-discovery')
|
const MockDiscovery = require('./mock-discovery')
|
||||||
|
|
||||||
describe('compliance tests', () => {
|
describe('compliance tests', () => {
|
||||||
|
let intervalId
|
||||||
|
|
||||||
tests({
|
tests({
|
||||||
async setup () {
|
setup () {
|
||||||
await new Promise(resolve => setTimeout(resolve, 10))
|
const mockDiscovery = new MockDiscovery({
|
||||||
return new MockDiscovery()
|
discoveryDelay: 1
|
||||||
|
})
|
||||||
|
|
||||||
|
intervalId = setInterval(mockDiscovery._discoverPeer, 1000)
|
||||||
|
|
||||||
|
return mockDiscovery
|
||||||
},
|
},
|
||||||
async teardown () {
|
teardown () {
|
||||||
await new Promise(resolve => setTimeout(resolve, 10))
|
clearInterval(intervalId)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
const { EventEmitter } = require('events')
|
const { EventEmitter } = require('events')
|
||||||
|
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
const PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
const PeerInfo = require('peer-info')
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emits 'peer' events on discovery.
|
* Emits 'peer' events on discovery.
|
||||||
@ -37,10 +37,12 @@ class MockDiscovery extends EventEmitter {
|
|||||||
if (!this._isRunning) return
|
if (!this._isRunning) return
|
||||||
|
|
||||||
const peerId = await PeerId.create({ bits: 512 })
|
const peerId = await PeerId.create({ bits: 512 })
|
||||||
const peerInfo = new PeerInfo(peerId)
|
|
||||||
|
|
||||||
this._timer = setTimeout(() => {
|
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)
|
}, this.options.discoveryDelay || 1000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,13 @@ class MockPeerStore extends EventEmitter {
|
|||||||
constructor (peers) {
|
constructor (peers) {
|
||||||
super()
|
super()
|
||||||
this.peers = peers
|
this.peers = peers
|
||||||
|
this.protoBook = {
|
||||||
|
get: () => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get (peerId) {
|
||||||
|
return this.peers.get(peerId.toB58String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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')
|
||||||
@ -21,10 +23,13 @@ describe('multicodec topology compliance tests', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!registrar) {
|
if (!registrar) {
|
||||||
const peerStore = new MockPeerStore([])
|
const peers = new Map()
|
||||||
|
const peerStore = new MockPeerStore(peers)
|
||||||
|
const connectionManager = new EventEmitter()
|
||||||
|
|
||||||
registrar = {
|
registrar = {
|
||||||
peerStore,
|
peerStore,
|
||||||
|
connectionManager,
|
||||||
getConnection: () => { }
|
getConnection: () => { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user