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

Using long strings in require statements, will result in more gas

Summary

from solidity 0.8.4, we can use custom errors instead of long strings in require statements as custom errors takes less gas than the long strings in require statements.

Vulnerability Details

Proof of code to show the difference in gas cost is as follows:

Place below code in ChoosingRam.sol

code
error ChoosingRam__RamIsSelected();
error ChoosingRam__NotOrganiser();
modifier RamIsNotSelectedCustom() {
if (!isRamSelected) {
revert ChoosingRam__RamIsSelected();
}
_;
}
modifier OnlyOrganiserCustom() {
if (ramNFT.organiser() == msg.sender) {
revert ChoosingRam__NotOrganiser();
}
_;
}
function selectRamIfNotSelectedCustom() public RamIsNotSelectedCustom OnlyOrganiserCustom {
if (block.timestamp < 1728691200) {
revert ChoosingRam__TimeToBeLikeRamIsNotFinish();
}
if (block.timestamp > 1728777600) {
revert ChoosingRam__EventIsFinished();
}
uint256 random = uint256(keccak256(abi.encodePacked(block.timestamp, block.prevrandao))) % ramNFT.tokenCounter();
selectedRam = ramNFT.getCharacteristics(random).ram;
isRamSelected = true;
}

Place below code in Dussehra.t.sol and run the command - forge test --mt test__GasCostIsLowForCustomErrors -vvv

code
function test__GasCostIsLowForCustomErrors() public {
vm.txGasPrice(1);
uint256 gasStart = gasleft();
vm.expectRevert();
choosingRam.selectRamIfNotSelected();
uint256 gasEnd = gasleft();
uint256 gasUsedFirst = (gasStart - gasEnd) * tx.gasprice;
console.log("Gas cost of the long string errors", gasUsedFirst);
uint256 gasStartCustom = gasleft();
vm.expectRevert();
choosingRam.selectRamIfNotSelectedCustom();
uint256 gasEndCustom = gasleft();
uint256 gasUsedCustom = (gasStartCustom - gasEndCustom) * tx.gasprice;
console.log("Gas cost of custom errors", gasUsedCustom);
assert(gasUsedFirst > gasUsedCustom);
}

Impact

Increase in Gas cost

Tools Used

Foundry

Recommendations

Make below code changes in ChoosingRam.sol

code
+ error ChoosingRam__RamIsSelected();
+ error ChoosingRam__NotOrganiser();
modifier RamIsNotSelected() {
- require(!isRamSelected, "Ram is selected!");
+ if(!isRamSelected){
+ revert ChoosingRam__RamIsSelected();
+ }
_;
}
modifier OnlyOrganiser() {
- require(ramNFT.organiser() == msg.sender, "Only organiser can call this function!");
+ if(ramNFT.organiser() == msg.sender){
+ revert ChoosingRam__NotOrganiser();
+ }
_;
}
Updates

Lead Judging Commences

bube Lead Judge about 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.