Skip to content

Commit

Permalink
ipfs: remove wrapping blockservice and add ToBlockServiceBlocker helper
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
Jorropo committed Jan 15, 2024
1 parent 5edc09a commit 54c62ec
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 196 deletions.
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)))
}
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

0 comments on commit 54c62ec

Please sign in to comment.