Repository: ipfs/boxo (language: go)

Issue:
gateway/blocks-backend:  GetBlock should not perform IPLD decoding
The gateway BlocksBackend's `GetBlock` function under the hood uses the dagservice's Get function which means it attempts to do an IPLD decoding of the block. This is not good because it means the gateway needs to understand the blocks codec in order to return it even when it doesn't really need to.

https://github.com/ipfs/boxo/blob/171b0b72118578e2f42f7e5aa45b52ad33a04231/gateway/backend_blocks.go#L213

https://github.com/ipfs/boxo/blob/171b0b72118578e2f42f7e5aa45b52ad33a04231/gateway/backend_blocks.go#L553

As an example: rainbow does not support dag-jose and so sending a `format=raw` request to a rainbow gateway will fail even though it could pass.

A fix here is to use the blockservice directly instead of the dagservice for the GetBlock function's retrieval purposes after doing any path traversals required to get to the final block.


Discussion / hints:
Do we still need dagservice to determine if a directory is being read?

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 848e49d77..c3f3e81aa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,7 @@ The following emojis are used to highlight certain changes:
 
 - Fix memory leaks due to not cleaning up wantlists [#829](https://github.com/ipfs/boxo/pull/829), [#833](https://github.com/ipfs/boxo/pull/833)
 - `ipns`: Improved interop with legacy clients by restoring support for `[]byte` CID in `Value` field. `Value()` will convert it to a valid `path.Path`. Empty `Value()` will produce `NoopPath` (`/ipfs/bafkqaaa`) to avoid breaking existing code that expects a valid record to always produce a valid content path. [#830](https://github.com/ipfs/boxo/pull/830)
+- `gateway/blocks-backend`: Removed IPLD decoding in GetBlock funciton in gateway's BlocksBackend. Now it's querying the blockService directly instead of dagService. [#845](https://github.com/ipfs/boxo/pull/845)
 
 
 ## [v0.27.3]
diff --git a/gateway/backend_blocks.go b/gateway/backend_blocks.go
index 20c4339bf..c2fde00fe 100644
--- a/gateway/backend_blocks.go
+++ b/gateway/backend_blocks.go
@@ -211,12 +211,25 @@ func (bb *BlocksBackend) GetAll(ctx context.Context, path path.ImmutablePath) (C
 }
 
 func (bb *BlocksBackend) GetBlock(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, files.File, error) {
-	md, nd, err := bb.getNode(ctx, path)
+	roots, lastSeg, remainder, err := bb.getPathRoots(ctx, path)
+	if err != nil {
+		return ContentPathMetadata{}, nil, err
+	}
+
+	md := ContentPathMetadata{
+		PathSegmentRoots:     roots,
+		LastSegment:          lastSeg,
+		LastSegmentRemainder: remainder,
+	}
+
+	lastRoot := lastSeg.RootCid()
+
+	b, err := bb.blockService.GetBlock(ctx, lastRoot)
 	if err != nil {
 		return md, nil, err
 	}
 
-	return md, files.NewBytesFile(nd.RawData()), nil
+	return md, files.NewBytesFile(b.RawData()), nil
 }
 
 func (bb *BlocksBackend) Head(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, *HeadResponse, error) {


Test patch: