Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas in FlashGovernanceArbiter.assertGovernanceApproved(): flashGovernanceConfig.asset and flashGovernanceConfig.amount should get cached #336

Open
code423n4 opened this issue Feb 2, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons

Comments

@code423n4
Copy link
Contributor

Handle

Dravee

Vulnerability details

Impact

SLOADs are expensive (~100 gas) compared to MLOADs/MSTOREs (~3 gas). Minimizing them can save gas.

Proof of Concept

The code is as such (see @Audit tags):

File: FlashGovernanceArbiter.sol
60:   function assertGovernanceApproved(
61:     address sender,
62:     address target,
63:     bool emergency
64:   ) public {
65:     if (
66:       pendingFlashDecision[target][sender].unlockTime < block.timestamp && 
67:       IERC20(flashGovernanceConfig.asset).transferFrom(sender, address(this), flashGovernanceConfig.amount) //@audit flashGovernanceConfig.amount SLOAD 1 //@audit flashGovernanceConfig.asset SLOAD 1
68:     ) {
69:       require(
70:         emergency || (block.timestamp - security.lastFlashGovernanceAct > security.epochSize), 
71:         "Limbo: flash governance disabled for rest of epoch"
72:       );
73:       pendingFlashDecision[target][sender] = flashGovernanceConfig;
74:       pendingFlashDecision[target][sender].unlockTime += block.timestamp;
75: 
76:       security.lastFlashGovernanceAct = block.timestamp;
77:       emit flashDecision(sender, flashGovernanceConfig.asset, flashGovernanceConfig.amount, target); //@audit flashGovernanceConfig.amount SLOAD 2 //@audit flashGovernanceConfig.asset SLOAD 2
78:     } else {
79:       revert("LIMBO: governance decision rejected.");
80:     }
81:   }

It's possible to save 2 SLOAD (~200 gas) by caching flashGovernanceConfig.asset in a address variable, and flashGovernanceConfig.amount in a uint256 memory variables and use them instead of reading them repeatedly from storage.

Tools Used

VS Code

Recommended Mitigation Steps

Cache the storage values in memory variables and use them instead of repeatedly reading them from storage.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Feb 2, 2022
code423n4 added a commit that referenced this issue Feb 2, 2022
@gititGoro gititGoro added the sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons label Feb 5, 2022
@jack-the-pug
Copy link
Collaborator

Making this the main issue for all: "Cache the storage values in memory" Gas Optimization

@jack-the-pug jack-the-pug added the duplicate This issue or pull request already exists label Feb 16, 2022
@jack-the-pug jack-the-pug removed the duplicate This issue or pull request already exists label Mar 1, 2022
@CloudEllie CloudEllie reopened this Mar 3, 2022
@gititGoro gititGoro added unresolved indicate confirmed issues that haven't been resolved with a PR and removed unresolved indicate confirmed issues that haven't been resolved with a PR labels May 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Projects
None yet
Development

No branches or pull requests

4 participants