Repository navigation
feat(incentive): add EIP-712 voucher signing, channel state, and rate enforcement (#319) - #407
Conversation
…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]>
There was a problem hiding this comment.
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.
…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]>
|
/gemini review |
There was a problem hiding this comment.
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.
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.
Closes #319.
Summary
Fills in the
decdn-incentivecrate with the off-chain voucher primitivescdn/client/v1will 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 theStablePaymentChanneldomain. The Solidity struct is generated byalloy::sol!so the digest matches the on-chain contract byte-for-byte. Sign with anySignerSync; 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-channelChannelStatethat enforces the same monotonic invariants as the on-chaincloseChannel/disputeChannelreverts: matchingchannel_idandtoken, strictly increasingnonce, non-decreasingamountandbytes_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 inU256to avoid floats, applies a default 1 % tolerance to absorb the client-sideceil(bytes / 1 MB) × raterounding, and clamps the tolerance at 100 % so caller bugs can't underflow.What's deliberately left out
The issue stays inside
crates/incentive. So:PrivateKeySigner::random()so the sign / verify path is exercised end-to-end without touching disk.cdn/client/v1wire format. The ALPN doesn't exist yet; vouchers travel as in-memorySignedVoucherfor now. Wire serialization is a small follow-up once that ALPN lands.openChannel/closeChannel/disputeChannel/settleChannel. Stays in Implement on-chain CapacityBond and PaymentChannel contract integration #327.Notes / decisions taken
"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.USDCis 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.sol!macro uses the Rust struct name as the on-chain Solidity type-string, so the generated struct is namedVoucherinside a private module to avoid the name clash with the public RustVouchertype. 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— cleancargo test -p decdn-incentive --lib— 35 tests pass (12 voucher, 12 channel, 11 rate)cargo clippy -p decdn-incentive --all-targets— no warningscargo clippy --workspace --all-targets— no warnings introduced elsewherecargo fmt --all -- --check— cleancargo test --workspace --lib— 355 passed, 0 failed across all cratesChannelErrorvariant has at least one rejection test, plus a "rejected voucher must not advance state" composite test🤖 Generated with Claude Code