Skip to content
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

ipfs: remove wrapping blockservice and add ToBlockServiceBlocker helper #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 8 additions & 84 deletions ipfs/blockservice.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,22 @@
package ipfs

import (
"context"

"github.com/ipfs-shipyard/nopfs"
blockservice "github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
exchange "github.com/ipfs/boxo/exchange"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
)

var _ blockservice.BlockService = (*BlockService)(nil)

// BlockService implements a blocking BlockService.
type BlockService struct {
blocker *nopfs.Blocker
bs blockservice.BlockService
}

// WrapBlockService wraps the given BlockService with a content-blocking layer
// for Get and Add operations.
func WrapBlockService(bs blockservice.BlockService, blocker *nopfs.Blocker) blockservice.BlockService {
logger.Debug("BlockService wrapped with content blocker")

return &BlockService{
blocker: blocker,
bs: bs,
}
}

// Closes the BlockService and the Blocker.
func (nbs *BlockService) Close() error {
nbs.blocker.Close()
return nbs.bs.Close()
}

// Gets a block unless CID has been blocked.
func (nbs *BlockService) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
if err := nbs.blocker.IsCidBlocked(c).ToError(); err != nil {
logger.Warn(err.Response)
return nil, err
}
return nbs.bs.GetBlock(ctx, c)
}

// GetsBlocks reads several blocks. Blocked CIDs are filtered out of ks.
func (nbs *BlockService) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block {
var filtered []cid.Cid
for _, c := range ks {
if err := nbs.blocker.IsCidBlocked(c).ToError(); err != nil {
logger.Warn(err.Response)
logger.Warnf("GetBlocks dropped blocked block: %s", err)
} else {
filtered = append(filtered, c)
func ToBlockServiceBlocker(blocker *nopfs.Blocker) blockservice.Blocker {
return func(c cid.Cid) error {
err := blocker.IsCidBlocked(c).ToError()
if err != nil {
logger.Warnf("blocked blocks for blockservice: (%s) %s", c, err)
}
}
return nbs.bs.GetBlocks(ctx, filtered)
}

// Blockstore returns the underlying Blockstore.
func (nbs *BlockService) Blockstore() blockstore.Blockstore {
return nbs.bs.Blockstore()
}

// Exchange returns the underlying Exchange.
func (nbs *BlockService) Exchange() exchange.Interface {
return nbs.bs.Exchange()
}

// AddBlock adds a block unless the CID is blocked.
func (nbs *BlockService) AddBlock(ctx context.Context, o blocks.Block) error {
if err := nbs.blocker.IsCidBlocked(o.Cid()).ToError(); err != nil {
logger.Warn(err.Response)
return err
}
return nbs.bs.AddBlock(ctx, o)
}

// AddBlocks adds multiple blocks. Blocks with blocked CIDs are dropped.
func (nbs *BlockService) AddBlocks(ctx context.Context, bs []blocks.Block) error {
var filtered []blocks.Block
for _, o := range bs {
if err := nbs.blocker.IsCidBlocked(o.Cid()).ToError(); err != nil {
logger.Warn(err.Response)
logger.Warnf("AddBlocks dropped blocked block: %s", err)
} else {
filtered = append(filtered, o)
}
}
return nbs.bs.AddBlocks(ctx, filtered)
}

// DeleteBlock deletes a block.
func (nbs *BlockService) DeleteBlock(ctx context.Context, o cid.Cid) error {
return nbs.bs.DeleteBlock(ctx, o)
// Deprecated: This is broken, it discard previous [blockservice.Option] passed in, use [ToBlockServiceBlocker] and pass [blockservice.WithContentBlocker] option when constructing your own blockservice instead.
func WrapBlockService(bs blockservice.BlockService, blocker *nopfs.Blocker) blockservice.BlockService {
return blockservice.New(bs.Blockstore(), bs.Exchange(), blockservice.WithContentBlocker(ToBlockServiceBlocker(blocker)))
Copy link
Author

@Jorropo Jorropo Jan 15, 2024

Choose a reason for hiding this comment

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

This loose other options passed in like WriteThrough or custom allowlist. Might be worth fully breaking the API. Outside of Rainbow and Kubo I don't know of nopfs consumers.

}
64 changes: 33 additions & 31 deletions ipfs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,53 @@ go 1.20

require (
github.com/ipfs-shipyard/nopfs v0.0.12
github.com/ipfs/boxo v0.15.0
github.com/ipfs/go-block-format v0.1.2
github.com/ipfs/boxo v0.17.1-0.20240115075812-7e0cfb11f299
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipld/go-ipld-prime v0.21.0
github.com/libp2p/go-libp2p v0.32.0
github.com/libp2p/go-libp2p v0.32.2
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-block-format v0.2.0 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
github.com/ipfs/go-ipld-format v0.5.0 // indirect
github.com/ipfs/go-ipfs-util v0.0.3 // indirect
github.com/ipfs/go-ipld-format v0.6.0 // indirect
github.com/ipfs/go-log v1.0.5 // indirect
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect
github.com/libp2p/go-libp2p-kad-dht v0.23.0 // indirect
github.com/libp2p/go-libp2p-kbucket v0.5.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.4.1 // indirect
github.com/libp2p/go-libp2p-kad-dht v0.24.4 // indirect
github.com/libp2p/go-libp2p-kbucket v0.6.3 // indirect
github.com/libp2p/go-libp2p-record v0.2.0 // indirect
github.com/libp2p/go-libp2p-routing-helpers v0.7.3 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/miekg/dns v1.1.56 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/miekg/dns v1.1.57 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.12.0 // indirect
github.com/multiformats/go-multiaddr v0.12.1 // indirect
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
Expand All @@ -59,26 +59,28 @@ require (
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gonum.org/v1/gonum v0.11.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/tools v0.16.1 // indirect
gonum.org/v1/gonum v0.14.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
)
Loading
Loading