@@ -28,6 +28,9 @@ type BlockedExchange struct {
2828
2929// GetBlock gets a block from the exchange only if it's not blocked.
3030func (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.
5257func (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.
145159func (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.
167186func (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