From d63ca2c295f837fe59c94e7ab1b2d940ceeee732 Mon Sep 17 00:00:00 2001 From: andreivladbrg Date: Wed, 18 Oct 2023 23:31:52 +0300 Subject: [PATCH] test: add DeployOptimized utils contracts test: move and rename functions from Base_Test --- test/Base.t.sol | 59 ++------------ .../concrete/comptroller/Comptroller.t.sol | 2 +- test/utils/DeployOptimized.sol | 77 +++++++++++++++++++ test/utils/Precompiles.t.sol | 37 +++++---- 4 files changed, 104 insertions(+), 71 deletions(-) create mode 100644 test/utils/DeployOptimized.sol diff --git a/test/Base.t.sol b/test/Base.t.sol index beca5a0c1..4fe15d316 100644 --- a/test/Base.t.sol +++ b/test/Base.t.sol @@ -3,7 +3,6 @@ pragma solidity >=0.8.19 <0.9.0; import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; -import { StdCheats } from "forge-std/StdCheats.sol"; import { ISablierV2Comptroller } from "../src/interfaces/ISablierV2Comptroller.sol"; import { ISablierV2LockupDynamic } from "../src/interfaces/ISablierV2LockupDynamic.sol"; @@ -22,12 +21,13 @@ import { Assertions } from "./utils/Assertions.sol"; import { Calculations } from "./utils/Calculations.sol"; import { Constants } from "./utils/Constants.sol"; import { Defaults } from "./utils/Defaults.sol"; +import { DeployOptimized } from "./utils/DeployOptimized.sol"; import { Events } from "./utils/Events.sol"; import { Fuzzers } from "./utils/Fuzzers.sol"; import { Users } from "./utils/Types.sol"; /// @notice Base test contract with common logic needed by all tests. -abstract contract Base_Test is Assertions, Calculations, Constants, Events, Fuzzers, StdCheats { +abstract contract Base_Test is Assertions, Calculations, Constants, DeployOptimized, Events, Fuzzers { /*////////////////////////////////////////////////////////////////////////// VARIABLES //////////////////////////////////////////////////////////////////////////*/ @@ -132,7 +132,7 @@ abstract contract Base_Test is Assertions, Calculations, Constants, Events, Fuzz return user; } - /// @dev Conditionally deploys V2 Core normally or from a source precompiled with `--via-ir`. + /// @dev Conditionally deploys V2 Core normally or from a optimized source compiled with `--via-ir`. /// We cannot use the {DeployCore} script because some tests rely on hard coded addresses for the /// deployed contracts. Since the script itself would have to be deployed, using it would bump the /// deployer's nonce, which would in turn lead to different addresses (recall that the addresses @@ -145,10 +145,8 @@ abstract contract Base_Test is Assertions, Calculations, Constants, Events, Fuzz new SablierV2LockupDynamic(users.admin, comptroller, nftDescriptor, defaults.MAX_SEGMENT_COUNT()); lockupLinear = new SablierV2LockupLinear(users.admin, comptroller, nftDescriptor); } else { - comptroller = deployPrecompiledComptroller(users.admin); - nftDescriptor = deployPrecompiledNFTDescriptor(); - lockupDynamic = deployPrecompiledDynamic(users.admin, comptroller, nftDescriptor); - lockupLinear = deployPrecompiledLinear(users.admin, comptroller, nftDescriptor); + (comptroller, lockupDynamic, lockupLinear, nftDescriptor) = + deployOptimizedCore(users.admin, defaults.MAX_SEGMENT_COUNT()); } vm.label({ account: address(comptroller), newLabel: "Comptroller" }); @@ -157,53 +155,6 @@ abstract contract Base_Test is Assertions, Calculations, Constants, Events, Fuzz vm.label({ account: address(nftDescriptor), newLabel: "NFTDescriptor" }); } - /// @dev Deploys {SablierV2Comptroller} from a source precompiled with `--via-ir`. - function deployPrecompiledComptroller(address initialAdmin) internal returns (ISablierV2Comptroller) { - return ISablierV2Comptroller( - deployCode("out-optimized/SablierV2Comptroller.sol/SablierV2Comptroller.json", abi.encode(initialAdmin)) - ); - } - - /// @dev Deploys {SablierV2LockupDynamic} from a source precompiled with `--via-ir`. - function deployPrecompiledDynamic( - address initialAdmin, - ISablierV2Comptroller comptroller_, - ISablierV2NFTDescriptor nftDescriptor_ - ) - internal - returns (ISablierV2LockupDynamic) - { - return ISablierV2LockupDynamic( - deployCode( - "out-optimized/SablierV2LockupDynamic.sol/SablierV2LockupDynamic.json", - abi.encode(initialAdmin, address(comptroller_), address(nftDescriptor_), defaults.MAX_SEGMENT_COUNT()) - ) - ); - } - - /// @dev Deploys {SablierV2LockupLinear} from a source precompiled with `--via-ir`. - function deployPrecompiledLinear( - address initialAdmin, - ISablierV2Comptroller comptroller_, - ISablierV2NFTDescriptor nftDescriptor_ - ) - internal - returns (ISablierV2LockupLinear) - { - return ISablierV2LockupLinear( - deployCode( - "out-optimized/SablierV2LockupLinear.sol/SablierV2LockupLinear.json", - abi.encode(initialAdmin, address(comptroller_), address(nftDescriptor_)) - ) - ); - } - - /// @dev Deploys {SablierV2NFTDescriptor} from a source precompiled with `--via-ir`. - function deployPrecompiledNFTDescriptor() internal returns (ISablierV2NFTDescriptor) { - return - ISablierV2NFTDescriptor(deployCode("out-optimized/SablierV2NFTDescriptor.sol/SablierV2NFTDescriptor.json")); - } - /*////////////////////////////////////////////////////////////////////////// CALL EXPECTS //////////////////////////////////////////////////////////////////////////*/ diff --git a/test/unit/concrete/comptroller/Comptroller.t.sol b/test/unit/concrete/comptroller/Comptroller.t.sol index 4d1185ff6..635946627 100644 --- a/test/unit/concrete/comptroller/Comptroller.t.sol +++ b/test/unit/concrete/comptroller/Comptroller.t.sol @@ -16,7 +16,7 @@ contract Comptroller_Unit_Concrete_Test is Base_Test { if (!isTestOptimizedProfile()) { comptroller = new SablierV2Comptroller(users.admin); } else { - comptroller = deployPrecompiledComptroller(users.admin); + comptroller = deployOptimizedComptroller(users.admin); } vm.label({ account: address(comptroller), newLabel: "SablierV2Comptroller" }); } diff --git a/test/utils/DeployOptimized.sol b/test/utils/DeployOptimized.sol new file mode 100644 index 000000000..a21645664 --- /dev/null +++ b/test/utils/DeployOptimized.sol @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity >=0.8.19 <0.9.0; + +import { StdCheats } from "forge-std/StdCheats.sol"; + +import { ISablierV2Comptroller } from "../../src/interfaces/ISablierV2Comptroller.sol"; +import { ISablierV2LockupDynamic } from "../../src/interfaces/ISablierV2LockupDynamic.sol"; +import { ISablierV2LockupLinear } from "../../src/interfaces/ISablierV2LockupLinear.sol"; +import { ISablierV2NFTDescriptor } from "../../src/interfaces/ISablierV2NFTDescriptor.sol"; + +abstract contract DeployOptimized is StdCheats { + /// @dev Deploys {SablierV2Comptroller} from a optimized source compiled with `--via-ir`. + function deployOptimizedComptroller(address initialAdmin) internal returns (ISablierV2Comptroller) { + return ISablierV2Comptroller( + deployCode("out-optimized/SablierV2Comptroller.sol/SablierV2Comptroller.json", abi.encode(initialAdmin)) + ); + } + + /// @dev Deploys {SablierV2LockupDynamic} from a optimized source compiled with `--via-ir`. + function deployOptimizedDynamic( + address initialAdmin, + ISablierV2Comptroller comptroller_, + ISablierV2NFTDescriptor nftDescriptor_, + uint256 maxSegmentCount + ) + internal + returns (ISablierV2LockupDynamic) + { + return ISablierV2LockupDynamic( + deployCode( + "out-optimized/SablierV2LockupDynamic.sol/SablierV2LockupDynamic.json", + abi.encode(initialAdmin, address(comptroller_), address(nftDescriptor_), maxSegmentCount) + ) + ); + } + + /// @dev Deploys {SablierV2LockupLinear} from a optimized source compiled with `--via-ir`. + function deployOptimizedLinear( + address initialAdmin, + ISablierV2Comptroller comptroller_, + ISablierV2NFTDescriptor nftDescriptor_ + ) + internal + returns (ISablierV2LockupLinear) + { + return ISablierV2LockupLinear( + deployCode( + "out-optimized/SablierV2LockupLinear.sol/SablierV2LockupLinear.json", + abi.encode(initialAdmin, address(comptroller_), address(nftDescriptor_)) + ) + ); + } + + /// @dev Deploys {SablierV2NFTDescriptor} from a optimized source compiled with `--via-ir`. + function deployOptimizedNFTDescriptor() internal returns (ISablierV2NFTDescriptor) { + return + ISablierV2NFTDescriptor(deployCode("out-optimized/SablierV2NFTDescriptor.sol/SablierV2NFTDescriptor.json")); + } + + function deployOptimizedCore( + address initialAdmin, + uint256 maxSegmentCount + ) + internal + returns ( + ISablierV2Comptroller comptroller_, + ISablierV2LockupDynamic lockupDynamic_, + ISablierV2LockupLinear lockupLinear_, + ISablierV2NFTDescriptor nftDescriptor_ + ) + { + comptroller_ = deployOptimizedComptroller(initialAdmin); + nftDescriptor_ = deployOptimizedNFTDescriptor(); + lockupDynamic_ = deployOptimizedDynamic(initialAdmin, comptroller_, nftDescriptor_, maxSegmentCount); + lockupLinear_ = deployOptimizedLinear(initialAdmin, comptroller_, nftDescriptor_); + } +} diff --git a/test/utils/Precompiles.t.sol b/test/utils/Precompiles.t.sol index abc5558ab..bbf9475cc 100644 --- a/test/utils/Precompiles.t.sol +++ b/test/utils/Precompiles.t.sol @@ -25,14 +25,15 @@ contract Precompiles_Test is Base_Test { function test_DeployComptroller() external onlyTestOptimizedProfile { address actualComptroller = address(precompiles.deployComptroller(users.admin)); - address expectedComptroller = address(deployPrecompiledComptroller(users.admin)); + address expectedComptroller = address(deployOptimizedComptroller(users.admin)); assertEq(actualComptroller.code, expectedComptroller.code, "bytecodes mismatch"); } function test_DeployLockupDynamic() external onlyTestOptimizedProfile { ISablierV2Comptroller comptroller = precompiles.deployComptroller(users.admin); address actualLockupDynamic = address(precompiles.deployLockupDynamic(users.admin, comptroller, nftDescriptor)); - address expectedLockupDynamic = address(deployPrecompiledDynamic(users.admin, comptroller, nftDescriptor)); + address expectedLockupDynamic = + address(deployOptimizedDynamic(users.admin, comptroller, nftDescriptor, defaults.MAX_SEGMENT_COUNT())); bytes memory expectedLockupDynamicCode = adjustBytecode(expectedLockupDynamic.code, expectedLockupDynamic, actualLockupDynamic); assertEq(actualLockupDynamic.code, expectedLockupDynamicCode, "bytecodes mismatch"); @@ -41,7 +42,7 @@ contract Precompiles_Test is Base_Test { function test_DeployLockupLinear() external onlyTestOptimizedProfile { ISablierV2Comptroller comptroller = precompiles.deployComptroller(users.admin); address actualLockupLinear = address(precompiles.deployLockupLinear(users.admin, comptroller, nftDescriptor)); - address expectedLockupLinear = address(deployPrecompiledLinear(users.admin, comptroller, nftDescriptor)); + address expectedLockupLinear = address(deployOptimizedLinear(users.admin, comptroller, nftDescriptor)); bytes memory expectedLockupLinearCode = adjustBytecode(expectedLockupLinear.code, expectedLockupLinear, actualLockupLinear); assertEq(actualLockupLinear.code, expectedLockupLinearCode, "bytecodes mismatch"); @@ -49,7 +50,7 @@ contract Precompiles_Test is Base_Test { function test_DeployNFTDescriptor() external onlyTestOptimizedProfile { address actualNFTDescriptor = address(precompiles.deployNFTDescriptor()); - address expectedNFTDescriptor = address(deployPrecompiledNFTDescriptor()); + address expectedNFTDescriptor = address(deployOptimizedNFTDescriptor()); assertEq(actualNFTDescriptor.code, expectedNFTDescriptor.code, "bytecodes mismatch"); } @@ -61,21 +62,25 @@ contract Precompiles_Test is Base_Test { ISablierV2NFTDescriptor actualNFTDescriptor ) = precompiles.deployCore(users.admin); - address expectedComptroller = address(deployPrecompiledComptroller(users.admin)); - assertEq(address(actualComptroller).code, expectedComptroller.code, "bytecodes mismatch"); + ( + ISablierV2Comptroller expectedComptroller, + ISablierV2LockupDynamic expectedLockupDynamic, + ISablierV2LockupLinear expectedLockupLinear, + ISablierV2NFTDescriptor expectedNFTDescriptor + ) = deployOptimizedCore(users.admin, defaults.MAX_SEGMENT_COUNT()); + + bytes memory expectedLockupDynamicCode = adjustBytecode( + address(expectedLockupDynamic).code, address(expectedLockupDynamic), address(actualLockupDynamic) + ); - address expectedLockupDynamic = address(deployPrecompiledDynamic(users.admin, actualComptroller, nftDescriptor)); - bytes memory expectedLockupDynamicCode = - adjustBytecode(expectedLockupDynamic.code, expectedLockupDynamic, address(actualLockupDynamic)); - assertEq(address(actualLockupDynamic).code, expectedLockupDynamicCode, "bytecodes mismatch"); + bytes memory expectedLockupLinearCode = adjustBytecode( + address(expectedLockupLinear).code, address(expectedLockupLinear), address(actualLockupLinear) + ); - address expectedLockupLinear = address(deployPrecompiledLinear(users.admin, actualComptroller, nftDescriptor)); - bytes memory expectedLockupLinearCode = - adjustBytecode(expectedLockupLinear.code, expectedLockupLinear, address(actualLockupLinear)); + assertEq(address(actualComptroller).code, address(expectedComptroller).code, "bytecodes mismatch"); + assertEq(address(actualLockupDynamic).code, expectedLockupDynamicCode, "bytecodes mismatch"); assertEq(address(actualLockupLinear).code, expectedLockupLinearCode, "bytecodes mismatch"); - - address expectedNFTDescriptor = address(deployPrecompiledNFTDescriptor()); - assertEq(address(actualNFTDescriptor).code, expectedNFTDescriptor.code, "bytecodes mismatch"); + assertEq(address(actualNFTDescriptor).code, address(expectedNFTDescriptor).code, "bytecodes mismatch"); } /// @dev The expected bytecode has to be adjusted because {SablierV2Lockup} inherits from {NoDelegateCall}, which