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 Optimizations #68

Open
code423n4 opened this issue Feb 23, 2022 · 1 comment
Open

Gas Optimizations #68

code423n4 opened this issue Feb 23, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue

Comments

@code423n4
Copy link
Contributor

1. Remove Unuse Variable __gap to Save Gas

Impact

uint256[50] private __gap;

Variable __gap was not used in VUSD.sol and some other contracts. They will increase the size of deployment with no real benefit

Proof of Concept

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/VUSD.sol#L30

Recommended Mitigation Steps

Remove Line 30 in VUSD.sol.

2. Variable "name" - Use bytes32 Rather Than String

Impact

string public name;

If data can fit into 32 bytes, then you should use bytes32 datatype rather than bytes or strings as it is much cheaper in solidity. Basically, Any fixed size variable in solidity is cheaper than variable size. That will save gas on the contract.

Proof of Concept

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L28

Recommended Mitigation Steps

bytes32 public name;

3. Long Revert Strings are Waste of Gas

Impact

Shortening revert strings to fit in 32 bytes will decrease deployment time gas and will decrease runtime gas when the revert condition has been met.

Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.

Proof of Concept

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L487
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/AMM.sol#L511
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L84
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/ClearingHouse.sol#L101
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L174
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L354
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L453
https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/VUSD.sol#L55

Recommended Mitigation Steps

Shorten the revert strings to fit in 32 bytes.

Or consider using Custom Errors (solc >=0.8.4).

4. Variable Default Value is 0 and thus Initialized to 0 is Waste of Gas

Impact

uint constant VUSD_IDX = 0;

The local variable need not be initialized to 0 because the default value is 0. Avoiding this anti-pattern can save a few opcodes and therefore a tiny bit of gas.

Proof of Concept

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/MarginAccount.sol#L31

Recommended Mitigation Steps

Remove explicit 0 initialization.

uint constant VUSD_IDX;

5. Constructor Does Not Check for Zero Addresses

Impact

A wrong user input or wallets defaulting to the zero addresses for a missing input can lead to the contract needing to redeploy or wasted gas.

Proof of Concept

https://github.com/code-423n4/2022-02-hubble/blob/main/contracts/Registry.sol#L12-L23

Recommended Mitigation Steps

Requires Addresses is not zero.

require(_oracle != address(0), "Address Can't Be Zero")
require(_clearingHouse != address(0), "Address Can't Be Zero")
require(_insuranceFund != address(0), "Address Can't Be Zero")
require(_marginAccount != address(0), "Address Can't Be Zero")
require(_vusd != address(0), "Address Can't Be Zero")

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Feb 23, 2022
code423n4 added a commit that referenced this issue Feb 23, 2022
@atvanguard atvanguard added the sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue label Feb 26, 2022
@moose-code
Copy link
Collaborator

uint256[50] private __gap is common for smart contract upgrades to keep storage bits grouped logically together.

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 disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
Projects
None yet
Development

No branches or pull requests

3 participants