From ecca2eba8e8350f6003a5e0001eaea4de5e543a9 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Mon, 24 Mar 2025 15:16:46 +0100 Subject: [PATCH] Support WithIgnoreProviders() in provider query manager Adds `Routing.IgnoreProviders`. This requires initializing a custom providerQueryManager and using it instead of the default created internally in Bitswap. Since the default is created with some internal default configuration options (MaxProviders), this hardcodes it. --- config/init.go | 7 ++++--- config/routing.go | 4 ++++ core/node/bitswap.go | 21 ++++++++++++++++++--- docs/config.md | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/config/init.go b/config/init.go index f5217f413..a0351bd8b 100644 --- a/config/init.go +++ b/config/init.go @@ -48,9 +48,10 @@ func InitWithIdentity(identity Identity) (*Config, error) { }, Routing: Routing{ - Type: nil, - Methods: nil, - Routers: nil, + Type: nil, + Methods: nil, + Routers: nil, + IgnoreProviders: []peer.ID{}, }, // setup the node mount points. diff --git a/config/routing.go b/config/routing.go index 52d8b7296..a6f899a1c 100644 --- a/config/routing.go +++ b/config/routing.go @@ -6,6 +6,8 @@ import ( "os" "runtime" "strings" + + peer "github.com/libp2p/go-libp2p/core/peer" ) const ( @@ -41,6 +43,8 @@ type Routing struct { LoopbackAddressesOnLanDHT Flag `json:",omitempty"` + IgnoreProviders []peer.ID + Routers Routers Methods Methods diff --git a/core/node/bitswap.go b/core/node/bitswap.go index a310709bd..4bcc97066 100644 --- a/core/node/bitswap.go +++ b/core/node/bitswap.go @@ -5,11 +5,13 @@ import ( "time" "github.com/ipfs/boxo/bitswap" + "github.com/ipfs/boxo/bitswap/client" bsnet "github.com/ipfs/boxo/bitswap/network/bsnet" blockstore "github.com/ipfs/boxo/blockstore" exchange "github.com/ipfs/boxo/exchange" "github.com/ipfs/boxo/exchange/providing" provider "github.com/ipfs/boxo/provider" + rpqm "github.com/ipfs/boxo/routing/providerquerymanager" "github.com/ipfs/kubo/config" irouting "github.com/ipfs/kubo/routing" "github.com/libp2p/go-libp2p/core/host" @@ -61,6 +63,7 @@ type bitswapIn struct { fx.In Mctx helpers.MetricsCtx + Cfg *config.Config Host host.Host Rt irouting.ProvideManyRouter Bs blockstore.GCBlockstore @@ -71,12 +74,24 @@ type bitswapIn struct { // Additional options to bitswap.New can be provided via the "bitswap-options" // group. func Bitswap(provide bool) interface{} { - return func(in bitswapIn, lc fx.Lifecycle) *bitswap.Bitswap { + return func(in bitswapIn, lc fx.Lifecycle) (*bitswap.Bitswap, error) { bitswapNetwork := bsnet.NewFromIpfsHost(in.Host) var provider routing.ContentDiscovery if provide { - provider = in.Rt + // We need to hardcode the default because it is an + // internal setting in boxo. + pqm, err := rpqm.New(bitswapNetwork, + in.Rt, + rpqm.WithMaxProviders(10), + rpqm.WithIgnoreProviders(in.Cfg.Routing.IgnoreProviders...), + ) + if err != nil { + return nil, err + } + in.BitswapOpts = append(in.BitswapOpts, bitswap.WithClientOption(client.WithDefaultProviderQueryManager(false))) + provider = pqm + } bs := bitswap.New(helpers.LifecycleCtx(in.Mctx, lc), bitswapNetwork, provider, in.Bs, in.BitswapOpts...) @@ -85,7 +100,7 @@ func Bitswap(provide bool) interface{} { return bs.Close() }, }) - return bs + return bs, nil } } diff --git a/docs/config.md b/docs/config.md index 206d1f719..6056510b8 100644 --- a/docs/config.md +++ b/docs/config.md @@ -119,6 +119,7 @@ config file at runtime. - [`Routing.Type`](#routingtype) - [`Routing.AcceleratedDHTClient`](#routingaccelerateddhtclient) - [`Routing.LoopbackAddressesOnLanDHT`](#routingloopbackaddressesonlandht) + - [`Routing.IgnoreProviders`](#routingignoreproviders) - [`Routing.Routers`](#routingrouters) - [`Routing.Routers: Type`](#routingrouters-type) - [`Routing.Routers: Parameters`](#routingrouters-parameters) @@ -1718,6 +1719,19 @@ Default: `false` Type: `bool` (missing means `false`) +### `Routing.IgnoreProviders` + +An array of peerIDs. Any provider record associated to one of these peer IDs is ignored. + +Apart from ignoring specific providers for reasons like misbehaviour etc. this +setting is useful to ignore providers as a way to indicate preference, when the same provider +is found under different peerIDs (i.e. one for HTTP and one for Bitswap retrieval). + +Default: `[]` + +Type: `array[peerID]` + + ### `Routing.Routers` **EXPERIMENTAL: `Routing.Routers` configuration may change in future release**