Skip to content

Commit

Permalink
feat: integrate gossipsub by default
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed May 24, 2019
1 parent bde885e commit 0af82b9
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 219 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
"cids": "~0.6.0",
"debug": "^4.1.1",
"length-prefixed-stream": "github:jacobheun/length-prefixed-stream#v2.0.0-rc.1",
"libp2p": "~0.25.2",
"libp2p": "libp2p/js-libp2p#feat/integrate-gossipsub-by-default",
"libp2p-bootstrap": "~0.9.7",
"libp2p-floodsub": "~0.16.1",
"libp2p-gossipsub": "ChainSafe/gossipsub-js#master",
"libp2p-kad-dht": "~0.14.12",
"libp2p-mplex": "~0.8.5",
"libp2p-secio": "~0.11.1",
Expand Down
5 changes: 5 additions & 0 deletions src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const main = async (processArgs) => {
type: 'boolean',
default: false
})
.option('pubsubRouter', {
desc: 'Specifies the pubsub router implementation',
type: 'string',
default: 'gossipsub'
})
.fail((msg, err, yargs) => {
if (err) {
throw err // preserve stack
Expand Down
24 changes: 15 additions & 9 deletions src/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const Bootstrap = require('libp2p-bootstrap')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const KadDHT = require('libp2p-kad-dht')
const FloodSub = require('libp2p-floodsub')
const GossipSub = require('libp2p-gossipsub')
const pullToStream = require('pull-stream-to-stream')
const PeerBook = require('peer-book')
const PeerInfo = require('peer-info')
Expand Down Expand Up @@ -125,7 +127,7 @@ class Pubsub {
*/
subscribe (topic, options, handler) {
return new Promise((resolve, reject) => {
this.libp2p._pubsub.subscribe(topic, options, handler, (err) => {
this.libp2p._pubSub.subscribe(topic, options, handler, (err) => {
if (err) return reject(err)
resolve()
})
Expand All @@ -140,7 +142,7 @@ class Pubsub {
*/
publish (topic, data) {
return new Promise((resolve, reject) => {
this.libp2p._pubsub.publish(topic, data, (err) => {
this.libp2p._pubSub.publish(topic, data, (err) => {
if (err) return reject(err)
resolve()
})
Expand All @@ -153,7 +155,7 @@ class Pubsub {
*/
getTopics () {
return new Promise((resolve, reject) => {
this.libp2p._pubsub.ls((err, topics) => {
this.libp2p._pubSub.ls((err, topics) => {
if (err) return reject(err)
resolve(topics)
})
Expand Down Expand Up @@ -256,8 +258,8 @@ class DaemonLibp2p extends Libp2p {
constructor (libp2pOpts, { announceAddrs }) {
super(libp2pOpts)
this.announceAddrs = announceAddrs
this.needsPullStream = libp2pOpts.config.EXPERIMENTAL.pubsub
this._pubsub = this.pubsub
this.needsPullStream = libp2pOpts.config.pubsub.enabled
this._pubSub = this.pubsub
this.pubsub = new Pubsub(this)
}
get contentRouting () {
Expand Down Expand Up @@ -389,6 +391,8 @@ class DaemonLibp2p extends Libp2p {
* @param {string} opts.id
* @param {string} opts.bootstrapPeers
* @param {string} opts.hostAddrs
* @param {boolean} opts.pubsub
* @param {string} opts.pubsubRouter
* @returns {Libp2p}
*/
const createLibp2p = async ({
Expand All @@ -402,7 +406,8 @@ const createLibp2p = async ({
connMgrLo,
connMgrHi,
id,
pubsub
pubsub,
pubsubRouter
} = {}) => {
const peerInfo = await getPeerInfo(id)
const peerBook = new PeerBook()
Expand Down Expand Up @@ -437,7 +442,8 @@ const createLibp2p = async ({
peerDiscovery: [
Bootstrap
],
dht: KadDHT
dht: KadDHT,
pubsub: pubsubRouter === 'floodsub' ? GossipSub : FloodSub
},
config: {
peerDiscovery: {
Expand All @@ -458,8 +464,8 @@ const createLibp2p = async ({
enabled: dht,
kBucketSize: 20
},
EXPERIMENTAL: {
pubsub: pubsub
pubsub: {
enabled: pubsub
}
}
}, {
Expand Down
Loading

0 comments on commit 0af82b9

Please sign in to comment.