Skip to content

Conversation

@yashranaway
Copy link

@yashranaway yashranaway commented Oct 2, 2025

Description

This PR implements Bitswap protocol (/ipfs/bitswap/1.2.0) support for the libp2p network backend, allowing the --ipfs-server option to work with both litep2p and libp2p backends.

Closes: #9837

Previously, the --ipfs-server option was only functional with the litep2p network backend. This change ensures compatibility across both network backends by registering the Bitswap request-response protocol in the libp2p backend when transaction storage with IPFS serving is enabled.

Integration

This change is backward compatible and requires no action from downstream projects:

  • Existing nodes using litep2p with --ipfs-server continue to work unchanged
  • Existing nodes using libp2p can now enable --ipfs-server to serve transaction data via Bitswap
  • The Bitswap protocol registration happens automatically when bitswap_config is present in the network parameters

Example usage:

# Now works with libp2p backend (previously only worked with litep2p)
./substrate-node --ipfs-server --network-backend libp2p

Review Notes

Implementation approach:

  • Files modified:

    • substrate/client/network/src/service.rs: Added Bitswap protocol registration in libp2p NetworkWorker::new()
    • substrate/frame/transaction-storage/README.md: Updated documentation to mention libp2p support
  • Key changes:

    // In NetworkWorker::new()
    + // If Bitswap is enabled, register its request-response protocol for libp2p backend.
    + if let Some(bitswap_cfg) = params.bitswap_config {
    +     request_response_protocols.push(bitswap_cfg);
    + }
  • Technical details:

    • Leverages existing BitswapRequestHandler implementation (substrate/client/network/src/bitswap/mod.rs)
    • Registers /ipfs/bitswap/1.2.0 protocol in libp2p's request-response protocols list
    • The NetworkBackend trait already provides bitswap_server() method for libp2p backend
    • No changes needed to the core Bitswap handler logic
  • Testing:

    • Code compiles successfully with cargo check -p sc-network
    • Existing Bitswap tests continue to pass
    • Manual testing recommended: run node with --ipfs-server using libp2p and verify IPFS client can fetch transaction CIDs

Checklist

  • My PR includes a detailed description as outlined in the "Description" and its two subsections above.
  • My PR follows the labeling requirements of this project (at minimum one label for T required)
    • External contributors: ask maintainers to put the right label on your PR.
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

AI Disclosure

  • Used Claude for Code refactoring and overall tests etc

…erver is enabled; docs: mention libp2p support
@yashranaway yashranaway requested a review from a team as a code owner October 2, 2025 20:08
@cla-bot-2021
Copy link

cla-bot-2021 bot commented Oct 2, 2025

User @yashranaway, please sign the CLA here.

@yashranaway yashranaway changed the title network(libp2p): register Bitswap (/ipfs/bitswap/1.2.0) when --ipfs-s… feat(network): implement Bitswap protocol support for libp2p network backend Oct 2, 2025
@bkontur bkontur requested a review from dmitry-markin October 3, 2025 08:58
@bkontur
Copy link
Contributor

bkontur commented Oct 3, 2025

cc: @dmitry-markin

@bkchr bkchr requested a review from a team October 7, 2025 08:24
@dmitry-markin
Copy link
Contributor

Hi @yashranaway and thanks for looking into this! Unfortunately, Bitswap is not a request-response protocol how it is implemented in the libp2p network backend. It as an asynchronous protocol with requests coming over one substream and responses sent over another, with no limit on the number of messages sent over a substream. libp2p request-response protocol uses the same substream for a pair of request-response messages and closes the substream after the exchange. It can't be used to implement Bitswap, the implementation in libp2p network backend is invalid.

In order to implement Bitswap for libp2p network backend you would need to implement a dedicated NetworkBehavior and handle the substreams manually as per Bitswap spec.

@yashranaway
Copy link
Author

@dmitry-markin hey,I've implemented Bitswap as a proper NetworkBehaviour with a custom connection handler that manages persistent bidirectional substreams, as per the Bitswap spec. please let me if any updates are needed

@dmitry-markin
Copy link
Contributor

Hey, given the PR looks like AI-generated, can you please:

  1. Label it as such, referencing the tools used.
  2. Perform self-review, fixing obvious issues and simplifying the code.
  3. Perform manual testing you recommend in the PR description ensuring the code works. You can use https://github.com/paritytech/polkadot-bulletin-chain/tree/main/examples to automate testing. You will need to run bulletin with libp2p network backend, commenting out the litep2p network backend check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants