Skip to content

Commit

Permalink
change test library to abstract contract to avoid test contract size …
Browse files Browse the repository at this point in the history
…limiting
  • Loading branch information
RnkSngh committed Mar 20, 2024
1 parent 1c30fc4 commit 110753b
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 28 deletions.
3 changes: 2 additions & 1 deletion test/Dispatcher.base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "../contracts/examples/Mars.sol";
import "../contracts/core/OpLightClient.sol";
import "../contracts/utils/DummyLightClient.sol";
import "../contracts/core/OpProofVerifier.sol";
import {TestUtilsTest} from "./TestUtils.t.sol";

struct LocalEnd {
IbcChannelReceiver receiver;
Expand All @@ -31,7 +32,7 @@ struct ChannelHandshakeSetting {
}

// Base contract for testing Dispatcher
contract Base is IbcEventsEmitter, ProofBase {
contract Base is IbcEventsEmitter, ProofBase, TestUtilsTest {
uint64 UINT64_MAX = 18_446_744_073_709_551_615;

Height ZERO_HEIGHT = Height(0, 0);
Expand Down
3 changes: 1 addition & 2 deletions test/Dispatcher.client.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.s
import "../contracts/examples/Mars.sol";
import "../contracts/core/OpLightClient.sol";
import "./Dispatcher.base.t.sol";
import {TestUtilsTest} from "./TestUtils.t.sol";

abstract contract DispatcherUpdateClientTestSuite is Base {
function test_updateOptimisticConsensusState_success() public {
Expand All @@ -28,6 +27,6 @@ abstract contract DispatcherUpdateClientTestSuite is Base {

contract DispatcherUpdateClientTest is DispatcherUpdateClientTestSuite {
function setUp() public virtual override {
(dispatcherProxy, dispatcherImplementation) = TestUtilsTest.deployDispatcherProxyAndImpl(portPrefix, opLightClient);
(dispatcherProxy, dispatcherImplementation) = deployDispatcherProxyAndImpl(portPrefix, opLightClient);
}
}
4 changes: 1 addition & 3 deletions test/Dispatcher.proof.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import {Dispatcher} from "../contracts/core/Dispatcher.sol";
import {IDispatcher} from "../contracts/interfaces/IDispatcher.sol";
import "../contracts/examples/Mars.sol";
import {IbcDispatcher, IbcEventsEmitter} from "../contracts/interfaces/IbcDispatcher.sol";
import {TestUtilsTest} from "./TestUtils.t.sol";
import "../contracts/core/OpLightClient.sol";
import "./Proof.base.t.sol";
import {stdStorage, StdStorage} from "forge-std/Test.sol";
import {TestUtilsTest} from "./TestUtils.t.sol";

abstract contract DispatcherIbcWithRealProofsSuite is IbcEventsEmitter, Base {
using stdStorage for StdStorage;
Expand Down Expand Up @@ -138,7 +136,7 @@ contract DispatcherIbcWithRealProofs is DispatcherIbcWithRealProofsSuite {
super.setUp();
consensusStateManager = new OptimisticLightClient(1, opProofVerifier, l1BlockProvider);
(dispatcherProxy, dispatcherImplementation) =
TestUtilsTest.deployDispatcherProxyAndImpl("polyibc.eth1.", consensusStateManager);
deployDispatcherProxyAndImpl("polyibc.eth1.", consensusStateManager);
mars = new Mars(dispatcherProxy);
}
}
7 changes: 2 additions & 5 deletions test/Dispatcher.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Dispatcher} from "../contracts/core/Dispatcher.sol";
import {IbcEventsEmitter} from "../contracts/interfaces/IbcDispatcher.sol";
import {IbcReceiver} from "../contracts/interfaces/IbcReceiver.sol";
import {DummyLightClient} from "../contracts/utils/DummyLightClient.sol";
import {TestUtilsTest} from "./TestUtils.t.sol";
import "../contracts/examples/Mars.sol";
import "../contracts/core/OpLightClient.sol";
import "./Dispatcher.base.t.sol";
Expand Down Expand Up @@ -181,8 +180,7 @@ abstract contract ChannelHandshakeTestSuite is ChannelHandshakeUtils {

contract ChannelHandshakeTest is ChannelHandshakeTestSuite {
function setUp() public virtual override {
(dispatcherProxy, dispatcherImplementation) =
TestUtilsTest.deployDispatcherProxyAndImpl(portPrefix, dummyConsStateManager);
(dispatcherProxy, dispatcherImplementation) = deployDispatcherProxyAndImpl(portPrefix, dummyConsStateManager);
mars = new Mars(dispatcherProxy);
_local = LocalEnd(mars, portId, "channel-1", connectionHops, "1.0", "1.0");
_remote = CounterParty("eth2.7E5F4552091A69125d5DfCb7b8C2659029395Bdf", "channel-2", "1.0");
Expand All @@ -204,8 +202,7 @@ contract ChannelOpenTestBaseSetup is Base {
RevertingBytesMars revertingBytesMars;

function setUp() public virtual override {
(dispatcherProxy, dispatcherImplementation) =
TestUtilsTest.deployDispatcherProxyAndImpl(portPrefix, dummyConsStateManager);
(dispatcherProxy, dispatcherImplementation) = deployDispatcherProxyAndImpl(portPrefix, dummyConsStateManager);
ChannelHandshakeSetting memory setting =
ChannelHandshakeSetting(ChannelOrder.ORDERED, feeEnabled, true, validProof);

Expand Down
2 changes: 1 addition & 1 deletion test/TestUtils.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Vm} from "forge-std/Vm.sol";

pragma solidity ^0.8.0;

library TestUtilsTest {
abstract contract TestUtilsTest {
bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
Expand Down
5 changes: 2 additions & 3 deletions test/VirtualChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct VirtualChainData {
}

// A test contract that keeps two types of dApps, 1. regular IBC-enabled dApp, 2. universal channel dApp
contract VirtualChain is Test, IbcEventsEmitter {
contract VirtualChain is Test, IbcEventsEmitter, TestUtilsTest {
IDispatcher public dispatcherProxy;
Dispatcher public dispatcherImplementation;
UniversalChannelHandler public ucHandler;
Expand All @@ -59,8 +59,7 @@ contract VirtualChain is Test, IbcEventsEmitter {
constructor(uint256 seed, string memory portPrefix) {
_seed = seed;

(dispatcherProxy, dispatcherImplementation) =
TestUtilsTest.deployDispatcherProxyAndImpl(portPrefix, new DummyLightClient());
(dispatcherProxy, dispatcherImplementation) = deployDispatcherProxyAndImpl(portPrefix, new DummyLightClient());
ucHandler = new UniversalChannelHandler(dispatcherProxy);

mars = new Mars(dispatcherProxy);
Expand Down
10 changes: 4 additions & 6 deletions test/upgradeableProxy/Dispatcher.upgrade.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.13;
import "forge-std/console2.sol";
import {DispatcherUpdateClientTestSuite} from "../Dispatcher.client.t.sol";
import {DispatcherIbcWithRealProofsSuite} from "../Dispatcher.proof.t.sol";
import {TestUtilsTest} from "../TestUtils.t.sol";
import {Mars} from "../../contracts/examples/Mars.sol";
import "../../contracts/core/OpLightClient.sol";
import {ChannelHandshakeTestSuite, ChannelHandshakeTest, ChannelHandshakeUtils} from "../Dispatcher.t.sol";
Expand All @@ -23,7 +22,7 @@ import {IDispatcher} from "../../contracts/interfaces/IDispatcher.sol";
import {DispatcherV2Initializable} from "./upgrades/DispatcherV2Initializable.sol";
import {DispatcherV2} from "./upgrades/DispatcherV2.sol";

library UpgradeTestUtils {
abstract contract UpgradeTestUtils {
function upgradeDispatcher(LightClient consensusStateManager, string memory portPrefix, address dispatcherProxy)
public
returns (DispatcherV2Initializable newDispatcherImplementation)
Expand Down Expand Up @@ -110,10 +109,9 @@ contract ChannelHandShakeUpgradeUtil is ChannelHandshakeUtils {
}
}

contract DispatcherUpgradeTest is ChannelHandShakeUpgradeUtil {
contract DispatcherUpgradeTest is ChannelHandShakeUpgradeUtil, UpgradeTestUtils {
function setUp() public override {
(dispatcherProxy, dispatcherImplementation) =
TestUtilsTest.deployDispatcherProxyAndImpl(portPrefix, dummyConsStateManager);
(dispatcherProxy, dispatcherImplementation) = deployDispatcherProxyAndImpl(portPrefix, dummyConsStateManager);
mars = new Mars(dispatcherProxy);
_local = LocalEnd(mars, portId, "channel-1", connectionHops, "1.0", "1.0");
_remote = CounterParty("eth2.7E5F4552091A69125d5DfCb7b8C2659029395Bdf", "channel-2", "1.0");
Expand All @@ -124,7 +122,7 @@ contract DispatcherUpgradeTest is ChannelHandShakeUpgradeUtil {
sendPacket(_local.channelId);

// Upgrade dispatcherProxy for tests
UpgradeTestUtils.upgradeDispatcher(newLightClient, "adfsafsa", address(dispatcherProxy));
upgradeDispatcher(newLightClient, "adfsafsa", address(dispatcherProxy));
}

function test_SentPacketState_Conserved() public {
Expand Down
12 changes: 5 additions & 7 deletions test/upgradeableProxy/DispatcherUUPS.accessControl.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import "../Dispatcher.base.t.sol";
import {DispatcherV2} from "./upgrades/DispatcherV2.sol";
import {DispatcherV2Initializable} from "./upgrades/DispatcherV2Initializable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
import {TestUtilsTest} from "../TestUtils.t.sol";
import {DummyLightClient} from "../../contracts/utils/DummyLightClient.sol";

contract DispatcherUUPSAccessControl is Base {
Expand All @@ -22,16 +21,15 @@ contract DispatcherUUPSAccessControl is Base {
DispatcherV2Initializable dispatcherImplementation3;

function setUp() public override {
(dispatcherProxy, dispatcherImplementation) =
TestUtilsTest.deployDispatcherProxyAndImpl(portPrefix, dummyConsStateManager);
(dispatcherProxy, dispatcherImplementation) = deployDispatcherProxyAndImpl(portPrefix, dummyConsStateManager);
dispatcherImplementation2 = new DispatcherV2();
dispatcherImplementation3 = new DispatcherV2Initializable();
}

function test_Dispatcher_Allows_Upgrade() public {
assertEq(address(dispatcherImplementation), TestUtilsTest.getProxyImplementation(address(dispatcherProxy), vm));
assertEq(address(dispatcherImplementation), getProxyImplementation(address(dispatcherProxy), vm));
UUPSUpgradeable(address(dispatcherProxy)).upgradeTo(address(dispatcherImplementation2));
assertEq(address(dispatcherImplementation2), TestUtilsTest.getProxyImplementation(address(dispatcherProxy), vm));
assertEq(address(dispatcherImplementation2), getProxyImplementation(address(dispatcherProxy), vm));
assertEq(dispatcherProxy.portPrefix(), portPrefix);
assertEq(
address(uint160(uint256(vm.load(address(dispatcherProxy), bytes32(uint256(110)))))),
Expand All @@ -40,10 +38,10 @@ contract DispatcherUUPSAccessControl is Base {
}

function test_Dispatcher_Allows_Upgrade_To_And_Call() public {
assertEq(address(dispatcherImplementation), TestUtilsTest.getProxyImplementation(address(dispatcherProxy), vm));
assertEq(address(dispatcherImplementation), getProxyImplementation(address(dispatcherProxy), vm));
bytes memory initData = abi.encodeWithSignature("initialize(string,address)", portPrefix2, lightClient2);
UUPSUpgradeable(address(dispatcherProxy)).upgradeToAndCall(address(dispatcherImplementation3), initData);
assertEq(address(dispatcherImplementation3), TestUtilsTest.getProxyImplementation(address(dispatcherProxy), vm));
assertEq(address(dispatcherImplementation3), getProxyImplementation(address(dispatcherProxy), vm));
assertEq(dispatcherProxy.portPrefix(), portPrefix2);
assertEq(
address(uint160(uint256(vm.load(address(dispatcherProxy), bytes32(uint256(110)))))), address(lightClient2)
Expand Down

0 comments on commit 110753b

Please sign in to comment.