Skip to content

Commit 8346892

Browse files
committed
Optimised the loop in validatePayment to loop over Proving Periods instead of epochs
1 parent 5701658 commit 8346892

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
@@ -1632,15 +1632,48 @@ contract FilecoinWarmStorageService is
16321632

16331633
// Count proven epochs and find the last proven epoch
16341634
uint256 provenEpochCount = 0;
1635-
uint256 lastProvenEpoch = fromEpoch;
1635+
uint256 lastProvenEpoch = 0;
16361636

1637-
// Check each epoch in the range
1638-
for (uint256 epoch = fromEpoch + 1; epoch <= toEpoch; epoch++) {
1639-
bool isProven = isEpochProven(dataSetId, epoch);
16401637

1641-
if (isProven) {
1642-
provenEpochCount++;
1643-
lastProvenEpoch = epoch;
1638+
// Updated algorithm
1639+
uint256 activationEpoch = provingActivationEpoch[dataSetId];
1640+
if(activationEpoch != 0 && toEpoch >= activationEpoch && toEpoch < block.number){
1641+
// 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
1642+
1643+
if(fromEpoch < activationEpoch){
1644+
fromEpoch = activationEpoch;
1645+
}
1646+
1647+
uint256 startingPeriod = getProvingPeriodForEpoch(dataSetId, fromEpoch + 1);
1648+
uint256 endingPeriod = getProvingPeriodForEpoch(dataSetId, toEpoch);
1649+
1650+
// lets handle first period separately
1651+
uint256 startingPeriod_deadline = _calcPeriodDeadline(dataSetId, startingPeriod);
1652+
1653+
if(toEpoch < startingPeriod_deadline){
1654+
if(_isPeriodProven(dataSetId, startingPeriod)){
1655+
provenEpochCount = (toEpoch - fromEpoch);
1656+
lastProvenEpoch = toEpoch;
1657+
}
1658+
} else {
1659+
if(_isPeriodProven(dataSetId, startingPeriod)){
1660+
provenEpochCount += (startingPeriod_deadline - fromEpoch);
1661+
}
1662+
1663+
// now loop through the proving periods between endingPeriod and startingPeriod.
1664+
for(uint256 period = startingPeriod + 1; period <= endingPeriod - 1; period++){
1665+
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
1666+
provenEpochCount += maxProvingPeriod;
1667+
lastProvenEpoch = _calcPeriodDeadline(dataSetId, period);
1668+
}
1669+
}
1670+
1671+
// now handle the last period separately
1672+
if(_isPeriodProven(dataSetId, endingPeriod)){
1673+
// then the epochs to add = `endingPeriod_starting` to `toEpoch`. But since `endingPeriod_starting` is simply the ending of its previous period, so
1674+
provenEpochCount += (toEpoch - _calcPeriodDeadline(dataSetId, endingPeriod - 1));
1675+
lastProvenEpoch = toEpoch;
1676+
}
16441677
}
16451678
}
16461679

@@ -1669,6 +1702,18 @@ contract FilecoinWarmStorageService is
16691702
});
16701703
}
16711704

1705+
function _isPeriodProven(uint256 dataSetId, uint256 periodId) private view returns(bool){
1706+
uint256 currentPeriod = getProvingPeriodForEpoch(dataSetId, block.number);
1707+
if(periodId == currentPeriod){
1708+
return provenThisPeriod[dataSetId];
1709+
}
1710+
return provenPeriods[dataSetId][periodId];
1711+
}
1712+
1713+
function _calcPeriodDeadline(uint256 dataSetId, uint256 periodId) private view returns(uint256){
1714+
return provingActivationEpoch[dataSetId] + ((periodId+1) * maxProvingPeriod); // we need to do `periodId + 1` since it starts from 0
1715+
}
1716+
16721717
function railTerminated(uint256 railId, address terminator, uint256 endEpoch) external override {
16731718
require(msg.sender == paymentsContractAddress, Errors.CallerNotPayments(paymentsContractAddress, msg.sender));
16741719

0 commit comments

Comments
 (0)