51100 sc insight gas inefficiency in prize removal logic
Submitted on Jul 31st 2025 at 06:44:21 UTC by @AasifUsmani for Attackathon | Plume Network
Report ID: #51100
Report Type: Smart Contract
Report severity: Insight
Target: https://github.com/immunefi-team/attackathon-plume-network/blob/main/plume/src/spin/Raffle.sol
Impacts: (see Impact Details)
Description
Brief/Intro
The removePrize function in the Raffle contract removes a prize ID from the prizeIds array by searching for the ID, overwriting it with the last element, and then popping the array. However, if the prize ID to remove is already the last element, the function still performs an unnecessary assignment and loop iteration. This results in avoidable gas costs, especially as the array grows.
Vulnerability Details
When removing a prize, the function always loops through the entire prizeIds array to find the target ID, even if the ID is already the last element. Once found, it overwrites the element with itself (if it is the last) and pops the array. This redundant assignment and looping wastes gas, especially as the array grows. The logic can be optimized by checking if the prize ID is already the last element and simply calling pop() directly.
Impact Details
Unnecessary gas usage: Redundant assignment and looping increase transaction costs, especially for large arrays.
Scalability: As the number of prizes grows, the inefficiency becomes more pronounced, leading to higher operational costs for the protocol.
Recommendations
Add a check to see if the prizeId being removed is already the last element; if so, call pop() directly. Example suggested implementation:
function removePrize(
uint256 prizeId
) external onlyRole(ADMIN_ROLE) prizeIsActive(prizeId) {
prizes[prizeId].isActive = false;
uint256 len = prizeIds.length;
if (prizeId == prizeIds[len - 1]) {
prizeIds.pop();
} else {
for (uint256 i = 0; i < len; i++) {
if (prizeIds[i] == prizeId) {
prizeIds[i] = prizeIds[len - 1];
prizeIds.pop();
break;
}
}
}
emit PrizeRemoved(prizeId);
}References
https://github.com/immunefi-team/attackathon-plume-network/blob/580cc6d61b08a728bd98f11b9a2140b84f41c802/plume/src/spin/Raffle.sol#L189
Proof of Concept
Was this helpful?