Skip to content

feat(payments): implement PayWord hash-chained per-MB meter (protocol + incentive + contracts) #1752

Closed

Description

@yigitdot

Implements the design landed in ADR 003 by #1751 (closes #1670).

One issue, not three. The earlier attempt split this across #1693 / #1694 / #1695 and all three were closed. The layers cannot land independently anyway: a voucher that gains chain_root breaks the sol! mirror, the InterfaceFreeze typehash pin, and the wire encoding in the same change. #1693–#1695 are superseded by this issue.

Pre-launch, so this is one straight wire/ABI cut — no compatibility shims, no dual-format readers, no version negotiation. Every side changes in the same PR.

Design (canon)

Key constants: CHUNK_BYTES = 1_048_576 (== BYTES_PER_MB), MAX_CHAIN_LENGTH: u8 = 255, chunk_price ≡ rate_per_mb.

crates/protocol

  • ChunkPreimage { preimage: [u8; 32], index: u8 } — fixed 33-byte encoding, no length prefix, no varint. index == 0 on the wire is a protocol error.
  • Insert it into ClientMessage at discriminant 4 (beside Voucher), renumbering StreamEnd to 5.
  • Voucher wire struct gains chain_root and chunk_price.
  • CHUNK_BYTES / MAX_CHAIN_LENGTH constants; retire VOUCHER_INTERVAL_BYTES.
  • Four new VoucherRejectReason variants: BadPreimage, ChainIndexTooLarge, UnanchoredPreimage, ChainRootReuse — handler-emitted, so exempt from the PoolError mirror obligation.

crates/incentive

  • chain_root / chunk_price in the EIP-712 Voucher and its sol! mirror — the two must stay byte-identical to the contract typehash.
  • Per-lane seed derivation. A signer MUST derive an independent seed and root per (pool_id, signer, provider) lane; reuse across lanes is the cross-lane preimage spend footgun and costs the payer real money.
  • LaneState gains chain_root and the deepest verified index; deepest-preimage-wins verification (ignore index ≤ verified with no hashing, reject index > MAX_CHAIN_LENGTH before hashing).
  • Root-reuse rejection: refuse a chain_root seen on another lane or retired on this one.
  • PoolStateStore record widens to (last_amount, last_bytes_delivered, chain_root, highest_index). Keep design: best-effort async voucher-watermark persistence (chain-floored) — drop fsync-before-ack #1672's frontier/redeemed durability split exactly as-is — chain state is frontier, and losing it forfeits the node's own un-signed tail, which is the safe direction.

crates/node + crates/client-pull

  • Buyer: one chain-advancing task per lane, serializing the index and the rollover decision under a local lock while streams run concurrently.
  • Buyer: send the epoch's chain_root voucher once at stream open and once per rollover, before that stream's first preimage of the epoch. Resends are idempotent (cumulative-max).
  • Seller: credit-window floor becomes one chunk; pacing resumes on a ChunkPreimage or a Voucher. Do not disturb feat: credit-slow-start — fixed-ramp credit window (start slow, ramp quick) #1669's ramp.
  • Redeem planner: prefer the latest signed voucher (rollover or close, walk-free); attach a preimage only for an abandoned sub-rollover tail.

contracts

  • VOUCHER_TYPEHASH gains bytes32 chainRoot,uint256 chunkPrice.
  • LaneVoucher gains chainRoot, chunkPrice, chainIndex, preimage — keep the struct static (160 → 288 calldata bytes).
  • Bounded chain walk in redeemMany: require chainIndex <= MAX_CHAIN_LENGTH, hash forward from preimage, require equality with chainRoot. Never hash from a stored intermediate; store no chain state.
  • claimed = cumulative + chainIndex × chunkPrice over claimedBytes = bytesDelivered + chainIndex × CHUNK_BYTES; desired = claimed − w.amount; rate floor applies to the extended pair.
  • New errors BadPreimage / ChainIndexTooLarge — caller errors that revert the whole call, alongside a bad signature. Zero-paying vouchers stay skipped, not reverted.
  • Update the InterfaceFreeze.t.sol typehash pin.

Tests worth naming

  • Sealed voucher: chainRoot = 0 at index 0 with a zero preimage settles exactly amount; any index > 0 against a zero root reverts.
  • Real-root voucher redeemed at index 0 (root passed as its own preimage) settles exactly amount.
  • Full-depth walk at index 255; index 256 reverts ChainIndexTooLarge.
  • Rollover: redeeming old-then-new and new-only collect the same total; a superseded chain pays 0 and is skipped.
  • Concurrency: two streams on one lane with interleaved, out-of-order, and skipped indexes converge on the same lane total; a preimage before its epoch voucher on that stream is rejected UnanchoredPreimage.
  • Partial trailing chunk settles via a closing amount-voucher, exact to 1 USDC base unit.

🤖 Generated with Claude Code

Activity

  1. self-assigned this
    on Aug 20, 2026
  2. added
    kind:feature
    rust
    area:contracts
    prio:high
    on Aug 20, 2026
  3. moved this from Backlog to In progress in deCDNon Aug 20, 2026
  4. moved this from In progress to Done in deCDNon Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:contractskind:featureprio:highrust

Type

No type

Fields

Priority

High

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions