Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ChainSafe/js-libp2p-gossipsub
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1852ec82f1316fecba40cd96410d4bda9ff23192
Choose a base ref
...
head repository: ChainSafe/js-libp2p-gossipsub
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: aafe59aa84cb78b367bcfaf26c8df94fe80c4a30
Choose a head ref
Loading
142 changes: 142 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,145 @@
<a name="0.4.0"></a>
# [0.4.0](https://github.com/ChainSafe/gossipsub-js/compare/v0.3.0...v0.4.0) (2020-04-23)


### Chores

* remove peer-info usage ([602ccaa](https://github.com/ChainSafe/gossipsub-js/commit/602ccaa))


### BREAKING CHANGES

* using new topology api with peer-id instead of peer-info and new pubsub internal peer data structure



<a name="0.3.0"></a>
# [0.3.0](https://github.com/ChainSafe/gossipsub-js/compare/v0.2.6...v0.3.0) (2020-04-23)


### Bug Fixes

* add buffer and update deps ([d8e9d1b](https://github.com/ChainSafe/gossipsub-js/commit/d8e9d1b))



<a name="0.2.6"></a>
## [0.2.6](https://github.com/ChainSafe/gossipsub-js/compare/v0.2.5...v0.2.6) (2020-04-04)



<a name="0.2.5"></a>
## [0.2.5](https://github.com/ChainSafe/gossipsub-js/compare/v0.2.4...v0.2.5) (2020-03-21)



<a name="0.2.4"></a>
## [0.2.4](https://github.com/ChainSafe/gossipsub-js/compare/v0.2.3...v0.2.4) (2020-02-14)


### Bug Fixes

* remove use of assert module ([#65](https://github.com/ChainSafe/gossipsub-js/issues/65)) ([e0a37cc](https://github.com/ChainSafe/gossipsub-js/commit/e0a37cc))



<a name="0.2.3"></a>
## [0.2.3](https://github.com/ChainSafe/gossipsub-js/compare/v0.0.5...v0.2.3) (2020-02-03)


### Bug Fixes

* bind is not needed ([7faae98](https://github.com/ChainSafe/gossipsub-js/commit/7faae98))
* fix double publish bug ([519f868](https://github.com/ChainSafe/gossipsub-js/commit/519f868)), closes [/github.com/ChainSafe/gossipsub-js/blob/master/test/floodsub.spec.js#L21](https://github.com//github.com/ChainSafe/gossipsub-js/blob/master/test/floodsub.spec.js/issues/L21) [/travis-ci.com/ChainSafe/gossipsub-js/jobs/278427804#L270](https://github.com//travis-ci.com/ChainSafe/gossipsub-js/jobs/278427804/issues/L270)
* fix heartbeat startup/shutdown timing bug ([a8302f9](https://github.com/ChainSafe/gossipsub-js/commit/a8302f9))
* parameter to _onPeerDisconnected ([d43ceb5](https://github.com/ChainSafe/gossipsub-js/commit/d43ceb5))


### Chores

* update dependencies ([7a44b66](https://github.com/ChainSafe/gossipsub-js/commit/7a44b66))


### Code Refactoring

* switch to async iterators ([3027835](https://github.com/ChainSafe/gossipsub-js/commit/3027835))


### Performance Improvements

* do not depend on floodsub ([#63](https://github.com/ChainSafe/gossipsub-js/issues/63)) ([f825e07](https://github.com/ChainSafe/gossipsub-js/commit/f825e07))


### BREAKING CHANGES

* getPeersSubscribed from parent class renamed to getSubscribers to remove redundant wording
* Switch to using async/await and async iterators for all the API. Moreover, gossipsub does not need the libp2p instance anymore, receiving a registerar that enables it to receive the necessary events from libp2p



<a name="0.2.2"></a>
## [0.2.2](https://github.com/ChainSafe/gossipsub-js/compare/v0.0.5...v0.2.2) (2020-01-24)


### Bug Fixes

* bind is not needed ([7faae98](https://github.com/ChainSafe/gossipsub-js/commit/7faae98))
* fix double publish bug ([519f868](https://github.com/ChainSafe/gossipsub-js/commit/519f868)), closes [/github.com/ChainSafe/gossipsub-js/blob/master/test/floodsub.spec.js#L21](https://github.com//github.com/ChainSafe/gossipsub-js/blob/master/test/floodsub.spec.js/issues/L21) [/travis-ci.com/ChainSafe/gossipsub-js/jobs/278427804#L270](https://github.com//travis-ci.com/ChainSafe/gossipsub-js/jobs/278427804/issues/L270)
* fix heartbeat startup/shutdown timing bug ([a8302f9](https://github.com/ChainSafe/gossipsub-js/commit/a8302f9))
* parameter to _onPeerDisconnected ([d43ceb5](https://github.com/ChainSafe/gossipsub-js/commit/d43ceb5))


### Chores

* update dependencies ([7a44b66](https://github.com/ChainSafe/gossipsub-js/commit/7a44b66))


### Code Refactoring

* switch to async iterators ([3027835](https://github.com/ChainSafe/gossipsub-js/commit/3027835))


### BREAKING CHANGES

* getPeersSubscribed from parent class renamed to getSubscribers to remove redundant wording
* Switch to using async/await and async iterators for all the API. Moreover, gossipsub does not need the libp2p instance anymore, receiving a registerar that enables it to receive the necessary events from libp2p



<a name="0.2.1"></a>
## 0.2.1 (2019-01-14)


### Bug Fixes

* bind is not needed ([7faae98](https://github.com/ChainSafe/gossipsub-js/commit/7faae98))



<a name="0.2.0"></a>
## [0.2.0](https://github.com/ChainSafe/gossipsub-js/compare/v0.0.5...v0.1.0) (2019-12-02)

### Chores

* update dependencies ([7a44b66](https://github.com/ChainSafe/gossipsub-js/commit/7a44b66))

### BREAKING CHANGES

* getPeersSubscribed from parent class renamed to getSubscribers to remove redundant wording.



<a name="0.1.0"></a>
## [0.1.0](https://github.com/ChainSafe/gossipsub-js/compare/v0.0.5...v0.1.0) (2019-19-14)

### Code Refactoring

* switch to async iterators ([ec8db51](https://github.com/ChainSafe/gossipsub-js/commit/2c32d25))

### BREAKING CHANGES

* Switch to using async/await and async iterators.

<a name="0.0.2"></a>
## [0.0.2](https://github.com/ChainSafe/gossipsub-js/compare/v0.0.1...v0.0.2) (2019-06-04)

37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ js-libp2p-gossipsub
==================

[![](https://img.shields.io/badge/made%20by-ChainSafe-blue.svg?style=flat-square)](https://chainsafe.io/)
[![Travis CI](https://flat.badgen.net/travis/ipfs/aegir)](https://travis-ci.com/ipfs/aegir)
[![Travis CI](https://flat.badgen.net/travis/ChainSafe/gossipsub-js)](https://travis-ci.com/ChainSafe/gossipsub-js)

## Lead Maintainer

@@ -20,6 +20,8 @@ js-libp2p-gossipsub
## Specs
Gossipsub is an implementation of pubsub based on meshsub and floodsub. You can read the specification [here](https://github.com/libp2p/specs/tree/master/pubsub/gossipsub).

`libp2p-gossipsub` currently implements the `v1.0` of the spec.

## Install

`npm install libp2p-gossipsub`
@@ -29,20 +31,29 @@ Gossipsub is an implementation of pubsub based on meshsub and floodsub. You can
```javascript
const Gossipsub = require('libp2p-gossipsub')

const gsub = new Gossipsub(node)
const registrar = {
handle: (multicodecs, handle) => {
// register multicodec to libp2p
// handle function is called everytime a remote peer opens a stream to the peer.
},
register: (multicodecs, handlers) => {
// handlers will be used to notify pubsub of peer connection establishment or closing
},
unregister: (id) => {

gsub.start((err) => {
if (err) {
console.log('Upsy', err)
}
gsub.on('fruit', (data) => {
console.log(data)
})
gsub.subscribe('fruit')
}

const gsub = new Gossipsub(peerId, registrar, options)

gsub.publish('fruit', new Buffer('banana'))
await gsub.start()

gsub.on('fruit', (data) => {
console.log(data)
})
gsub.subscribe('fruit')

gsub.publish('fruit', new Buffer('banana'))
```

## API
@@ -51,13 +62,17 @@ gsub.start((err) => {

```js
const options = {…}
const gossipsub = new Gossipsub(libp2pNode, options)
const gossipsub = new Gossipsub(peerId, registrar, options)
```

Options is an optional object with the following key-value pairs:

* **`fallbackToFloodsub`**: boolean identifying whether the node should fallback to the floodsub protocol, if another connecting peer does not support gossipsub (defaults to **true**).
* **`emitSelf`**: boolean identifying whether the node should emit to self on publish, in the event of the topic being subscribed (defaults to **false**).
* **`msgIdFn`**: a function with signature `(message) => string` defining the message id given a message, used internally to deduplicate gossip (defaults to `(message) => message.from + message.seqno.toString('hex')`)
* **`messageCache`**: optional, a customized `MessageCache` instance, see the implementation for the interface.

For the remaining API, see https://github.com/libp2p/js-libp2p-pubsub

## Contribute

52 changes: 28 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "libp2p-gossipsub",
"version": "0.0.5",
"version": "0.4.0",
"description": "A javascript implementation of gossipsub",
"leadMaintainer": "Cayman Nava <caymannava@gmail.com>",
"main": "src/index.js",
"files": [
"src",
"dist"
],
"types": "src/index.d.ts",
"scripts": {
"lint": "aegir lint",
"release": "aegir release",
@@ -32,43 +34,45 @@
"lint"
],
"dependencies": {
"async": "^2.6.2",
"err-code": "^1.1.2",
"libp2p-floodsub": "~0.17.1",
"libp2p-pubsub": "~0.2.0",
"multistream-select": "~0.14.6",
"peer-id": "~0.12.2",
"peer-info": "~0.15.1",
"buffer": "^5.6.0",
"debug": "^4.1.1",
"err-code": "^2.0.0",
"it-length-prefixed": "^3.0.0",
"it-pipe": "^1.0.1",
"libp2p-pubsub": "^0.5.0",
"p-map": "^4.0.0",
"peer-id": "~0.13.3",
"protons": "^1.0.1",
"pull-length-prefixed": "^1.3.3",
"pull-stream": "^3.6.13"
"time-cache": "^0.3.0"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.7",
"aegir": "^20.0.0",
"@types/chai": "^4.2.3",
"@types/mocha": "^7.0.2",
"aegir": "^21.9.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chai-spies": "^1.0.0",
"detect-node": "^2.0.4",
"dirty-chai": "^2.0.1",
"libp2p": "~0.25.5",
"libp2p-secio": "~0.11.1",
"libp2p-spdy": "~0.13.3",
"libp2p-tcp": "~0.13.0",
"libp2p-websockets": "~0.12.2",
"it-pair": "^1.0.0",
"libp2p-floodsub": "^0.21.0",
"lodash": "^4.17.15",
"mocha": "^5.2.0",
"mocha": "^7.1.1",
"p-times": "^2.1.0",
"p-wait-for": "^3.1.0",
"promisify-es6": "^1.0.3",
"sinon": "^7.3.2"
"sinon": "^9.0.2"
},
"contributors": [
"Cayman <caymannava@gmail.com>",
"ChainSafe <aidan@chainsafe.io>",
"ChainSafe <superadmin@chainsafe.io>",
"Mikerah <mikerahqc@protonmail.com>",
"Mikerah <mikerah14@gmail.com>",
"Vasco Santos <vasco.santos@moxy.studio>",
"Vasco Santos <vasco.santos@ua.pt>"
"Alan Shaw <alan.shaw@protocol.ai>",
"Tuyen <tuyen@wetrust.io>",
"achingbrain <alex@achingbrain.net>",
"Jacob Heun <jacobheun@gmail.com>",
"ChainSafe <aidan@chainsafe.io>",
"Marin Petrunić <marin.petrunic@gmail.com>",
"Hugo Dias <hugomrdias@gmail.com>"
]
}
49 changes: 22 additions & 27 deletions src/heartbeat.js
Original file line number Diff line number Diff line change
@@ -12,52 +12,47 @@ class Heartbeat {
this.gossipsub = gossipsub
}

start (callback) {
start () {
if (this._heartbeatTimer) {
const errMsg = 'Heartbeat timer is already running'
this.gossipsub.log(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_HEARTBEAT_ALREADY_RUNNING'))
throw errcode(new Error(errMsg), 'ERR_HEARTBEAT_ALREADY_RUNNING')
}

const heartbeatTimer = {
const heartbeat = this._heartbeat.bind(this)

const timeout = setTimeout(() => {
heartbeat()
this._heartbeatTimer.runPeriodically(heartbeat, constants.GossipSubHeartbeatInterval)
}, constants.GossipSubHeartbeatInitialDelay)

this._heartbeatTimer = {
_onCancel: null,
_timeoutId: null,
_intervalId: null,
runPeriodically: (fn, period) => {
heartbeatTimer._timeoutId = setInterval(fn, period)
this._heartbeatTimer._intervalId = setInterval(fn, period)
},
cancel: (cb) => {
clearTimeout(heartbeatTimer._timeoutId)
cb()
cancel: () => {
clearTimeout(timeout)
clearInterval(this._heartbeatTimer._intervalId)
}
}

const heartbeat = this._heartbeat.bind(this)
setTimeout(() => {
heartbeat()
heartbeatTimer.runPeriodically(heartbeat, constants.GossipSubHeartbeatInterval)
}, constants.GossipSubHeartbeatInitialDelay)

this._heartbeatTimer = heartbeatTimer
callback()
}

/**
* Unmounts the gossipsub protocol and shuts down every connection
*
* @override
* @param {Function} callback
* @returns {void}
*/
stop (callback) {
stop () {
if (!this._heartbeatTimer) {
const errMsg = 'Heartbeat timer is not running'
this.gossipsub.log(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_HEARTBEAT_NO_RUNNING'))
throw errcode(new Error(errMsg), 'ERR_HEARTBEAT_NO_RUNNING')
}
this._heartbeatTimer.cancel(() => {
this._heartbeatTimer = null
callback()
})

this._heartbeatTimer.cancel()
this._heartbeatTimer = null
}

/**
@@ -88,7 +83,7 @@ class Heartbeat {
return
}

this.gossipsub.log('HEARTBEAT: Add mesh link to %s in %s', peer.info.id.toB58String(), topic)
this.gossipsub.log('HEARTBEAT: Add mesh link to %s in %s', peer.id.toB58String(), topic)
peers.add(peer)
const peerGrafts = tograft.get(peer)
if (!peerGrafts) {
@@ -107,7 +102,7 @@ class Heartbeat {
peersArray = peersArray.slice(0, idontneed)

peersArray.forEach((peer) => {
this.gossipsub.log('HEARTBEAT: Remove mesh link to %s in %s', peer.info.id.toB58String(), topic)
this.gossipsub.log('HEARTBEAT: Remove mesh link to %s in %s', peer.id.toB58String(), topic)
peers.delete(peer)
const peerPrunes = toprune.get(peer)
if (!peerPrunes) {
Loading