Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions service_contracts/abi/FilecoinWarmStorageService.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,31 @@
],
"anonymous": false
},
{
"type": "event",
"name": "LifecycleReserveToppedUp",
"inputs": [
{
"name": "dataSetId",
"type": "uint256",
"indexed": true,
"internalType": "uint256"
},
{
"name": "amountAdded",
"type": "uint256",
"indexed": false,
"internalType": "uint256"
},
{
"name": "totalLifecycleReserve",
"type": "uint256",
"indexed": false,
"internalType": "uint256"
}
],
"anonymous": false
},
{
"type": "event",
"name": "PieceAdded",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ interface IFilecoinWarmStorageService is IFilecoinServiceMetadata, IPDPProvingSc
event FaultRecord(uint256 indexed dataSetId, uint256 periodsFaulted, uint256 deadline);
event FilBeamControllerChanged(address oldController, address newController);
event Initialized(uint64 version);
event LifecycleReserveToppedUp(uint256 indexed dataSetId, uint256 amountAdded, uint256 totalLifecycleReserve);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
event PDPPaymentTerminated(uint256 indexed dataSetId, uint256 endEpoch, uint256 pdpRailId);
event PieceAdded(
Expand Down
25 changes: 25 additions & 0 deletions service_contracts/src/lib/FWSSFilecoinPayV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
pragma solidity 0.8.37;

import {FilecoinPayV2} from "@filecoin-pay-v2/FilecoinPayV2.sol";
import {NONE} from "@filecoin-pay-v2/lib/Permissions.sol";
import {FLAG_CALLBACK} from "@filecoin-pay-v2/lib/RailId.sol";
import {newScript} from "@filecoin-pay-v2/lib/Script.sol";
import {Errors} from "../Errors.sol";

/// @title FWSSFilecoinPayV2
Expand All @@ -23,4 +26,26 @@ abstract contract FWSSFilecoinPayV2 {
function _isV1Rail(uint256 railId) internal pure returns (bool) {
return railId < 1 << 64;
}

/// @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 Moves part of the payer's FilecoinPayV2 balance into a data set's PDP rail
/// @dev Lock and setRail need no grant from the payer, so the script runs with no permissions.
/// @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 amount The amount to add to the rail's available balance
function _lockOnPdpRail(uint256 dataSetId, address token, address payer, address payee, uint256 amount) internal {
uint64 nonce = _pdpRailNonce(dataSetId);
// ScriptBuilder grows at the free-memory pointer, so nothing may allocate between newScript() and build().
filecoinPayV2.executeScript(
NONE, newScript().setToken(token).setRail(payer, payee, nonce).lock(uint128(amount)).build()
);
}
}
21 changes: 18 additions & 3 deletions service_contracts/src/modules/FWSSAuthorizationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "../lib/PriceListUSDFC.sol";
import {LibRails} from "../lib/LibRails.sol";
import {LibStorageTerms} from "../lib/LibStorageTerms.sol";
import {PAY_VERSION_FILECOIN_PAY_V2, StorageTermsRecord} from "../lib/StorageTerms.sol";
import {StoragePricing} from "../lib/StoragePricing.sol";
import {FWSSEIP712} from "../lib/FWSSEIP712.sol";
import {FWSSPDPVerifier} from "../lib/FWSSPDPVerifier.sol";
Expand Down Expand Up @@ -66,6 +67,12 @@ contract FWSSAuthorizationModule is FWSSStorage, FWSSEIP712, FWSSPDPVerifier, FW

event DataSetAuthorizerSet(uint256 indexed dataSetId, address indexed authorizer);

/// @notice Emitted when the payer tops up the lifecycle reserve, on FilecoinPayV1 and FilecoinPayV2 alike.
/// @dev FilecoinPayV2 logs nothing for a lock, so this is the only log of a FilecoinPayV2 top-up.
/// @param amountAdded The increase applied: the low 96 bits of the requested amount
/// @param totalLifecycleReserve lifecycleReserveBalance after the top-up
event LifecycleReserveToppedUp(uint256 indexed dataSetId, uint256 amountAdded, uint256 totalLifecycleReserve);

/// @notice Emitted when a service is terminated.
/// @param approver The address that authorized termination: the payer, one of the payer's
/// session keys (SessionKeyRegistry), or the service provider. Cross-reference with
Expand Down Expand Up @@ -296,6 +303,8 @@ contract FWSSAuthorizationModule is FWSSStorage, FWSSEIP712, FWSSPDPVerifier, FW
* @notice Pre-funds the lifecycle reserve beyond the automatic target
* @dev Useful before scheduling many piece removals or before terminating.
* Cannot be called after termination; FilecoinPay forbids raising lockupFixed on a terminated rail.
* On FilecoinPayV1 it raises the PDP rail's lockupFixed; on FilecoinPayV2 it locks `amount` more of the
* payer's FilecoinPayV2 balance on the PDP rail. Neither deposits new funds.
* @param dataSetId The ID of the data set
* @param amount Additional amount to add to the lifecycle reserve
*/
Expand All @@ -306,10 +315,16 @@ contract FWSSAuthorizationModule is FWSSStorage, FWSSEIP712, FWSSPDPVerifier, FW
require(msg.sender == payer, Errors.CallerNotPayer(dataSetId, payer, msg.sender));
require(info.pdpEndEpoch == 0, Errors.DataSetPaymentAlreadyTerminated(dataSetId));

uint256 pdpRailId = info.pdpRailId;
uint96 newBalance = info.lifecycleReserveBalance + uint96(amount);
paymentContract.modifyRailLockup(pdpRailId, DEFAULT_LOCKUP_PERIOD, newBalance);
StorageTermsRecord storage terms = LibStorageTerms.getRecord(info.storageTermsId);
uint96 increase = uint96(amount);
uint96 newBalance = info.lifecycleReserveBalance + increase;
if (terms.payVersion == PAY_VERSION_FILECOIN_PAY_V2) {
_lockOnPdpRail(dataSetId, terms.token, payer, info.payee, increase);
} else {
paymentContract.modifyRailLockup(info.pdpRailId, DEFAULT_LOCKUP_PERIOD, newBalance);
}
info.lifecycleReserveBalance = newBalance;
emit LifecycleReserveToppedUp(dataSetId, increase, newBalance);
}

function requirePaymentNotTerminated(uint256 dataSetId) internal view {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,8 @@ contract FWSSAuthorizationModuleTest is FilecoinWarmStorageServiceFixture {
uint256 amount = (uint256(1) << 97) | topUp;
uint96 expectedBalance = info.lifecycleReserveBalance + uint96(topUp);

vm.expectEmit(address(pdpServiceWithPayments));
emit IFilecoinWarmStorageService.LifecycleReserveToppedUp(dataSetId, topUp, expectedBalance);
vm.prank(client);
pdpServiceWithPayments.topUpLifecycleReserve(dataSetId, amount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {PDPListener} from "@pdp/PDPVerifier.sol";
import {FilecoinPayV2} from "@filecoin-pay-v2/FilecoinPayV2.sol";
import {IExtsload} from "@filecoin-pay-v2/interfaces/IExtsload.sol";
import {CREATE_RAIL, DEPOSIT} from "@filecoin-pay-v2/lib/Permissions.sol";
import {CREATE_RAIL, DEPOSIT, NONE} from "@filecoin-pay-v2/lib/Permissions.sol";
import {FLAG_CALLBACK, RailId, RailIdLibrary, Stream, StreamState} from "@filecoin-pay-v2/lib/RailId.sol";
import {ServiceProviderRegistryStorage} from "../../src/ServiceProviderRegistryStorage.sol";
import {DataSetInfoView} from "../../src/lib/FWSSViewTypes.sol";
Expand All @@ -21,6 +21,7 @@ import {
STORAGE_PRICE_PER_TIB_PER_MONTH
} from "../../src/lib/PriceListUSDFC.sol";
import {PAY_VERSION_FILECOIN_PAY_V2, StorageTerms} from "../../src/lib/StorageTerms.sol";
import {FWSSAuthorizationModule} from "../../src/modules/FWSSAuthorizationModule.sol";
import {FWSSDataSetModule} from "../../src/modules/FWSSDataSetModule.sol";
import {FWSSStorageTermsModule} from "../../src/modules/FWSSStorageTermsModule.sol";
import {FilecoinWarmStorageServiceFixture} from "../helpers/FilecoinWarmStorageServiceFixture.sol";
Expand Down Expand Up @@ -415,4 +416,77 @@ contract FWSSDataSetModuleFilecoinPayV2Test is FilecoinWarmStorageServiceFixture
uint256 dataSetId = _create(serviceProvider, _payload(0, false));
assertFalse(harness.isV1Rail(pdpServiceWithPayments.getDataSet(dataSetId).pdpRailId));
}

function testTopUpLocksMoreOnFilecoinPayV2PdpRail() public {
_fundV2Payer(payer, 10e18);
uint256 dataSetId = _create(serviceProvider, _payload(0, false));
uint256 pdpRailId = pdpServiceWithPayments.getDataSet(dataSetId).pdpRailId;
uint256 balanceBefore = _usdfcBalance();

// LOCK takes no permission, so FWSS runs the script with an empty scope
vm.expectCall(
address(filecoinPayV2),
abi.encodePacked(bytes4(keccak256("executeScript(uint256,bytes)")), abi.encode(NONE))
);
vm.expectEmit(address(pdpServiceWithPayments));
emit FWSSAuthorizationModule.LifecycleReserveToppedUp(dataSetId, 1e18, LIFECYCLE_RESERVE_TARGET + 1e18);
vm.prank(payer);
pdpServiceWithPayments.topUpLifecycleReserve(dataSetId, 1e18);

assertEq(pdpServiceWithPayments.getDataSet(dataSetId).lifecycleReserveBalance, LIFECYCLE_RESERVE_TARGET + 1e18);
assertEq(_rail(pdpRailId).available, LIFECYCLE_RESERVE_TARGET + 1e18, "lock mirrors reserve");
assertEq(_usdfcBalance(), balanceBefore - 1e18, "locked from the payer's balance");
}

function testTopUpRevertsOnShortFilecoinPayV2Balance() public {
_fundV2Payer(payer, MIN_FUNDS);
uint256 dataSetId = _create(serviceProvider, _payload(0, false));
uint256 pdpRailId = pdpServiceWithPayments.getDataSet(dataSetId).pdpRailId;
_fundV2Payer(payer, 1e18 - 1);

vm.prank(payer);
vm.expectRevert(FilecoinPayV2.InsufficientBalance.selector);
pdpServiceWithPayments.topUpLifecycleReserve(dataSetId, 1e18);

assertEq(pdpServiceWithPayments.getDataSet(dataSetId).lifecycleReserveBalance, LIFECYCLE_RESERVE_TARGET);
assertEq(_rail(pdpRailId).available, LIFECYCLE_RESERVE_TARGET);

vm.prank(payer);
pdpServiceWithPayments.topUpLifecycleReserve(dataSetId, 1e18 - 1);
assertEq(_rail(pdpRailId).available, LIFECYCLE_RESERVE_TARGET + 1e18 - 1);
assertEq(_usdfcBalance(), 0);
}

function testTopUpOnFilecoinPayV2LocksTheTruncatedAmount() public {
_fundV2Payer(payer, 10e18);
uint256 dataSetId = _create(serviceProvider, _payload(0, false));

// lock takes a uint128, the reserve a uint96: bits above 96 must not reach the lock
vm.expectEmit(address(pdpServiceWithPayments));
emit FWSSAuthorizationModule.LifecycleReserveToppedUp(dataSetId, 1e18, LIFECYCLE_RESERVE_TARGET + 1e18);
vm.prank(payer);
pdpServiceWithPayments.topUpLifecycleReserve(dataSetId, (uint256(1) << 97) | 1e18);

assertEq(pdpServiceWithPayments.getDataSet(dataSetId).lifecycleReserveBalance, LIFECYCLE_RESERVE_TARGET + 1e18);
assertEq(
_rail(pdpServiceWithPayments.getDataSet(dataSetId).pdpRailId).available, LIFECYCLE_RESERVE_TARGET + 1e18
);
}

function testTopUpOnNativeFilFilecoinPayV2Rail() public {
bytes32 termsId = _registerV2Terms(address(0), 18, uint96(STORAGE_PRICE_PER_TIB_PER_MONTH));
vm.startPrank(payer);
filecoinPayV2.deposit{value: 1 ether}();
filecoinPayV2.authorize(address(pdpServiceWithPayments), CREATE_RAIL);
vm.stopPrank();
uint256 dataSetId = _create(serviceProvider, _termsPayload(0, termsId, false));

vm.prank(payer);
pdpServiceWithPayments.topUpLifecycleReserve(dataSetId, 0.1 ether);

uint256 pdpRailId = pdpServiceWithPayments.getDataSet(dataSetId).pdpRailId;
assertEq(_rail(pdpRailId).available, LIFECYCLE_RESERVE_TARGET + 0.1 ether);
assertEq(filecoinPayV2.balanceOf(payer, 0), 1 ether - LIFECYCLE_RESERVE_TARGET - 0.1 ether);
assertEq(_usdfcBalance(), 0);
}
}
Loading