Skip to content

feat(contracts): implement PayWord chain redemption in PaymentPool #1693

Closed as not planned

Description

@yigitdot

Implements the Solidity half of the PayWord model landed in ADR 003 by #1692 (issue #1670). ADR 003 currently leads the code — PaymentPool.sol still implements the pre-PayWord voucher.

Spec: ADR 003 § Hash-chain metering (PayWord), § Redemption and Close, § Chain walk and redemption cost.

Scope

New constants

uint256 constant CHUNK_BYTES      = 1_048_576;  // equals BYTES_PER_MB
uint256 constant MAX_CHAIN_LENGTH = 255;

Typehash (PaymentPool.sol:93) — two fields added:

keccak256("Voucher(bytes32 poolId,address signer,address provider,uint256 amount,uint256 bytesDelivered,bytes32 chainRoot,uint256 chunkPrice)")

redeem (:392) — new signature takes chainRoot, chunkPrice (signed) plus chainIndex, preimage (unsigned redeem params). May take the signed fields as a single Voucher calldata struct instead; ABI-equivalent for a fixed-size type.

_redeemVoucher (:659-682) — resolve the claim before the existing arithmetic:

  • require(chainIndex <= MAX_CHAIN_LENGTH) → ChainIndexTooLarge
  • walk chainIndex keccak rounds forward from preimage, require == chainRoot → BadPreimage
  • claimed = amount + chainIndex * chunkPrice, claimedBytes = bytesDelivered + chainIndex * CHUNK_BYTES
  • then desired = claimed - w.amount and bytesDelta against claimedBytes; everything downstream is unchanged

Rate floor (:655) — runs on claimed/claimedBytes, not the signed values. No standalone require(chunkPrice >= deliveryFloor): a below-floor chunk price passes only when the anchor over-pays enough to cover it, in which case the claim as a whole is above the floor, which is the only property the contract needs.

New errors: BadPreimage, ChainIndexTooLarge.

redeemMany (:444) — per-entry tuple gains the four fields. A bad preimage or an out-of-range index reverts the batch (caller error, like a bad signature), it is not skipped.

Implementation notes

  • Hash from scratch space in assembly. Round-tripping through abi.encodePacked allocates per iteration and costs roughly 2× for the same result. At ~60 gas/step the full 255-depth walk is ~16k gas; the naive form is ~2× that.
  • No new storage. The contract stores no chain state — Lane {amount, bytesDelivered} is unchanged, and claimed - paid is idempotent over whichever proof is presented. No migration.
  • No branch on chainRoot == 0. A sealed voucher (zero root, index 0, zero preimage) satisfies the ordinary walk with zeros. A real-root voucher at index 0 passes the root as its own preimage. Both reach "settle exactly amount" through the same code path.
  • Overflow is not a concern: chainIndex <= 255 and chunkPrice <= MAX_RATE_PER_MB (10^12) bound the product below 2^48.

Tests worth having

  • index 0 with zero root and zero preimage settles exactly amount
  • index 0 with a real root, root passed as its own preimage, settles exactly amount
  • no preimage exists that redeems a zero-root voucher above index 0
  • walk at 1, mid-chain, and exactly 255; chainIndex = 256 reverts ChainIndexTooLarge
  • re-presenting a lower index, or the same index twice, pays 0 and reverts NothingToRedeem
  • partial pay on a drained pool, then top-up and re-present the same voucher+preimage collects the remainder
  • rollover: redeem old chain, then the new voucher whose amount folds in 255 × chunk_price; total is unchanged either way, and redeeming the superseded chain after pays 0
  • rate floor evaluated on claimed/claimedBytes, incl. the fat-anchor/cheap-tail case that must pass
  • gas snapshot for the 255-depth walk

Blocked on #1692 merging.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions