Skip to content

feat(protocol): cdn/client/v1 paid-delivery wire types (#317) [1/3] - #731

Merged
alpergundogdu merged 2 commits into
mainfrom
feat/317-client-protocol-types
May 31, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
feat/317-client-protocol-types

Conversation

@thiras

@thiras thiras commented May 31, 2026 •

Copy link
Copy Markdown
Contributor

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.rs with the ClientMessage enum and all cdn/client/v1 payload types per ADR 005 §cdn/client/v1 and ADR 014 §1:

  • StreamRequest (+ StreamRequestExt for voucher-interval negotiation and the ephemeral NodeId↔Ethereum binding, carried as separate trailing bytes per the ADR 013 Tier-1 two-phase pattern)
  • StreamResponse (signed StreamResponseBody + unsigned error / voucher_interval_mb + 65-byte slash_sig)
  • ChunkData, wire Voucher ({signature, amount, nonce}, u256 as big-endian bytes — the leaf crate has no U256), VoucherAck, StreamEnd
  • StreamError / VoucherRejectReason (frozen variant order, mirroring decdn_incentive::ChannelError ∪ VoucherError)

Folds in #252: StreamResponse::validate() and ProbeResponse::validate() both reject rate_per_mb == 0 on receive via a shared MessageValidationError::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_mb bound enforced (1..=MAX_VOUCHER_INTERVAL_MB) on both StreamResponse::validate and the new StreamRequestExt::validate — closes the unbounded unvouchered-byte window and the interval * MB_BYTES u64 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).
  • StreamResponse ok/error consistency: ok=true ⇒ no error; ok=false ⇒ exactly one delivery-side error; the mid-stream-only VoucherRejected may never ride in the response (StreamErrorWithOk / MissingStreamError / VoucherRejectedInResponse).
  • ClientBinding grouping: merged StreamRequestExt.ethereum_address + binding_signature into Option<ClientBinding> so a half-populated binding is unrepresentable; added BINDING_SIG_LEN + ClientBinding::validate length check (was previously unvalidated).
  • ClientMessage::validate dispatcher as the single decode-then-validate seam, plus StreamError::is_delivery_side / is_mid_stream to express the error-domain split in code.
  • Docs: dropped the non-existent decdn_incentive::StreamSlashData references (3 sites) for an analogous-to-ProbeSlashData forward note; relabelled MAX_VOUCHER_INTERVAL_MB as the hardcoded ceiling (not governable).

Note: the Sig65 newtype suggestion was deferred — ProbeResponse.slash_sig: Vec<u8> is consumed across node/cli/incentive, so the newtype would ripple beyond this leaf-only PR. Its concrete safety gap (unvalidated binding signature) is closed by ClientBinding::validate.

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-zero byte_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-protocol green (113 tests); clippy/doc/fmt clean.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ClientMessage and 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.

Comment thread crates/protocol/src/client.rs Resolved
Comment thread crates/protocol/src/client.rs Resolved
Comment thread crates/protocol/src/client.rs Resolved

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/protocol/src/client.rs Resolved
Comment thread crates/protocol/src/client.rs Resolved
Comment thread crates/protocol/src/message.rs Resolved
Comment thread crates/protocol/src/client.rs Resolved
Comment thread crates/protocol/src/client.rs Resolved
Comment thread crates/protocol/src/message.rs Resolved
@github-actions

github-actions Bot commented May 31, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 89.66% (was 89.41%, ▲ +0.25%) — 22709/25327 lines

File Coverage Lines Δ
crates/protocol/src/client.rs 99.50% 601/604 new
crates/protocol/src/message.rs 100.00% 218/218 ● 0.00%
History
Commit Total Δ vs base
ae7472c 89.56% ▲ +0.14%
db01cbb 89.56% ▲ +0.15%
514019e (HEAD) 89.66% ▲ +0.25%

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]>
@thiras
thiras force-pushed the feat/317-client-protocol-types branch from ae7472c to db01cbb Compare May 31, 2026 16:46
… 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]>
@alpergundogdu
alpergundogdu merged commit 16e400f into main May 31, 2026
38 checks passed
@alpergundogdu
alpergundogdu deleted the feat/317-client-protocol-types branch May 31, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants