Core Contracts

Regnum Aurum Acquisition Corp
HardhatReal World AssetsNFT
77,280 USDC
View results
Submission Details
Severity: medium
Invalid

[M-4] Redundant Logic and Inefficiency in `_updateWeights`

Description:
The _updateWeights function is designed to update the weights for a time-weighted average calculation by either creating a new period or updating an existing one. However, despite checking if weightPeriod.startTime == 0, both branches of the conditional statement execute the same logic, differing only in the initial period start time calculation.

This redundancy results in unnecessary computation, increased gas costs, and confusion regarding the function's intended behavior. Additionally, the function does not differentiate between the initialization of a new period and the update of an existing one, which may lead to incorrect weight calculations.

Impact:
The presence of an ineffective conditional structure makes the function inefficient and misleading:

  • Unnecessary gas consumption: due to redundant calculations.

  • Lack of distinction between the initial and subsequent periods: making the conditional check unnecessary.

  • Potential miscalculations in weight updates: as the same formula is applied regardless of whether it is the first or subsequent update.

Proof of Concept:
The function's logic results in identical operations regardless of whether it's the first or a subsequent update:

function _updateWeights(uint256 newWeight) internal {
uint256 currentTime = block.timestamp; // Fetch current block timestamp
uint256 duration = getPeriodDuration(); // Fetch duration (7 days)
if (weightPeriod.startTime == 0) { // Check if it's the initial period
uint256 nextPeriodStart = ((currentTime / duration) + 1) * duration; // Calculate next period start time
TimeWeightedAverage.createPeriod(
weightPeriod,
nextPeriodStart, // Assign calculated start time
duration, // Assign duration (7 days)
newWeight, // Assign new weight
WEIGHT_PRECISION // Assign precision constant
);
} else {
// This else block executes the same logic, making the if-check pointless
uint256 nextPeriodStart = ((currentTime / duration) + 1) * duration; // Again, calculate next period start
TimeWeightedAverage.createPeriod(
weightPeriod,
nextPeriodStart, // Same value as before
duration, // Same duration as before
newWeight, // Same newWeight
WEIGHT_PRECISION // Same precision
);
}
}

Despite the if condition checking whether it is the initial period, both branches of the statement calculate nextPeriodStart in the same manner and pass identical values to createPeriod. This means the conditional structure does not impact the function's behavior, rendering it redundant.

Recommended Mitigation:
The formula used in the else condition should be reevaluated to ensure the function properly differentiates between the initialization of a new period and the update of an existing one. The logic should be adjusted to correctly apply weight updates for subsequent periods while avoiding unnecessary computations.

Tools used:\Manual Review

Updates

Lead Judging Commences

inallhonesty Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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