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 optimization: Struct layout #30

Open
code423n4 opened this issue Oct 23, 2021 · 0 comments
Open

Gas optimization: Struct layout #30

code423n4 opened this issue Oct 23, 2021 · 0 comments
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

gzeon

Vulnerability details

Impact

Detailed description of the impact of this finding.

Proof of Concept

https://github.com/code-423n4/2021-10-mochi/blob/main/projects/mochi-core/contracts/interfaces/IMochiVault.sol
L6-L12: the struct can be reordered into
struct Detail {
Status status;
address referrer;
uint256 collateral;
uint256 debt;
uint256 debtIndex;
}

such that status and referrer are put into the same slot, should save ~2000 gas per borrow

Tools Used

None

Recommended Mitigation Steps

Reorder the struct as suggested, and modify impacted code at
IMochiVault.sol L28-L34
DutchAuctionLiquidator.sol L77

Extra

Realistically, the range of debtIndex (start at 1e18 and increase by fee per year) probably fit in a uint88(11bytes) so you can pack (status(1byte), referrer(20bytes), debtIndex(11bytes)) all in 32 bytes, saving another storage slot.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Oct 23, 2021
code423n4 added a commit that referenced this issue Oct 23, 2021
@r2moon r2moon added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Oct 27, 2021
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 confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

2 participants