MorpheusAI

MorpheusAI
Foundry
22,500 USDC
View results
Submission Details
Severity: low
Invalid

Optimize gas consumption in library by pre-computing and using timePassed* variables

Summary

Same pieces of data are calculated multiple times throughout calculate*Reward functions. Passing this data pre-computed would optimize gas cost and provide better readability

Vulnerability Details

After validating startTime and endTime, the variable payoutStart_ is repeatedly used to compute a time difference: either startTime_ - payoutStart_ (which was already saved as a variable at line 48), or endTime_ - payoutStart_ (which can be saved in a variable named timePassedTotal_). These could be pre-calculated in the getPeriodReward function and passed to the calculate*Reward functions. The following changes are needed:

function _calculatePartPeriodReward:

  • line 102: Change signature to replace first two parameters with the timePassed_ parameter, which is equal to the difference between them (either startTime or endTime minus payoutStart). This would also solve a naming inconsistency: function signature currently contains "startTime" while line 75 provides "endTime" as argument, which is confusing. Function would change as following:
    before: function calculatePartPeriodReward(uint128 payoutStart, uint128 startTime_, uint128 interval_, uint256 initialAmount_, uint256 decreaseAmount_, bool toEnd_ ) private pure returns (uint256)
    after: function calculatePartPeriodReward(uint128 timePassed, uint128 interval_, uint256 initialAmount_, uint256 decreaseAmount_, bool toEnd_ ) private pure returns (uint256)

  • line 110: before: uint256 intervalsPassed_ = (startTime_ - payoutStart_) / interval_;
    after: uint256 intervalsPassed_ = timePassed_ / interval_;

  • line 119: before: intervalPart_ = interval_ * (intervalsPassed_ + 1) + payoutStart_ - startTime_;
    after: intervalPart_ = interval_ * (intervalsPassed_ + 1) - timePassed_;

  • line 121: before: intervalPart_ = startTime_ - interval_ * intervalsPassed_ - payoutStart_;
    after: intervalPart_ = timePassed_ - interval_ * intervalsPassed_;

function _calculateFullPeriodReward:

  • line 131: Change signature to replace first three parameters with timePassedBefore_ (defined on line 48) and timePassedTotal_ (endTime_ - payoutStart_):
    before: function calculateFullPeriodReward(uint128 payoutStart, uint128 startTime_, uint128 endTime_, uint128 interval_, uint256 initialAmount_, uint256 decreaseAmount_ ) private pure returns (uint256)
    after: function calculateFullPeriodReward(uint128 timePassedBefore, uint128 timePassedTotal_, uint128 interval_, uint256 initialAmount_, uint256 decreaseAmount_ ) private pure returns (uint256)

  • line 140: not needed anymore

  • line 153: before: uint256 ip_ = ((endTime_ - payoutStart_ - intervalsPassedBefore_ * interval_) / interval_);
    after: uint256 ip_ = ((timePassedTotal_ - intervalsPassedBefore_ * interval_) / interval_);
    If taking into account a defect that I submitted in parallel, this can be further reduced to:
    after 2: uint256 ip_ = timePassedTotal_ / interval_ - intervalsPassedBefore_;

Impact

Gas used inefficiently, cluttered code

Tools Used

Manual inspection

Recommendations

Apply changes mentioned above for an optimized gas consumption and better readability

Updates

Lead Judging Commences

inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.