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

1 stars 0 forks source link

Gas Optimizations #166

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

make a local copy of multiple use of a global variable and use one require only for one logic to save gas

poc

https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Gravity.sol#L301 https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Gravity.sol#L396

before '''

    require(
        _currentValset.validators.length == _currentValset.powers.length &&
            _currentValset.validators.length == _v.length &&
            _currentValset.validators.length == _r.length &&
            _currentValset.validators.length == _s.length,
        "Malformed current validator set"
    );

'''

after '''

    uint256 length = _currentValset.validators.length;
    require(length == _currentValset.powers.length, "Malformed current validator set"); 
    require(length == _v.length, "Malformed current validator set");
    require(length == _r.length, "Malformed current validator set");
    require(length == _s.length, "Malformed current validator set");

'''