Skip to content
Draft
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
34 changes: 29 additions & 5 deletions service_contracts/src/lib/LibRails.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.37;

import {Errors} from "../Errors.sol";
import {Errors as PayErrors} from "@fws-payments/Errors.sol";
import {FilecoinPayV1} from "@fws-payments/FilecoinPayV1.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {StoragePricing} from "./StoragePricing.sol";
Expand Down Expand Up @@ -307,9 +308,12 @@ library LibRails {
/// @dev Does not terminate rails. Leaves CDN rails unchanged. Caller must already have
/// established that the data set is live (`pdpEndEpoch == 0`) and that `newPayee` differs
/// from the snapshotted payee. Reverts if the payer cannot settle lockup through the
/// current epoch (Pay's rate-change rule).
/// current epoch (Pay's rate-change rule). A cache-miss rail that Pay already reports as
/// inactive or settled is dropped so the live PDP rail can still move. A cache-miss rail
/// that is terminated but not yet settled still reverts.
/// @return newPdpRailId Replacement PDP rail paying `newPayee`
/// @return newCacheMissRailId Replacement cache-miss rail, or 0 when the data set has none
/// or that rail is already inactive or settled
function retargetRails(
FilecoinPayV1 payments,
uint256 dataSetId,
Expand All @@ -326,17 +330,37 @@ library LibRails {

newCacheMissRailId = 0;
if (oldCacheMissRailId != 0) {
FilecoinPayV1.RailView memory oldCacheMiss = payments.getRail(oldCacheMissRailId);
require(oldCacheMiss.endEpoch == 0, Errors.CacheMissPaymentAlreadyTerminated(dataSetId));
newCacheMissRailId =
_retargetRail(payments, oldCacheMissRailId, oldCacheMiss, newPayee, oldCacheMiss.lockupFixed);
try payments.getRail(oldCacheMissRailId) returns (FilecoinPayV1.RailView memory oldCacheMiss) {
require(oldCacheMiss.endEpoch == 0, Errors.CacheMissPaymentAlreadyTerminated(dataSetId));
newCacheMissRailId =
_retargetRail(payments, oldCacheMissRailId, oldCacheMiss, newPayee, oldCacheMiss.lockupFixed);
} catch (bytes memory reason) {
if (!_isRailInactiveOrSettled(reason)) {
assembly ("memory-safe") {
revert(add(reason, 32), mload(reason))
}
}
}
}

emit DataSetPayeeRetargeted(
dataSetId, oldPayee, newPayee, oldPdpRailId, newPdpRailId, oldCacheMissRailId, newCacheMissRailId
);
}

/// @notice Whether Pay reverted because `railId` is already inactive or fully settled.
/// @dev reason is the revert data from getRail. Other reverts are not this error.
function _isRailInactiveOrSettled(bytes memory reason) private pure returns (bool) {
if (reason.length < 4) {
return false;
}
bytes4 selector;
assembly ("memory-safe") {
selector := mload(add(reason, 32))
}
return selector == PayErrors.RailInactiveOrSettled.selector;
}

/// @notice Open a replacement for one live rail that pays `newPayee`.
/// @dev Does not terminate the old rail. Zeroes its fixed lockup and any payment rate, then
/// opens a replacement with the same period, validator, and commission. `lockupFixed` is
Expand Down
3 changes: 2 additions & 1 deletion service_contracts/src/modules/FWSSDataSetModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ contract FWSSDataSetModule is EIP712Upgradeable, FWSSStorage, FWSSPDPVerifier {
* current registry worker, so operators can rotate to a new worker first and pay retarget gas
* from that wallet (old worker spends nothing). Recipient is always registry payee. Does not
* terminate rails. Leaves the CDN rail unchanged. Reverts when the payer cannot cover lockup
* through the current epoch.
* through the current epoch. A cache-miss rail that is already inactive or settled does not
* block the PDP rail; its id is cleared.
* @param dataSetId The data set whose rails to retarget
*/
function retargetDataSetPayee(uint256 dataSetId) external {
Expand Down
Loading