Skip to content

feat(config): ability to disable Bitswap fully or just server #10782

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

gystemd
Copy link
Contributor

@gystemd gystemd commented Apr 10, 2025

This pull request introduces changes to the Bitswap configuration and functionality, including the addition of new configuration options, modifications to the Bitswap server/client behavior, and the inclusion of new tests to verify these changes.

Bitswap Configuration Enhancements:

  • Added a new BitswapConfig struct in config/bitswap.go to hold Bitswap configuration options, including Enabled and ServerEnabled flags.
  • Updated the Config struct in config/config.go to include the new BitswapConfig field.

Bitswap Server/Client Behavior:

  • Modified the Bitswap function in core/node/bitswap.go to respect the ServerEnabled flag, using an empty blockstore when the server is disabled to prevent serving blocks to other peers.
  • Updated the OnlineExchange function to return a no-op exchange when Bitswap is disabled.
  • Added a noopExchange struct to handle cases where Bitswap is disabled, ensuring no blocks are served or retrieved.

Testing:

  • Introduced a new test file test/cli/bitswap_config_test.go with multiple test cases to verify the behavior of Bitswap configuration, including scenarios where the server is enabled, disabled, or Bitswap is completely disabled.

Closes #10717

@gystemd gystemd requested a review from a team as a code owner April 10, 2025 21:58
@lidel lidel changed the title Add Bitswap disable option feat(config): ability to disable Bitswap fully or just server Apr 11, 2025
Copy link
Member

@lidel lidel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @gystemd, this partially works, but does not fully disable the server (it is still possible for other peers to send us WANT and GET for CIDs we dont have).

See ideas (A) and (B) below, I suspect we will end up with (B) but let's see what is easier.

package config

// BitswapConfig holds Bitswap configuration options
type BitswapConfig struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: package is already named config, so this will be config.BitswapConfig – please name to config.Bitswap

}
bs := bitswap.New(helpers.LifecycleCtx(in.Mctx, lc), bitswapNetwork, provider, in.Bs, in.BitswapOpts...)

bs := bitswap.New(helpers.LifecycleCtx(in.Mctx, lc), bitswapNetwork, provider, blockstoree, in.BitswapOpts...)
Copy link
Member

@lidel lidel Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid this does not actually disable bitswap server, it only makes sure the server will always return "i dont have this blocks" because empty datastore is used.

The problem with this approach is that such node will still announce itself as bitswap server – you can see it by making ipfs idProtocols section will still have:

"/ipfs/bitswap",
"/ipfs/bitswap/1.0.0",
"/ipfs/bitswap/1.1.0",
"/ipfs/bitswap/1.2.0",

This is a problem because such node will still receive bitswap queries ans asks for random CIDs from other peers, and will have to respond to them, effectively wasting resources.

Desired behavior

What we want for Bitswap.ServerEnabled=false is to completely "stop acting as bitswap server" and that means removing it from libp2p identify response (ipfs id), so other peers won't even talk to us over bitswap on their own (and only when we connect to them, as a client).

There are two ways of doing it – not sure which one will be easier, it is fine for you to investugate before making a decision:

  • (A) When server is disabled, do not initialize entire bitswap (which is wrapper for both client+server), and initialize only the client
  • (B) Initialize noop server with emty blockstore, but make sure we dont announce ourselves as /ipfs/bitswap* server to other peers.
    • If client-only approach is too complex to execute in Kubo codebase, it is sensible to keep your solution with noop server, and just finding a way to remove /ipfs/bitswap* from libp2p identify response (which can be inspected via ipfs id <peerid> or vole libp2p identify)
    • This may require changes to https://github.com/ipfs/boxo – perhaps adding a new configuration option for Bitswap server that disables annoucing /ipfs/bitswap to libp2p host

I'd say see if (A) is easy to do, but if you find yourself being blocked by a bigger refactor (e.g. all the commands that expect bitswap server to be always present), consider (B) instead, as it would have smaller code surface, and also be easier to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I investigated both the suggested solutions , but I got stuck in trying to prevent the node to communicate /ipfs/bitswap* as supported protocols: the identify protocol is segregated inside the go-libp2p library and I cannot manipulate its internals. I have found a way to remove the handler associated to the /ipfs/bitswap* protocol in the Host's multiplexer (Mux). This way, the protocol is effectively removed from the list of supported protocols, but it results in the client not being able to send messages anymore (I suspect it is due to this boxo function). What if we register a stream handler for that just immediately shutsdown? I mean replacing this handler with an empty method that just closes the stream. This way maybe the node will stop responding completely to other nodes bitswap requests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gystemd thank you for looking into this.

I wonder if we could do this earlier bitswap/network/bsnet – I'm thinking Start instead of handleNewStream:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test that confirms ipfs id peerid no longer includes /ipfs/bitswap protocols when Bitswap.ServerEnabled is set to false (ensuring we no longer announce ourselves as server)

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.

config: Flag to disable Bitswap client and server
2 participants