sherlock-audit / 2024-06-velocimeter-judging

11 stars 7 forks source link

Round Opal Kestrel - "_reset" function can be optimized. #715

Closed sherlock-admin4 closed 4 months ago

sherlock-admin4 commented 4 months ago

Round Opal Kestrel

Low/Info

"_reset" function can be optimized.

Summary

since votes is a unit and its if (_votes != 0) { then there is only one case that is _votes>0. _votes can never be less than 0.

Vulnerability Detail

function _reset(uint _tokenId) internal { address[] storage _poolVote = poolVote[_tokenId]; uint _poolVoteCnt = _poolVote.length; uint256 _totalWeight = 0;

    for (uint i = 0; i < _poolVoteCnt; i ++) {
        address _pool = _poolVote[i];
        uint256 _votes = votes[_tokenId][_pool];

        if (_votes != 0) {
            _updateFor(gauges[_pool]);
            weights[_pool] -= _votes;
            votes[_tokenId][_pool] -= _votes;
            if (_votes > 0) {
                IBribe(external_bribes[gauges[_pool]])._withdraw(uint256(_votes), _tokenId);
                _totalWeight += _votes;
            } else {
   @>>             _totalWeight -= _votes;
            }
            emit Abstained(_tokenId, _votes);
        }
    }
    totalWeight -= uint256(_totalWeight);
    usedWeights[_tokenId] = 0;
    delete poolVote[_tokenId];
}

Impact

it can cause code to be more efficient.

Code Snippet

https://github.com/sherlock-audit/2024-06-velocimeter/blob/main/v4-contracts/contracts/Voter.sol#L223

Tool used

Manual Review

Recommendation

  if (_votes != 0) {
            _updateFor(gauges[_pool]);
            weights[_pool] -= _votes;
            votes[_tokenId][_pool] -= _votes;
            if (_votes > 0) {
                IBribe(external_bribes[gauges[_pool]])._withdraw(uint256(_votes), _tokenId);
                _totalWeight += _votes;

            }
            emit Abstained(_tokenId, _votes);
        }
    }