Skip to content

Commit c192dd2

Browse files
committed
updated abi and restructured validatePayment to prevent possible stak too deep error
1 parent 42a7895 commit c192dd2

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

service_contracts/src/FilecoinWarmStorageService.sol

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,83 +1513,94 @@ contract FilecoinWarmStorageService is
15131513
uint256 provenEpochCount = 0;
15141514
uint256 lastProvenEpoch = fromEpoch;
15151515

1516+
(provenEpochCount, lastProvenEpoch) = findProvenEpochs(dataSetId, fromEpoch, toEpoch);
15161517

1517-
// Updated algorithm
1518+
// If no epochs are proven, we can't settle anything
1519+
if (provenEpochCount == 0) {
1520+
return ValidationResult({
1521+
modifiedAmount: 0,
1522+
settleUpto: fromEpoch,
1523+
note: "No proven epochs in the requested range"
1524+
});
1525+
}
1526+
1527+
// Calculate the modified amount based on proven epochs
1528+
uint256 modifiedAmount = (proposedAmount * provenEpochCount) / totalEpochsRequested;
1529+
1530+
// Calculate how many epochs were not proven (faulted)
1531+
uint256 faultedEpochs = totalEpochsRequested - provenEpochCount;
1532+
1533+
// Emit event for logging
1534+
emit PaymentArbitrated(railId, dataSetId, proposedAmount, modifiedAmount, faultedEpochs);
1535+
1536+
return ValidationResult({
1537+
modifiedAmount: modifiedAmount,
1538+
settleUpto: lastProvenEpoch, // Settle up to the last proven epoch
1539+
note: ""
1540+
});
1541+
}
1542+
1543+
function findProvenEpochs(uint256 dataSetId, uint256 fromEpoch, uint256 toEpoch)
1544+
public
1545+
view
1546+
returns (uint256, uint256)
1547+
{
1548+
uint256 provenEpochCount = 0;
1549+
uint256 lastProvenEpoch = fromEpoch;
15181550
uint256 activationEpoch = provingActivationEpoch[dataSetId];
1519-
if(toEpoch >= activationEpoch && toEpoch < block.number){
1520-
// if `toEpoch` lies after activation, and `fromEpoch` lies before activation, then update the `fromEpoch`, as follows :
1521-
if(fromEpoch < activationEpoch){
1551+
if (toEpoch >= activationEpoch && toEpoch < block.number) {
1552+
// if `toEpoch` lies after activation, and `fromEpoch` lies before activation, then update the `fromEpoch`, as follows :
1553+
if (fromEpoch < activationEpoch) {
15221554
fromEpoch = activationEpoch - 1; // we have done -1 because starting epoch is considered as `fromEpoch + 1`
15231555
}
15241556

15251557
uint256 startingPeriod = getProvingPeriodForEpoch(dataSetId, fromEpoch + 1);
15261558
uint256 endingPeriod = getProvingPeriodForEpoch(dataSetId, toEpoch);
15271559

1528-
// lets handle first period separately
1560+
// lets handle first period separately
15291561
uint256 startingPeriod_deadline = _calcPeriodDeadline(dataSetId, startingPeriod);
15301562

1531-
if(toEpoch < startingPeriod_deadline){ // alternative way to check the same : `startingPeriod == endingPeriod`
1532-
if(_isPeriodProven(dataSetId, startingPeriod)){
1563+
if (toEpoch < startingPeriod_deadline) {
1564+
// alternative way to check the same : `startingPeriod == endingPeriod`
1565+
if (_isPeriodProven(dataSetId, startingPeriod)) {
15331566
provenEpochCount = (toEpoch - fromEpoch);
15341567
lastProvenEpoch = toEpoch;
15351568
}
15361569
} else {
1537-
if(_isPeriodProven(dataSetId, startingPeriod)){
1570+
if (_isPeriodProven(dataSetId, startingPeriod)) {
15381571
provenEpochCount += (startingPeriod_deadline - fromEpoch);
15391572
}
15401573

1541-
// now loop through the proving periods between endingPeriod and startingPeriod.
1542-
for(uint256 period = startingPeriod + 1; period <= endingPeriod - 1; period++){
1543-
if(_isPeriodProven(dataSetId, period)){
1574+
// now loop through the proving periods between endingPeriod and startingPeriod.
1575+
for (uint256 period = startingPeriod + 1; period <= endingPeriod - 1; period++) {
1576+
if (_isPeriodProven(dataSetId, period)) {
15441577
provenEpochCount += maxProvingPeriod;
15451578
lastProvenEpoch = _calcPeriodDeadline(dataSetId, period);
15461579
}
15471580
}
15481581

15491582
// now handle the last period separately
1550-
if(_isPeriodProven(dataSetId, endingPeriod)){
1583+
if (_isPeriodProven(dataSetId, endingPeriod)) {
15511584
// then the epochs to add = `endingPeriod_starting` to `toEpoch`. But since `endingPeriod_starting` is simply the ending of its previous period, so
15521585
provenEpochCount += (toEpoch - _calcPeriodDeadline(dataSetId, endingPeriod - 1));
15531586
lastProvenEpoch = toEpoch;
15541587
}
15551588
}
15561589
}
15571590

1558-
// If no epochs are proven, we can't settle anything
1559-
if (provenEpochCount == 0) {
1560-
return ValidationResult({
1561-
modifiedAmount: 0,
1562-
settleUpto: fromEpoch,
1563-
note: "No proven epochs in the requested range"
1564-
});
1565-
}
1566-
1567-
// Calculate the modified amount based on proven epochs
1568-
uint256 modifiedAmount = (proposedAmount * provenEpochCount) / totalEpochsRequested;
1569-
1570-
// Calculate how many epochs were not proven (faulted)
1571-
uint256 faultedEpochs = totalEpochsRequested - provenEpochCount;
1572-
1573-
// Emit event for logging
1574-
emit PaymentArbitrated(railId, dataSetId, proposedAmount, modifiedAmount, faultedEpochs);
1575-
1576-
return ValidationResult({
1577-
modifiedAmount: modifiedAmount,
1578-
settleUpto: lastProvenEpoch, // Settle up to the last proven epoch
1579-
note: ""
1580-
});
1591+
return (provenEpochCount, lastProvenEpoch);
15811592
}
15821593

1583-
function _isPeriodProven(uint256 dataSetId, uint256 periodId) private view returns(bool){
1594+
function _isPeriodProven(uint256 dataSetId, uint256 periodId) private view returns (bool) {
15841595
uint256 currentPeriod = getProvingPeriodForEpoch(dataSetId, block.number);
1585-
if(periodId == currentPeriod){
1596+
if (periodId == currentPeriod) {
15861597
return provenThisPeriod[dataSetId];
1587-
}
1598+
}
15881599
return provenPeriods[dataSetId][periodId];
15891600
}
15901601

1591-
function _calcPeriodDeadline(uint256 dataSetId, uint256 periodId) private view returns(uint256){
1592-
return provingActivationEpoch[dataSetId] + ((periodId+1) * maxProvingPeriod); // we need to do `periodId + 1` since it starts from 0
1602+
function _calcPeriodDeadline(uint256 dataSetId, uint256 periodId) private view returns (uint256) {
1603+
return provingActivationEpoch[dataSetId] + ((periodId + 1) * maxProvingPeriod); // we need to do `periodId + 1` since it starts from 0
15931604
}
15941605

15951606
function railTerminated(uint256 railId, address terminator, uint256 endEpoch) external override {

0 commit comments

Comments
 (0)