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

1 stars 0 forks source link

Gas Optimizations #164

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas

[G-01] Cache Array Length Outside of Loop.

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

  lib\OrderCombiner.sol::247 => for (uint256 j = 0; j < offer.length; ++j) {
  lib\OrderCombiner.sol::291 => for (uint256 j = 0; j < consideration.length; ++j) {
  lib\OrderCombiner.sol::598 => for (uint256 j = 0; j < consideration.length; ++j) {
  lib\OrderCombiner.sol::621 => for (uint256 i = 0; i < executions.length; ) {
  lib\OrderFulfiller.sol::217 => for (uint256 i = 0; i < orderParameters.offer.length; ) {
  lib\OrderFulfiller.sol::306 => for (uint256 i = 0; i < orderParameters.consideration.length; ) {

Recommendation

Store the array’s length in a variable before the for-loop.

[G-02] No need to initialize variables with default values

Impact

If a variable is not set/initialized, it is assumed to have the default value (0, false, 0x0 etc depending on the data type). Explicitly initializing it with its default value is an anti-pattern and wastes gas.

Proof of Concept

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

Recommendation

Remove explicit default initializations.

Tools used

manual

Gas Diffchecker

yarn profile https://www.diffchecker.com/8afIz8bO

Gas Before

·----------------------------------------------------------------|---------------------------|---------------|-----------------------------·
|                      Solc version: 0.8.13                      ·  Optimizer enabled: true  ·  Runs: 15000  ·  Block limit: 30000000 gas  │
·································································|···························|···············|······························
|  Methods                                                                                                                                 │
······························|··································|·············|·············|···············|···············|··············
|  Contract                   ·  Method                          ·  Min        ·  Max        ·  Avg          ·  # calls      ·  usd (avg)  │
······························|··································|·············|·············|···············|···············|··············
|  Conduit                    ·  execute                         ·      78300  ·    2316049  ·       474674  ·            6  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Conduit                    ·  executeBatch1155                ·          -  ·          -  ·       100007  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Conduit                    ·  executeWithBatch1155            ·     100500  ·     370881  ·       267219  ·            3  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  acceptOwnership                 ·          -  ·          -  ·        33337  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  cancelOwnershipTransfer         ·          -  ·          -  ·        28115  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  createConduit                   ·     723522  ·     723666  ·       723653  ·           31  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  transferOwnership               ·          -  ·          -  ·        50442  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  updateChannel                   ·      36079  ·     120728  ·       106630  ·           19  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  EIP1271Wallet              ·  approveNFT                      ·          -  ·          -  ·        49681  ·           10  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  EIP1271Wallet              ·  registerDigest                  ·          -  ·          -  ·        44157  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  EIP1271Wallet              ·  revertWithMessage               ·          -  ·          -  ·        21683  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  EIP1271Wallet              ·  setValid                        ·      21705  ·      43617  ·        32661  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ExcessReturnDataRecipient  ·  setRevertDataSize               ·          -  ·          -  ·        43441  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Reenterer                  ·  prepare                         ·          -  ·          -  ·       763392  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  cancel                          ·      44124  ·      61272  ·        56915  ·           16  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillAdvancedOrder            ·     104014  ·     209693  ·       162960  ·          112  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillAvailableAdvancedOrders  ·     173305  ·     229465  ·       205136  ·           16  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillAvailableOrders          ·     172879  ·     229282  ·       206787  ·           13  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillBasicOrder               ·      93563  ·    1624267  ·       665050  ·          160  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillOrder                    ·     102728  ·     213402  ·       174621  ·          105  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  incrementNonce                  ·          -  ·          -  ·        47051  ·            6  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  matchAdvancedOrders             ·     206604  ·     272820  ·       255463  ·           67  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  matchOrders                     ·     166719  ·     366937  ·       268321  ·          105  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  validate                        ·      58025  ·      69440  ·        67068  ·           21  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC1155                ·  mint                            ·      47333  ·      49989  ·        49333  ·          130  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC1155                ·  setApprovalForAll               ·      26136  ·      46036  ·        45809  ·          182  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC20                  ·  approve                         ·      28872  ·      46236  ·        45626  ·          190  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC20                  ·  blockTransfer                   ·      22006  ·      43918  ·        32962  ·            4  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC20                  ·  mint                            ·      33947  ·      68483  ·        57537  ·           88  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC20                  ·  setNoReturnData                 ·      21954  ·      43866  ·        32910  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC721                 ·  mint                            ·      51544  ·      68656  ·        66050  ·          158  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC721                 ·  setApprovalForAll               ·      26199  ·      46111  ·        45366  ·          270  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Deployments                                                   ·                                           ·  % of limit   ·             │
·································································|·············|·············|···············|···············|··············
|  Consideration                                                 ·          -  ·          -  ·      5347596  ·       17.8 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  EIP1271Wallet                                                 ·     600395  ·     600407  ·       600406  ·          2 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  ExcessReturnDataRecipient                                     ·          -  ·          -  ·       511260  ·        1.7 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  Reenterer                                                     ·          -  ·          -  ·       534991  ·        1.8 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  TestERC1155                                                   ·          -  ·          -  ·      1012405  ·        3.4 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  TestERC20                                                     ·          -  ·          -  ·      1039822  ·        3.5 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  TestERC721                                                    ·          -  ·          -  ·      1094209  ·        3.6 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  TestZone                                                      ·          -  ·          -  ·       283643  ·        0.9 %  ·          -  │
·----------------------------------------------------------------|-------------|-------------|---------------|---------------|-------------·

Gas After

·----------------------------------------------------------------|---------------------------|---------------|-----------------------------·
|                      Solc version: 0.8.13                      ·  Optimizer enabled: true  ·  Runs: 15000  ·  Block limit: 30000000 gas  │
·································································|···························|···············|······························
|  Methods                                                                                                                                 │
······························|··································|·············|·············|···············|···············|··············
|  Contract                   ·  Method                          ·  Min        ·  Max        ·  Avg          ·  # calls      ·  usd (avg)  │
······························|··································|·············|·············|···············|···············|··············
|  Conduit                    ·  execute                         ·      78300  ·    2202995  ·       455832  ·            6  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Conduit                    ·  executeBatch1155                ·          -  ·          -  ·       100007  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Conduit                    ·  executeWithBatch1155            ·     100500  ·     370893  ·       267227  ·            3  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  acceptOwnership                 ·          -  ·          -  ·        33337  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  cancelOwnershipTransfer         ·          -  ·          -  ·        28115  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  createConduit                   ·     723522  ·     723666  ·       723653  ·           31  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  transferOwnership               ·          -  ·          -  ·        50442  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ConduitController          ·  updateChannel                   ·      36091  ·     120728  ·       106631  ·           19  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  EIP1271Wallet              ·  approveNFT                      ·          -  ·          -  ·        49693  ·           10  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  EIP1271Wallet              ·  registerDigest                  ·          -  ·          -  ·        44157  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  EIP1271Wallet              ·  revertWithMessage               ·          -  ·          -  ·        21683  ·            1  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  EIP1271Wallet              ·  setValid                        ·      21705  ·      43617  ·        32661  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  ExcessReturnDataRecipient  ·  setRevertDataSize               ·          -  ·          -  ·        43441  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Reenterer                  ·  prepare                         ·          -  ·          -  ·       763404  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  cancel                          ·      44124  ·      61260  ·        56913  ·           16  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillAdvancedOrder            ·     103898  ·     209532  ·       162767  ·          112  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillAvailableAdvancedOrders  ·     173088  ·     229025  ·       204810  ·           16  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillAvailableOrders          ·     172612  ·     228842  ·       206432  ·           13  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillBasicOrder               ·      93563  ·    1624293  ·       665060  ·          160  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  fulfillOrder                    ·     102612  ·     213241  ·       174439  ·          105  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  incrementNonce                  ·          -  ·          -  ·        47051  ·            6  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  matchAdvancedOrders             ·     206212  ·     272478  ·       255062  ·           67  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  matchOrders                     ·     166542  ·     366330  ·       267910  ·          105  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Seaport                    ·  validate                        ·      58001  ·      69440  ·        67066  ·           21  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC1155                ·  mint                            ·      47321  ·      49989  ·        49456  ·          166  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC1155                ·  setApprovalForAll               ·      26136  ·      46036  ·        45879  ·          254  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC20                  ·  approve                         ·      28884  ·      46248  ·        45572  ·          162  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC20                  ·  blockTransfer                   ·      22006  ·      43918  ·        32962  ·            4  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC20                  ·  mint                            ·      33947  ·      68483  ·        55519  ·           74  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC20                  ·  setNoReturnData                 ·      21954  ·      43866  ·        32910  ·            2  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC721                 ·  mint                            ·      51544  ·      68656  ·        65632  ·          136  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  TestERC721                 ·  setApprovalForAll               ·      26211  ·      46111  ·        45230  ·          226  ·          -  │
······························|··································|·············|·············|···············|···············|··············
|  Deployments                                                   ·                                           ·  % of limit   ·             │
·································································|·············|·············|···············|···············|··············
|  Consideration                                                 ·          -  ·          -  ·      5335122  ·       17.8 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  EIP1271Wallet                                                 ·     600395  ·     600407  ·       600406  ·          2 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  ExcessReturnDataRecipient                                     ·          -  ·          -  ·       511260  ·        1.7 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  Reenterer                                                     ·          -  ·          -  ·       534991  ·        1.8 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  TestERC1155                                                   ·          -  ·          -  ·      1012405  ·        3.4 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  TestERC20                                                     ·          -  ·          -  ·      1039822  ·        3.5 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  TestERC721                                                    ·          -  ·          -  ·      1094209  ·        3.6 %  ·          -  │
·································································|·············|·············|···············|···············|··············
|  TestZone                                                      ·          -  ·          -  ·       283643  ·        0.9 %  ·          -  │
·----------------------------------------------------------------|-------------|-------------|---------------|---------------|-------------·