Skip to content

feat(incentive): stream slash-sig, BindNodeId verify, wire-voucher bridge (#317) [2/3] - #732

Merged
thiras merged 4 commits into
mainfrom
feat/317-incentive-vouchers
May 31, 2026
Merged

thiras merged 4 commits into
mainfrom
feat/317-incentive-vouchers

Conversation

@thiras

@thiras thiras commented May 31, 2026

Copy link
Copy Markdown
Contributor

Stacked PR 2 of 3 for #317 — base feat/317-client-protocol-types (#731). Review/merge after [1/3]. Followed by [3/3] node handler+requester.

Base will retarget to main automatically once #731 merges.

What

The off-chain crypto/conversion layer the cdn/client/v1 handler and requester need, all mirroring the existing probe_sig/voucher patterns:

  • stream_sig.rs — StreamSlashData, the EIP-712 signed view of a StreamResponse ({hash, ok, ratePerMb, totalBytes, channelId, timestampUs, redirect}) over the shared SlashJudge domain (ADR 014 §1). Includes from_response_body (redirect: Option<NodeId> → bytes32(0)/bytes), an independent EIP-712 digest reconstruction test, type-hash, and per-field tamper guards.
  • bind_sig.rs — verify_binding for the ephemeral client BindNodeId(nodeId, nonce=0) binding (ADR 003 §Off-Chain Ephemeral Binding), EOA ecrecover against the CapacityBond domain. ERC-1271 (ADR 024) deferred.
  • client_bridge.rs — wire_voucher_to_signed / signed_to_wire_voucher (reconstructing channel_id/token/bytes_delivered from stream context) and voucher_reject_reason, an exhaustive ChannelError → VoucherRejectReason map (no wildcard, so a new variant fails to compile) with Store(_) → RetrySignal (transient, never a wire rejection — incentive: no replay-protection window enforced on incoming voucher nonces, allowing a client to resubmit a previously accepted voucher after a node restart and receive a second byte-delivery for the same payment #527).

Tests

Full mirror of the probe_sig suite for stream_sig, bind_sig round-trip/tamper, and client_bridge roundtrip + exhaustiveness. cargo nextest run -p decdn-incentive green; clippy/doc 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 off-chain cryptography/translation layer needed by the upcoming cdn/client/v1 handler/requester: EIP-712 signing/verification for stream slashing evidence, verification of ephemeral client NodeId↔EOA bindings, and a bridge between protocol “wire” vouchers and the incentive channel/voucher types.

Changes:

  • Introduce StreamSlashData (EIP-712 typed data) for StreamResponseBody slash_sig signing/recovery/verification with canonical digest and tamper-guard tests.
  • Add BindNodeId EIP-712 verification (verify_binding) for ephemeral client bindings under the CapacityBond domain.
  • Add client_bridge helpers to convert between wire vouchers and SignedVoucher, plus an exhaustive ChannelError → VoucherRejectReason mapping (with transient Store signaled as retry).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
crates/incentive/src/stream_sig.rs New EIP-712 struct + signing/recovery/verification for StreamResponse slash_sig, with strong digest/type-hash tests.
crates/incentive/src/bind_sig.rs New EIP-712 domain + verification helper for BindNodeId ephemeral bindings.
crates/incentive/src/client_bridge.rs New conversion layer between protocol wire vouchers and incentive SignedVoucher, plus rejection mapping/retry signal.
crates/incentive/src/lib.rs Exposes the new modules and re-exports their public API.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/incentive/src/bind_sig.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 three new modules to the incentive crate: bind_sig for ephemeral client binding verification, client_bridge to bridge wire vouchers with the state machine, and stream_sig for EIP-712 stream response signatures. The review feedback highlights two important improvements: importing Signature from alloy::primitives in bind_sig.rs for consistency, and changing voucher_reject_reason from a const fn to a regular pub fn in client_bridge.rs to avoid potential compilation issues when matching on types with non-trivial destructors like std::io::Error.

Comment thread crates/incentive/src/bind_sig.rs Resolved
Comment thread crates/incentive/src/client_bridge.rs Resolved
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
@thiras
thiras force-pushed the feat/317-incentive-vouchers branch from de4a504 to 5454a3a Compare May 31, 2026 16:46
thiras and others added 2 commits May 31, 2026 20:16
… 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]>
…idge (#317)

Adds the off-chain crypto/conversion layer the `cdn/client/v1` handler and
requester need (#317), all mirroring the existing `probe_sig`/`voucher`
patterns:

- `stream_sig.rs` — `StreamSlashData`, the EIP-712 signed view of a
  `StreamResponse` (`{hash, ok, ratePerMb, totalBytes, channelId, timestampUs,
  redirect}`) over the shared `SlashJudge` domain (ADR 014 §1). Includes a
  `from_response_body` helper (`redirect: Option<NodeId>` → `bytes32(0)`/bytes)
  and an independent EIP-712 digest reconstruction test, plus type-hash and
  per-field tamper guards.
- `bind_sig.rs` — `verify_binding` for the ephemeral client `BindNodeId(nodeId,
  nonce=0)` binding (ADR 003 §Off-Chain Ephemeral Binding), EOA `ecrecover`
  against the `CapacityBond` domain. ERC-1271 (ADR 024) deferred.
- `client_bridge.rs` — `wire_voucher_to_signed` / `signed_to_wire_voucher`
  (reconstructing `channel_id`/`token`/`bytes_delivered` from stream context,
  u256 as big-endian bytes) and `voucher_reject_reason`, an exhaustive
  `ChannelError` → `VoucherRejectReason` map (no wildcard, so a new variant
  fails to compile) with `Store(_)` → `RetrySignal` (transient, never a wire
  rejection — #527).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras force-pushed the feat/317-incentive-vouchers branch from 5454a3a to 0e243d3 Compare May 31, 2026 17:28
Base automatically changed from feat/317-client-protocol-types to main May 31, 2026 17:34
@github-actions

github-actions Bot commented May 31, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 89.83% (was 89.66%, ▲ +0.16%) — 23172/25796 lines

File Coverage Lines Δ
crates/incentive/src/bind_sig.rs 96.81% 91/94 new
crates/incentive/src/client_bridge.rs 99.34% 151/152 new
crates/incentive/src/stream_sig.rs 99.10% 221/223 new
History
Commit Total Δ vs base
0e243d3 89.83% ▲ +0.16%
3637aa3 (HEAD) 89.83% ▲ +0.16%

@alpergundogdu alpergundogdu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review — ADR consistency (PR 732, reviewed together with #733)

Reviewed the full stack (this branch still carries #731's two protocol commits since #731 isn't merged to main yet, so the diff vs main includes client.rs/message.rs — I read them as context but focused findings on the parts this PR owns plus the incentive layer).

Overall the crypto/conversion layer is faithful to the ADRs. Things I verified as consistent:

  • StreamResponse EIP-712 struct (stream_sig.rs) is byte-for-byte the type string in ADR 014 §EIP-712 Type Definitions (StreamResponse(bytes32 hash,bool ok,uint64 ratePerMb,uint64 totalBytes,bytes32 channelId,uint64 timestampUs,bytes32 redirect)), and the redirect: None → bytes32(0) mapping matches ADR 014 §1. The independent canonical-digest test is a nice drift guard.
  • BindNodeId(bytes32 nodeId,uint64 nonce) with nonce = 0 over the CapacityBond domain matches ADR 003 §Off-Chain Ephemeral Binding + §Binding Message Format; EOA-only with ERC-1271 deferred is exactly ADR 024 §Off-Chain ERC-1271 Verification.
  • VoucherRejectReason variant order and the frozen ClientMessage/StreamError discriminants match the enums in ADR 005 §VoucherRejected semantics and the freezing rules in ADR 013 §Protocol Enums.
  • Zero-rate (#252) + mandatory 65-byte slash_sig validation match ADR 014 §1 / ADR 005 §Rate bounds.

Finding 1 (moderate) — ADR 005 §Voucher wire format is missing bytesDelivered

client_bridge.rs (and the client.rs Voucher doc) reconstruct the full typed data as {channelId, amount, nonce, bytesDelivered, token} and cite ADR 005 §Voucher wire format. But ADR 005 actually says:

The EIP-712 signed data covers the full structure from ADR 003: {channelId, amount, nonce, token}. … the rest are derived from stream context — channel_id is in StreamRequest and token is fixed at channel open.

That field list (and the "derived = channel_id + token" prose) omits bytesDelivered. The canonical type in ADR 003 §324 / §598 (VOUCHER_TYPEHASH = "Voucher(bytes32 channelId,uint256 amount,uint256 nonce,uint256 bytesDelivered,address token)") includes it, and the implementation correctly follows ADR 003. So the code is right; ADR 005 §Voucher wire format is stale and should be corrected to list the 5-field structure and note bytesDelivered as a third stream-context-derived field. As written, the section the PR cites contradicts what the PR implements.

Finding 2 (minor) — voucher_reject_reason signature/mirror drift vs ADR 005 §Mirror obligation

ADR 005 §Mirror obligation specifies the conversion as fn voucher_reject_reason(&ChannelError) -> VoucherRejectReason and describes the mirror as "ChannelError ∪ VoucherError minus the Signature wrapper." The implementation is fn voucher_reject_reason(&ChannelError) -> Result<VoucherRejectReason, RetrySignal> and additionally excludes the transient ChannelError::Store variant (mapped to RetrySignal, not a reject reason). This is a reasonable design — and the no-wildcard exhaustive match obligation is honored — but the ADR's stated return type and the "minus the Signature wrapper" wording (which should also say "and the transient Store variant", returning a Result) are now out of date. Worth a one-line ADR 005 update so the spec matches the compile-enforced shape.

The Store → RetrySignal decision is the protocol-layer half of a larger divergence from ADR 003 §Off-chain voucher state persistence — see my detailed note on #733, where the handler resolves it by tearing down the stream rather than sending the in-band transient wire rejection ADR 003 §332 calls for. These two should be reconciled together (either a transient VoucherRejectReason lands here, or ADR 003 §332 is updated).

None of these block merge of the stack; they're ADR-vs-code reconciliations (Finding 1 is the one most worth fixing, since the cited section directly contradicts the implementation).


Generated by Claude Code

…with code (#317)

ADR-vs-code reconciliations from Alper's #732 review; the code is
canonical, the prose was stale.

- ADR 005 §Voucher wire format listed the typed data as the 4-field
  {channelId, amount, nonce, token}, omitting bytesDelivered. Corrected
  to the 5-field VOUCHER_TYPEHASH (ADR 003 §598) and noted bytesDelivered
  as a third stream-context-derived field (node's per-channel counter).
- ADR 003 §904 had the inverse error — it claimed bytesDelivered is
  transmitted on the wire. The protocol Voucher struct carries only
  {signature, amount, nonce}; corrected to mark bytesDelivered derived.
- ADR 005 §Mirror obligation: return type updated to
  Result<VoucherRejectReason, RetrySignal>, excluded the transient
  ChannelError::Store variant, and flagged the handler-side wire
  surfacing of transient store failures as an open reconciliation with
  ADR 003 §Off-chain voucher state persistence (deferred to #733).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit 11ca234 into main May 31, 2026
19 checks passed
@thiras
thiras deleted the feat/317-incentive-vouchers branch May 31, 2026 18:21
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