You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(5) Multiple Address Mappings Can Be Combined Into A Single Mapping Of An Address To A Struct, Where Appropriate
Severity: Gas Optimizations
Saves a storage slot for the mapping. Depending on the circumstances and sizes of types, can avoid a Gsset (20000 gas) per mapping combined. Reads and subsequent writes can also be cheaper when a function requires both values and they both fit in the same storage slot.
Proof Of Concept
mapping(address => uint256) public override projectPublished;
mapping(address => mapping(bytes32 => bool)) public override approvedHashes;
(8) Using Private Rather Than Public For Constants, Saves Gas
Severity: Gas Optimizations
If needed, the value can be read from the verified contract source code. Savings are due to the compiler not having to create non-payable getter functions for deployment calldata, and not adding another entry to the method ID table
(9) Help The Optimizer By Saving A Storage Variable’s Reference Instead Of Repeatedly Fetching It
Severity: Gas Optimizations
To help the optimizer, declare a storage type variable and use it instead of repeatedly fetching the reference in a map or an array.
The effect can be quite significant.
As an example, instead of repeatedly calling someMap[someIndex], save its reference like this: SomeStruct storage someStruct = someMap[someIndex] and use it.
(10) ++i/i++ Should Be Unchecked{++i}/unchecked{i++} When It Is Not Possible For Them To Overflow, As Is The Case When Used In For- And While-loops
Severity: Gas Optimizations
The unchecked keyword is new in solidity version 0.8.0, so this only applies to that version or higher, which these instances are. This saves 30-40 gas PER LOOP
Proof Of Concept
for (uint256 i = 0; i < _communities[_communityID].memberCount; i++) {
booleans are more expensive than uint256 or any type that takes up a full word because each write operation emits an extra SLOAD to first read the slot's contents, replace the bits taken up by the boolean, and then write back. This is the compiler's defense against contract upgrades and pointer aliasing, and it cannot be disabled.
(1) .length Should Not Be Looked Up In Every Loop Of A For-loop
Severity: Gas Optimizations
The overheads outlined below are PER LOOP, excluding the first loop
storage arrays incur a Gwarmaccess (100 gas)
memory arrays use MLOAD (3 gas)
calldata arrays use CALLDATALOAD (3 gas)
Caching the length changes each of these to a DUP (3 gas), and gets rid of the extra DUP needed to store the stack offset
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L603
(2) ++i Costs Less Gas Than i++, Especially When It’s Used In For-loops (--i/i-- Too)
Severity: Gas Optimizations
Saves 6 gas per loop
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L624
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFiProxy.sol#L87
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFiProxy.sol#L136
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L248
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L311
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L322
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L603
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L710
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/libraries/Tasks.sol#L181
Recommended Mitigation Steps
(3) It Costs More Gas To Initialize Variables To Zero Than To Let The Default Of Zero Be Applied
Severity: Gas Optimizations
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L624
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFiProxy.sol#L87
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFiProxy.sol#L136
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L248
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L311
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L322
(4) Using > 0 Costs More Gas Than != 0 When Used On A Uint In A Require() Statement
Severity: Gas Optimizations
This change saves 6 gas per instance
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L764
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L195
(5) Multiple Address Mappings Can Be Combined Into A Single Mapping Of An Address To A Struct, Where Appropriate
Severity: Gas Optimizations
Saves a storage slot for the mapping. Depending on the circumstances and sizes of types, can avoid a Gsset (20000 gas) per mapping combined. Reads and subsequent writes can also be cheaper when a function requires both values and they both fit in the same storage slot.
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L59
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFi.sol#L64
(6) Multiplication/division By Two Should Use Bit Shifting
Severity: Gas Optimizations
* 2 is equivalent to << 1 and / 2 is the same as >> 1. The MUL and DIV opcodes cost 5 gas, whereas SHL and SHR only cost 3 gas
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L686
(7) += Costs More Gas Than = + For State Variables
Severity: Gas Optimizations
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L423
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L435
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L423
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L435
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L798
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFi.sol#L289
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L179
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L250
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L290
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L431
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L440
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L456
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L431
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L440
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L456
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L431
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L440
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L456
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L616
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L616
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L711
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L772
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/libraries/SignatureDecoder.sol#L83
(8) Using Private Rather Than Public For Constants, Saves Gas
Severity: Gas Optimizations
If needed, the value can be read from the verified contract source code. Savings are due to the compiler not having to create non-payable getter functions for deployment calldata, and not adding another entry to the method ID table
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L60
Recommended Mitigation Steps
Set variable to private.
(9) Help The Optimizer By Saving A Storage Variable’s Reference Instead Of Repeatedly Fetching It
Severity: Gas Optimizations
To help the optimizer, declare a storage type variable and use it instead of repeatedly fetching the reference in a map or an array.
The effect can be quite significant.
As an example, instead of repeatedly calling someMap[someIndex], save its reference like this: SomeStruct storage someStruct = someMap[someIndex] and use it.
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L143
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Disputes.sol#L112
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L553
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L554
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L555
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L553
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L554
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L555
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L553
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L554
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L555
(10) ++i/i++ Should Be Unchecked{++i}/unchecked{i++} When It Is Not Possible For Them To Overflow, As Is The Case When Used In For- And While-loops
Severity: Gas Optimizations
The unchecked keyword is new in solidity version 0.8.0, so this only applies to that version or higher, which these instances are. This saves 30-40 gas PER LOOP
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L624
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFiProxy.sol#L87
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFiProxy.sol#L136
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L248
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L311
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L322
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L603
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L650
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L603
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L650
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L710
(11) Using Bools For Storage Incurs Overhead
Severity: Gas Optimizations
Proof Of Concept
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L55
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L61
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L55
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Community.sol#L61
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFi.sol#L50
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/HomeFiProxy.sol#L30
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L68
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L78
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L84
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L68
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L78
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L84
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L68
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L78
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/Project.sol#L84
https://github.com/code-423n4/2022-08-rigor/tree/main/contracts/libraries/Tasks.sol#L16
The text was updated successfully, but these errors were encountered: