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
8 changes: 6 additions & 2 deletions service_contracts/src/FilecoinWarmStorageService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.20;

import {PDPListener} from "@pdp/PDPVerifier.sol";
import {IPDPVerifier} from "@pdp/interfaces/IPDPVerifier.sol";
import {Cids} from "@pdp/Cids.sol";
import {SessionKeyRegistry} from "@session-key-registry/SessionKeyRegistry.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
Expand Down Expand Up @@ -834,6 +835,11 @@ contract FilecoinWarmStorageService is
// Verify the signature
verifyAddPiecesSignature(payer, info.clientDataSetId, pieceData, nonce, metadataKeys, metadataValues, signature);

// Update payment rates immediately to enforce payment validation
// Get the current leaf count from PDPVerifier (updated before this call)
uint256 currentLeafCount = IPDPVerifier(pdpVerifierAddress).getDataSetLeafCount(dataSetId);
updatePaymentRates(dataSetId, currentLeafCount);

// Store metadata for each new piece
for (uint256 i = 0; i < pieceData.length; i++) {
uint256 pieceId = firstAdded + i;
Expand Down Expand Up @@ -963,7 +969,6 @@ contract FilecoinWarmStorageService is
// This marks when the data set became active for proving
provingActivationEpoch[dataSetId] = block.number;

// Update the payment rates
updatePaymentRates(dataSetId, leafCount);

return;
Expand Down Expand Up @@ -1010,7 +1015,6 @@ contract FilecoinWarmStorageService is
provingDeadlines[dataSetId] = nextDeadline;
provenThisPeriod[dataSetId] = false;

// Update the payment rates based on current data set size
updatePaymentRates(dataSetId, leafCount);
}

Expand Down
20 changes: 11 additions & 9 deletions service_contracts/test/FilecoinWarmStorageService.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,9 @@ contract FilecoinWarmStorageServiceTest is MockFVMTest {

// First batch (3 pieces) with key "meta" => metadataShort
Cids.Cid[] memory pieceData1 = new Cids.Cid[](3);
pieceData1[0].data = bytes("1_0:1111");
pieceData1[1].data = bytes("1_1:111100000");
pieceData1[2].data = bytes("1_2:11110000000000");
pieceData1[0] = Cids.CommPv2FromDigest(0, 4, keccak256(abi.encodePacked("1_0:1111")));
pieceData1[1] = Cids.CommPv2FromDigest(0, 4, keccak256(abi.encodePacked("1_1:111100000")));
pieceData1[2] = Cids.CommPv2FromDigest(0, 4, keccak256(abi.encodePacked("1_2:11110000000000")));
string[] memory keys1 = new string[](1);
string[] memory values1 = new string[](1);
keys1[0] = "meta";
Expand All @@ -817,8 +817,10 @@ contract FilecoinWarmStorageServiceTest is MockFVMTest {

// Second batch (2 pieces) with key "meta" => metadataLong
Cids.Cid[] memory pieceData2 = new Cids.Cid[](2);
pieceData2[0].data = bytes("2_0:22222222222222222222");
pieceData2[1].data = bytes("2_1:222222222222222222220000000000000000000000000000000000000000");
pieceData2[0] = Cids.CommPv2FromDigest(0, 4, keccak256(abi.encodePacked("2_0:22222222222222222222")));
pieceData2[1] = Cids.CommPv2FromDigest(
0, 4, keccak256(abi.encodePacked("2_1:222222222222222222220000000000000000000000000000000000000000000"))
);
string[] memory keys2 = new string[](1);
string[] memory values2 = new string[](1);
keys2[0] = "meta";
Expand Down Expand Up @@ -4558,7 +4560,7 @@ contract FilecoinWarmStorageServiceTest is MockFVMTest {

// Prepare piece data
Cids.Cid[] memory pieceData = new Cids.Cid[](1);
pieceData[0].data = bytes("test_piece_1");
pieceData[0] = Cids.CommPv2FromDigest(0, 4, keccak256(abi.encodePacked("test_piece_1")));
string[] memory keys = new string[](0);
string[] memory values = new string[](0);

Expand Down Expand Up @@ -4604,7 +4606,7 @@ contract FilecoinWarmStorageServiceTest is MockFVMTest {

// Prepare piece data
Cids.Cid[] memory pieceData = new Cids.Cid[](1);
pieceData[0].data = bytes("test_piece");
pieceData[0] = Cids.CommPv2FromDigest(0, 4, keccak256(abi.encodePacked("test_piece_1")));
string[] memory keys = new string[](0);
string[] memory values = new string[](0);

Expand Down Expand Up @@ -4678,7 +4680,7 @@ contract FilecoinWarmStorageServiceTest is MockFVMTest {

// Prepare piece data
Cids.Cid[] memory pieceData = new Cids.Cid[](1);
pieceData[0].data = bytes("test");
pieceData[0] = Cids.CommPv2FromDigest(0, 4, keccak256(abi.encodePacked("test_piece_1")));
string[] memory keys = new string[](0);
string[] memory values = new string[](0);

Expand Down Expand Up @@ -4725,7 +4727,7 @@ contract FilecoinWarmStorageServiceTest is MockFVMTest {

// Prepare piece data
Cids.Cid[] memory pieceData = new Cids.Cid[](1);
pieceData[0].data = bytes("test");
pieceData[0] = Cids.CommPv2FromDigest(0, 4, keccak256(abi.encodePacked("test_piece_1")));
string[] memory keys = new string[](0);
string[] memory values = new string[](0);

Expand Down
19 changes: 19 additions & 0 deletions service_contracts/test/mocks/SharedMocks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ contract MockPDPVerifier {

// Track data set service providers for testing
mapping(uint256 => address) public dataSetServiceProviders;
// Track simple leaf counts per data set for tests (approximate via bytes length)
mapping(uint256 => uint256) public dataSetLeafCount;

event DataSetCreated(uint256 indexed setId, address indexed owner);
event DataSetServiceProviderChanged(
Expand All @@ -118,6 +120,9 @@ contract MockPDPVerifier {
// Track service provider
dataSetServiceProviders[setId] = msg.sender;

// initialize leaf count to 0
dataSetLeafCount[setId] = 0;

emit DataSetCreated(setId, msg.sender);
return setId;
}
Expand All @@ -128,6 +133,7 @@ contract MockPDPVerifier {
}

delete dataSetServiceProviders[setId];
delete dataSetLeafCount[setId];
emit DataSetDeleted(setId, 0);
}

Expand All @@ -150,9 +156,22 @@ contract MockPDPVerifier {
}

bytes memory extraData = abi.encode(nonce, allKeys, allValues, signature);

uint256 leafCount = 0;
for (uint256 i = 0; i < pieceData.length; i++) {
(uint256 padding, uint8 height,) = Cids.validateCommPv2(pieceData[i]);
leafCount += Cids.leafCount(padding, height);
}
dataSetLeafCount[dataSetId] += leafCount;

listenerAddr.piecesAdded(dataSetId, firstAdded, pieceData, extraData);
}

// Expose leaf count similar to real PDPVerifier
function getDataSetLeafCount(uint256 setId) external view returns (uint256) {
return dataSetLeafCount[setId];
}

/**
* @notice Simulates service provider change for testing purposes
* @dev This function mimics the PDPVerifier's claimDataSetOwnership functionality
Expand Down
Loading