Skip to content

Commit e3bddea

Browse files
committed
implemented required changes
1 parent f724dea commit e3bddea

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
# VIM
22
*.swp
3-
4-
# Foundry
5-
foundry.lock

service_contracts/src/Errors.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ library Errors {
201201
/// @param railId The rail ID
202202
error RailNotAssociated(uint256 railId);
203203

204-
/// @notice The epoch range is invalid (toEpoch must be > fromEpoch)
204+
/// @notice The epoch range is invalid
205+
/// @notice Will be emitted if any of the following conditions is NOT met - (fromEpoch must be less than toEpoch), (toEpoch must be greater than block number), (toEpoch must be greater than the activation epoch)
205206
/// @param fromEpoch The starting epoch (exclusive)
206207
/// @param toEpoch The ending epoch (inclusive)
207208
error InvalidEpochRange(uint256 fromEpoch, uint256 toEpoch);

service_contracts/src/FilecoinWarmStorageService.sol

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ contract FilecoinWarmStorageService is
14451445
uint256 provenEpochCount = 0;
14461446
uint256 lastProvenEpoch = fromEpoch;
14471447

1448-
(provenEpochCount, lastProvenEpoch) = _findProvenEpochs(dataSetId, fromEpoch, toEpoch);
1448+
(provenEpochCount, lastProvenEpoch) = _findProvenEpochs(dataSetId, fromEpoch, toEpoch, provenEpochCount, lastProvenEpoch);
14491449

14501450
// If no epochs are proven, we can't settle anything
14511451
if (provenEpochCount == 0) {
@@ -1469,53 +1469,56 @@ contract FilecoinWarmStorageService is
14691469
});
14701470
}
14711471

1472-
function _findProvenEpochs(uint256 dataSetId, uint256 fromEpoch, uint256 toEpoch)
1472+
function _findProvenEpochs(uint256 dataSetId, uint256 fromEpoch, uint256 toEpoch, uint256 provenEpochCount, uint256 lastProvenEpoch)
14731473
internal
14741474
view
14751475
returns (uint256, uint256)
14761476
{
1477-
uint256 provenEpochCount = 0;
1478-
uint256 lastProvenEpoch = fromEpoch;
14791477
uint256 activationEpoch = provingActivationEpoch[dataSetId];
1478+
if (toEpoch < activationEpoch){
1479+
revert Errors.InvalidEpochRange(fromEpoch, toEpoch);
1480+
}
1481+
if(toEpoch > block.number){
1482+
revert Errors.InvalidEpochRange(fromEpoch, toEpoch);
1483+
}
14801484
uint256 currentPeriod = getProvingPeriodForEpoch(dataSetId, block.number);
1481-
if (toEpoch >= activationEpoch && toEpoch < block.number) {
1482-
// if `toEpoch` lies after activation, and `fromEpoch` lies before activation, then update the `fromEpoch`, as follows :
1483-
if (fromEpoch < activationEpoch - 1) {
1484-
fromEpoch = activationEpoch - 1;
1485-
}
14861485

1487-
uint256 startingPeriod = getProvingPeriodForEpoch(dataSetId, fromEpoch + 1);
1488-
uint256 endingPeriod = getProvingPeriodForEpoch(dataSetId, toEpoch);
1486+
// if `toEpoch` lies after activation, and `fromEpoch` lies before activation, then update the `fromEpoch`, as follows :
1487+
if (fromEpoch < activationEpoch - 1) {
1488+
fromEpoch = activationEpoch - 1;
1489+
}
14891490

1490-
// lets handle first period separately
1491-
uint256 startingPeriod_deadline = _calcPeriodDeadline(dataSetId, startingPeriod);
1491+
uint256 startingPeriod = getProvingPeriodForEpoch(dataSetId, fromEpoch + 1);
1492+
uint256 endingPeriod = getProvingPeriodForEpoch(dataSetId, toEpoch);
14921493

1493-
if (toEpoch < startingPeriod_deadline) {
1494-
// alternative way to check the same : `startingPeriod == endingPeriod`
1495-
if (_isPeriodProven(dataSetId, startingPeriod, currentPeriod)) {
1496-
provenEpochCount = (toEpoch - fromEpoch); // epochs : (`from + 1` -> `to`) (both inclusive)
1497-
lastProvenEpoch = toEpoch;
1498-
}
1499-
} else {
1500-
if (_isPeriodProven(dataSetId, startingPeriod, currentPeriod)) {
1501-
provenEpochCount += (startingPeriod_deadline - fromEpoch); // epochs : (`from + 1` -> `deadline`)
1502-
}
1494+
// handle first period separately
1495+
uint256 startingPeriodDeadline = _calcPeriodDeadline(dataSetId, startingPeriod);
15031496

1504-
// now loop through the proving periods between endingPeriod and startingPeriod.
1505-
for (uint256 period = startingPeriod + 1; period <= endingPeriod - 1; period++) {
1506-
if (_isPeriodProven(dataSetId, period, currentPeriod)) {
1507-
provenEpochCount += maxProvingPeriod;
1508-
lastProvenEpoch = _calcPeriodDeadline(dataSetId, period);
1509-
}
1510-
}
1497+
if (toEpoch < startingPeriodDeadline) {
1498+
// alternative way to check the same : `startingPeriod == endingPeriod`
1499+
if (_isPeriodProven(dataSetId, startingPeriod, currentPeriod)) {
1500+
provenEpochCount = (toEpoch - fromEpoch); // epochs : (`from + 1` -> `to`) (both inclusive)
1501+
lastProvenEpoch = toEpoch;
1502+
}
1503+
} else {
1504+
if (_isPeriodProven(dataSetId, startingPeriod, currentPeriod)) {
1505+
provenEpochCount += (startingPeriodDeadline - fromEpoch); // epochs : (`from + 1` -> `deadline`)
1506+
}
15111507

1512-
// now handle the last period separately
1513-
if (_isPeriodProven(dataSetId, endingPeriod, currentPeriod)) {
1514-
// then the epochs to add = `endingPeriod_starting` to `toEpoch`. But since `endingPeriod_starting` is simply the ending of its previous period + 1, so epochs : (`deadline + 1` -> `to`)
1515-
provenEpochCount += (toEpoch - _calcPeriodDeadline(dataSetId, endingPeriod - 1));
1516-
lastProvenEpoch = toEpoch;
1508+
// now loop through the proving periods between endingPeriod and startingPeriod.
1509+
for (uint256 period = startingPeriod + 1; period <= endingPeriod - 1; period++) {
1510+
if (_isPeriodProven(dataSetId, period, currentPeriod)) {
1511+
provenEpochCount += maxProvingPeriod;
1512+
lastProvenEpoch = _calcPeriodDeadline(dataSetId, period);
15171513
}
15181514
}
1515+
1516+
// now handle the last period separately
1517+
if (_isPeriodProven(dataSetId, endingPeriod, currentPeriod)) {
1518+
// then the epochs to add = `endingPeriod_starting` to `toEpoch`. But since `endingPeriod_starting` is simply the ending of its previous period + 1, so epochs : (`deadline + 1` -> `to`)
1519+
provenEpochCount += (toEpoch - _calcPeriodDeadline(dataSetId, endingPeriod - 1));
1520+
lastProvenEpoch = toEpoch;
1521+
}
15191522
}
15201523

15211524
return (provenEpochCount, lastProvenEpoch);
@@ -1525,7 +1528,6 @@ contract FilecoinWarmStorageService is
15251528
if (periodId == currentPeriod) {
15261529
return provenThisPeriod[dataSetId];
15271530
}
1528-
// return provenPeriods[dataSetId][periodId];
15291531
uint256 isProven = provenPeriods[dataSetId][periodId >> 8] & (1 << (periodId & 255));
15301532
return isProven != 0;
15311533
}

0 commit comments

Comments
 (0)