Cudos contest - 0xNazgul's results

Decentralised cloud computing for Web3.

General Information

Platform: Code4rena

Start Date: 03/05/2022

Pot Size: $75,000 USDC

Total HM: 6

Participants: 55

Period: 7 days

Judge: Albert Chon

Total Solo HM: 2

Id: 116

League: COSMOS

Cudos

Findings Distribution

Researcher Performance

Rank: 46/55

Findings: 1

Award: $96.72

🌟 Selected for report: 0

🚀 Solo Findings: 0

Awards

96.7172 USDC - $96.72

Labels

bug
G (Gas Optimization)

External Links

Use ++index instead of index++ to increment a loop counter

Context: Gravity.sol#L124-L136, Gravity.sol#L219-L259, Gravity.sol#L261-L269, Gravity.sol#L364-L468, Gravity.sol#L479-L593 (For both), Gravity.sol#L640-L694

Description: Due to reduced stack operations, using ++index saves 5 gas per iteration.

Recommendation: Use ++index to increment a loop counter.

Catching The Array Length Prior To Loop

Context: Gravity.sol#L124-L136, Gravity.sol#L219-L259, Gravity.sol#L261-L269, Gravity.sol#L364-L468, Gravity.sol#L479-L593 (For both), Gravity.sol#L640-L694

Description: One can save gas by caching the array length (in stack) and using that set variable in the loop. Replace state variable reads and writes within loops with local variable reads and writes. This is done by assigning state variable values to new local variables, reading and/or writing the local variables in a loop, then after the loop assigning any changed local variables to their equivalent state variables.

Recommendation: Simply do something like so before the for loop: uint length = variable.length. Then add length in place of variable.length in the for loop.

Use calldata Instead of memory For Function Parameters

Context: Gravity.sol#L124-L136, Gravity.sol#L140-L142, Gravity.sol#L276-L358 (For all five), Gravity.sol#L364-L468 (For all seven), Gravity.sol#L479-L593 (For all five), Gravity.sol#L611-L630 (For all three)

Description: The dynamic array arr has the storage location memory. When the function gets called externally, the array values are kept in calldata and copied to memory during ABI decoding (using the opcode calldataload and mstore). And during the for loop, arr[i] accesses the value in memory using a mload. However, for the above example this is inefficient.

Recommendation: Use calldata instead of memory for function parameters to avoid using memory with array values when a function is getting called externally.

Functions Visibility Can Be Declared External

Context: Gravity.sol#L124-L136, Gravity.sol#L140-L142, Gravity.sol#L144-L162, Gravity.sol#L166-L168, Gravity.sol#L170-L172, Gravity.sol#L276-L358, Gravity.sol#L364-L468, Gravity.sol#L479-L593, Gravity.sol#L595-L609, Gravity.sol#L611-L630

Description: Several functions across multiple contracts have a public visibility and can be marked with external visibility to save gas.

Recommendation: Change the functions visibility to external to save gas.

State Variables That Can Be Set To Immutable

Context: CosmosToken.sol#L5

Description: Solidity 0.6.5 introduced immutable as a major feature. It allows setting contract-level variables at construction time which gets stored in code rather than storage. Each call to it reads from storage, using a sload costing 2100 gas cold or 100 gas warm. Setting it to immutable will have each storage read of the state variable to be replaced by the instruction push32 value, where value is set during contract construction time and this costs only 3 gas.

Recommendation: Set the state variable to immutable

Setting The Constructor To Payable

Context: All Contracts

Description: You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. Making the constructor payable eliminates the need for an initial check of msg.value == 0 and saves 21 gas on deployment with no security risks.

Recommendation: Set the constructor to payable.

Function Ordering via Method ID

Context: All Contracts

Description: Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use This tool to help find alternative function names with lower Method IDs while keeping the original name intact.

Recommendation: Find a lower method ID name for the most called functions for example mostCalled() vs. mostCalled_41q() is cheaper by 44 gas.

Upgrade To At Least 0.8.4

Context: All Contracts

Description: Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks for free!

The advantages of versions =0.8.*= over =<0.8.0= are:

  • Safemath by default from =0.8.0= (can be more gas efficient than /some/ library based safemath).
  • Low level inliner from =0.8.2=, leads to cheaper runtime gas. Especially relevant when the contract has small functions. For example, OpenZeppelin libraries typically have a lot of small helper functions and if they are not inlined, they cost an additional 20 to 40 gas because of 2 extra =jump= instructions and additional stack operations needed for function calls.
  • Optimizer improvements in packed structs: Before =0.8.3=, storing packed structs, in some cases used an additional storage read operation. After EIP-2929, if the slot was already cold, this means unnecessary stack operations and extra deploy time costs. However, if the slot was already warm, this means additional cost of =100= gas alongside the same unnecessary stack operations and extra deploy time costs.
  • Custom errors from =0.8.4=, leads to cheaper deploy time cost and run time cost. Note: the run time cost is only relevant when the revert condition is met. In short, replace revert strings by custom errors.

Recommendation: Upgrade to at least 0.8.4 for the additional benefits.

#0 - V-Staykov

2022-05-10T11:20:55Z

I find this particularly high quality.

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter