Skip to content

Commit

Permalink
test: add DeployOptimized utils contracts
Browse files Browse the repository at this point in the history
test: move and rename functions from Base_Test
  • Loading branch information
andreivladbrg committed Oct 18, 2023
1 parent edf3609 commit d63ca2c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 71 deletions.
59 changes: 5 additions & 54 deletions test/Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
//////////////////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -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
Expand All @@ -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" });
Expand All @@ -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
//////////////////////////////////////////////////////////////////////////*/
Expand Down
2 changes: 1 addition & 1 deletion test/unit/concrete/comptroller/Comptroller.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
}
Expand Down
77 changes: 77 additions & 0 deletions test/utils/DeployOptimized.sol
Original file line number Diff line number Diff line change
@@ -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_);
}
}
37 changes: 21 additions & 16 deletions test/utils/Precompiles.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -41,15 +42,15 @@ 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");
}

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");
}

Expand All @@ -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
Expand Down

0 comments on commit d63ca2c

Please sign in to comment.