Skip to content

feat(incentive): add EIP-712 voucher signing, channel state, and rate enforcement (#319) - #407

Merged
thiras merged 3 commits into
mainfrom
claude/sleepy-snyder-f24cfd
May 6, 2026
Merged

thiras merged 3 commits into
mainfrom
claude/sleepy-snyder-f24cfd

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #319.

Summary

Fills in the decdn-incentive crate with the off-chain voucher primitives cdn/client/v1 will need for paid delivery, per ADR 003. Three small modules, ~1.1k lines total, signer-agnostic — the crate stays a leaf on the dependency graph and ships without a keystore prerequisite.

  • voucher — EIP-712 typed voucher (channelId, amount, nonce, bytesDelivered, token) under the StablePaymentChannel domain. The Solidity struct is generated by alloy::sol! so the digest matches the on-chain contract byte-for-byte. Sign with any SignerSync; verify by recovering and comparing to the channel's client address. The voucher type hash and the EIP-712 domain separator are locked to the canonical ADR 003 wording with regression tests so any drift in alloy's encoding (or in the ADR) trips a test before it costs gas.
  • channel — per-channel ChannelState that enforces the same monotonic invariants as the on-chain closeChannel / disputeChannel reverts: matching channel_id and token, strictly increasing nonce, non-decreasing amount and bytes_delivered, amount <= deposit, and a recoverable signature from the channel's client. Rejected vouchers leave state untouched. Signature check is intentionally last — it's the most expensive (ecrecover).
  • rate — verify_rate(amount_delta, bytes_delta, rate_per_mb, tolerance_bps) for the per-voucher rate check from the issue. Cross-multiplies in U256 to avoid floats, applies a default 1 % tolerance to absorb the client-side ceil(bytes / 1 MB) × rate rounding, and clamps the tolerance at 100 % so caller bugs can't underflow.

What's deliberately left out

The issue stays inside crates/incentive. So:

Notes / decisions taken

  • Domain name follows ADR 003 ("StablePaymentChannel"), not the issue body's "deCDN Payment Channel". The contract dictates this — any other name produces an unrecoverable digest. The same goes for the voucher field set: ADR 003 specifies five fields (channelId, amount, nonce, bytesDelivered, token) plus signature; the issue body's three-field summary was condensed.
  • USDC is in 6-decimal base units (µUSDC) end-to-end. No decimal conversion in the signing path — the voucher carries raw base units and the contract never re-derives them.
  • The sol! macro uses the Rust struct name as the on-chain Solidity type-string, so the generated struct is named Voucher inside a private module to avoid the name clash with the public Rust Voucher type. There's a regression test that locks the type-string to the ADR's exact wording in case this ever drifts.

Test plan

  • cargo build -p decdn-incentive — clean
  • cargo test -p decdn-incentive --lib — 35 tests pass (12 voucher, 12 channel, 11 rate)
  • cargo clippy -p decdn-incentive --all-targets — no warnings
  • cargo clippy --workspace --all-targets — no warnings introduced elsewhere
  • cargo fmt --all -- --check — clean
  • cargo test --workspace --lib — 355 passed, 0 failed across all crates
  • Voucher type hash test asserts the canonical EIP-712 type-string from ADR 003 §EIP-712 Voucher Signature
  • Domain separator test asserts the canonical EIP-712 separator
  • Fixed-vector regression: deterministic key + voucher recovers a known address (catches digest drift before the on-chain contract does)
  • Each ChannelError variant has at least one rejection test, plus a "rejected voucher must not advance state" composite test

🤖 Generated with Claude Code

…ment (#319)

Implements the off-chain voucher primitives the `cdn/client/v1` ALPN will
need for paid blob delivery, per ADR 003.

- `voucher`: EIP-712 typed voucher (`channelId`, `amount`, `nonce`,
  `bytesDelivered`, `token`) under the `StablePaymentChannel` domain.
  Sign with any `SignerSync` impl; verify by recovering and matching the
  channel's client address. Type hash and domain separator are locked
  to the canonical ADR 003 wording with regression tests.
- `channel`: per-channel `ChannelState` enforcing the same monotonic
  invariants as the on-chain `closeChannel` / `disputeChannel` reverts —
  signature, channel id, token, increasing nonce, non-decreasing amount
  and bytes, amount within deposit. Rejected vouchers leave state
  unchanged.
- `rate`: `verify_rate(amount_delta, bytes_delta, rate_per_mb,
  tolerance_bps)` for the per-voucher rate check, with a default 1%
  tolerance to absorb the client-side `ceil(bytes/1MB) * rate` rounding.

Signer-agnostic by design — the crate takes any `SignerSync`, so the
keystore→signer wiring (issue #406) and on-chain settlement (issue
#327) are independent follow-ups.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

@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 core off-chain payment voucher logic for the incentive crate, including EIP-712 voucher signing and verification, per-channel state tracking, and rate enforcement. The implementation ensures that voucher updates are monotonic and adhere to on-chain invariants. Feedback was provided to improve the test coverage for channel state updates by explicitly verifying that the state remains unchanged across all possible validation failure paths.

Comment thread crates/incentive/src/channel.rs Resolved
…on test

The previous version of `rejected_voucher_does_not_advance_state` only
exercised three rejection paths (amount drop, stale nonce, wrong token).
Extended to all seven variants of `ChannelError` so each one is asserted
to leave state unchanged.

Switched the per-case structure to a `[(SignedVoucher, &str); 7]` table
+ single iteration loop — borrows `state` mutably exactly once per case,
which a closure version couldn't (borrow conflict on subsequent
`state.channel_id` reads).

Addresses gemini-code-assist review on #407.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@alpergundogdu alpergundogdu changed the title feat(incentive): EIP-712 voucher signing + channel state + rate enforcement (#319) feat(incentive): add EIP-712 voucher signing, channel state, and rate enforcement (#319) May 5, 2026
@alpergundogdu

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 core primitives for off-chain payment channels in the incentive crate. It implements ChannelState for tracking voucher progress and enforcing monotonicity, verify_rate for validating that payments meet the required rate per megabyte, and EIP-712 voucher signing and verification logic. Feedback was provided regarding an unused proptest dependency added to the crate's development dependencies.

Comment thread crates/incentive/Cargo.toml Resolved
No proptest macros or imports anywhere in the crate; the dependency was
added speculatively but never used. Tests still pass (35 passed).

Addresses gemini-code-assist review on PR #407.
@thiras
thiras merged commit 0028a25 into main May 6, 2026
11 checks passed
@thiras
thiras deleted the claude/sleepy-snyder-f24cfd branch May 6, 2026 08:38
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.

Implement EIP-712 payment voucher creation and verification

2 participants