Removal of last token in the array can be optimized #46
Labels
bug
Warden finding
G (Gas Optimization)
sponsor acknowledged
Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Handle
0xRajeev
Vulnerability details
Impact
The removal of the last token in the array can be optimized by checking if index == k-1, i.e. last element match, when there is no need to copy and simply popping will do. This will avoid the copy which involves several SLOADs/SSTOREs.
Proof of Concept
https://github.com/code-423n4/2021-09-yaxis/blob/cf7d9448e70b5c1163a1773adb4709d9d6ad6c99/contracts/v3/Manager.sol#L485
https://github.com/code-423n4/2021-09-yaxis/blob/cf7d9448e70b5c1163a1773adb4709d9d6ad6c99/contracts/v3/Harvester.sol#L141
Tools Used
Manual Analysis
Recommended Mitigation Steps
Example: Check if index != k-1 for tokens[_vault][index] = tokens[_vault][k-1];
The text was updated successfully, but these errors were encountered: