Skip to content

Commit

Permalink
Cleanup and update to boxo with latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Nov 15, 2024
1 parent adc6960 commit 0693583
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 57 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/felixge/httpsnoop v1.0.4
github.com/ipfs-shipyard/nopfs v0.0.12
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231024163508-120e0c51ee3a
github.com/ipfs/boxo v0.21.1-0.20240726111146-e95eeb2ae5f1
github.com/ipfs/boxo v0.21.1-0.20241115120301-beab6fcdfb3f
github.com/ipfs/go-block-format v0.2.0
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-datastore v0.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.21.1-0.20240726111146-e95eeb2ae5f1 h1:wsetxKWIhOhGi8exgTrZfhxiky76YSwdTcm1ZdcIqAU=
github.com/ipfs/boxo v0.21.1-0.20240726111146-e95eeb2ae5f1/go.mod h1:NmweAYeY1USOaJJxouy7DLr/Y5M8UBSsCI2KRivO+TY=
github.com/ipfs/boxo v0.21.1-0.20241115120301-beab6fcdfb3f h1:o/GGQmDJ3CYeNlpaV2LKQW7lBhn2mzDn9jpNzVPjsFo=
github.com/ipfs/boxo v0.21.1-0.20241115120301-beab6fcdfb3f/go.mod h1:NmweAYeY1USOaJJxouy7DLr/Y5M8UBSsCI2KRivO+TY=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-bitswap v0.11.0 h1:j1WVvhDX1yhG32NTC9xfxnqycqYIlhzEzLXG/cU1HyQ=
Expand Down
70 changes: 14 additions & 56 deletions setup_bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"context"
"github.com/ipfs/boxo/routing/providerquerymanager"
"github.com/libp2p/go-libp2p/core/peerstore"
"time"

"github.com/ipfs/boxo/routing/providerquerymanager"

"github.com/ipfs/boxo/bitswap"
bsclient "github.com/ipfs/boxo/bitswap/client"
wl "github.com/ipfs/boxo/bitswap/client/wantlist"
Expand All @@ -25,13 +25,16 @@ import (

func setupBitswapExchange(ctx context.Context, cfg Config, h host.Host, cr routing.ContentRouting, bstore blockstore.Blockstore) exchange.Interface {
bsctx := metri.CtxScope(ctx, "ipfs_bitswap")
n := &providerQueryNetwork{cr, h}
pqm, err := providerquerymanager.New(ctx, n, providerquerymanager.WithMaxInProcessRequests(100))

bn := bsnet.NewFromIpfsHost(h, cr)

// Custom query manager with the content router and the host
// and our custom options to overwrite the default.
pqm, err := providerquerymanager.New(ctx, h, cr, providerquerymanager.WithMaxInProcessRequests(100))

if err != nil {
panic(err)
}
cr = &wrapProv{pqm: pqm}
bn := bsnet.NewFromIpfsHost(h, cr)

// --- Client Options
// bitswap.RebroadcastDelay: default is 1 minute to search for a random
Expand All @@ -46,7 +49,7 @@ func setupBitswapExchange(ctx context.Context, cfg Config, h host.Host, cr routi
bsclient.RebroadcastDelay(rebroadcastDelay),
bsclient.ProviderSearchDelay(providerSearchDelay),
bsclient.WithoutDuplicatedBlockStats(),
bsclient.WithDefaultLookupManagement(false),
bsclient.WithCustomLookupManagement(pqm),
}

// If peering and shared cache are both enabled, we initialize both a
Expand Down Expand Up @@ -95,58 +98,13 @@ func setupBitswapExchange(ctx context.Context, cfg Config, h host.Host, cr routi
}

type providerQueryNetwork struct {
bsnet.BitSwapNetwork
routing.ContentRouting
host.Host
}

func (p *providerQueryNetwork) ConnectTo(ctx context.Context, id peer.ID) error {
return p.Host.Connect(ctx, peer.AddrInfo{ID: id})
}

func (p *providerQueryNetwork) FindProvidersAsync(ctx context.Context, c cid.Cid, i int) <-chan peer.ID {
out := make(chan peer.ID, i)
go func() {
defer close(out)
providers := p.ContentRouting.FindProvidersAsync(ctx, c, i)
for info := range providers {
if info.ID == p.Host.ID() {
continue // ignore self as provider
}
p.Host.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.TempAddrTTL)
select {
case <-ctx.Done():
return
case out <- info.ID:
}
}
}()
return out
}

type wrapProv struct {
pqm *providerquerymanager.ProviderQueryManager
}

var _ routing.ContentRouting = (*wrapProv)(nil)

func (r *wrapProv) Provide(ctx context.Context, c cid.Cid, b bool) error {
return routing.ErrNotSupported
}

func (r *wrapProv) FindProvidersAsync(ctx context.Context, c cid.Cid, _ int) <-chan peer.AddrInfo {
retCh := make(chan peer.AddrInfo)
go func() {
defer close(retCh)
provsCh := r.pqm.FindProvidersAsync(ctx, c)
for p := range provsCh {
select {
case retCh <- peer.AddrInfo{ID: p}:
case <-ctx.Done():
}
}
}()
return retCh
}
// func (pqn *providerQueryNetwork) FindProvidersAsync(ctx context.Context, c cid.Cid, n int) <-chan peer.AddrInfo {
// return pqn.ContentRouting.FindProvidersAsync(ctx, c, n)
// }

type noopPeerLedger struct{}

Expand Down

0 comments on commit 0693583

Please sign in to comment.