-
Notifications
You must be signed in to change notification settings - Fork 104
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
feat: provide default libp2p instance #127
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { gossipsub } from '@chainsafe/libp2p-gossipsub' | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { bootstrap } from '@libp2p/bootstrap' | ||
import { ipniContentRouting } from '@libp2p/ipni-content-routing' | ||
import { kadDHT, type DualKadDHT } from '@libp2p/kad-dht' | ||
import { mplex } from '@libp2p/mplex' | ||
import { webRTC, webRTCDirect } from '@libp2p/webrtc' | ||
import { webSockets } from '@libp2p/websockets' | ||
import { webTransport } from '@libp2p/webtransport' | ||
import { createLibp2p as create } from 'libp2p' | ||
import { autoNATService } from 'libp2p/autonat' | ||
import { circuitRelayTransport, circuitRelayServer } from 'libp2p/circuit-relay' | ||
import { identifyService } from 'libp2p/identify' | ||
import type { CreateLibp2pOptions } from './libp2p.js' | ||
import type { Libp2p } from '@libp2p/interface-libp2p' | ||
import type { PubSub } from '@libp2p/interface-pubsub' | ||
|
||
export async function createLibp2p (opts: CreateLibp2pOptions): Promise<Libp2p<{ dht: DualKadDHT, pubsub: PubSub }>> { | ||
return create({ | ||
...opts, | ||
addresses: { | ||
listen: [ | ||
'/webrtc' | ||
] | ||
}, | ||
transports: [ | ||
webRTC(), | ||
webRTCDirect(), | ||
webTransport(), | ||
webSockets(), | ||
circuitRelayTransport({ | ||
discoverRelays: 1 | ||
}) | ||
], | ||
connectionEncryption: [ | ||
noise() | ||
], | ||
streamMuxers: [ | ||
yamux(), | ||
mplex() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we kill off mplex? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe - do we have stats for how well used yamux is yet? js-ipfs never shipped with it included so you'd lose compatibility with any existing deployments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, I was wanting to make sure we don't proliferate mplex, but as long as we prefer yamux that is the key thing (which you're doing). I'm not aware of harm of one also offering mplex, and assuming there are no downsides to it, I'm game to include it for compatibility. That said, if go-libp2p ships without mplex by default, I think we'd be fine to remove it. I don't have stats here and I don't think we need to block to find that out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Muxers are negotiated in-order so yamux will be preferred over mplex by any peer that supports both. |
||
], | ||
peerDiscovery: [ | ||
bootstrap({ | ||
list: [ | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt' | ||
] | ||
}) | ||
], | ||
contentRouters: [ | ||
ipniContentRouting('https://cid.contact') | ||
], | ||
services: { | ||
identify: identifyService(), | ||
autoNAT: autoNATService(), | ||
pubsub: gossipsub(), | ||
dht: kadDHT({ | ||
clientMode: true | ||
}), | ||
relay: circuitRelayServer({ | ||
advertise: true | ||
}) | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { gossipsub } from '@chainsafe/libp2p-gossipsub' | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { bootstrap } from '@libp2p/bootstrap' | ||
import { ipniContentRouting } from '@libp2p/ipni-content-routing' | ||
import { type DualKadDHT, kadDHT } from '@libp2p/kad-dht' | ||
import { mdns } from '@libp2p/mdns' | ||
import { mplex } from '@libp2p/mplex' | ||
import { tcp } from '@libp2p/tcp' | ||
import { webSockets } from '@libp2p/websockets' | ||
import { createLibp2p as create } from 'libp2p' | ||
import { autoNATService } from 'libp2p/autonat' | ||
import { circuitRelayTransport, circuitRelayServer, type CircuitRelayService } from 'libp2p/circuit-relay' | ||
import { identifyService } from 'libp2p/identify' | ||
import { uPnPNATService } from 'libp2p/upnp-nat' | ||
import type { Libp2p } from '@libp2p/interface-libp2p' | ||
import type { PubSub } from '@libp2p/interface-pubsub' | ||
import type { Datastore } from 'interface-datastore' | ||
|
||
export interface CreateLibp2pOptions { | ||
datastore: Datastore | ||
start?: boolean | ||
} | ||
|
||
export async function createLibp2p (opts: CreateLibp2pOptions): Promise<Libp2p<{ dht: DualKadDHT, pubsub: PubSub, relay: CircuitRelayService }>> { | ||
return create({ | ||
...opts, | ||
addresses: { | ||
listen: [ | ||
'/ip4/0.0.0.0/tcp/0' | ||
] | ||
}, | ||
transports: [ | ||
tcp(), | ||
webSockets(), | ||
circuitRelayTransport({ | ||
discoverRelays: 1 | ||
}) | ||
], | ||
connectionEncryption: [ | ||
noise() | ||
], | ||
streamMuxers: [ | ||
yamux(), | ||
mplex() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we remove? |
||
], | ||
peerDiscovery: [ | ||
mdns(), | ||
bootstrap({ | ||
list: [ | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt' | ||
] | ||
Comment on lines
+50
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is duplicated. Maybe factor it out, and also add comments on where this list comes from? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}) | ||
], | ||
contentRouters: [ | ||
ipniContentRouting('https://cid.contact') | ||
], | ||
services: { | ||
identify: identifyService(), | ||
autoNAT: autoNATService(), | ||
upnp: uPnPNATService(), | ||
pubsub: gossipsub(), | ||
dht: kadDHT(), | ||
relay: circuitRelayServer({ | ||
advertise: true | ||
}) | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be "optional" dependencies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for others that are browser only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, optional dependencies means "if this dependency failed to install, carry on anyway".
I think what you are saying is "let the user decide whether to install this dep or not", which is what "peerDependencies" used to mean, now npm installs peer deps whether you like it or not so I'm not sure there's a way to do that.