re-al-Foundation / rwa-contracts

0 stars 0 forks source link

[CCM-06C] Significantly Inefficient Caching System #77

Closed chasebrownn closed 5 months ago

chasebrownn commented 5 months ago

CCM-06C: Significantly Inefficient Caching System

Type Severity Location
Gas Optimization CrossChainMigrator.sol:L76, L273-L278, L280, L282, L284, L288, L292, L296, L534-L539

Description:

The CrossChainMigrator::migrateNFTBatch function utilizes a contract-level lCache to circumvent the stack-too-deep error which significantly increases the function's gas footprint.

Example:

(lCache.startTime,
lCache.endTime,
lCache.lockedAmount,
lCache.multiplier,
lCache.claimed,
lCache.maxPayout) = passiveIncomeNFT.locks(_tokenIds[i]);

uint8 durationInMonths = uint8((lCache.endTime - lCache.startTime) / 30 days);

if (lCache.multiplier != piCalculator.determineMultiplier(BOOST_START, BOOST_END, lCache.startTime, durationInMonths)) {
    // if early claim, just mint them remaining in `maxPayout`.
    lockedAmounts[i] = lCache.maxPayout;
}
else {
    // otherwise, just calculate amount to mint/lock as normal.
    lockedAmounts[i] = lCache.lockedAmount + ((lCache.lockedAmount * (lCache.multiplier - 1e18)) / 1e18) - lCache.claimed;
}

// if lock is expired -> just mint them RWA tokens
if (block.timestamp >= lCache.endTime) {
    revert ExpiredNFT(_tokenIds[i]);
}

durations[i] = lCache.endTime - block.timestamp;

if (durations[i] > MAX_DURATION) {
    durations[i] = MAX_DURATION;
}

emit MigrationMessageSent_PINFT(
    msg.sender,
    _tokenIds[i],
    lockedAmounts[i],
    SafeCast.toUint208(lockedAmounts[i].calculateVotingPower(durations[i]))
);
_clearLockCache();

Recommendation:

We advise an in-memory struct to be utilized instead, as the stack-too-deep error does not relate to the memory needs of the function but rather the number of variables.

chasebrownn commented 5 months ago

Resolved