code-423n4 / 2022-05-opensea-seaport-findings

1 stars 0 forks source link

Gas Optimizations #159

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

C4-001 : Free gas savings for using solidity 0.8.10+

C4-002 : Adding unchecked directive can save gas

C4-003 : Use Shift Right/Left instead of Division/Multiplication if possible

C4-004 : Cache array length in for loops can save gas

C4-005 : Use Custom Errors instead of Revert Strings to save Gas

C4-006 : There is no need to assign default values to variables

C4-007 : Free gas savings for using solidity 0.8.10+

C4-001 : Free gas savings for using solidity 0.8.10+

Impact

Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.

Proof of Concept

All Contracts

Solidity 0.8.10 has a useful change which reduced gas costs of external calls which expect a return value: https://blog.soliditylang.org/2021/11/09/solidity-0.8.10-release-announcement/

Solidity 0.8.13 has some improvements too but not well tested.

Code Generator: Skip existence check for external contract if return data is expected. In this case, the ABI decoder will revert if the contract does not exist

All Contracts

Tools Used

None

Recommended Mitigation Steps

Consider to upgrade pragma to at least 0.8.10.

C4-002 : Adding unchecked directive can save gas

Impact

For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.

Proof of Concept

  1. Navigate to the following contracts.
  contracts/lib/CriteriaResolution.sol::56 => for (uint256 i = 0; i < totalCriteriaResolvers; ++i) {
  contracts/lib/CriteriaResolution.sol::166 => for (uint256 i = 0; i < totalAdvancedOrders; ++i) {
  contracts/lib/CriteriaResolution.sol::184 => for (uint256 j = 0; j < totalItems; ++j) {
  contracts/lib/CriteriaResolution.sol::199 => for (uint256 j = 0; j < totalItems; ++j) {
  contracts/lib/OrderCombiner.sol::181 => for (uint256 i = 0; i < totalOrders; ++i) {
  contracts/lib/OrderCombiner.sol::247 => for (uint256 j = 0; j < offer.length; ++j) {
  contracts/lib/OrderCombiner.sol::291 => for (uint256 j = 0; j < consideration.length; ++j) {
  contracts/lib/OrderCombiner.sol::373 => for (uint256 i = 0; i < totalOrders; ++i) {
  contracts/lib/OrderCombiner.sol::470 => uint256 totalFilteredExecutions = 0;
  contracts/lib/OrderCombiner.sol::473 => for (uint256 i = 0; i < totalOfferFulfillments; ++i) {
  contracts/lib/OrderCombiner.sol::498 => for (uint256 i = 0; i < totalConsiderationFulfillments; ++i) {
  contracts/lib/OrderCombiner.sol::577 => for (uint256 i = 0; i < totalOrders; ++i) {
  contracts/lib/OrderCombiner.sol::598 => for (uint256 j = 0; j < consideration.length; ++j) {
  contracts/lib/OrderCombiner.sol::621 => for (uint256 i = 0; i < executions.length; ) {
  contracts/lib/OrderCombiner.sol::751 => uint256 totalFilteredExecutions = 0;
  contracts/lib/OrderCombiner.sol::754 => for (uint256 i = 0; i < totalFulfillments; ++i) {
  contracts/lib/OrderFulfiller.sol::217 => for (uint256 i = 0; i < orderParameters.offer.length; ) {
  contracts/lib/OrderFulfiller.sol::306 => for (uint256 i = 0; i < orderParameters.consideration.length; ) {
  contracts/lib/OrderFulfiller.sol::471 => for (uint256 i = 0; i < totalOrders; ++i) {

Tools Used

None

Recommended Mitigation Steps

Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.

Unchecked{i++};

C4-003 : Use Shift Right/Left instead of Division/Multiplication if possible

Impact

A division/multiplication by any number x being a power of 2 can be calculated by shifting log2(x) to the right/left.

While the DIV opcode uses 5 gas, the SHR opcode only uses 3 gas. Furthermore, Solidity's division operation also includes a division-by-0 prevention which is bypassed using shifting.

Proof of Concept

Contracts

Tools Used

None

Recommended Mitigation Steps

A division/multiplication by any number x being a power of 2 can be calculated by shifting log2(x) to the right/left.

C4-004 : Cache array length in for loops can save gas

Impact

Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.

Caching the array length in the stack saves around 3 gas per iteration.

Proof of Concept

  1. Navigate to the following smart contract line.
  contracts/lib/OrderFulfiller.sol::217 => for (uint256 i = 0; i < orderParameters.offer.length; ) {
  contracts/lib/OrderFulfiller.sol::306 => for (uint256 i = 0; i < orderParameters.consideration.length; ) {

Tools Used

None

Recommended Mitigation Steps

Consider to cache array length.

C4-005 : Use Custom Errors instead of Revert Strings to save Gas

Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)

Source Custom Errors in Solidity:

Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them.

Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).

Instances include:

All require Statements

Tools Used

Code Review

Recommended Mitigation Steps

Recommended to replace revert strings with custom errors.

C4-006 : There is no need to assign default values to variables

Impact - Gas Optimization

When a variable is declared solidity assigns the default value. In case the contract assigns the value again, it costs extra gas.

Example: uint x = 0 costs more gas than uint x without having any different functionality.

Proof of Concept

  contracts/lib/BasicOrderFulfiller.sol::948 => for (uint256 i = 0; i < totalAdditionalRecipients; ) {
  contracts/lib/BasicOrderFulfiller.sol::1040 => for (uint256 i = 0; i < totalAdditionalRecipients; ) {
  contracts/lib/CriteriaResolution.sol::56 => for (uint256 i = 0; i < totalCriteriaResolvers; ++i) {
  contracts/lib/CriteriaResolution.sol::166 => for (uint256 i = 0; i < totalAdvancedOrders; ++i) {
  contracts/lib/CriteriaResolution.sol::184 => for (uint256 j = 0; j < totalItems; ++j) {
  contracts/lib/CriteriaResolution.sol::199 => for (uint256 j = 0; j < totalItems; ++j) {
  contracts/lib/OrderCombiner.sol::181 => for (uint256 i = 0; i < totalOrders; ++i) {
  contracts/lib/OrderCombiner.sol::247 => for (uint256 j = 0; j < offer.length; ++j) {
  contracts/lib/OrderCombiner.sol::291 => for (uint256 j = 0; j < consideration.length; ++j) {
  contracts/lib/OrderCombiner.sol::373 => for (uint256 i = 0; i < totalOrders; ++i) {
  contracts/lib/OrderCombiner.sol::470 => uint256 totalFilteredExecutions = 0;
  contracts/lib/OrderCombiner.sol::473 => for (uint256 i = 0; i < totalOfferFulfillments; ++i) {
  contracts/lib/OrderCombiner.sol::498 => for (uint256 i = 0; i < totalConsiderationFulfillments; ++i) {
  contracts/lib/OrderCombiner.sol::577 => for (uint256 i = 0; i < totalOrders; ++i) {
  contracts/lib/OrderCombiner.sol::598 => for (uint256 j = 0; j < consideration.length; ++j) {
  contracts/lib/OrderCombiner.sol::621 => for (uint256 i = 0; i < executions.length; ) {
  contracts/lib/OrderCombiner.sol::751 => uint256 totalFilteredExecutions = 0;
  contracts/lib/OrderCombiner.sol::754 => for (uint256 i = 0; i < totalFulfillments; ++i) {
  contracts/lib/OrderFulfiller.sol::217 => for (uint256 i = 0; i < orderParameters.offer.length; ) {
  contracts/lib/OrderFulfiller.sol::306 => for (uint256 i = 0; i < orderParameters.consideration.length; ) {
  contracts/lib/OrderFulfiller.sol::471 => for (uint256 i = 0; i < totalOrders; ++i) {
  contracts/lib/OrderValidator.sol::272 => for (uint256 i = 0; i < totalOrders; ) {
  contracts/lib/OrderValidator.sol::350 => for (uint256 i = 0; i < totalOrders; ) {

Tools Used

Code Review

Recommended Mitigation Steps

uint x = 0 costs more gas than uint x without having any different functionality.

C4-007: > 0 can be replaced with != 0 for gas optimization

Impact

!= 0 is a cheaper operation compared to > 0, when dealing with uint. (Before Pragma 0.8.13)

Proof of Concept

  1. Navigate to the following contracts.
contracts/FixedPricePassThruGate.sol::51 => if (msg.value > 0) {
contracts/MerkleResistor.sol::156 => startTime,     // start time will usually be in the past, if pctUpFront > 0
contracts/SpeedBumpPriceGate.sol::77 => if (msg.value > 0) {
contracts/VoterID.sol::52 => *     => 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^
contracts/VoterID.sol::208 => ///  checks if `to` is a smart contract (code size > 0). If so, it calls
contracts/VoterID.sol::348 => return size > 0;

Tools Used

Code Review

Recommended Mitigation Steps

Use "!=0" instead of ">0" for the gas optimization.

C4-007 : Free gas savings for using solidity 0.8.10+

Impact

Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.

Proof of Concept

All Contracts

Solidity 0.8.10 has a useful change which reduced gas costs of external calls which expect a return value: https://blog.soliditylang.org/2021/11/09/solidity-0.8.10-release-announcement/

Solidity 0.8.13 has some improvements too but not well tested.

Code Generator: Skip existence check for external contract if return data is expected. In this case, the ABI decoder will revert if the contract does not exist

All Contracts

Tools Used

None

Recommended Mitigation Steps

Consider to upgrade pragma to at least 0.8.10.

HardlyDifficult commented 2 years ago

Free gas savings for using solidity 0.8.10+

Seaport 1.0 used 0.8.13, and 1.1 uses 0.8.14

Adding unchecked directive can save gas Cache array length in for loops can save gas

These should offer small savings.

Use Shift Right/Left instead of Division/Multiplication if possible

This can offer small savings.

Use Custom Errors instead of Revert Strings to save Gas

This can save gas, particularly at deployment time. However ATM custom errors are not as well supported (e.g. etherscan) so it may be intentional to avoid them for now.

There is no need to assign default values to variables 0 can be replaced with != 0 for gas optimization

In my testing these changes make very little difference.