Skip to content

perf(validatePayment): loop over proving periods instead of epochs #252

@wjmelements

Description

@wjmelements

When we settle a Payment segment, FilecoinWarmStorageService.validatePayment loops over epochs like this:

        // Check each epoch in the range
        for (uint256 epoch = fromEpoch + 1; epoch <= toEpoch; epoch++) {
            bool isProven = isEpochProven(dataSetId, epoch);

            if (isProven) {
                provenEpochCount++;
                lastProvenEpoch = epoch;
            }
        }

isEpochProven ultimately does

        // Get the period this epoch belongs to
        uint256 periodId = getProvingPeriodForEpoch(dataSetId, epoch);

        // Special case: current ongoing proving period
        uint256 currentPeriod = getProvingPeriodForEpoch(dataSetId, block.number);
        if (periodId == currentPeriod) {
            // For the current period, check if it has been proven already
            return provenThisPeriod[dataSetId];
        }

        // For past periods, check the provenPeriods mapping
        return provenPeriods[dataSetId][periodId];

getProvingPeriodForEpoch calculates periodId this way:

        return (epoch - activationEpoch) / maxProvingPeriod;

So the top level loop can be optimized by looping over proving periods instead of over epochs. The first and last epochs might need special handling, but for the other proving periods, can count like provenEpochCount += maxProvingPeriod.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

🎉 Done

Relationships

None yet

Development

No branches or pull requests

Issue actions