Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 11 additions & 0 deletions service_contracts/src/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,15 @@ library Errors {
/// @param requiredEpoch The first epoch at which abandonment is allowed
/// @param currentBlock The current block number
error DataSetNotAbandoned(uint256 dataSetId, uint256 requiredEpoch, uint256 currentBlock);

/// @notice The data set payee already matches the registry payee
/// @param dataSetId The data set ID
/// @param payee The current payee
error DataSetPayeeAlreadyCurrent(uint256 dataSetId, address payee);

/// @notice Only the registry worker may call this function
/// @param dataSetId The data set ID
/// @param expectedWorker The current registry worker
/// @param caller The caller address
error CallerNotProviderWorker(uint256 dataSetId, address expectedWorker, address caller);
}
164 changes: 153 additions & 11 deletions service_contracts/src/ServiceProviderRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ contract ServiceProviderRegistry is
}

/// @notice Version of the contract implementation
string public constant VERSION = "1.2.0";
string public constant VERSION = "1.3.0";

/// @notice EIP-712 typehash for removing a provider (payee-authorized voluntary exit)
bytes32 public constant REMOVE_PROVIDER_TYPEHASH = keccak256("RemoveProvider(uint256 providerId,uint256 nonce)");

/// @notice Maximum length for service URL
uint256 private constant MAX_SERVICE_URL_LENGTH = 256;
Expand Down Expand Up @@ -102,6 +105,14 @@ contract ServiceProviderRegistry is
/// @notice Emitted when a provider is removed
event ProviderRemoved(uint256 indexed providerId);

/// @notice Emitted when the payee (beneficiary) of a provider is changed
event PayeeChanged(uint256 indexed providerId, address indexed oldPayee, address indexed newPayee);

/// @notice Emitted when the worker (serviceProvider) of a provider is changed
event ServiceProviderChanged(
uint256 indexed providerId, address indexed oldServiceProvider, address indexed newServiceProvider
);

/// @notice Emitted when the contract is upgraded
event ContractUpgraded(string version, address implementation);

Expand Down Expand Up @@ -166,7 +177,7 @@ contract ServiceProviderRegistry is
}

/// @notice Register as a new service provider with a specific product type
/// @param payee Address that will receive payments (cannot be changed after registration)
/// @param payee Address that will receive payments (may later differ from the worker)
/// @param name Provider name (optional, max 128 chars)
/// @param description Provider description (max 256 chars)
/// @param productType The type of product to register
Expand All @@ -187,8 +198,13 @@ contract ServiceProviderRegistry is
// Validate payee address
require(payee != address(0), "Payee cannot be zero address");

// Check if address is already registered
require(addressToProviderId[msg.sender] == 0, "Address already registered");
// Reject addresses still mapped to an active provider (live or former worker). Soft-deleted
// providers leave stale mappings; clear those so the address can register again.
uint256 existingProviderId = addressToProviderId[msg.sender];
if (existingProviderId != 0) {
require(!providers[existingProviderId].isActive, "Address already registered");
delete addressToProviderId[msg.sender];
}

// Check payment amount is exactly the registration fee
require(msg.value == REGISTRATION_FEE, "Incorrect fee amount");
Expand Down Expand Up @@ -406,21 +422,86 @@ contract ServiceProviderRegistry is
emit ProviderInfoUpdated(providerId);
}

/// @notice Change the payee (beneficiary) for a provider
/// @dev Registry owner only (e.g. FilOzone Safe). Rare recovery path with human vetting —
/// neither the worker nor the current payee can rotate the beneficiary on-chain.
/// @param providerId The provider to update
/// @param newPayee The new beneficiary address
function changePayee(uint256 providerId, address newPayee)
external
onlyOwner
providerExists(providerId)
providerActive(providerId)
{
require(newPayee != address(0), "Payee cannot be zero address");
address oldPayee = providers[providerId].payee;
require(newPayee != oldPayee, "Payee unchanged");

providers[providerId].payee = newPayee;
emit PayeeChanged(providerId, oldPayee, newPayee);
}

/// @notice Change the worker (serviceProvider) for a provider
/// @dev Common wallet-rotation path: current payee calls directly (e.g. Glif/MetaMask).
/// Registry owner may also call for recovery. The worker cannot rotate itself.
/// Former workers stay in `addressToProviderId` so `currentWorker(old)` still resolves.
/// @param providerId The provider to update
/// @param newServiceProvider The new worker address
function changeServiceProvider(uint256 providerId, address newServiceProvider)
external
providerExists(providerId)
providerActive(providerId)
{
address payee = providers[providerId].payee;
require(msg.sender == payee || msg.sender == owner(), "Only payee or owner can change worker");

require(newServiceProvider != address(0), "Service provider cannot be zero address");
uint256 existingProviderId = addressToProviderId[newServiceProvider];
if (existingProviderId != 0 && existingProviderId != providerId) {
require(!providers[existingProviderId].isActive, "Address already registered");
}

address oldServiceProvider = providers[providerId].serviceProvider;
require(newServiceProvider != oldServiceProvider, "Service provider unchanged");

// Keep addressToProviderId[oldServiceProvider] so PDPVerifier can resolve via currentWorker.
providers[providerId].serviceProvider = newServiceProvider;
addressToProviderId[newServiceProvider] = providerId;

emit ServiceProviderChanged(providerId, oldServiceProvider, newServiceProvider);
}

/// @notice Remove provider registration (soft delete)
/// @dev Payee must call directly. A stolen worker cannot remove the registration after the
/// payee has moved.
/// @param providerId The provider to remove
/// @param signature Empty for a plain payee call, or payee EIP-712 over RemoveProvider
function removeProvider(uint256 providerId, bytes calldata signature)
external
providerExists(providerId)
providerActive(providerId)
{
bytes32 structHash =
keccak256(abi.encode(REMOVE_PROVIDER_TYPEHASH, providerId, providerActionNonce[providerId]));
_requirePayeeAuthorization(providerId, structHash, signature);
_removeProvider(providerId);
}

/// @notice Remove the caller's provider registration (soft delete)
/// @dev Kept for compatibility when the caller is still both worker and payee.
function removeProvider() external {
uint256 providerId = addressToProviderId[msg.sender];
require(providerId != 0, "Provider not registered");

require(providers[providerId].payee == msg.sender, "Only payee can remove provider");
unchecked {
providerActionNonce[providerId]++;
}
_removeProvider(providerId);
}

/// @notice Internal function to remove provider
function _removeProvider(uint256 providerId)
private
providerExists(providerId)
providerActive(providerId)
onlyServiceProvider(providerId)
{
/// @dev Caller must already have established payee authorization and that the provider is active.
function _removeProvider(uint256 providerId) private {
// Soft delete - mark as inactive
providers[providerId].isActive = false;

Expand Down Expand Up @@ -450,6 +531,67 @@ contract ServiceProviderRegistry is
emit ProviderRemoved(providerId);
}

/// @notice Resolve the current worker for an address that is or was a worker
/// @dev Former workers remain in `addressToProviderId`, so data-set snapshotted addresses still
/// resolve to the live `serviceProvider`.
/// @param workerOrFormerWorker Current or previous worker address
/// @return The live serviceProvider for that provider, or address(0) if unknown/inactive
function currentWorker(address workerOrFormerWorker) external view returns (address) {
uint256 providerId = addressToProviderId[workerOrFormerWorker];
if (providerId == 0 || !providers[providerId].isActive) {
return address(0);
}
return providers[providerId].serviceProvider;
}

/// @notice Resolve provider ID from a current or former worker address
/// @param workerOrFormerWorker Current or previous worker address
/// @return providerId The provider ID, or 0 if unknown
function getProviderIdByWorkerAddress(address workerOrFormerWorker) external view returns (uint256 providerId) {
providerId = addressToProviderId[workerOrFormerWorker];
}

/// @notice Require that the current payee is `msg.sender`, optionally binding EIP-712 `signature`
/// @dev Workers cannot submit these actions (including with a stolen payee signature). Every
/// successful authorization bumps `providerActionNonce` so any previously signed typed-data
/// for this provider is invalidated — including after a plain Glif call with empty signature.
function _requirePayeeAuthorization(uint256 providerId, bytes32 structHash, bytes calldata signature) private {
address payee = providers[providerId].payee;
require(msg.sender == payee, "Only payee can call this function");

if (signature.length != 0) {
bytes32 digest = _hashTypedDataV4(structHash);
address recovered = _recoverSigner(digest, signature);
require(recovered == payee, "Invalid payee signature");
}

unchecked {
providerActionNonce[providerId]++;
}
}

/// @notice Recover the signer of an EIP-712 digest
function _recoverSigner(bytes32 digest, bytes memory signature) private pure returns (address) {
require(signature.length == 65, "Invalid signature length");

bytes32 r;
bytes32 s;
uint8 v;
assembly ("memory-safe") {
r := mload(add(signature, 32))
s := mload(add(signature, 64))
v := byte(0, mload(add(signature, 96)))
}
if (v < 27) {
v += 27;
}
require(v == 27 || v == 28, "Unsupported signature v");

address recovered = ecrecover(digest, v, r, s);
require(recovered != address(0), "Invalid signature");
return recovered;
}

/// @notice Get complete provider information
/// @param providerId The ID of the provider
/// @return info The provider information
Expand Down
9 changes: 7 additions & 2 deletions service_contracts/src/ServiceProviderRegistryStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ contract ServiceProviderRegistryStorage {

/// @notice Main provider information
struct ServiceProviderInfo {
address serviceProvider; // Address that controls the provider registration
address payee; // Address that receives payments (cannot be changed after registration)
address serviceProvider; // Worker address that operates the provider
address payee; // Beneficiary address that receives payments and authorizes role changes
string name; // Optional provider name (max 128 chars)
string description; //Service description, ToC, contract info, website..
bool isActive;
Expand Down Expand Up @@ -71,4 +71,9 @@ contract ServiceProviderRegistryStorage {

/// @notice Count of active providers
uint256 public activeProviderCount;

/// @notice Per-provider EIP-712 nonce for payee-authorized actions
/// @dev Appended for upgrade safety. Former workers keep their `addressToProviderId` entries
/// so `currentWorker` can resolve from data-set-snapshotted addresses without a second map.
mapping(uint256 providerId => uint256 nonce) public providerActionNonce;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ interface IFilecoinWarmStorageService is IFilecoinServiceMetadata, IPDPProvingSc
event ProviderApproved(uint256 indexed providerId);
event ProviderUnapproved(uint256 indexed providerId);
event RailRateUpdated(uint256 indexed dataSetId, uint256 railId, uint256 newRate);
event DataSetPayeeRetargeted(
uint256 indexed dataSetId,
address indexed oldPayee,
address indexed newPayee,
uint256 oldPdpRailId,
uint256 newPdpRailId,
uint256 oldCacheMissRailId,
uint256 newCacheMissRailId
);
event ServiceTerminated(
address indexed approver,
uint256 indexed dataSetId,
Expand All @@ -84,6 +93,8 @@ interface IFilecoinWarmStorageService is IFilecoinServiceMetadata, IPDPProvingSc
error CallerNotPayerOrPayee(uint256 dataSetId, address expectedPayer, address expectedPayee, address caller);
error CallerNotPayments(address expected, address actual);
error CallerNotServiceProvider(uint256 dataSetId, address expectedServiceProvider, address caller);
error CallerNotProviderWorker(uint256 dataSetId, address expectedWorker, address caller);
error DataSetPayeeAlreadyCurrent(uint256 dataSetId, address payee);
error CannotDisableLegacyStorageTerms();
error ChallengeWindowTooEarly(uint256 dataSetId, uint256 windowStart, uint256 nowBlock);
error ClientDataSetAlreadyRegistered(uint256 clientDataSetId);
Expand Down Expand Up @@ -239,6 +250,7 @@ interface IFilecoinWarmStorageService is IFilecoinServiceMetadata, IPDPProvingSc
function registerStorageTerms(StorageTerms calldata terms) external returns (bytes32 storageTermsId);
function removeApprovedProvider(uint256 providerId, uint256 index) external;
function renounceOwnership() external;
function retargetDataSetPayee(uint256 dataSetId) external;
function selectors() external view returns (bytes4[] memory);
function serviceCommissionBps() external pure returns (uint256);
function serviceProviderRegistry() external view returns (ServiceProviderRegistry);
Expand Down
80 changes: 80 additions & 0 deletions service_contracts/src/lib/LibRails.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ event DataSetAbandoned(uint256 indexed dataSetId, uint256 pdpRailId, uint256 cac

event RailRateUpdated(uint256 indexed dataSetId, uint256 railId, uint256 newRate);

event DataSetPayeeRetargeted(
uint256 indexed dataSetId,
address indexed oldPayee,
address indexed newPayee,
uint256 oldPdpRailId,
uint256 newPdpRailId,
uint256 oldCacheMissRailId,
uint256 newCacheMissRailId
);

/// @title LibRails
/// @notice Payment rail operations inlined into the FWSS modules.
library LibRails {
Expand Down Expand Up @@ -293,6 +303,76 @@ library LibRails {
return reserveBalance;
}

/// @notice Zero the old PDP and cache-miss rails and open replacements paying `newPayee`.
/// @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).
/// @return newPdpRailId Replacement PDP rail paying `newPayee`
/// @return newCacheMissRailId Replacement cache-miss rail, or 0 when the data set has none
function retargetRails(
FilecoinPayV1 payments,
uint256 dataSetId,
uint256 oldPdpRailId,
uint256 oldCacheMissRailId,
address newPayee,
uint96 lifecycleReserveBalance
) internal returns (uint256 newPdpRailId, uint256 newCacheMissRailId) {
FilecoinPayV1.RailView memory oldPdp = payments.getRail(oldPdpRailId);
require(oldPdp.endEpoch == 0, Errors.DataSetPaymentAlreadyTerminated(dataSetId));

address oldPayee = oldPdp.to;
uint256 pdpRate = oldPdp.paymentRate;
uint256 lockupPeriod = oldPdp.lockupPeriod;

// Stop future accrual on the old rail without terminating it.
payments.modifyRailPayment(oldPdpRailId, 0, 0);
payments.modifyRailLockup(oldPdpRailId, lockupPeriod, 0);

newPdpRailId = payments.createRail(
oldPdp.token,
oldPdp.from,
newPayee,
oldPdp.validator,
oldPdp.commissionRateBps,
oldPdp.serviceFeeRecipient
);
payments.modifyRailLockup(newPdpRailId, lockupPeriod, lifecycleReserveBalance);
if (pdpRate > 0) {
payments.modifyRailPayment(newPdpRailId, pdpRate, 0);
}

newCacheMissRailId = 0;
if (oldCacheMissRailId != 0) {
FilecoinPayV1.RailView memory oldCacheMiss = payments.getRail(oldCacheMissRailId);
require(oldCacheMiss.endEpoch == 0, Errors.CacheMissPaymentAlreadyTerminated(dataSetId));

uint256 cacheMissRate = oldCacheMiss.paymentRate;
uint256 cacheMissPeriod = oldCacheMiss.lockupPeriod;
uint256 cacheMissFixed = oldCacheMiss.lockupFixed;

payments.modifyRailPayment(oldCacheMissRailId, 0, 0);
payments.modifyRailLockup(oldCacheMissRailId, cacheMissPeriod, 0);

newCacheMissRailId = payments.createRail(
oldCacheMiss.token,
oldCacheMiss.from,
newPayee,
oldCacheMiss.validator,
oldCacheMiss.commissionRateBps,
oldCacheMiss.serviceFeeRecipient
);
payments.modifyRailLockup(newCacheMissRailId, cacheMissPeriod, cacheMissFixed);
if (cacheMissRate > 0) {
Comment thread
snadrus marked this conversation as resolved.
Resolved
payments.modifyRailPayment(newCacheMissRailId, cacheMissRate, 0);
Comment thread
snadrus marked this conversation as resolved.
Resolved
}
}

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

function updateStorageRates(
FilecoinPayV1 payments,
uint256 dataSetId,
Expand Down
Loading
Loading