Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: low
Invalid

Simplify if-else statements to save gas and improve readability

Summary

Any code is written once, but read a lot of times. so, we need to ensure that our code readability is very high such that anyone can easily understand our code.

Apart from it, We need to keep our code simple such that it consumes less gas. Its ideal to removed code thats used multiple times or code thats not called any time to simplify gas costs.

Vulnerability Details

Place below code in ChoosingRam.sol

Code
function increaseValuesOfParticipants_Simplified(uint256 tokenIdOfChallenger, uint256 tokenIdOfAnyParticipant)
public
RamIsNotSelected
{
if (tokenIdOfChallenger > ramNFT.tokenCounter()) {
revert ChoosingRam__InvalidTokenIdOfChallenger();
}
if (tokenIdOfAnyParticipant > ramNFT.tokenCounter()) {
revert ChoosingRam__InvalidTokenIdOfPerticipent();
}
if (ramNFT.getCharacteristics(tokenIdOfChallenger).ram != msg.sender) {
revert ChoosingRam__CallerIsNotChallenger();
}
if (block.timestamp > 1728691200) {
revert ChoosingRam__TimeToBeLikeRamFinish();
}
uint256 random = uint256(keccak256(abi.encodePacked(block.timestamp, block.prevrandao, msg.sender))) % 2;
uint256 tokenId = random == 0 ? tokenIdOfChallenger : tokenIdOfAnyParticipant;
RamNFT.CharacteristicsOfRam memory characteristics = ramNFT.getCharacteristics(tokenId);
if (!characteristics.isJitaKrodhah) {
ramNFT.updateCharacteristics(tokenId, true, false, false, false, false);
} else if (!characteristics.isDhyutimaan) {
ramNFT.updateCharacteristics(tokenId, true, true, false, false, false);
} else if (!characteristics.isVidvaan) {
ramNFT.updateCharacteristics(tokenId, true, true, true, false, false);
} else if (!characteristics.isAatmavan) {
ramNFT.updateCharacteristics(tokenId, true, true, true, true, false);
} else if (!characteristics.isSatyavaakyah) {
ramNFT.updateCharacteristics(tokenId, true, true, true, true, true);
selectedRam = characteristics.ram;
}
}

Place below code in Dussehra.t.sol and run - forge test --mt test__gasCostSimplified

code
function test__gasCostSimplified() public participants {
vm.txGasPrice(1);
uint256 gasStart = gasleft();
vm.startPrank(player1);
choosingRam.increaseValuesOfParticipants(0, 1);
uint256 gasEnd = gasleft();
uint256 gasUsedFirst = (gasStart - gasEnd) * tx.gasprice;
console.log("Gas cost of complex if-else statements", gasUsedFirst);
uint256 gasStartCustom = gasleft();
vm.startPrank(player2);
choosingRam.increaseValuesOfParticipants_Simplified(1, 0);
uint256 gasEndCustom = gasleft();
uint256 gasUsedCustom = (gasStartCustom - gasEndCustom) * tx.gasprice;
console.log("Gas cost of simplified if-else", gasUsedCustom);
assert(gasUsedFirst > gasUsedCustom);
}

Impact

Save gas cost and improve readability

Tools Used

Recommendations

Make below code changes in ChoossingRam.sol

Code
function increaseValuesOfParticipants(uint256 tokenIdOfChallenger, uint256 tokenIdOfAnyParticipant)
public
RamIsNotSelected
{
...
+ uint256 tokenId = random == 0 ? tokenIdOfChallenger : tokenIdOfAnyParticipant;
+ RamNFT.CharacteristicsOfRam memory characteristics = ramNFT.getCharacteristics(tokenId);
- if (random == 0) {
- if (ramNFT.getCharacteristics(tokenIdOfChallenger).isJitaKrodhah == false) {
- ramNFT.updateCharacteristics(tokenIdOfChallenger, true, false, false, false, false);
- } else if (ramNFT.getCharacteristics(tokenIdOfChallenger).isDhyutimaan == false) {
- ramNFT.updateCharacteristics(tokenIdOfChallenger, true, true, false, false, false);
- } else if (ramNFT.getCharacteristics(tokenIdOfChallenger).isVidvaan == false) {
- ramNFT.updateCharacteristics(tokenIdOfChallenger, true, true, true, false, false);
- } else if (ramNFT.getCharacteristics(tokenIdOfChallenger).isAatmavan == false) {
- ramNFT.updateCharacteristics(tokenIdOfChallenger, true, true, true, true, false);
- } else if (ramNFT.getCharacteristics(tokenIdOfChallenger).isSatyavaakyah == false) {
- ramNFT.updateCharacteristics(tokenIdOfChallenger, true, true, true, true, true);
- selectedRam = ramNFT.getCharacteristics(tokenIdOfChallenger).ram;
- }
- } else {
- if (ramNFT.getCharacteristics(tokenIdOfAnyPerticipent).isJitaKrodhah == false) {
- ramNFT.updateCharacteristics(tokenIdOfAnyPerticipent, true, false, false, false, false);
- } else if (ramNFT.getCharacteristics(tokenIdOfAnyPerticipent).isDhyutimaan == false) {
- ramNFT.updateCharacteristics(tokenIdOfAnyPerticipent, true, true, false, false, false);
- } else if (ramNFT.getCharacteristics(tokenIdOfAnyPerticipent).isVidvaan == false) {
- ramNFT.updateCharacteristics(tokenIdOfAnyPerticipent, true, true, true, false, false);
- } else if (ramNFT.getCharacteristics(tokenIdOfAnyPerticipent).isAatmavan == false) {
- ramNFT.updateCharacteristics(tokenIdOfAnyPerticipent, true, true, true, true, false);
- } else if (ramNFT.getCharacteristics(tokenIdOfAnyPerticipent).isSatyavaakyah == false) {
- ramNFT.updateCharacteristics(tokenIdOfAnyPerticipent, true, true, true, true, true);
- selectedRam = ramNFT.getCharacteristics(tokenIdOfAnyPerticipent).ram;
- }
- }
+ if (!characteristics.isJitaKrodhah) {
+ ramNFT.updateCharacteristics(tokenId, true, false, false, false, false);
+ } else if (!characteristics.isDhyutimaan) {
+ ramNFT.updateCharacteristics(tokenId, true, true, false, false, false);
+ } else if (!characteristics.isVidvaan) {
+ ramNFT.updateCharacteristics(tokenId, true, true, true, false, false);
+ } else if (!characteristics.isAatmavan) {
+ ramNFT.updateCharacteristics(tokenId, true, true, true, true, false);
+ } else if (!characteristics.isSatyavaakyah) {
+ ramNFT.updateCharacteristics(tokenId, true, true, true, true, true);
+ selectedRam = characteristics.ram;
+ }
+ }

Updates

Lead Judging Commences

bube Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Info/Gas/Invalid according to docs

Support

FAQs

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