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.
CCM-06C: Significantly Inefficient Caching System
Description:
The
CrossChainMigrator::migrateNFTBatch
function utilizes a contract-levellCache
to circumvent the stack-too-deep error which significantly increases the function's gas footprint.Example:
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.