Skip to content

Commit 8d2b62f

Browse files
committed
fix: noop if no denylist
1 parent 494fbce commit 8d2b62f

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

ipfs/blockservice.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ type BlockService struct {
2222
}
2323

2424
// WrapBlockService wraps the given BlockService with a content-blocking layer
25-
// for Get and Add operations.
25+
// for Get and Add operations. This is the fx.Decorate compatible version.
2626
func WrapBlockService(bs blockservice.BlockService, blocker *nopfs.Blocker) blockservice.BlockService {
27-
logger.Debug("BlockService wrapped with content blocker")
2827

2928
wrapped := &BlockService{
3029
blocker: blocker,
@@ -45,13 +44,14 @@ func WrapBlockService(bs blockservice.BlockService, blocker *nopfs.Blocker) bloc
4544
blocker: blocker,
4645
}
4746
}
48-
4947
return wrapped
5048
}
5149

5250
// Closes the BlockService and the Blocker.
5351
func (nbs *BlockService) Close() error {
54-
nbs.blocker.Close()
52+
if nbs.blocker != nil {
53+
nbs.blocker.Close()
54+
}
5555
return nbs.bs.Close()
5656
}
5757

@@ -81,15 +81,20 @@ func (nbs *BlockService) Exchange() exchange.Interface {
8181

8282
// AddBlock adds a block unless the CID is blocked.
8383
func (nbs *BlockService) AddBlock(ctx context.Context, o blocks.Block) error {
84-
if err := nbs.blocker.IsCidBlocked(o.Cid()).ToError(); err != nil {
85-
logger.Warn(err.Response)
86-
return err
84+
if nbs.blocker != nil {
85+
if err := nbs.blocker.IsCidBlocked(o.Cid()).ToError(); err != nil {
86+
logger.Warn(err.Response)
87+
return err
88+
}
8789
}
8890
return nbs.bs.AddBlock(ctx, o)
8991
}
9092

9193
// AddBlocks adds multiple blocks. Blocks with blocked CIDs are dropped.
9294
func (nbs *BlockService) AddBlocks(ctx context.Context, bs []blocks.Block) error {
95+
if nbs.blocker == nil {
96+
return nbs.bs.AddBlocks(ctx, bs)
97+
}
9398
var filtered []blocks.Block
9499
for _, o := range bs {
95100
if err := nbs.blocker.IsCidBlocked(o.Cid()).ToError(); err != nil {

ipfs/blockstore.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ type BlockedBlockstore struct {
2222

2323
// Get returns a block only if it's not blocked.
2424
func (bs *BlockedBlockstore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
25+
if bs.blocker == nil {
26+
return bs.Blockstore.Get(ctx, c)
27+
}
2528
if err := bs.blocker.IsCidBlocked(c).ToError(); err != nil {
26-
blockstoreLogger.Warnf("Get blocked block: %s", c)
29+
blockstoreLogger.Debugf("Get blocked block: %s", c)
2730
return nil, err
2831
}
2932
return bs.Blockstore.Get(ctx, c)
3033
}
3134

3235
// GetSize returns the size of a block only if it's not blocked.
3336
func (bs *BlockedBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
37+
if bs.blocker == nil {
38+
return bs.Blockstore.GetSize(ctx, c)
39+
}
3440
if err := bs.blocker.IsCidBlocked(c).ToError(); err != nil {
3541
blockstoreLogger.Warnf("GetSize blocked block: %s", c)
3642
return 0, err
@@ -41,6 +47,9 @@ func (bs *BlockedBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error
4147
// Has returns whether a block exists only if it's not blocked.
4248
// If a block is blocked, it returns false (as if it doesn't exist).
4349
func (bs *BlockedBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
50+
if bs.blocker == nil {
51+
return bs.Blockstore.Has(ctx, c)
52+
}
4453
if err := bs.blocker.IsCidBlocked(c).ToError(); err != nil {
4554
blockstoreLogger.Debugf("Has blocked block: %s", c)
4655
// Return false for blocked content, as if it doesn't exist
@@ -51,6 +60,9 @@ func (bs *BlockedBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
5160

5261
// Put adds a block to the blockstore if it's not blocked.
5362
func (bs *BlockedBlockstore) Put(ctx context.Context, b blocks.Block) error {
63+
if bs.blocker == nil {
64+
return bs.Blockstore.Put(ctx, b)
65+
}
5466
if err := bs.blocker.IsCidBlocked(b.Cid()).ToError(); err != nil {
5567
blockstoreLogger.Warnf("Put blocked block: %s", b.Cid())
5668
return err
@@ -60,6 +72,9 @@ func (bs *BlockedBlockstore) Put(ctx context.Context, b blocks.Block) error {
6072

6173
// PutMany adds multiple blocks to the blockstore, filtering out blocked ones.
6274
func (bs *BlockedBlockstore) PutMany(ctx context.Context, blks []blocks.Block) error {
75+
if bs.blocker == nil {
76+
return bs.Blockstore.PutMany(ctx, blks)
77+
}
6378
var filtered []blocks.Block
6479
for _, b := range blks {
6580
if err := bs.blocker.IsCidBlocked(b.Cid()).ToError(); err != nil {
@@ -88,6 +103,10 @@ func (bs *BlockedBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, e
88103
return nil, err
89104
}
90105

106+
if bs.blocker == nil {
107+
return in, nil
108+
}
109+
91110
out := make(chan cid.Cid)
92111
go func() {
93112
defer close(out)

ipfs/exchange.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type BlockedExchange struct {
2828

2929
// GetBlock gets a block from the exchange only if it's not blocked.
3030
func (ex *BlockedExchange) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
31+
if ex.blocker == nil {
32+
return ex.Interface.GetBlock(ctx, c)
33+
}
3134
if err := ex.blocker.IsCidBlocked(c).ToError(); err != nil {
3235
exchangeLogger.Warnf("GetBlock blocked: %s", c)
3336
return nil, err
@@ -40,16 +43,21 @@ func (ex *BlockedExchange) GetBlock(ctx context.Context, c cid.Cid) (blocks.Bloc
4043
}
4144

4245
// Double-check the returned block (in case exchange returns different CID)
43-
if err := ex.blocker.IsCidBlocked(blk.Cid()).ToError(); err != nil {
44-
exchangeLogger.Warnf("GetBlock returned blocked block: %s", blk.Cid())
45-
return nil, err
46+
if ex.blocker != nil {
47+
if err := ex.blocker.IsCidBlocked(blk.Cid()).ToError(); err != nil {
48+
exchangeLogger.Warnf("GetBlock returned blocked block: %s", blk.Cid())
49+
return nil, err
50+
}
4651
}
4752

4853
return blk, nil
4954
}
5055

5156
// GetBlocks gets multiple blocks from the exchange, filtering out blocked ones.
5257
func (ex *BlockedExchange) GetBlocks(ctx context.Context, ks []cid.Cid) (<-chan blocks.Block, error) {
58+
if ex.blocker == nil {
59+
return ex.Interface.GetBlocks(ctx, ks)
60+
}
5361
// Filter the input CIDs
5462
var filtered []cid.Cid
5563
for _, c := range ks {
@@ -84,9 +92,11 @@ func (ex *BlockedExchange) GetBlocks(ctx context.Context, ks []cid.Cid) (<-chan
8492
return
8593
}
8694
// Check if the returned block is blocked
87-
if err := ex.blocker.IsCidBlocked(blk.Cid()).ToError(); err != nil {
88-
exchangeLogger.Debugf("GetBlocks filtered blocked block from response: %s", blk.Cid())
89-
continue
95+
if ex.blocker != nil {
96+
if err := ex.blocker.IsCidBlocked(blk.Cid()).ToError(); err != nil {
97+
exchangeLogger.Debugf("GetBlocks filtered blocked block from response: %s", blk.Cid())
98+
continue
99+
}
90100
}
91101
select {
92102
case out <- blk:
@@ -124,6 +134,10 @@ func (ex *BlockedExchange) NewSession(ctx context.Context) exchange.Fetcher {
124134
if sesEx, ok := ex.Interface.(exchange.SessionExchange); ok {
125135
// Create a session from the underlying exchange and wrap it
126136
underlyingSession := sesEx.NewSession(ctx)
137+
if ex.blocker == nil {
138+
// If no blocker, return the session directly
139+
return underlyingSession
140+
}
127141
return &BlockedFetcher{
128142
Fetcher: underlyingSession,
129143
blocker: ex.blocker,
@@ -143,6 +157,9 @@ type BlockedFetcher struct {
143157

144158
// GetBlock gets a block from the fetcher only if it's not blocked.
145159
func (bf *BlockedFetcher) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
160+
if bf.blocker == nil {
161+
return bf.Fetcher.GetBlock(ctx, c)
162+
}
146163
if err := bf.blocker.IsCidBlocked(c).ToError(); err != nil {
147164
exchangeLogger.Warnf("GetBlock blocked: %s", c)
148165
return nil, err
@@ -155,16 +172,21 @@ func (bf *BlockedFetcher) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block
155172
}
156173

157174
// Double-check the returned block (in case fetcher returns different CID)
158-
if err := bf.blocker.IsCidBlocked(blk.Cid()).ToError(); err != nil {
159-
exchangeLogger.Warnf("GetBlock returned blocked block: %s", blk.Cid())
160-
return nil, err
175+
if bf.blocker != nil {
176+
if err := bf.blocker.IsCidBlocked(blk.Cid()).ToError(); err != nil {
177+
exchangeLogger.Warnf("GetBlock returned blocked block: %s", blk.Cid())
178+
return nil, err
179+
}
161180
}
162181

163182
return blk, nil
164183
}
165184

166185
// GetBlocks gets multiple blocks from the fetcher, filtering out blocked ones.
167186
func (bf *BlockedFetcher) GetBlocks(ctx context.Context, ks []cid.Cid) (<-chan blocks.Block, error) {
187+
if bf.blocker == nil {
188+
return bf.Fetcher.GetBlocks(ctx, ks)
189+
}
168190
// Filter the input CIDs
169191
var filtered []cid.Cid
170192
for _, c := range ks {
@@ -199,9 +221,11 @@ func (bf *BlockedFetcher) GetBlocks(ctx context.Context, ks []cid.Cid) (<-chan b
199221
return
200222
}
201223
// Check if the returned block is blocked
202-
if err := bf.blocker.IsCidBlocked(blk.Cid()).ToError(); err != nil {
203-
exchangeLogger.Debugf("GetBlocks filtered blocked block from response: %s", blk.Cid())
204-
continue
224+
if bf.blocker != nil {
225+
if err := bf.blocker.IsCidBlocked(blk.Cid()).ToError(); err != nil {
226+
exchangeLogger.Debugf("GetBlocks filtered blocked block from response: %s", blk.Cid())
227+
continue
228+
}
205229
}
206230
select {
207231
case out <- blk:

0 commit comments

Comments
 (0)