Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: medium
Invalid

Gas Saving Tips: Use a modifier to save gas

Summary:

Gas Saving Tips: Use a modifier to save gas.

Vulnerability Details:

Currently, a require() function is used to restrict access to the setPassword() and getPassword() functions to only the owner of the contract.

Impact:

A check is done any time either of setPassword() and getPassword() functions are called, and these checks would use extra gas.

Tools Used:

Replit IDE, Foundry, Remix, PhindAI

Recommendations:

A onlyOwner modifier should be created and used to restrict access to the setPassword() and getPassword() functions. This modifier checks if the sender is the owner of the contract and throws an error if they are not. This saves gas because the check is done only once per function call, instead of being done in each function.

// create modifier
modifier onlyOwner() {
if(msg.sender != s_owner) {
revert PasswordStore__NotOwner();
}
_;
}
// implement modifier in setPassword function
function setPassword(string memory newPassword) external onlyOwner {
s_password = newPassword;
emit SetPassword(msg.sender, newPassword);
}
// implement modifier in getPassword function
function getPassword() external view onlyOwner returns (string memory) {
return s_password;
}
Updates

Lead Judging Commences

inallhonesty Lead Judge
about 2 years ago
inallhonesty Lead Judge about 2 years ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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