code-423n4 / 2022-05-velodrome-findings

0 stars 0 forks source link

Gas Optimizations #191

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1. Title: Using != is more gas efficient

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L614 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L772-L774

Recommended Mitigation Steps: Change to !=

    require(_value != 0);

========================================================================

2. Title: unnecessary variable set. the default value of uint is 0

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Velo.sol#L9 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L584-L585 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L622

Recommended Mitigation Steps: remove 0 value

========================================================================

3. Title: Using storage to declare Struct variable inside function

Proof of Concept: https://github.com/code-423n4/2022-05-rubicon/blob/main/contracts/rubiconPools/BathPair.sol#L299 https://github.com/code-423n4/2022-05-rubicon/blob/main/contracts/rubiconPools/BathPair.sol#L214 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L582-L583

Recommended Mitigation Steps:

    LockedBalance storage _locked = locked[_tokenId];

========================================================================

4. Title: Using multiple require instead && can save gas

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L307 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L846

Recommended Mitigation Steps:

        require(attachments[_tokenId] == 0, "attached");
    require(!voted[_tokenId], "attached");

========================================================================

5. Title: Using delete statement can save gas

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L484-L486 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L498-L500

Recommended Mitigation Steps:

    delete ownerToNFTokenIdList[_from][current_count];

========================================================================

6. Title: Using calldata to store struct data type can save gas

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L579-L580

Recommended Mitigation Steps:

    function _checkpoint(
        uint _tokenId,
        LockedBalance calldata old_locked,
        LockedBalance calldata new_locked
    ) internal {

========================================================================

7. Title: Cheaper to use ++ instead + 1

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L705 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L1076

Recommended Mitigation Steps:

    uint user_epoch = ++user_point_epoch[_tokenId];

========================================================================

8. Title: Use supply_before that already been cache

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L730 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L854

Recommended Mitigation Steps:

supply_before += _value;

========================================================================

9. Title: Using msg.sender directly is more effective

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L746

Recommended Mitigation Steps: Using msg.sender directly instead of caching it to from. delete L#746 and replace all from with msg.sender

========================================================================

10. Title: Using > is cheaper than >=

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L849

Recommended Mitigation Steps: 1 second difference can be ignored to validate. using > operator can save gas

    require(block.timestamp >= _locked.end, "The lock didn't expire");

========================================================================

11. Title: Gas opt to substract

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L895 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L951 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L1081

Recommended Mitigation Steps: Change to:

    _max = --_mid;

========================================================================

12. Title: Caching .length for loop can save gas

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L1146

Recommended Mitigation Steps: Change to:

    uint256 Length = _tokenIds.length;
    for (uint i = 0; i < Length; i++) {

========================================================================

13. Title: Using += to increase value on var

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L1148

Recommended Mitigation Steps: Change to:

    votes += _balanceOfNFT(tId, block.timestamp);

========================================================================

14. Title: Gas improvement on returning lower value

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L1153

Recommended Mitigation Steps: by setting lower in returns and deleting L#1168 can save gas

function getPastVotesIndex(address account, uint timestamp) public view returns (uint32 lower) { //@audit-info: set here

========================================================================

15. Title: Using unchecked and prefix increment

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L1225 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L1249

Recommended Mitigation Steps:

    for (uint i = 0; i < srcRepOld.length;) {
        uint tId = srcRepOld[i];
        if (tId != _tokenId) {
            srcRepNew.push(tId);
                }
        }
        unchecked{
        ++i; //@audit-info: Place here with unchecked
        }

========================================================================

16. Title: Using unchecked can save gas

Proof of Concept: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Voter.sol#L112

Recommended Mitigation Steps:

unchecked{
    _totalWeight += _votes;
}

========================================================================

pooltypes commented 2 years ago

Duplicate of #131

GalloDaSballo commented 2 years ago

Minor gas savings, storage pointer type savings must have a POC to be valid.

Should save about 100 - 500 gas