Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
315d8e0
feat(fwss): wire FilecoinPayV2 beside v1 in the payment facets
CodeWarriorr Nov 17, 2026
f4d0eec
chore(deps): ignore the filecoin-pay-v2 shadowing warning
CodeWarriorr Nov 17, 2026
cdc5ab7
feat(fwss): create new data sets' rails on FilecoinPayV2
CodeWarriorr Nov 18, 2026
993ee75
feat(fwss): open FilecoinPayV2 rails only for data sets that opt in
CodeWarriorr Nov 19, 2026
75aacff
docs(spec): say FilecoinPayV2 data sets stop at creation for now
CodeWarriorr Nov 19, 2026
da4d3ad
feat(fwss): select FilecoinPayV2 through the storage terms
CodeWarriorr Nov 20, 2026
9348f93
feat(fwss): let FilecoinPayV2 reject a short balance at creation
CodeWarriorr Nov 20, 2026
be305a8
Merge branch 'feat/filecoin-pay-v2' into feat/fwss-pay-v2-dataset-cre…
CodeWarriorr Nov 20, 2026
6857bcc
style(fwss): format with forge 1.7.1, as CI does
CodeWarriorr Nov 20, 2026
c973f60
docs(tools): decode getStorageTerms with payVersion in the upgrade ch…
CodeWarriorr Nov 20, 2026
039efb1
test(fwss): pin a FilecoinPayV1 terms id to its released value
CodeWarriorr Nov 20, 2026
7a95c54
refactor(fwss): hash every storage terms id with payVersion
CodeWarriorr Nov 23, 2026
a34a326
refactor(fwss): share the PDP rail nonce and the reserve refill math
CodeWarriorr Nov 23, 2026
9ff7383
feat(fwss): refill the reserve on FilecoinPayV2 when scheduling removals
CodeWarriorr Nov 23, 2026
6c90ac1
refactor(fwss): move FilecoinPayV2 rail creation into LibRailsV2
CodeWarriorr Nov 26, 2026
0e1ee4c
docs(storage): drop the payVersion note the slot table already shows
CodeWarriorr Nov 26, 2026
14aef14
Merge remote-tracking branch 'origin/feat/fwss-pay-v2-dataset-created…
CodeWarriorr Nov 26, 2026
ceb0bfc
Merge remote-tracking branch 'origin/feat/filecoin-pay-v2' into feat/…
CodeWarriorr Nov 26, 2026
7b81bd1
refactor(fwss): move the PDP rail nonce and lock into LibRailsV2
CodeWarriorr Nov 26, 2026
a6e8668
feat(fwss): pay one-time fees as they arise on FilecoinPayV2
CodeWarriorr Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Storage terms gain a `payVersion` field: `0` is FilecoinPayV1, `1` is FilecoinPayV2.
A data set created with FilecoinPayV2 terms opens its rails on FilecoinPayV2 in one script, with FWSS as the operator.
The payer deposits the selected currency into FilecoinPayV2 and authorizes FWSS with `CREATE_RAIL`.
FWSS locks the lifecycle reserve and the CDN lockups on the new rails, and FilecoinPayV2 reverts the script when the balance is short.
In the same script FWSS locks the terminate fee and the CDN lockups on the new rails and pays the creation fee to the payee, and FilecoinPayV2 reverts the script when the balance is short.
The stored rail IDs are FilecoinPayV2 rail hashes.
Record zero and terms registered before the field existed read `0`, so their data sets stay on FilecoinPayV1 as before.
The other data set callbacks still act on FilecoinPayV1 rails, so FilecoinPayV2 data sets do not yet work end to end.
Expand All @@ -21,6 +21,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- **Breaking ABI:** `StorageTerms` appends `uint8 payVersion`, which changes the selectors of `registerStorageTerms` and `getStorageTermsId`, the return data of `getStorageTerms`, and the `StorageTermsRegistered` event.
Terms IDs now hash under `filecoin.FWSS.StorageTerms.v2` with `payVersion` appended, so `getStorageTermsId` no longer returns the 1.6.0 ID of a definition.
Records registered under 1.6.0 keep their IDs; record zero is unaffected.
- FilecoinPayV2 data sets keep no lifecycle reserve to refill: FWSS pays each one-time fee to the payee when it arises.
Their PDP rail keeps only the scaled terminate fee locked, which `lifecycleReserveBalance` reports, and `pendingOneTimePayments` stays zero.
Scheduling piece removals pays its fee with a lock and a disburse on the PDP rail, so payment the rail already holds stays.
A payer balance short of a fee reverts with FilecoinPayV2 `InsufficientBalance`.

## [1.6.0] - FWSS Storage Terms and Piece Metadata Cleanup

Expand Down
10 changes: 6 additions & 4 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ The terminate fee applies only in the **consent case**: when the SP calls `termi
#### Lifecycle Reserve

The lifecycle reserve is seeded at **0.50 units of the selected currency**, scaled to its decimals, when the dataset is created.
Datasets on FilecoinPayV1 hold it as `lockupFixed` on their PDP rail; datasets on FilecoinPayV2 hold it as a lock on their PDP rail.
Datasets on FilecoinPayV1 hold it as `lockupFixed` on their PDP rail.
Datasets on FilecoinPayV2 have no reserve to refill: FWSS pays each one-time fee from the client's FilecoinPayV2 balance when it arises, so their PDP rail keeps only the scaled terminate fee locked.
Dollar amounts in the fee table describe the intended stablecoin economics, not an exchange-rate conversion.

```
Expand Down Expand Up @@ -163,7 +164,8 @@ Clients pay for storage by depositing the currency selected by their dataset's t
The selected terms' `payVersion` decides where the rails open.
With FilecoinPayV1 terms, including record zero, they open on FilecoinPayV1, where the client grants FWSS operator approval for that currency.
A client opts a new dataset into FilecoinPayV2 by signing the ID of terms registered with `payVersion` 1.
On FilecoinPayV2 the client authorizes FWSS with `CREATE_RAIL`, and FWSS opens the rails and locks their reserves in one `executeScript` call.
On FilecoinPayV2 the client authorizes FWSS with `CREATE_RAIL`, and in one `executeScript` call FWSS opens the rails, locks the terminate fee and the CDN lockups, and pays the creation fee to the payee.
Scheduling piece removals on FilecoinPayV2 pays its fee to the payee at once, with a lock and a disburse on the PDP rail.
That grant has no rate or lockup allowance like the FilecoinPayV1 operator approval, so only FWSS's own logic bounds what it locks and charges on those rails.
The rest of this specification after creation still describes FilecoinPayV1 rails; FilecoinPayV2 data sets do not yet support piece additions, settlement, termination or the CDN flows.
Funds flow to providers over time based on the storage rate.
Expand All @@ -176,7 +178,7 @@ lockupRequired = finalRate × EPOCHS_PER_MONTH

An empty dataset has zero streaming lockup and retains its fixed lifecycle reserve.
On FilecoinPayV1, creation also requires approval and available-funds headroom for the scaled monthly dataset fee, anticipating later piece additions.
On FilecoinPayV2 the balance only has to cover the locks; FilecoinPayV2 reverts the script when it does not.
On FilecoinPayV2 the balance only has to cover the locks and the creation fee; FilecoinPayV2 reverts the script when it does not.
For datasets with data, streaming lockup covers the truncated per-epoch rate for the lockup period.

**Pre-flight check precision:** Creation checks use the scaled monthly dataset fee directly, rather than reconstructing it from a truncated per-epoch rate. This is conservative, particularly for lower-decimal currencies.
Expand Down Expand Up @@ -465,7 +467,7 @@ sequenceDiagram
FWSS->>ServiceProviderRegistry: getProviderPayee(providerId)
ServiceProviderRegistry-->>FWSS: payee address
alt FilecoinPayV2 storage terms
FWSS->>FilecoinPayV2: executeScript(CREATE_RAIL, createRail + lock per rail)
FWSS->>FilecoinPayV2: executeScript(CREATE_RAIL, createRail + lock per rail, disburse creation fee)
FilecoinPayV2-->>FWSS: rails created
else FilecoinPayV1 storage terms
FWSS->>FilecoinPayV1: createRail(client, payee, ...)
Expand Down
52 changes: 44 additions & 8 deletions service_contracts/src/lib/LibRailsV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
pragma solidity 0.8.37;

import {FilecoinPayV2} from "@filecoin-pay-v2/FilecoinPayV2.sol";
import {CREATE_RAIL} from "@filecoin-pay-v2/lib/Permissions.sol";
import {CREATE_RAIL, NONE} from "@filecoin-pay-v2/lib/Permissions.sol";
import {FLAG_CALLBACK, RailId, RailIdLibrary, Stream} from "@filecoin-pay-v2/lib/RailId.sol";
import {ScriptBuilder, newScript} from "@filecoin-pay-v2/lib/Script.sol";
import {CDNPaymentRailsToppedUp} from "./LibRails.sol";
import {StoragePricing} from "./StoragePricing.sol";
import {
CREATE_DATA_SET_FEE,
DEFAULT_CACHE_MISS_LOCKUP_AMOUNT,
DEFAULT_CDN_LOCKUP_AMOUNT,
LIFECYCLE_RESERVE_TARGET
TERMINATE_FEE
} from "./PriceListUSDFC.sol";

/// @title LibRailsV2
Expand All @@ -23,7 +24,8 @@ library LibRailsV2 {

error DataSetIdTooLarge(uint256 dataSetId);

/// @notice Opens the data set's rails on FilecoinPayV2 in one script, with this contract as their operator.
/// @notice Opens the data set's rails on FilecoinPayV2 in one script, with this contract as their operator,
/// and pays the creation fee to the payee.
/// @param payments The FilecoinPayV2 contract instance
/// @param dataSetId The data set the rail nonces derive from
/// @param token The currency selected by the data set's storage terms; address(0) is native FIL
Expand All @@ -46,11 +48,13 @@ library LibRailsV2 {
require(dataSetId < CDN_RAIL_NONCE_FLAG, DataSetIdTooLarge(dataSetId));
bool hasCDN = filBeamBeneficiaryAddress != address(0);
uint64 cacheMissNonce = uint64(dataSetId);
uint64 pdpNonce = FLAG_CALLBACK | cacheMissNonce;
uint64 pdpNonce = pdpRailNonce(dataSetId);
uint64 cdnNonce = CDN_RAIL_NONCE_FLAG | cacheMissNonce;

// The lifecycle reserve is a lock on the PDP rail. A payer balance short of the locks reverts in executeScript.
uint256 lifecycleReserve = StoragePricing.scaleAmount(LIFECYCLE_RESERVE_TARGET, tokenDecimals);
// The PDP rail keeps only the termination fee locked, the one fee that may come due when the payer has nothing
// left. The creation fee passes through the rail to the payee. A short payer balance reverts in executeScript.
uint256 createFee = StoragePricing.scaleAmount(CREATE_DATA_SET_FEE, tokenDecimals);
uint256 terminateFee = StoragePricing.scaleAmount(TERMINATE_FEE, tokenDecimals);
uint256 cacheMissLockup;
uint256 cdnLockup;
if (hasCDN) {
Expand All @@ -59,8 +63,8 @@ library LibRailsV2 {
}

// ScriptBuilder grows at the free-memory pointer, so nothing may allocate between newScript() and build().
ScriptBuilder script =
newScript().setToken(token).createRail(payer, payee, pdpNonce).lock(uint128(lifecycleReserve));
ScriptBuilder script = newScript().setToken(token).createRail(payer, payee, pdpNonce)
.lock(uint128(terminateFee + createFee)).disburse(uint128(createFee));
if (hasCDN) {
script = script.createRail(payer, payee, cacheMissNonce).lock(uint128(cacheMissLockup))
.createRail(payer, filBeamBeneficiaryAddress, cdnNonce).lock(uint128(cdnLockup));
Expand All @@ -75,6 +79,38 @@ library LibRailsV2 {
}
}

/// @notice The nonce of a data set's PDP rail, which settles through the FWSS callback
/// @param dataSetId The data set the rail pays for
/// @return The rail nonce with FLAG_CALLBACK set
function pdpRailNonce(uint256 dataSetId) internal pure returns (uint64) {
return FLAG_CALLBACK | uint64(dataSetId);
}

/// @notice Pays a one-time fee from the payer's FilecoinPayV2 balance to the payee through the data set's PDP rail
/// @dev Lock, disburse and setRail need no grant from the payer, so the script runs with no permissions. The lock
/// and the disburse cancel out, so the rail's available balance, and the payment it holds, stay as they were.
/// @param payments The FilecoinPayV2 contract instance
/// @param dataSetId The data set the PDP rail pays for
/// @param token The rail's currency; address(0) is native FIL
/// @param payer The rail's payer
/// @param payee The rail's recipient
/// @param fee The amount to pay
function chargeOnPdpRail(
FilecoinPayV2 payments,
uint256 dataSetId,
address token,
address payer,
address payee,
uint256 fee
) internal {
uint64 nonce = pdpRailNonce(dataSetId);
// ScriptBuilder grows at the free-memory pointer, so nothing may allocate between newScript() and build().
payments.executeScript(
NONE,
newScript().setToken(token).setRail(payer, payee, nonce).lock(uint128(fee)).disburse(uint128(fee)).build()
);
}

/// @notice Derives the id of a rail this contract operates
/// @param payer The rail's payer
/// @param recipient The rail's recipient
Expand Down
29 changes: 22 additions & 7 deletions service_contracts/src/modules/FWSSAuthorizationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {IPDPVerifier} from "@pdp/interfaces/IPDPVerifier.sol";
import {Cids} from "@pdp/Cids.sol";
import {SessionKeyRegistry} from "@session-key-registry/SessionKeyRegistry.sol";
import {FilecoinPayV1} from "@fws-payments/FilecoinPayV1.sol";
import {FilecoinPayV2} from "@filecoin-pay-v2/FilecoinPayV2.sol";
import {Errors} from "../Errors.sol";
import {FWSSStorage} from "../storage/FWSSStorage.sol";
import {
Expand All @@ -22,6 +23,7 @@ import {
TERMINATE_FEE
} from "../lib/PriceListUSDFC.sol";
import {LibRails} from "../lib/LibRails.sol";
import {LibRailsV2} from "../lib/LibRailsV2.sol";
import {LibStorageTerms} from "../lib/LibStorageTerms.sol";
import {StoragePricing} from "../lib/StoragePricing.sol";
import {FWSSEIP712} from "../lib/FWSSEIP712.sol";
Expand All @@ -36,6 +38,7 @@ import {LibSignatureVerification} from "../lib/LibSignatureVerification.sol";
/// the lifecycle reserve, and attaching the data set authorizer.
contract FWSSAuthorizationModule is FWSSStorage, FWSSEIP712, FWSSPDPVerifier, FWSSFilecoinPayV2 {
using LibRails for FilecoinPayV1;
using LibRailsV2 for FilecoinPayV2;

FilecoinPayV1 internal immutable paymentContract;
SessionKeyRegistry public immutable sessionKeyRegistry;
Expand Down Expand Up @@ -220,13 +223,25 @@ contract FWSSAuthorizationModule is FWSSStorage, FWSSEIP712, FWSSPDPVerifier, FW
// Verify the signature
verifySchedulePieceRemovalsSignature(dataSetId, payer, info.clientDataSetId, pieceIds, signature);

uint96 newPending = _addPendingFee(
info.pendingOneTimePayments, StoragePricing.scaleAmount(SCHEDULE_PIECE_REMOVALS_FEE, _tokenDecimals(info))
);
info.lifecycleReserveBalance = paymentContract.replenishReserveIfNeeded(
info.pdpRailId, info.pdpEndEpoch, info.lifecycleReserveBalance, newPending, _tokenDecimals(info)
);
info.pendingOneTimePayments = newPending;
if (_isV1Rail(info.pdpRailId)) {
uint96 newPending = _addPendingFee(
info.pendingOneTimePayments,
StoragePricing.scaleAmount(SCHEDULE_PIECE_REMOVALS_FEE, _tokenDecimals(info))
);
info.lifecycleReserveBalance = paymentContract.replenishReserveIfNeeded(
info.pdpRailId, info.pdpEndEpoch, info.lifecycleReserveBalance, newPending, _tokenDecimals(info)
);
info.pendingOneTimePayments = newPending;
} else {
// FilecoinPayV2 lets FWSS take the fee from the payer's balance now, so it never waits in the reserve.
filecoinPayV2.chargeOnPdpRail(
dataSetId,
LibStorageTerms.getRecord(info.storageTermsId).token,
payer,
info.payee,
StoragePricing.scaleAmount(SCHEDULE_PIECE_REMOVALS_FEE, _tokenDecimals(info))
);
}

// piecesAdded may flush the fee before PDPVerifier drops these pieces, so pending alone cannot signal the
// smaller leaf count to nextProvingPeriod. An empty batch removes nothing.
Expand Down
18 changes: 16 additions & 2 deletions service_contracts/src/modules/FWSSDataSetModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
LIFECYCLE_RESERVE_TARGET,
SERVICE_COMMISSION_BPS,
STORAGE_PRICE_PER_TIB_PER_MONTH,
TERMINATE_FEE,
TOKEN_DECIMALS
} from "../lib/PriceListUSDFC.sol";
import {FilecoinPayV2} from "@filecoin-pay-v2/FilecoinPayV2.sol";
Expand Down Expand Up @@ -257,8 +258,7 @@ contract FWSSDataSetModule is EIP712Upgradeable, FWSSStorage, FWSSPDPVerifier, F

railToDataSet[pdpRailId] = dataSetId;
info.pdpRailId = pdpRailId;
info.lifecycleReserveBalance = uint96(StoragePricing.scaleAmount(LIFECYCLE_RESERVE_TARGET, terms.tokenDecimals));
info.pendingOneTimePayments = uint96(StoragePricing.scaleAmount(CREATE_DATA_SET_FEE, terms.tokenDecimals));
_recordCreationFees(info, terms.payVersion == PAY_VERSION_FILECOIN_PAY_V2, terms.tokenDecimals);
if (hasCDN) {
info.cacheMissRailId = cacheMissRailId;
info.cdnRailId = cdnRailId;
Expand All @@ -279,6 +279,20 @@ contract FWSSDataSetModule is EIP712Upgradeable, FWSSStorage, FWSSPDPVerifier, F
emit DataSetStorageTermsSelected(dataSetId, storageTermsId);
}

/// @notice Records a new data set's lifecycle reserve and the creation fee it still owes
/// @param info The new data set
/// @param onFilecoinPayV2 Whether the data set's rails live on FilecoinPayV2
/// @param tokenDecimals The selected currency's precision
function _recordCreationFees(DataSetInfo storage info, bool onFilecoinPayV2, uint8 tokenDecimals) private {
if (onFilecoinPayV2) {
// createRails already paid the creation fee; the reserve stays at the locked termination fee.
info.lifecycleReserveBalance = uint96(StoragePricing.scaleAmount(TERMINATE_FEE, tokenDecimals));
return;
}
info.lifecycleReserveBalance = uint96(StoragePricing.scaleAmount(LIFECYCLE_RESERVE_TARGET, tokenDecimals));
info.pendingOneTimePayments = uint96(StoragePricing.scaleAmount(CREATE_DATA_SET_FEE, tokenDecimals));
}

/**
* @notice Handles data set deletion after voluntary termination or via the abandonment path.
* @dev Called by the PDPVerifier contract when a data set is deleted.
Expand Down
Loading
Loading