Repository navigation
feat(protocol): cdn/client/v1 paid-delivery wire types (#317) [1/3] - #731
Conversation
There was a problem hiding this comment.
Pull request overview
Adds the initial cdn/client/v1 paid-delivery protocol wire types to the protocol leaf crate and extends shared validation errors for rate/signature checks.
Changes:
- Introduces
ClientMessageand paid-delivery payloads (StreamRequest,StreamResponse,ChunkData,Voucher, errors/reject reasons). - Adds validation for zero-rate responses and voucher/signature lengths.
- Re-exports the new client protocol types from
decdn-protocol.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
crates/protocol/src/client.rs |
Adds cdn/client/v1 wire types, constants, validation helpers, and protocol tests. |
crates/protocol/src/lib.rs |
Exposes the new client module and public protocol types. |
crates/protocol/src/message.rs |
Adds shared validation errors and zero-rate validation for probe responses. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Code Review
This pull request introduces the cdn/client/v1 paid-delivery protocol types, including StreamRequest, StreamResponse, ChunkData, and Voucher, along with their serialization and validation logic. Feedback highlights compilation errors on stable Rust due to the use of const fn on validation methods that call Vec::len(). Additionally, the reviewer suggests adding validation methods for StreamRequest and ChunkData to enforce protocol bounds on voucher intervals, signature lengths, and chunk sizes, as well as adding corresponding error variants to MessageValidationError.
Coverage reportTotal: 89.66% (was 89.41%, ▲ +0.25%) — 22709/25327 lines
History
|
Introduce `crates/protocol/src/client.rs` with the `ClientMessage` enum and
all `cdn/client/v1` payload types per ADR 005 §`cdn/client/v1` and ADR 014 §1:
- `StreamRequest` (frozen base fields only) plus `StreamRequestExt` for
voucher-interval negotiation and the ephemeral client NodeId↔Ethereum
binding. The ext is carried as separate trailing bytes via the ADR 005
two-phase deserialization pattern (`encode_stream_request` /
`parse_stream_request_ext`) — keeping the base frozen so a future ext field
stays Tier-1 backward-compatible (old senders emit fewer trailing bytes; new
optional fields are appended and old receivers skip them).
- `StreamResponse` (signed `StreamResponseBody` + unsigned `error` /
`voucher_interval_mb` + 65-byte `slash_sig`)
- `ChunkData`, `Voucher` (wire `{signature, amount, nonce}` with u256 fields as
big-endian bytes — the leaf crate has no `U256`), `VoucherAck`, `StreamEnd`
- `StreamError` / `VoucherRejectReason` (frozen variant order, mirroring
`decdn_incentive::ChannelError` ∪ `VoucherError`)
`StreamResponse::validate()` and `ProbeResponse::validate()` both reject
`rate_per_mb == 0` on receive (#252) via a shared `MessageValidationError::
RateIsZero`; the rate-decode hook is shared (`pub(crate)`). Validation error
text is response-type-neutral so it reads correctly for both probe and stream
responses. Adds constants (`CHUNK_SIZE`, `MB_BYTES`, voucher-interval bounds,
`VOUCHER_SIG_LEN`) and a full test suite: per-variant discriminant pins,
roundtrips, fixed-byte wire-stability, two-phase ext (incl. forward-compat),
validation, and a full framing-stack roundtrip.
Leaf crate only — no behavior. Handler (node) and voucher logic (incentive)
follow in stacked PRs.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
ae7472c to
db01cbb
Compare
… seam Address PR review findings on the cdn/client/v1 wire types: several real invariants lived only in doc comments. Convert them into enforced checks on MessageValidationError-returning validate() methods. - Enforce voucher_interval_mb ∈ 1..=MAX_VOUCHER_INTERVAL_MB on both StreamResponse and the new StreamRequestExt::validate, closing the unbounded unvouchered-byte window (and interval * MB_BYTES u64 overflow). - Add StreamResponse ok/error consistency rules (ok=true ⇒ no error; ok=false ⇒ exactly one delivery-side error; never mid-stream-only VoucherRejected) via StreamErrorWithOk / MissingStreamError / VoucherRejectedInResponse. - Group StreamRequestExt.ethereum_address + binding_signature into Option<ClientBinding> so a half-populated binding is unrepresentable, add BINDING_SIG_LEN + ClientBinding::validate length check. - Add ClientMessage::validate dispatcher as the single decode-then-validate seam, and StreamError::is_delivery_side/is_mid_stream to express the error-domain split in code. - Fix docs: drop the non-existent decdn_incentive::StreamSlashData references (3 sites) for an analogous-to-ProbeSlashData forward note; relabel MAX_VOUCHER_INTERVAL_MB as the hardcoded ceiling, not governable. - Tests (+18): malformed ext remainder rejection, interval bounds, decode roundtrip in variant-order loops, empty ChunkData, non-zero byte_offset, ok/error consistency, ClientBinding, dispatcher, domain-split accessors. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Stacked PR 1 of 3 for #317 — base
main. Followed by [2/3] incentive sigs/bridge and [3/3] node handler+requester.What
Adds
crates/protocol/src/client.rswith theClientMessageenum and allcdn/client/v1payload types per ADR 005 §cdn/client/v1and ADR 014 §1:StreamRequest(+StreamRequestExtfor voucher-interval negotiation and the ephemeral NodeId↔Ethereum binding, carried as separate trailing bytes per the ADR 013 Tier-1 two-phase pattern)StreamResponse(signedStreamResponseBody+ unsignederror/voucher_interval_mb+ 65-byteslash_sig)ChunkData, wireVoucher({signature, amount, nonce}, u256 as big-endian bytes — the leaf crate has noU256),VoucherAck,StreamEndStreamError/VoucherRejectReason(frozen variant order, mirroringdecdn_incentive::ChannelError ∪ VoucherError)Folds in #252:
StreamResponse::validate()andProbeResponse::validate()both rejectrate_per_mb == 0on receive via a sharedMessageValidationError::RateIsZero.Scope
Leaf crate only — no behavior. The node handler and incentive voucher logic land in the stacked PRs.
Review fixes (commit 2)
Multi-agent review found several invariants that lived only in doc comments. Converted them into enforced checks on the
validate()seam:voucher_interval_mbbound enforced (1..=MAX_VOUCHER_INTERVAL_MB) on bothStreamResponse::validateand the newStreamRequestExt::validate— closes the unbounded unvouchered-byte window and theinterval * MB_BYTESu64 overflow (inverse of protocol: ProbeResponse.rate_per_mb has no enforced upper bound, allowing a malicious or misconfigured node to advertise an astronomically high rate that overflows arithmetic in the client selection formula (rate × rtt × 1/reputation²) #378 for the rate field).StreamResponseok/error consistency:ok=true⇒ noerror;ok=false⇒ exactly one delivery-sideerror; the mid-stream-onlyVoucherRejectedmay never ride in the response (StreamErrorWithOk/MissingStreamError/VoucherRejectedInResponse).ClientBindinggrouping: mergedStreamRequestExt.ethereum_address+binding_signatureintoOption<ClientBinding>so a half-populated binding is unrepresentable; addedBINDING_SIG_LEN+ClientBinding::validatelength check (was previously unvalidated).ClientMessage::validatedispatcher as the single decode-then-validate seam, plusStreamError::is_delivery_side/is_mid_streamto express the error-domain split in code.decdn_incentive::StreamSlashDatareferences (3 sites) for an analogous-to-ProbeSlashDataforward note; relabelledMAX_VOUCHER_INTERVAL_MBas the hardcoded ceiling (not governable).Tests
Per-variant discriminant pins, roundtrips (now decode-roundtrip in the variant-order loops), fixed-byte wire-stability, ext two-phase + malformed-remainder rejection, empty
ChunkData, non-zerobyte_offset, the new ok/error and interval validation cases,ClientBinding, the dispatcher, the domain-split accessors, and a full framing-stack roundtrip.cargo nextest run -p decdn-protocolgreen (113 tests); clippy/doc/fmt clean.🤖 Generated with Claude Code