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