Skip to content

Commit cb01e08

Browse files
committed
Optimised the loop in validatePayment to loop over Proving Periods instead of epochs
1 parent 2fac39a commit cb01e08

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

service_contracts/foundry.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"lib/forge-std": {
3+
"rev": "f46d8301cf732f4f83846565aa475628265e51e0"
4+
},
5+
"lib/fws-payments": {
6+
"rev": "477228d2d1e93bf7b2aa7e24018e88994806ddba"
7+
},
8+
"lib/openzeppelin-contracts": {
9+
"rev": "a6ae04acf8e38ed49a70fdce5389df2752e3ecc4"
10+
},
11+
"lib/openzeppelin-contracts-upgradeable": {
12+
"rev": "3bfc52f2fbf5be95de632f346169dac6a669bd57"
13+
},
14+
"lib/pdp": {
15+
"rev": "61681392933926fbccb142ab7767e037680850b4"
16+
},
17+
"lib/session-key-registry": {
18+
"rev": "e472ca2b525fb2396832216182b64a0c165cb49c"
19+
}
20+
}

service_contracts/src/FilecoinWarmStorageService.sol

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,15 +1545,48 @@ contract FilecoinWarmStorageService is
15451545

15461546
// Count proven epochs and find the last proven epoch
15471547
uint256 provenEpochCount = 0;
1548-
uint256 lastProvenEpoch = fromEpoch;
1548+
uint256 lastProvenEpoch = 0;
15491549

1550-
// Check each epoch in the range
1551-
for (uint256 epoch = fromEpoch + 1; epoch <= toEpoch; epoch++) {
1552-
bool isProven = isEpochProven(dataSetId, epoch);
15531550

1554-
if (isProven) {
1555-
provenEpochCount++;
1556-
lastProvenEpoch = epoch;
1551+
// Updated algorithm
1552+
uint256 activationEpoch = provingActivationEpoch[dataSetId];
1553+
if(activationEpoch != 0 && toEpoch >= activationEpoch && toEpoch < block.number){
1554+
// now, starting epoch - fromEpoch + 1. find period corres to it. find out how long that period lasts and add that many epochs. then keep adding 2880 epochs till period end passes the endEpoch
1555+
1556+
if(fromEpoch < activationEpoch){
1557+
fromEpoch = activationEpoch;
1558+
}
1559+
1560+
uint256 startingPeriod = getProvingPeriodForEpoch(dataSetId, fromEpoch + 1);
1561+
uint256 endingPeriod = getProvingPeriodForEpoch(dataSetId, toEpoch);
1562+
1563+
// lets handle first period separately
1564+
uint256 startingPeriod_deadline = _calcPeriodDeadline(dataSetId, startingPeriod);
1565+
1566+
if(toEpoch < startingPeriod_deadline){
1567+
if(_isPeriodProven(dataSetId, startingPeriod)){
1568+
provenEpochCount = (toEpoch - fromEpoch);
1569+
lastProvenEpoch = toEpoch;
1570+
}
1571+
} else {
1572+
if(_isPeriodProven(dataSetId, startingPeriod)){
1573+
provenEpochCount += (startingPeriod_deadline - fromEpoch);
1574+
}
1575+
1576+
// now loop through the proving periods between endingPeriod and startingPeriod.
1577+
for(uint256 period = startingPeriod + 1; period <= endingPeriod - 1; period++){
1578+
if(provenPeriods[dataSetId][period]){ // here to check if period is proven, we can simply access the mapping since a period in between cannot be the current period
1579+
provenEpochCount += maxProvingPeriod;
1580+
lastProvenEpoch = _calcPeriodDeadline(dataSetId, period);
1581+
}
1582+
}
1583+
1584+
// now handle the last period separately
1585+
if(_isPeriodProven(dataSetId, endingPeriod)){
1586+
// then the epochs to add = `endingPeriod_starting` to `toEpoch`. But since `endingPeriod_starting` is simply the ending of its previous period, so
1587+
provenEpochCount += (toEpoch - _calcPeriodDeadline(dataSetId, endingPeriod - 1));
1588+
lastProvenEpoch = toEpoch;
1589+
}
15571590
}
15581591
}
15591592

@@ -1582,6 +1615,18 @@ contract FilecoinWarmStorageService is
15821615
});
15831616
}
15841617

1618+
function _isPeriodProven(uint256 dataSetId, uint256 periodId) private view returns(bool){
1619+
uint256 currentPeriod = getProvingPeriodForEpoch(dataSetId, block.number);
1620+
if(periodId == currentPeriod){
1621+
return provenThisPeriod[dataSetId];
1622+
}
1623+
return provenPeriods[dataSetId][periodId];
1624+
}
1625+
1626+
function _calcPeriodDeadline(uint256 dataSetId, uint256 periodId) private view returns(uint256){
1627+
return provingActivationEpoch[dataSetId] + ((periodId+1) * maxProvingPeriod); // we need to do `periodId + 1` since it starts from 0
1628+
}
1629+
15851630
function railTerminated(uint256 railId, address terminator, uint256 endEpoch) external override {
15861631
require(msg.sender == paymentsContractAddress, Errors.CallerNotPayments(paymentsContractAddress, msg.sender));
15871632

0 commit comments

Comments
 (0)