Skip to content

Commit 432b099

Browse files
vasco-santosjacobheun
authored andcommitted
refactor: pubsub (#467)
* feat: peer-store v0 * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <[email protected]> * chore: address review * refactor: pubsub subsystem * chore: address review * chore: use topology interface * chore: address review * chore: address review * chore: simplify tests
1 parent 2afdbb7 commit 432b099

File tree

11 files changed

+500
-303
lines changed

11 files changed

+500
-303
lines changed

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,18 @@ class Node extends Libp2p {
211211

212212
**IMPORTANT NOTE**: All the methods listed in the API section that take a callback are also now Promisified. Libp2p is migrating away from callbacks to async/await, and in a future release (that will be announced in advance), callback support will be removed entirely. You can follow progress of the async/await endeavor at https://github.com/ipfs/js-ipfs/issues/1670.
213213

214-
#### Create a Node - `Libp2p.createLibp2p(options, callback)`
214+
#### Create a Node - `Libp2p.create(options)`
215215

216216
> Behaves exactly like `new Libp2p(options)`, but doesn't require a PeerInfo. One will be generated instead
217217
218218
```js
219-
const { createLibp2p } = require('libp2p')
220-
createLibp2p(options, (err, libp2p) => {
221-
if (err) throw err
222-
libp2p.start((err) => {
223-
if (err) throw err
224-
})
225-
})
219+
const { create } = require('libp2p')
220+
const libp2p = await create(options)
221+
222+
await libp2p.start()
226223
```
227224

228225
- `options`: Object of libp2p configuration options
229-
- `callback`: Function with signature `function (Error, Libp2p) {}`
230226

231227
#### Create a Node alternative - `new Libp2p(options)`
232228

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@
5555
"it-protocol-buffers": "^0.2.0",
5656
"latency-monitor": "~0.2.1",
5757
"libp2p-crypto": "^0.17.1",
58-
"libp2p-interfaces": "^0.1.3",
58+
"libp2p-interfaces": "^0.1.5",
5959
"mafmt": "^7.0.0",
6060
"merge-options": "^1.0.1",
6161
"moving-average": "^1.0.0",
6262
"multiaddr": "^7.1.0",
6363
"multistream-select": "^0.15.0",
6464
"once": "^1.4.0",
65+
"p-map": "^3.0.0",
6566
"p-queue": "^6.1.1",
6667
"p-settle": "^3.1.0",
6768
"peer-id": "^0.13.3",
@@ -90,8 +91,8 @@
9091
"libp2p-bootstrap": "^0.9.7",
9192
"libp2p-delegated-content-routing": "^0.2.2",
9293
"libp2p-delegated-peer-routing": "^0.2.2",
93-
"libp2p-floodsub": "~0.17.0",
94-
"libp2p-gossipsub": "~0.0.4",
94+
"libp2p-floodsub": "^0.19.0",
95+
"libp2p-gossipsub": "ChainSafe/gossipsub-js#beta/async",
9596
"libp2p-kad-dht": "^0.15.3",
9697
"libp2p-mdns": "^0.12.3",
9798
"libp2p-mplex": "^0.9.1",
@@ -103,6 +104,7 @@
103104
"lodash.times": "^4.3.2",
104105
"nock": "^10.0.6",
105106
"p-defer": "^3.0.0",
107+
"p-wait-for": "^3.1.0",
106108
"portfinder": "^1.0.20",
107109
"pull-goodbye": "0.0.2",
108110
"pull-length-prefixed": "^1.3.3",

src/connection-manager/topology.js

Lines changed: 0 additions & 108 deletions
This file was deleted.

src/index.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict'
22

33
const FSM = require('fsm-event')
4-
const EventEmitter = require('events').EventEmitter
4+
const { EventEmitter } = require('events')
55
const debug = require('debug')
66
const log = debug('libp2p')
77
log.error = debug('libp2p:error')
88
const errCode = require('err-code')
99
const promisify = require('promisify-es6')
1010

1111
const each = require('async/each')
12-
const nextTick = require('async/nextTick')
1312

1413
const PeerInfo = require('peer-info')
1514
const multiaddr = require('multiaddr')
@@ -66,6 +65,8 @@ class Libp2p extends EventEmitter {
6665
this._transport = [] // Transport instances/references
6766
this._discovery = [] // Discovery service instances/references
6867

68+
this.peerStore = new PeerStore()
69+
6970
// create the switch, and listen for errors
7071
this._switch = new Switch(this.peerInfo, this.peerStore, this._options.switch)
7172

@@ -147,7 +148,7 @@ class Libp2p extends EventEmitter {
147148
}
148149

149150
// start pubsub
150-
if (this._modules.pubsub && this._config.pubsub.enabled !== false) {
151+
if (this._modules.pubsub) {
151152
this.pubsub = pubsub(this, this._modules.pubsub, this._config.pubsub)
152153
}
153154

@@ -251,6 +252,7 @@ class Libp2p extends EventEmitter {
251252
this.state('stop')
252253

253254
try {
255+
this.pubsub && await this.pubsub.stop()
254256
await this.transportManager.close()
255257
await this._switch.stop()
256258
} catch (err) {
@@ -385,10 +387,16 @@ class Libp2p extends EventEmitter {
385387
const multiaddrs = this.peerInfo.multiaddrs.toArray()
386388

387389
// Start parallel tasks
390+
const tasks = [
391+
this.transportManager.listen(multiaddrs)
392+
]
393+
394+
if (this._config.pubsub.enabled) {
395+
this.pubsub && this.pubsub.start()
396+
}
397+
388398
try {
389-
await Promise.all([
390-
this.transportManager.listen(multiaddrs)
391-
])
399+
await Promise.all(tasks)
392400
} catch (err) {
393401
log.error(err)
394402
this.emit('error', err)
@@ -483,16 +491,15 @@ module.exports = Libp2p
483491
* Like `new Libp2p(options)` except it will create a `PeerInfo`
484492
* instance if one is not provided in options.
485493
* @param {object} options Libp2p configuration options
486-
* @param {function(Error, Libp2p)} callback
487-
* @returns {void}
494+
* @returns {Libp2p}
488495
*/
489-
module.exports.createLibp2p = promisify((options, callback) => {
496+
module.exports.create = async (options = {}) => {
490497
if (options.peerInfo) {
491-
return nextTick(callback, null, new Libp2p(options))
498+
return new Libp2p(options)
492499
}
493-
PeerInfo.create((err, peerInfo) => {
494-
if (err) return callback(err)
495-
options.peerInfo = peerInfo
496-
callback(null, new Libp2p(options))
497-
})
498-
})
500+
501+
const peerInfo = await PeerInfo.create()
502+
503+
options.peerInfo = peerInfo
504+
return new Libp2p(options)
505+
}

0 commit comments

Comments
 (0)