From 1ab43e054344f0afd1847e9828e601b7602de21a Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 8 Apr 2024 12:37:22 +0200 Subject: [PATCH] feat: peering shared cache --- go.mod | 4 +++- go.sum | 4 ++-- main.go | 7 +++++++ setup.go | 37 ++++++++++++++++++++++++++++++------- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 6324a81..65c30a0 100644 --- a/go.mod +++ b/go.mod @@ -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.18.0 + github.com/ipfs/boxo v0.18.1-0.20240408102328-6c1937446978 github.com/ipfs/go-cid v0.4.1 github.com/ipfs/go-datastore v0.6.0 github.com/ipfs/go-ds-badger4 v0.1.5 @@ -91,12 +91,14 @@ require ( github.com/ipfs/bbloom v0.0.4 // indirect github.com/ipfs/go-bitfield v1.1.0 // indirect github.com/ipfs/go-block-format v0.2.0 // indirect + github.com/ipfs/go-ipfs-pq v0.0.3 // indirect github.com/ipfs/go-ipfs-redirects-file v0.1.1 // indirect github.com/ipfs/go-ipfs-util v0.0.3 // indirect github.com/ipfs/go-ipld-cbor v0.1.0 // indirect github.com/ipfs/go-ipld-format v0.6.0 // indirect github.com/ipfs/go-ipld-legacy v0.2.1 // indirect github.com/ipfs/go-log v1.0.5 // indirect + github.com/ipfs/go-peertaskqueue v0.8.1 // indirect github.com/ipld/go-car/v2 v2.13.1 // indirect github.com/ipld/go-ipld-prime v0.21.0 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect diff --git a/go.sum b/go.sum index 34343d7..066afb4 100644 --- a/go.sum +++ b/go.sum @@ -240,8 +240,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231024163508-120e0c51ee3a h1:MKG github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231024163508-120e0c51ee3a/go.mod h1:6EekK/jo+TynwSE/ZOiOJd4eEvRXoavEC3vquKtv4yI= 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.18.0 h1:MOL9/AgoV3e7jlVMInicaSdbgralfqSsbkc31dZ9tmw= -github.com/ipfs/boxo v0.18.0/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80= +github.com/ipfs/boxo v0.18.1-0.20240408102328-6c1937446978 h1:kv1mKAXQ1HWfzDtBK0d9hhFG8Sk4yiY9VmIKvKhXFok= +github.com/ipfs/boxo v0.18.1-0.20240408102328-6c1937446978/go.mod h1:V5gJzbIMwKEXrg3IdvAxIdF7UPgU4RsXmNGS8MQ/0D4= 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-block-format v0.2.0 h1:ZqrkxBA2ICbDRbK8KJs/u0O3dlp6gmAuuXUJNiW1Ycs= diff --git a/main.go b/main.go index 324868d..d7b9656 100644 --- a/main.go +++ b/main.go @@ -205,6 +205,12 @@ Generate an identity seed and launch a gateway: EnvVars: []string{"RAINBOW_PEERING"}, Usage: "Multiaddresses of peers to stay connected to (comma-separated)", }, + &cli.BoolFlag{ + Name: "peering-shared-cache", + Value: false, + EnvVars: []string{"RAINBOW_PEERING_SHARED_CACHE"}, + Usage: "Enable shared cache between peers defined with --peering. When enabled, the peers are able to serve data over Bitswap to the given peers", + }, &cli.StringFlag{ Name: "blockstore", Value: "flatfs", @@ -318,6 +324,7 @@ share the same seed as long as the indexes are different. IpnsMaxCacheTTL: cctx.Duration("ipns-max-cache-ttl"), DenylistSubs: cctx.StringSlice("denylists"), Peering: peeringAddrs, + PeeringCache: cctx.Bool("peering-shared-cache"), GCInterval: cctx.Duration("gc-interval"), GCThreshold: cctx.Float64("gc-threshold"), } diff --git a/setup.go b/setup.go index c468fcb..60be0bc 100644 --- a/setup.go +++ b/setup.go @@ -15,8 +15,9 @@ import ( "github.com/dgraph-io/badger/v4/options" nopfs "github.com/ipfs-shipyard/nopfs" nopfsipfs "github.com/ipfs-shipyard/nopfs/ipfs" - bsclient "github.com/ipfs/boxo/bitswap/client" + "github.com/ipfs/boxo/bitswap" bsnet "github.com/ipfs/boxo/bitswap/network" + bsserver "github.com/ipfs/boxo/bitswap/server" "github.com/ipfs/boxo/blockservice" "github.com/ipfs/boxo/blockstore" bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice" @@ -75,7 +76,7 @@ type Node struct { dataDir string datastore datastore.Batching blockstore blockstore.Blockstore - bsClient *bsclient.Client + bs *bitswap.Bitswap bsrv blockservice.BlockService resolver resolver.Resolver @@ -112,6 +113,7 @@ type Config struct { DenylistSubs []string Peering []peer.AddrInfo + PeeringCache bool GCInterval time.Duration GCThreshold float64 @@ -319,17 +321,38 @@ func Setup(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cached } } + var pbrf bsserver.PeerBlockRequestFilter + if cfg.PeeringCache && len(cfg.Peering) > 0 { + peers := make(map[peer.ID]struct{}, len(cfg.Peering)) + for _, a := range cfg.Peering { + peers[a.ID] = struct{}{} + } + + pbrf = func(p peer.ID, c cid.Cid) bool { + _, ok := peers[p] + return ok + } + } else { + pbrf = func(p peer.ID, c cid.Cid) bool { + return false + } + } + bsctx := metri.CtxScope(ctx, "ipfs_bitswap") bn := bsnet.NewFromIpfsHost(h, router) - bswap := bsclient.New(bsctx, bn, blkst, + bswap := bitswap.New(bsctx, bn, blkst, + // --- Client Options // default is 1 minute to search for a random live-want (1 // CID). I think we want to search for random live-wants more // often although probably it overlaps with general // rebroadcasts. - bsclient.RebroadcastDelay(delay.Fixed(10*time.Second)), + bitswap.RebroadcastDelay(delay.Fixed(10*time.Second)), // ProviderSearchDelay: default is 1 second. - bsclient.ProviderSearchDelay(time.Second), - bsclient.WithoutDuplicatedBlockStats(), + bitswap.ProviderSearchDelay(time.Second), + bitswap.WithoutDuplicatedBlockStats(), + + // ---- Server Options + bitswap.WithPeerBlockRequestFilter(pbrf), ) bn.Start(bswap) @@ -392,7 +415,7 @@ func Setup(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cached blockstore: blkst, dataDir: cfg.DataDir, datastore: ds, - bsClient: bswap, + bs: bswap, ns: ns, vs: router, bsrv: bsrv,