Skip to content

Commit e2e5d56

Browse files
committed
updated abi and restructured validatePayment to prevent possible stak too deep error
1 parent a91fcfe commit e2e5d56

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
@@ -1547,83 +1547,94 @@ contract FilecoinWarmStorageService is
15471547
uint256 provenEpochCount = 0;
15481548
uint256 lastProvenEpoch = fromEpoch;
15491549

1550+
(provenEpochCount, lastProvenEpoch) = findProvenEpochs(dataSetId, fromEpoch, toEpoch);
15501551

1551-
// Updated algorithm
1552+
// If no epochs are proven, we can't settle anything
1553+
if (provenEpochCount == 0) {
1554+
return ValidationResult({
1555+
modifiedAmount: 0,
1556+
settleUpto: fromEpoch,
1557+
note: "No proven epochs in the requested range"
1558+
});
1559+
}
1560+
1561+
// Calculate the modified amount based on proven epochs
1562+
uint256 modifiedAmount = (proposedAmount * provenEpochCount) / totalEpochsRequested;
1563+
1564+
// Calculate how many epochs were not proven (faulted)
1565+
uint256 faultedEpochs = totalEpochsRequested - provenEpochCount;
1566+
1567+
// Emit event for logging
1568+
emit PaymentArbitrated(railId, dataSetId, proposedAmount, modifiedAmount, faultedEpochs);
1569+
1570+
return ValidationResult({
1571+
modifiedAmount: modifiedAmount,
1572+
settleUpto: lastProvenEpoch, // Settle up to the last proven epoch
1573+
note: ""
1574+
});
1575+
}
1576+
1577+
function findProvenEpochs(uint256 dataSetId, uint256 fromEpoch, uint256 toEpoch)
1578+
public
1579+
view
1580+
returns (uint256, uint256)
1581+
{
1582+
uint256 provenEpochCount = 0;
1583+
uint256 lastProvenEpoch = fromEpoch;
15521584
uint256 activationEpoch = provingActivationEpoch[dataSetId];
1553-
if(toEpoch >= activationEpoch && toEpoch < block.number){
1554-
// if `toEpoch` lies after activation, and `fromEpoch` lies before activation, then update the `fromEpoch`, as follows :
1555-
if(fromEpoch < activationEpoch){
1585+
if (toEpoch >= activationEpoch && toEpoch < block.number) {
1586+
// if `toEpoch` lies after activation, and `fromEpoch` lies before activation, then update the `fromEpoch`, as follows :
1587+
if (fromEpoch < activationEpoch) {
15561588
fromEpoch = activationEpoch - 1; // we have done -1 because starting epoch is considered as `fromEpoch + 1`
15571589
}
15581590

15591591
uint256 startingPeriod = getProvingPeriodForEpoch(dataSetId, fromEpoch + 1);
15601592
uint256 endingPeriod = getProvingPeriodForEpoch(dataSetId, toEpoch);
15611593

1562-
// lets handle first period separately
1594+
// lets handle first period separately
15631595
uint256 startingPeriod_deadline = _calcPeriodDeadline(dataSetId, startingPeriod);
15641596

1565-
if(toEpoch < startingPeriod_deadline){ // alternative way to check the same : `startingPeriod == endingPeriod`
1566-
if(_isPeriodProven(dataSetId, startingPeriod)){
1597+
if (toEpoch < startingPeriod_deadline) {
1598+
// alternative way to check the same : `startingPeriod == endingPeriod`
1599+
if (_isPeriodProven(dataSetId, startingPeriod)) {
15671600
provenEpochCount = (toEpoch - fromEpoch);
15681601
lastProvenEpoch = toEpoch;
15691602
}
15701603
} else {
1571-
if(_isPeriodProven(dataSetId, startingPeriod)){
1604+
if (_isPeriodProven(dataSetId, startingPeriod)) {
15721605
provenEpochCount += (startingPeriod_deadline - fromEpoch);
15731606
}
15741607

1575-
// now loop through the proving periods between endingPeriod and startingPeriod.
1576-
for(uint256 period = startingPeriod + 1; period <= endingPeriod - 1; period++){
1577-
if(_isPeriodProven(dataSetId, period)){
1608+
// now loop through the proving periods between endingPeriod and startingPeriod.
1609+
for (uint256 period = startingPeriod + 1; period <= endingPeriod - 1; period++) {
1610+
if (_isPeriodProven(dataSetId, period)) {
15781611
provenEpochCount += maxProvingPeriod;
15791612
lastProvenEpoch = _calcPeriodDeadline(dataSetId, period);
15801613
}
15811614
}
15821615

15831616
// now handle the last period separately
1584-
if(_isPeriodProven(dataSetId, endingPeriod)){
1617+
if (_isPeriodProven(dataSetId, endingPeriod)) {
15851618
// then the epochs to add = `endingPeriod_starting` to `toEpoch`. But since `endingPeriod_starting` is simply the ending of its previous period, so
15861619
provenEpochCount += (toEpoch - _calcPeriodDeadline(dataSetId, endingPeriod - 1));
15871620
lastProvenEpoch = toEpoch;
15881621
}
15891622
}
15901623
}
15911624

1592-
// If no epochs are proven, we can't settle anything
1593-
if (provenEpochCount == 0) {
1594-
return ValidationResult({
1595-
modifiedAmount: 0,
1596-
settleUpto: fromEpoch,
1597-
note: "No proven epochs in the requested range"
1598-
});
1599-
}
1600-
1601-
// Calculate the modified amount based on proven epochs
1602-
uint256 modifiedAmount = (proposedAmount * provenEpochCount) / totalEpochsRequested;
1603-
1604-
// Calculate how many epochs were not proven (faulted)
1605-
uint256 faultedEpochs = totalEpochsRequested - provenEpochCount;
1606-
1607-
// Emit event for logging
1608-
emit PaymentArbitrated(railId, dataSetId, proposedAmount, modifiedAmount, faultedEpochs);
1609-
1610-
return ValidationResult({
1611-
modifiedAmount: modifiedAmount,
1612-
settleUpto: lastProvenEpoch, // Settle up to the last proven epoch
1613-
note: ""
1614-
});
1625+
return (provenEpochCount, lastProvenEpoch);
16151626
}
16161627

1617-
function _isPeriodProven(uint256 dataSetId, uint256 periodId) private view returns(bool){
1628+
function _isPeriodProven(uint256 dataSetId, uint256 periodId) private view returns (bool) {
16181629
uint256 currentPeriod = getProvingPeriodForEpoch(dataSetId, block.number);
1619-
if(periodId == currentPeriod){
1630+
if (periodId == currentPeriod) {
16201631
return provenThisPeriod[dataSetId];
1621-
}
1632+
}
16221633
return provenPeriods[dataSetId][periodId];
16231634
}
16241635

1625-
function _calcPeriodDeadline(uint256 dataSetId, uint256 periodId) private view returns(uint256){
1626-
return provingActivationEpoch[dataSetId] + ((periodId+1) * maxProvingPeriod); // we need to do `periodId + 1` since it starts from 0
1636+
function _calcPeriodDeadline(uint256 dataSetId, uint256 periodId) private view returns (uint256) {
1637+
return provingActivationEpoch[dataSetId] + ((periodId + 1) * maxProvingPeriod); // we need to do `periodId + 1` since it starts from 0
16271638
}
16281639

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

0 commit comments

Comments
 (0)