@@ -1634,83 +1634,94 @@ contract FilecoinWarmStorageService is
16341634 uint256 provenEpochCount = 0 ;
16351635 uint256 lastProvenEpoch = fromEpoch;
16361636
1637+ (provenEpochCount, lastProvenEpoch) = findProvenEpochs (dataSetId, fromEpoch, toEpoch);
16371638
1638- // Updated algorithm
1639+ // If no epochs are proven, we can't settle anything
1640+ if (provenEpochCount == 0 ) {
1641+ return ValidationResult ({
1642+ modifiedAmount: 0 ,
1643+ settleUpto: fromEpoch,
1644+ note: "No proven epochs in the requested range "
1645+ });
1646+ }
1647+
1648+ // Calculate the modified amount based on proven epochs
1649+ uint256 modifiedAmount = (proposedAmount * provenEpochCount) / totalEpochsRequested;
1650+
1651+ // Calculate how many epochs were not proven (faulted)
1652+ uint256 faultedEpochs = totalEpochsRequested - provenEpochCount;
1653+
1654+ // Emit event for logging
1655+ emit PaymentArbitrated (railId, dataSetId, proposedAmount, modifiedAmount, faultedEpochs);
1656+
1657+ return ValidationResult ({
1658+ modifiedAmount: modifiedAmount,
1659+ settleUpto: lastProvenEpoch, // Settle up to the last proven epoch
1660+ note: ""
1661+ });
1662+ }
1663+
1664+ function findProvenEpochs (uint256 dataSetId , uint256 fromEpoch , uint256 toEpoch )
1665+ public
1666+ view
1667+ returns (uint256 , uint256 )
1668+ {
1669+ uint256 provenEpochCount = 0 ;
1670+ uint256 lastProvenEpoch = fromEpoch;
16391671 uint256 activationEpoch = provingActivationEpoch[dataSetId];
1640- if (toEpoch >= activationEpoch && toEpoch < block .number ){
1641- // if `toEpoch` lies after activation, and `fromEpoch` lies before activation, then update the `fromEpoch`, as follows :
1642- if (fromEpoch < activationEpoch){
1672+ if (toEpoch >= activationEpoch && toEpoch < block .number ) {
1673+ // if `toEpoch` lies after activation, and `fromEpoch` lies before activation, then update the `fromEpoch`, as follows :
1674+ if (fromEpoch < activationEpoch) {
16431675 fromEpoch = activationEpoch - 1 ; // we have done -1 because starting epoch is considered as `fromEpoch + 1`
16441676 }
16451677
16461678 uint256 startingPeriod = getProvingPeriodForEpoch (dataSetId, fromEpoch + 1 );
16471679 uint256 endingPeriod = getProvingPeriodForEpoch (dataSetId, toEpoch);
16481680
1649- // lets handle first period separately
1681+ // lets handle first period separately
16501682 uint256 startingPeriod_deadline = _calcPeriodDeadline (dataSetId, startingPeriod);
16511683
1652- if (toEpoch < startingPeriod_deadline){ // alternative way to check the same : `startingPeriod == endingPeriod`
1653- if (_isPeriodProven (dataSetId, startingPeriod)){
1684+ if (toEpoch < startingPeriod_deadline) {
1685+ // alternative way to check the same : `startingPeriod == endingPeriod`
1686+ if (_isPeriodProven (dataSetId, startingPeriod)) {
16541687 provenEpochCount = (toEpoch - fromEpoch);
16551688 lastProvenEpoch = toEpoch;
16561689 }
16571690 } else {
1658- if (_isPeriodProven (dataSetId, startingPeriod)){
1691+ if (_isPeriodProven (dataSetId, startingPeriod)) {
16591692 provenEpochCount += (startingPeriod_deadline - fromEpoch);
16601693 }
16611694
1662- // now loop through the proving periods between endingPeriod and startingPeriod.
1663- for (uint256 period = startingPeriod + 1 ; period <= endingPeriod - 1 ; period++ ){
1664- if (_isPeriodProven (dataSetId, period)){
1695+ // now loop through the proving periods between endingPeriod and startingPeriod.
1696+ for (uint256 period = startingPeriod + 1 ; period <= endingPeriod - 1 ; period++ ) {
1697+ if (_isPeriodProven (dataSetId, period)) {
16651698 provenEpochCount += maxProvingPeriod;
16661699 lastProvenEpoch = _calcPeriodDeadline (dataSetId, period);
16671700 }
16681701 }
16691702
16701703 // now handle the last period separately
1671- if (_isPeriodProven (dataSetId, endingPeriod)){
1704+ if (_isPeriodProven (dataSetId, endingPeriod)) {
16721705 // then the epochs to add = `endingPeriod_starting` to `toEpoch`. But since `endingPeriod_starting` is simply the ending of its previous period, so
16731706 provenEpochCount += (toEpoch - _calcPeriodDeadline (dataSetId, endingPeriod - 1 ));
16741707 lastProvenEpoch = toEpoch;
16751708 }
16761709 }
16771710 }
16781711
1679- // If no epochs are proven, we can't settle anything
1680- if (provenEpochCount == 0 ) {
1681- return ValidationResult ({
1682- modifiedAmount: 0 ,
1683- settleUpto: fromEpoch,
1684- note: "No proven epochs in the requested range "
1685- });
1686- }
1687-
1688- // Calculate the modified amount based on proven epochs
1689- uint256 modifiedAmount = (proposedAmount * provenEpochCount) / totalEpochsRequested;
1690-
1691- // Calculate how many epochs were not proven (faulted)
1692- uint256 faultedEpochs = totalEpochsRequested - provenEpochCount;
1693-
1694- // Emit event for logging
1695- emit PaymentArbitrated (railId, dataSetId, proposedAmount, modifiedAmount, faultedEpochs);
1696-
1697- return ValidationResult ({
1698- modifiedAmount: modifiedAmount,
1699- settleUpto: lastProvenEpoch, // Settle up to the last proven epoch
1700- note: ""
1701- });
1712+ return (provenEpochCount, lastProvenEpoch);
17021713 }
17031714
1704- function _isPeriodProven (uint256 dataSetId , uint256 periodId ) private view returns (bool ){
1715+ function _isPeriodProven (uint256 dataSetId , uint256 periodId ) private view returns (bool ) {
17051716 uint256 currentPeriod = getProvingPeriodForEpoch (dataSetId, block .number );
1706- if (periodId == currentPeriod){
1717+ if (periodId == currentPeriod) {
17071718 return provenThisPeriod[dataSetId];
1708- }
1719+ }
17091720 return provenPeriods[dataSetId][periodId];
17101721 }
17111722
1712- function _calcPeriodDeadline (uint256 dataSetId , uint256 periodId ) private view returns (uint256 ){
1713- return provingActivationEpoch[dataSetId] + ((periodId+ 1 ) * maxProvingPeriod); // we need to do `periodId + 1` since it starts from 0
1723+ function _calcPeriodDeadline (uint256 dataSetId , uint256 periodId ) private view returns (uint256 ) {
1724+ return provingActivationEpoch[dataSetId] + ((periodId + 1 ) * maxProvingPeriod); // we need to do `periodId + 1` since it starts from 0
17141725 }
17151726
17161727 function railTerminated (uint256 railId , address terminator , uint256 endEpoch ) external override {
0 commit comments