ipfs / helia

An implementation of IPFS in TypeScript
https://helia.io
Other
973 stars 106 forks source link

Remove DHT and bootstrapping from browser defaults #420

Open aschmahmann opened 9 months ago

aschmahmann commented 9 months ago

Background

Using the Amino DHT from browsers is currently problematic and expensive:

Because Helia is used for a variety of tasks spinning up expensive network calls that are unlikely to be useful is a problematic default. For instance, this helia example for working with a CAR spins up over 200 connections despite not needing the network at all. https://github.com/ipfs-examples/helia-examples/blob/c900274c2819ae320262009974d9a770e5a72955/examples/helia-create-car/src/provider/HeliaProvider.jsx#L38. While perhaps the example could change, I think it's worth considering that it's likely others will create examples in a similar way and it'd be better to give them a good onboarding experience by default.

Proposal

Let's remove the DHT and bootstrap-based peer discovery from the browser defaults. Such as: https://github.com/ipfs/helia/blob/6c88ee1530861357d23ea49d20aac3c1b5aa0bd9/packages/helia/src/utils/libp2p-defaults.browser.ts#L65-L73

https://github.com/ipfs/helia/blob/6c88ee1530861357d23ea49d20aac3c1b5aa0bd9/packages/helia/src/utils/libp2p-defaults.browser.ts#L58-L60

What's Next?

As things like WebRTC-direct are rolled out by default in kubo and become more present in the Amino DHT and if/when the Chromium bugs are fixed it would be reasonable to evaluate re-enabling the Amino DHT by default, but IMO we should keep the defaults at what works best now rather than what we hope will be best later.

achingbrain commented 9 months ago

The WebTransport throttling issue manifests itself as the WebTransport session erroring immediately after it's created. This means we try to dial a peer and fail, moving on to the next address to try again, causing a lot of thrashing in the application without yielding any viable connections to peers.

The primary use of the DHT is finding bitswap providers to service requests for CIDs. The ipfs-bitswap module does this by using libp2p content routing directly, of which @libp2p/kad-dht is an implementation.

https://github.com/ipfs/helia/pull/409 replaces ipfs-bitswap with @helia/bitswap - a lighter weight reimplementation - this uses the higher level Helia routing rather than dropping down to libp2p, so it can take advantage of @helia/delegated-routing-v1-http-api-client and hit HTTP delegate routers to find providers without requiring @libp2p/kad-dht or even libp2p at all.

The hope here is that we don't waste precious WebTransport connections dialling DHT peers, we can save them for bitswap operations. While this will be an improvement it is kicking the can down the road a bit, as at some point we'll hit the throttle limits and WebTransport will stop working until a page reload.

When WebRTC lands in go-libp2p and is turned on in Kubo by default this should get a lot better as it's a much better fit for distributed applications, given it's extensive use in peer to peer video conferencing.

So when we merge https://github.com/ipfs/helia/pull/409 we can remove the DHT from the browser build by default since we can use HTTP delegates to find providers, but we'll still run into problems actually fetching content via bitswap until WebRTC is usable.

In the interim, HTTP block brokers will be able to supply blocks for CIDs, though with some increased centralisation until they look up providers that support the IPFS HTTP Gateway protocol from HTTP delegates instead of hitting the same preconfigured trustless gateways over and over again.

aschmahmann commented 9 months ago

I'm pretty sure we have a version of routing-v1 implemented in javascript that implements the libp2p routing interface and so is usable with ipfs-bitswap. Also, the defaults as-is are almost entirely harmful rather than helpful so removing them now (even if it would theoretically break ipfs-bitswap if the Amino DHT was usable from the browser) seems like an improvment.

That being said, if this is going to happen in a PR landing soon anyway (not sure the timeline on #409) then no use debating whether this should happen beforehand/in parallel.

achingbrain commented 9 months ago

@helia/delegated-routing-v1-http-api-client supports the libp2p routing interfaces too, but it doesn't include the protocols supported by the providers, so it increases the chances of wasted dials, either trying to run identify or just blindly trying the protocol - I guess because it's largely modelled on KAD-DHT and the GET_PROVIDERS RPC call never had this information.

2color commented 9 months ago

So when we merge https://github.com/ipfs/helia/pull/409 we can remove the DHT from the browser build by default since we can use HTTP delegates to find providers, but we'll still run into problems actually fetching content via bitswap until WebRTC is usable.

Why do we need to wait for that PR?

With the DHT Client enabled in browser by default, we're just setting up users for failure, given how low the chances are of establishing a connection.

SgtPooki commented 3 months ago

go-libp2p has released version with webrtc, so this should be released in Kubo soon, and then we can move forward with these changes