Skip to content

Commit e94a390

Browse files
committed
ipip-412: refactor on top of ipip-402
moving spec details to trustless-gateway, rebasing on top of ipip-402
1 parent 742d641 commit e94a390

File tree

3 files changed

+177
-119
lines changed

3 files changed

+177
-119
lines changed

src/http-gateways/path-gateway.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,7 @@ The following response types require an explicit opt-in, can only be requested w
595595
- Raw Block (`?format=raw`)
596596
- Opaque bytes, see [application/vnd.ipld.raw](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw).
597597
- CAR (`?format=car`)
598-
- A CAR file or a stream that contains all blocks required to trustlessly verify the requested content path query, see [application/vnd.ipld.car](https://www.iana.org/assignments/media-types/application/vnd.ipld.car) and :cite[trustless-gateway].
599-
- **Note:** by default, block order in CAR response is not deterministic,
600-
blocks can be returned in different order, depending on implementation
601-
choices (traversal, speed at which blocks arrive from the network, etc).
602-
An opt-in ordered CAR responses MAY be introduced in a future IPIP.
598+
- A CAR file or a stream that contains all blocks required to trustlessly verify the requested content path query, see [application/vnd.ipld.car](https://www.iana.org/assignments/media-types/application/vnd.ipld.car) and Section 5 (CAR Responses) at :cite[trustless-gateway].
603599
- TAR (`?format=tar`)
604600
- Deserialized UnixFS files and directories as a TAR file or a stream, see :cite[ipip-0288].
605601
- IPNS Record

src/http-gateways/trustless-gateway.md

+153-24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ editors:
1313
- name: Henrique Dias
1414
github: hacdias
1515
url: https://hacdias.com/
16+
xref:
17+
- url
18+
- path-gateway
19+
- ipip-0412
1620
tags: ['httpGateways', 'lowLevelHttpGateways']
1721
order: 1
1822
---
@@ -25,11 +29,12 @@ The minimal implementation means:
2529

2630
- response type is always fully verifiable: client can decide between a raw block or a CAR stream
2731
- no UnixFS/IPLD deserialization
28-
- for CAR files:
29-
- the behavior is identical to :cite[path-gateway]
3032
- for raw blocks:
3133
- data is requested by CID, only supported path is `/ipfs/{cid}`
3234
- no path traversal or recursive resolution
35+
- for CAR files:
36+
- the pathing behavior is identical to :cite[path-gateway]
37+
3338

3439
# HTTP API
3540

@@ -63,13 +68,14 @@ Same as in :cite[path-gateway], but with limited number of supported response ty
6368

6469
### `Accept` (request header)
6570

66-
This HTTP header is required when running in a strict, trustless mode.
71+
A Client SHOULD sent this HTTP header to leverage content type negotiation
72+
based on section 12.5.1 of :cite[rfc9110].
6773

6874
Below response types MUST to be supported:
6975
- [application/vnd.ipld.raw](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw) – requests a single, verifiable raw block to be returned
7076

7177
Below response types SHOULD to be supported:
72-
- [application/vnd.ipld.car](https://www.iana.org/assignments/media-types/application/vnd.ipld.car) – disables IPLD/IPFS deserialization, requests a verifiable CAR stream to be returned, implementations MAY support optional parameters (:cite[ipip-0412])
78+
- [application/vnd.ipld.car](https://www.iana.org/assignments/media-types/application/vnd.ipld.car) – disables IPLD/IPFS deserialization, requests a verifiable CAR stream to be returned, implementations MAY support optional CAR content type parameters (:cite[ipip-0412])
7379
- [application/vnd.ipfs.ipns-record](https://www.iana.org/assignments/media-types/application/vnd.ipfs.ipns-record) – requests a verifiable :cite[ipns-record] (multicodec `0x0300`).
7480

7581
Gateway SHOULD return HTTP 400 Bad Request when running in strict trustless
@@ -168,35 +174,37 @@ Below MUST be implemented **in addition** to "HTTP Response" of :cite[path-gatew
168174

169175
MUST be returned and include additional format-specific parameters when possible.
170176

171-
If a CAR stream was requested, the response MUST include the parameter specifying CAR version.
172-
For example: `Content-Type: application/vnd.ipld.car; version=1`
177+
If a CAR stream was requested:
178+
- the response MUST include the parameter specifying CAR version. For example:
179+
`Content-Type: application/vnd.ipld.car; version=1`
180+
- the response SHOULD include additional content type parameters, as noted in
181+
"CAR Response" section of this document.
173182

174183
### `Content-Disposition` (response header)
175184

176185
MUST be returned and set to `attachment` to ensure requested bytes are not rendered by a web browser.
177186

178-
## Response Payload
179-
180-
### Block Response
187+
# Block Responses (application/vnd.ipld.raw)
181188

182189
An opaque bytes matching the requested block CID
183190
([application/vnd.ipld.raw](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw)).
184191

185192
The Body hash MUST match the Multihash from the requested CID.
186193

187-
### CAR Response
194+
# CAR Responses (application/vnd.ipld.car)
188195

189196
A CAR stream for the requested
190197
[application/vnd.ipld.car](https://www.iana.org/assignments/media-types/application/vnd.ipld.car)
191-
content type, path and optional `dag-scope` and `entity-bytes` URL parameters.
198+
content type (with optional `order` and `dups` params), path and optional
199+
`dag-scope` and `entity-bytes` URL parameters.
192200

193-
#### CAR version
201+
## CAR version
194202

195203
Value returned in
196204
[`CarV1Header.version`](https://ipld.io/specs/transport/car/carv1/#header)
197205
field MUST match the `version` parameter returned in `Content-Type` header.
198206

199-
#### CAR roots
207+
## CAR roots
200208

201209
The behavior associated with the
202210
[`CarV1Header.roots`](https://ipld.io/specs/transport/car/carv1/#header) field
@@ -210,27 +218,148 @@ As of 2023-06-20, the behavior of the `roots` CAR field remains an [unresolved
210218

211219
:::
212220

213-
#### CAR determinism
221+
## CAR `order` (content type parameter)
222+
223+
The `order` parameter allows clients to specify the desired block order in the
224+
response. It supports the following values:
225+
226+
- `dfs`: [Depth-First Search](https://en.wikipedia.org/wiki/Depth-first_search)
227+
order, enables streaming responses with minimal memory usage.
228+
- `unk` (or missing): Unknown order, which serves as the implicit default when the `order`
229+
parameter is unspecified. In this case, the client cannot make any assumptions
230+
about the block order: blocks may arrive in a random order or be a result of
231+
a custom DAG traversal algorithm.
232+
233+
A Gateway SHOULD always return explicit `order` in CAR's `Content-Type` response header.
234+
235+
A Gateway MAY skip `order` in CAR response if no order was explicitly requested
236+
by the client and the default order is unknown.
237+
238+
A Client MUST assume implicit `order=unk` when `order` is missing, unknown, or empty.
239+
240+
## CAR `dups` (content type parameter)
241+
242+
The `dups` parameter specifies whether duplicate blocks (the same block
243+
occurring multiple times in the requested DAG) will be present in the CAR
244+
response. Useful when a deterministic block order is used.
245+
246+
It accepts two values:
247+
- `y`: Duplicate blocks MUST be sent every time they occur during the DAG walk.
248+
- `n`: Duplicate blocks MUST be sent only once.
249+
250+
When set to `y`, light clients are able to discard blocks after
251+
reading them, removing the need for caching in-memory or on-disk.
252+
253+
Setting to `n` allows for more efficient data transfer of certain types of
254+
data, but introduces additional resource cost on the receiving end, as each
255+
block needs to be kept around in case its CID appears again.
256+
257+
A Client MUST not assume any implicit behavior when `dups` is missing.
258+
259+
If the `dups` parameter is not present in the `Content-Type` header, the
260+
behavior is unspecified, and the CAR response includes an arbitrary list of
261+
blocks. In this unknown state, the client MUST assume duplicates are not sent,
262+
but also MUST ignore duplicates if they are present.
263+
264+
A Gateway MUST return always return `dups` in `Content-Type` response header
265+
when the duplicate status is known at the time of response.
266+
267+
A Gateway MAY skip `dups` if it was not present in `Accept` header sent by the
268+
client or if it is not possible to tell the duplicate status.
269+
270+
:::warning
271+
272+
The specified parameter does not apply to virtual blocks identified by identity
273+
CIDs. CAR responses MUST never include these virtual blocks. The parameter in
274+
question is meant to control the behavior of non-virtual blocks in the
275+
response. Therefore, it does not have any effect on virtual blocks, and they
276+
should never be included in the CAR response, no matter if present, or what
277+
value is set.
278+
279+
:::
280+
214281

215-
The default CAR header and block order in a CAR response is not specified and is non-deterministic.
282+
## CAR parameters and determinism
283+
284+
The default header and block order in a CAR format is not specified by IPLD specifications.
216285

217286
Clients MUST NOT assume that CAR responses are deterministic (byte-for-byte identical) across different gateways.
218287

219288
Clients MUST NOT assume that CAR includes CIDs and their blocks in the same order across different gateways.
220289

290+
Clients MUST assume block order and duplicate status only if `Content-Type` returned with CAR responses includes optional `order` or `dups` parameters, as specified by :cite[ipip-0412].
291+
292+
A Gateway SHOULD support some aspects of determinism by implementing content type negotiation and signaling via `Accept` and `Content-Type` headers.
293+
221294
:::issue
222295

223-
In controlled environments, clients MAY choose to rely on undocumented CAR determinism,
224-
subject to the agreement of the following conditions between the client and the
225-
gateway:
296+
In controlled environments, clients MAY choose to rely on implicit and
297+
undocumented CAR determinism, subject to the agreement of the following
298+
conditions between the client and the gateway:
226299
- CAR version
227300
- content of [`CarV1Header.roots`](https://ipld.io/specs/transport/car/carv1/#header) field
228-
- order of blocks
229-
- status of duplicate blocks
301+
- order of blocks (`order` from :cite[ipip-0412])
302+
- status of duplicate blocks (`dups` from :cite[ipip-0412])
230303

231-
In the future, there may be an introduction of a convention to indicate aspects
232-
of determinism in CAR responses. Please refer to
233-
[IPIP-412](https://github.com/ipfs/specs/pull/412) for potential developments
234-
in this area.
304+
Mind this is undocumented behavior, and MUST NOT be used on public networks.
235305

236306
:::
307+
308+
### CAR format signaling in Request
309+
310+
Content type negotiation is based on section 12.5.1 of :cite[rfc9110].
311+
312+
Clients MAY indicate their preferred block order by sending an `Accept` header in
313+
the HTTP request. The `Accept` header format is as follows:
314+
315+
```
316+
Accept: application/vnd.ipld.car; version=1; order=dfs; dups=y
317+
```
318+
319+
In the future, when more orders or parameters exist, clients will be able to
320+
specify a list of preferences, for example:
321+
322+
```
323+
Accept: application/vnd.ipld.car;order=foo, application/vnd.ipld.car;order=dfs;dups=y;q=0.5
324+
```
325+
326+
The above example is a list of preferences, the client would really like to use
327+
the hypothetical `order=foo` however if this isn't available it would accept
328+
`order=dfs` with `dups=y` instead (lower priority indicated via `q` parameter,
329+
as noted in :cite[rfc9110]).
330+
331+
### CAR format signaling in Response
332+
333+
The Trustless Gateway MUST always respond with a `Content-Type` header that includes
334+
information about all supported and known parameters, even if the client did not
335+
specify them in the request.
336+
337+
The `Content-Type` header format is as follows:
338+
339+
```
340+
Content-Type: application/vnd.ipld.car;version=1;order=dfs;dups=n
341+
```
342+
343+
Gateway implementations SHOULD decide on the implicit default ordering or
344+
other parameters, and use it in responses when client did not explicitly
345+
specify any matching preference.
346+
347+
A Gateway MAY choose to implement only some of the parameters and return HTTP
348+
400 Bad Request or 406 Not Acceptable when client requested a response with
349+
unsupported content type variant.
350+
351+
A Client MUST verify `Content-Type` returned with CAR response before
352+
processing the payload, as the legacy gateway may not support optional content
353+
type parameters like `order` an `dups` and return plain
354+
`application/vnd.ipld.car`.
355+
356+
357+
# IPNS Record Responses (application/vnd.ipfs.ipns-record)
358+
359+
An opaque bytes matching the [Signed IPNS Record](https://specs.ipfs.tech/ipns/ipns-record/#ipns-record)
360+
for the requested [IPNS Name](https://specs.ipfs.tech/ipns/ipns-record/#ipns-name)
361+
returned as [application/vnd.ipfs.ipns-record](https://www.iana.org/assignments/media-types/application/vnd.ipfs.ipns-record).
362+
363+
A Client MUST confirm the record signature match `libp2p-key` from the requested IPNS Name.
364+
365+
A Client MUST [perform additional record verification according to the IPNS specification](https://specs.ipfs.tech/ipns/ipns-record/#record-verification).

0 commit comments

Comments
 (0)