libp2p / js-libp2p-pubsub-peer-discovery

A js-libp2p module that uses pubsub for mdns like peer discovery
Other
11 stars 6 forks source link

Compatibility with services in libp2p v0.45.0 #98

Closed joeltg closed 1 year ago

joeltg commented 1 year ago

libp2p v0.45.0 moved pubsub/DHT/fetch/identify/ping etc. into a separate "services" object, and as far as I can tell they are no longer available in the initial Components object, making it impossible (???) to make composable services that depend on e.g. pubsub to provide peer discovery. Can anyone confirm if this is true? What's the best approach to something like pubsub-peer-discovery in a post-v0.45.0 world? cc @achingbrain

joeltg commented 1 year ago

(@TimDaub fyi this will affect you if you try to upgrade)

achingbrain commented 1 year ago

This module still works for me if I declare a pubsub service that is an instance of gossipsub. It's not terribly obvious though.

I think what's missing is a way for services and other libp2p components to express their dependencies in a way that the user can discover a compile time - I've opened a PR which I think will fix this here: https://github.com/libp2p/js-libp2p/pull/1762 - I'd love some input on it if you've got the time.

The change to this module after that PR is merged would be to change these lines to add & { pubsub: PubSub } like:

import type { PubSub } from '@libp2p/interface-pubsub'

//... whole file here

export function pubsubPeerDiscovery (init: PubsubPeerDiscoveryInit = {}): (components: PubSubPeerDiscoveryComponents & { pubsub: PubSub }) => PeerDiscovery {
  return (components: PubSubPeerDiscoveryComponents) => new PubSubPeerDiscovery(components, init)
}
joeltg commented 1 year ago

Yeah, I was trying to develop a service similar to this one that depends on pubsub and I resorted to just casting const { pubsub } = components as Components & { pubsub?: PubSub } and asserting that it exists at runtime. Works for now!

Maybe I'm forgetting something (crazy how fast context gets garbage collected in my head) but why say PubSubPeerDiscoveryComponents & { pubsub: PubSub } and not just have pubsub: PubSub in the PubSubPeerDiscoveryComponents definition itself?

achingbrain commented 1 year ago

but why say PubSubPeerDiscoveryComponents & { pubsub: PubSub } and not just have pubsub: PubSub in the PubSubPeerDiscoveryComponents definition itself?

No, you're right - I was thinking of the circuit relay server updates in https://github.com/libp2p/js-libp2p/pull/1762 where the component has an implicit dependency on another component but doesn't actually interact with it in the code.

Hmac512 commented 1 year ago

I have a dirty patch that makes peer discovery work via gossipsub for 0.46.2 here: https://github.com/Hmac512/fix-libp2p-peer-discover-046/blob/9aadfdc80332e2bbde0ce78b91f9183799fba32d/src/gossipNode.ts#L169

Need to clean up the code. It tests forming a mesh with 10 nodes, and it runs successfully.

I left some extra stuff in the patches I added while debugging. Will simplify it later.

The fix is too dirty to be merged into the official packages, but posting here if anyone needs it.

What’s the status on exposing the services in the main js-libp2p?

maschad commented 1 year ago

We should probably update the README to outline how to configure these services with pubsub peer discovery.

Hmac512 commented 1 year ago

Would be helpful