The rewards of a user are tracked in a mapping of MysteryBox::rewardsOwned which maps an address to an array of MysteryBox::Reward. When a user transfers a reward to another address by calling the MysteryBox::transferReward function, an index is specified in the function and this MysteryBox::Reward is deleted from the array. However, there is no resizing of the array. Therefore this deleted MysteryBox::Reward remains in the array and will unecessarily bloat gas costs when retrieving the array, as in MysteryBox::claimAllRewards.
In the MysteryBox:transferReward function below, a specific index of the rewardsOwned array is deleted and there is no resizing or reordering of the array once this deletion has occurred.
As a result, gaps will be left in the array once a player transfers their rewards to another address. These gaps 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 be retrieved whenever the RewardsOwned is used. One such example is in the MysteryBox::claimAllRewards function.
Adding the following code into the TestMysteryBox.t.sol file illustrates the preservation of deleted data in the rewards array of players:
The output to the console was the following:
This verifies that the reward at index 1 has been transferred, but an empty reward struct remains in its place.
Players will face increased gas costs for users whenever the rewardsOwned array is retrieved or iterated over, as in the MysteryBox::claimAllRewards function, as deleted useless data is preserved.
Manual review
In the MysteryBox::transferReward function, the last element can be moved into the index to be deleted and the array can be resized to be one element less than its current length. However, if the _index parameter is the last element then we can simply resize. This is demonstrated 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.