@@ -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