code-423n4 / 2022-06-canto-v2-findings

0 stars 0 forks source link

Gas Optimizations #166

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Here are the Gas Optimizations I found:

-> COMPARISON OPERATORS Problem

In the EVM, there is no opcode for >= or <=. When using greater than or equal, two operations are performed: > and =.

Using strict comparison operators hence saves gas

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Accountant/AccountantDelegate.sol#:~:text=require(cNoteAmt%20%3E%3D%20noteDiff%2C%20%22AccountantDelegate%3A%3AsweepInterest%3AError%20calculating%20interest%20to%20sweep%22)%3B

-> REQUIRE()/REVERT() STRINGS LONGER THAN 32 BYTES COST EXTRA GAS

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Treasury/TreasuryDelegator.sol#:~:text=require(msg.sender%20%3D%3D%20admin%2C%20%22GovernorBravoDelegator%3A%3AsetImplementation%3A%20admin%20only%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Treasury/TreasuryDelegator.sol#:~:text=require(implementation_%20!%3D%20address(0)%2C%20%22GovernorBravoDelegator%3A%3AsetImplementation%3A%20invalid%20implementation%20address%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Treasury/TreasuryDelegator.sol#:~:text=require(msg.value%20%3D%3D%200%2C%20%22TreasuryDelegator%3A%3Afallback%3Acannot%20send%20value%20to%20fallback%22)%3B https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Accountant/AccountantDelegate.sol#:~:text=require(cNoteAmt%20%3E%3D%20noteDiff%2C%20%22AccountantDelegate%3A%3AsweepInterest%3AError%20calculating%20interest%20to%20sweep%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Accountant/AccountantDelegate.sol#:~:text=require(cNoteAmt%20%3E%3D%20noteDiff%2C%20%22AccountantDelegate%3A%3AsweepInterest%3AError%20calculating%20interest%20to%20sweep%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Accountant/AccountantDelegate.sol#:~:text=require(cnote.balanceOf(treasury)%20%3D%3D%200%2C%20%22AccountantDelegate%3A%3AsweepInterestError%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Accountant/AccountantDelegator.sol#:~:text=require(msg.sender%20%3D%3D%20admin%2C%20%22AccountantDelegator%3A%3A_setImplementation%3A%20admin%20only%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Accountant/AccountantDelegator.sol#:~:text=require(implementation_%20!%3D%20address(0)%2C%20%22AccountantDelegator%3A%3A_setImplementation%3A%20invalid%20implementation%20address%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Accountant/AccountantDelegator.sol#:~:text=require(msg.value%20%3D%3D%200%2C%22AccountantDelegator%3Afallback%3A%20cannot%20send%20value%20to%20fallback%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/Comp.sol#:~:text=require(signatory%20!%3D%20address(0)%2C%20%22Comp%3A%3AdelegateBySig%3A%20invalid%20signature%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/Comp.sol#:~:text=require(nonce%20%3D%3D%20nonces%5Bsignatory%5D%2B%2B%2C%20%22Comp%3A%3AdelegateBySig%3A%20invalid%20nonce%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/Comp.sol#:~:text=require(block.timestamp%20%3C%3D%20expiry%2C%20%22Comp%3A%3AdelegateBySig%3A%20signature%20expired%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/Comp.sol#:~:text=require(blockNumber%20%3C%20block.number%2C%20%22Comp%3A%3AgetPriorVotes%3A%20not%20yet%20determined%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/Comp.sol#:~:text=require(src%20!%3D%20address(0)%2C%20%22Comp%3A%3A_transferTokens%3A%20cannot%20transfer%20from%20the%20zero%20address%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/Comp.sol#:~:text=require(dst%20!%3D%20address(0)%2C%20%22Comp%3A%3A_transferTokens%3A%20cannot%20transfer%20to%20the%20zero%20address%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(comp.getPriorVotes(msg.sender%2C%20sub256(block.number%2C%201))%20%3E%20proposalThreshold()%2C%20%22GovernorAlpha%3A%3Apropose%3A%20proposer%20votes%20below%20proposal%20threshold%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(targets.length%20%3D%3D%20values.length%20%26%26%20targets.length%20%3D%3D%20signatures.length%20%26%26%20targets.length%20%3D%3D%20calldatas.length%2C%20%22GovernorAlpha%3A%3Apropose%3A%20proposal%20function%20information%20arity%20mismatch%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(targets.length%20!%3D%200%2C%20%22GovernorAlpha%3A%3Apropose%3A%20must%20provide%20actions%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(targets.length%20%3C%3D%20proposalMaxOperations()%2C%20%22GovernorAlpha%3A%3Apropose%3A%20too%20many%20actions%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(proposersLatestProposalState%20!%3D%20ProposalState.Active%2C%20%22GovernorAlpha%3A%3Apropose%3A%20one%20live%20proposal%20per%20proposer%2C%20found%20an%20already%20active%20proposal%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(proposersLatestProposalState%20!%3D%20ProposalState.Pending%2C%20%22GovernorAlpha%3A%3Apropose%3A%20one%20live%20proposal%20per%20proposer%2C%20found%20an%20already%20pending%20proposal%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(newProposal.id%20%3D%3D%200%2C%20%22GovernorAlpha%3A%3Apropose%3A%20ProposalID%20collsion%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(state(proposalId)%20%3D%3D%20ProposalState.Succeeded%2C%20%22GovernorAlpha%3A%3Aqueue%3A%20proposal%20can%20only%20be%20queued%20if%20it%20is%20succeeded%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(!,signature%2C%20data%2C%20eta)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(state%20!%3D%20ProposalState.Executed%2C%20%22GovernorAlpha%3A%3Acancel%3A%20cannot%20cancel%20executed%20proposal%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(msg.sender%20%3D%3D%20guardian%20%7C%7C%20comp.getPriorVotes(proposal.proposer%2C%20sub256(block.number%2C%201))%20%3C%20proposalThreshold()%2C%20%22GovernorAlpha%3A%3Acancel%3A%20proposer%20above%20threshold%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(proposalCount%20%3E%3D%20proposalId%20%26%26%20proposalId%20%3E%200%2C%20%22GovernorAlpha%3A%3Astate%3A%20invalid%20proposal%20id%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(signatory%20!%3D%20address(0)%2C%20%22GovernorAlpha%3A%3AcastVoteBySig%3A%20invalid%20signature%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(state(proposalId)%20%3D%3D%20ProposalState.Active%2C%20%22GovernorAlpha%3A%3A_castVote%3A%20voting%20is%20closed%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(receipt.hasVoted%20%3D%3D%20false%2C%20%22GovernorAlpha%3A%3A_castVote%3A%20voter%20already%20voted%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/CToken.sol#:~:text=require(initialExchangeRateMantissa%20%3E%200%2C%20%22initial%20exchange%20rate%20must%20be%20greater%20than%20zero.%22)%3B

->USING > 0 COSTS MORE GAS THAN != 0 WHEN USED ON A UINT IN A REQUIRE() STATEMENT

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(proposalCount%20%3E%3D%20proposalId%20%26%26%20proposalId%20%3E%200%2C%20%22GovernorAlpha%3A%3Astate%3A%20invalid%20proposal%20id%22)%3B

-> ++i costs less gas compared to i++ or i += 1 (Also --i costs less gas compared to i--- or i -= 1)

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=timelock.delay())%3B-,for%20(uint%20i%20%3D%200%3B%20i%20%3C%20proposal.targets.length%3B%20i%2B%2B),-%7B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=executed%20%3D%20true%3B-,for%20(uint%20i%20%3D%200%3B%20i%20%3C%20proposal.targets.length%3B%20i%2B%2B),-%7B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=canceled%20%3D%20true%3B-,for%20(uint%20i%20%3D%200%3B%20i%20%3C%20proposal.targets.length%3B%20i%2B%2B),-%7B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorBravoDelegate.sol#:~:text=for%20(uint%20i%20%3D%200%3B%20i%20%3C%20newProposal.targets.length%3B%20i%2B%2B)

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorBravoDelegate.sol#:~:text=for%20(uint%20i%20%3D%200%3B%20i%20%3C%20proposal.targets.length%3B%20i%2B%2B)

->SPLITTING REQUIRE() STATEMENTS THAT USE && SAVES GAS

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(targets.length%20%3D%3D%20values.length%20%26%26%20targets.length%20%3D%3D%20signatures.length%20%26%26%20targets.length%20%3D%3D%20calldatas.length%2C%20%22GovernorAlpha%3A%3Apropose%3A%20proposal%20function%20information%20arity%20mismatch%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(proposalCount%20%3E%3D%20proposalId%20%26%26%20proposalId%20%3E%200%2C%20%22GovernorAlpha%3A%3Astate%3A%20invalid%20proposal%20id%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorBravoDelegate.sol#:~:text=require(unigovProposal.targets,arity%20mismatch%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=require(proposalCount%20%3E%3D%20proposalId%20%26%26%20proposalId%20%3E%200%2C%20%22GovernorAlpha%3A%3Astate%3A%20invalid%20proposal%20id%22)%3B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/CToken.sol#:~:text=require(accrualBlockNumber%20%3D%3D%200%20%26%26%20borrowIndex%20%3D%3D%200%2C%20%22market%20may%20only%20be%20initialized%20once%22)%3B

->IT COSTS MORE GAS TO INITIALIZE VARIABLES TO ZERO THAN TO LET THE DEFAULT OF ZERO BE APPLIED

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=timelock.delay())%3B-,for%20(uint%20i%20%3D%200%3B%20i%20%3C%20proposal.targets.length%3B%20i%2B%2B),-%7B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=executed%20%3D%20true%3B-,for%20(uint%20i%20%3D%200%3B%20i%20%3C%20proposal.targets.length%3B%20i%2B%2B),-%7B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorAlpha.sol#:~:text=canceled%20%3D%20true%3B-,for%20(uint%20i%20%3D%200%3B%20i%20%3C%20proposal.targets.length%3B%20i%2B%2B),-%7B

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorBravoDelegate.sol#:~:text=for%20(uint%20i%20%3D%200%3B%20i%20%3C%20newProposal.targets.length%3B%20i%2B%2B)

https://github.com/Plex-Engineer/lending-market-v2/blob/ea5840de72eab58bec837bb51986ac73712fcfde/contracts/Governance/GovernorBravoDelegate.sol#:~:text=for%20(uint%20i%20%3D%200%3B%20i%20%3C%20proposal.targets.length%3B%20i%2B%2B)

GalloDaSballo commented 2 years ago

No immutables, I think this will save at most 50 gas