The rewards of a user are tracked in the mapping MysteryBox::rewardsOwned, which maps an address to an array of MysteryBox::Reward. When a player claims a reward by calling the MysteryBox::claimSingleReward function, an index is specified in the function and this MysteryBox::Reward is deleted from the array.
However, there is no resizing or reordering of the array. Therefore, this deleted MysteryBox::Reward remains in the array and will unnecessarily bloat gas costs when the array is retrieved or iterated through, as in MysteryBox::claimAllRewards.
As can be seen in the MysteryBox::claimSingleReward function, a specific index of the Rewards array is deleted and there is no resizing or reordering of the array once this deletion has occurred.
As a result, there will be gaps left in the array once a player claims their specific reward. These gaps in the array will be of type MysteryBox::Reward and have the values name: "" and value: 0. These empty reward structs remain in the same index in the array and will add to gas costs whenever it is retrieved or iterated through.
Adding the following code into the TestMysteryBox.t.sol file illustrates the preservation of deleted data in the rewards array of players:
The output of the console was the following:
This verifies that the reward at index 1 has been claimed, but an empty reward struct remains in its place.
This will increase gas costs for users whenever the rewards array of player has to be retrieved or iterated over.
Manual review
In the MysteryBox::claimSingleReward function, instead of deleting the index of the reward that was claimed, the last element in the reward array can be moved to this index and the array can be resized to its current length less one.
This ensures that the redundant data is deleted and only non-claimed rewards are stored. Additionally, if the _index of the reward that is being claimed is in fact the last element we can just immediately resize the rewards array.
This logic is shown below:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.