Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pubsub.unsubscribe should comply with interface-ipfs-core spec, since it links to it in the docs #300

Closed
noot opened this issue Dec 18, 2018 · 3 comments
Labels
exp/novice Someone with a little familiarity can pick up help wanted Seeking public contribution on this issue kind/bug A bug in existing code (including security flaws) P2 Medium: Good to have, but can wait until someone steps up

Comments

@noot
Copy link
Contributor

noot commented Dec 18, 2018

  • Version: 0.23.1
  • Platform: Ubuntu 18.04
  • Subsystem:

Type: Bug
Severity: high
Description:
Cannot unsubscribe from a pubsub topic after subscribing. Following the IPFS core API here: https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/PUBSUB.md

Steps to reproduce the error:

const libp2p = require("libp2p")
const TCP = require("libp2p-tcp")
const PeerInfo = require("peer-info")
const waterfall = require("async/waterfall")
const defaultsDeep = require("@nodeutils/defaults-deep")
const Mplex = require("libp2p-mplex")
const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap')

const bootstrappers = [
	'/ip4/127.0.0.1/tcp/43107/ipfs/Qmbm3gwdCZM9GQJg1fpsooZDJ6ma56AFPxqYup53NhvstB'
]

class MyBundle extends libp2p {
	constructor(_options) {
		const defaults = {
			modules: {
				transport: [TCP],
				streamMuxer: [Mplex],
				connEncryption: [SECIO],
				peerDiscovery: [Bootstrap]
			},
			config: {
				peerDiscovery: {
					bootstrap: {
						interval: 2000,
						enabled: true,
						list: bootstrappers
					},
				},
				EXPERIMENTAL: {
					pubsub: true
				}
			}
		}

		super(defaultsDeep(_options, defaults))
	}
}

function createNode(callback) {
	waterfall([
		(cb) => PeerInfo.create(cb),
		(peerInfo, cb) => {
			peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
			node = new MyBundle({peerInfo: peerInfo})
			node.start(cb)
		}
	], (err) => callback(err, node))
}

createNode((err, node) => {
	const topic = 'fruit-of-the-day'
	const receiveMsg = (msg) => console.log(msg.toString())

	node.pubsub.subscribe(topic, receiveMsg, (err) => {
	  if (err) {
	    return console.error(`failed to subscribe to ${topic}`, err)
	  }

	  console.log(`subscribed to ${topic}`)

	  // unsubscribe a second later
	  setTimeout(() => {
	    node.pubsub.unsubscribe(topic, receiveMsg, (err) => {
	      if (err) {
	        return console.error(`failed to unsubscribe from ${topic}`, err)
	      }

	      console.log(`unsubscribed from ${topic}`)
	    })
	  }, 1000)
	})
})

This code is from the IPFS core API as well as the libp2p pubsub tutorial. When running the code, you see that we successfully subscribe to the topic, and after waiting 1 second, we should also see an unsubscribe message. However this never happens.

Any help/feedback is much appreciated!

@jacobheun
Copy link
Contributor

Currently libp2p isn't actually doing anything with the callback, so you're never seeing the results. The unsubscribe is currently synchronous so the callback isn't actually needed, but we should add the callback since we're linking to the interface documents.

The unsubscribe should be happening. If you add an interval based message broadcast you should see it stop after the unsubscribe timeout is called.

setInterval(() => {
  node2.pubsub.publish(topic,
    Buffer.from('pomegranate'),
    () => {}
  )
}, 500)

@jacobheun jacobheun changed the title Cannot unsubscribe to pubsub topic pubsub.unsubscribe should comply with interface-ipfs-core spec, since it links to it in the docs Dec 19, 2018
@jacobheun jacobheun added kind/bug A bug in existing code (including security flaws) help wanted Seeking public contribution on this issue exp/novice Someone with a little familiarity can pick up status/ready Ready to be worked P2 Medium: Good to have, but can wait until someone steps up labels Dec 19, 2018
@noot
Copy link
Contributor Author

noot commented Dec 20, 2018

@jacobheun cool, looks like unsubscribe is working. thanks!

noot added a commit to noot/js-libp2p that referenced this issue Dec 20, 2018
jacobheun pushed a commit that referenced this issue Jan 4, 2019
fix linting issues
pubsub.unsubscribe: check that callback is a function; removed timeout from tests
remove console.log; add chai-checkmark
move done to end of waterfall
update pubsub test
fix lint; remove unneeded waterfall
@jacobheun
Copy link
Contributor

Resolved by #302 via 679d446

@ghost ghost removed the status/ready Ready to be worked label Feb 1, 2019
maschad pushed a commit to maschad/js-libp2p that referenced this issue Jun 21, 2023
…bp2p#300)

Create a private key object from the raw `Ed25519` private key and
export it as a JWK to obtain the public key.

Fixes libp2p#295
maschad pushed a commit to maschad/js-libp2p that referenced this issue Jun 21, 2023
## [1.0.12](libp2p/js-libp2p-crypto@v1.0.11...v1.0.12) (2023-02-08)

### Bug Fixes

* derive ed25519 public key from private key using node crypto ([libp2p#300](libp2p/js-libp2p-crypto#300)) ([874f820](libp2p/js-libp2p-crypto@874f820)), closes [libp2p#295](libp2p/js-libp2p-crypto#295)

### Trivial Changes

* replace err-code with CodeError ([libp2p#293](libp2p/js-libp2p-crypto#293)) ([4398cf6](libp2p/js-libp2p-crypto@4398cf6)), closes [js-libp2p#1269](libp2p#1269)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exp/novice Someone with a little familiarity can pick up help wanted Seeking public contribution on this issue kind/bug A bug in existing code (including security flaws) P2 Medium: Good to have, but can wait until someone steps up
Projects
None yet
Development

No branches or pull requests

2 participants