refactor: Rename InterchainAccountRouter tests for easier reuse (#4336)

### Description

Minor refactor of test
pull/4363/head
Nam Chu Hoai 3 months ago committed by GitHub
parent cab86f2f97
commit 0779a23db6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      solidity/contracts/mock/MockHyperlaneEnvironment.sol
  2. 173
      solidity/test/InterchainAccountRouter.t.sol

@ -8,8 +8,8 @@ import "../test/TestIsm.sol";
import {TypeCasts} from "../libs/TypeCasts.sol";
contract MockHyperlaneEnvironment {
uint32 originDomain;
uint32 destinationDomain;
uint32 public originDomain;
uint32 public destinationDomain;
mapping(uint32 => MockMailbox) public mailboxes;
mapping(uint32 => TestInterchainGasPaymaster) public igps;

@ -38,7 +38,7 @@ contract FailingIsm is IInterchainSecurityModule {
}
}
contract InterchainAccountRouterTest is Test {
contract InterchainAccountRouterTestBase is Test {
using TypeCasts for address;
event InterchainAccountCreated(
@ -55,8 +55,8 @@ contract InterchainAccountRouterTest is Test {
TestInterchainGasPaymaster internal igp;
InterchainAccountIsm internal icaIsm;
InterchainAccountRouter internal originRouter;
InterchainAccountRouter internal destinationRouter;
InterchainAccountRouter internal originIcaRouter;
InterchainAccountRouter internal destinationIcaRouter;
bytes32 internal ismOverride;
bytes32 internal routerOverride;
uint256 gasPaymentQuote;
@ -90,7 +90,7 @@ contract InterchainAccountRouterTest is Test {
return InterchainAccountRouter(address(proxy));
}
function setUp() public {
function setUp() public virtual {
environment = new MockHyperlaneEnvironment(origin, destination);
igp = new TestInterchainGasPaymaster();
@ -104,13 +104,13 @@ contract InterchainAccountRouterTest is Test {
);
address owner = address(this);
originRouter = deployProxiedIcaRouter(
originIcaRouter = deployProxiedIcaRouter(
environment.mailboxes(origin),
environment.igps(destination),
icaIsm,
owner
);
destinationRouter = deployProxiedIcaRouter(
destinationIcaRouter = deployProxiedIcaRouter(
environment.mailboxes(destination),
environment.igps(destination),
icaIsm,
@ -119,47 +119,55 @@ contract InterchainAccountRouterTest is Test {
environment.mailboxes(origin).setDefaultHook(address(igp));
routerOverride = TypeCasts.addressToBytes32(address(destinationRouter));
routerOverride = TypeCasts.addressToBytes32(
address(destinationIcaRouter)
);
ismOverride = TypeCasts.addressToBytes32(
address(environment.isms(destination))
);
ica = destinationRouter.getLocalInterchainAccount(
ica = destinationIcaRouter.getLocalInterchainAccount(
origin,
address(this),
address(originRouter),
address(originIcaRouter),
address(environment.isms(destination))
);
target = new Callable();
}
receive() external payable {}
}
contract InterchainAccountRouterTest is InterchainAccountRouterTestBase {
using TypeCasts for address;
function testFuzz_constructor(address _localOwner) public {
OwnableMulticall _account = destinationRouter
OwnableMulticall _account = destinationIcaRouter
.getDeployedInterchainAccount(
origin,
_localOwner,
address(originRouter),
address(originIcaRouter),
address(environment.isms(destination))
);
assertEq(_account.owner(), address(destinationRouter));
assertEq(_account.owner(), address(destinationIcaRouter));
}
function testFuzz_getRemoteInterchainAccount(
address _localOwner,
address _ism
) public {
address _account = originRouter.getRemoteInterchainAccount(
address _account = originIcaRouter.getRemoteInterchainAccount(
address(_localOwner),
address(destinationRouter),
address(destinationIcaRouter),
_ism
);
originRouter.enrollRemoteRouterAndIsm(
originIcaRouter.enrollRemoteRouterAndIsm(
destination,
routerOverride,
TypeCasts.addressToBytes32(_ism)
);
assertEq(
originRouter.getRemoteInterchainAccount(
originIcaRouter.getRemoteInterchainAccount(
destination,
address(_localOwner)
),
@ -184,16 +192,16 @@ contract InterchainAccountRouterTest is Test {
}
// act
originRouter.enrollRemoteRouters(domains, routers);
originIcaRouter.enrollRemoteRouters(domains, routers);
// assert
uint32[] memory actualDomains = originRouter.domains();
uint32[] memory actualDomains = originIcaRouter.domains();
assertEq(actualDomains.length, domains.length);
assertEq(abi.encode(originRouter.domains()), abi.encode(domains));
assertEq(abi.encode(originIcaRouter.domains()), abi.encode(domains));
for (uint256 i = 0; i < count; i++) {
bytes32 actualRouter = originRouter.routers(domains[i]);
bytes32 actualIsm = originRouter.isms(domains[i]);
bytes32 actualRouter = originIcaRouter.routers(domains[i]);
bytes32 actualIsm = originIcaRouter.isms(domains[i]);
assertEq(actualRouter, routers[i]);
assertEq(actualIsm, bytes32(0));
@ -208,17 +216,17 @@ contract InterchainAccountRouterTest is Test {
vm.assume(router != bytes32(0));
// arrange pre-condition
bytes32 actualRouter = originRouter.routers(destination);
bytes32 actualIsm = originRouter.isms(destination);
bytes32 actualRouter = originIcaRouter.routers(destination);
bytes32 actualIsm = originIcaRouter.isms(destination);
assertEq(actualRouter, bytes32(0));
assertEq(actualIsm, bytes32(0));
// act
originRouter.enrollRemoteRouterAndIsm(destination, router, ism);
originIcaRouter.enrollRemoteRouterAndIsm(destination, router, ism);
// assert
actualRouter = originRouter.routers(destination);
actualIsm = originRouter.isms(destination);
actualRouter = originIcaRouter.routers(destination);
actualIsm = originIcaRouter.isms(destination);
assertEq(actualRouter, router);
assertEq(actualIsm, ism);
}
@ -234,17 +242,21 @@ contract InterchainAccountRouterTest is Test {
destinations.length != isms.length
) {
vm.expectRevert(bytes("length mismatch"));
originRouter.enrollRemoteRouterAndIsms(destinations, routers, isms);
originIcaRouter.enrollRemoteRouterAndIsms(
destinations,
routers,
isms
);
return;
}
// act
originRouter.enrollRemoteRouterAndIsms(destinations, routers, isms);
originIcaRouter.enrollRemoteRouterAndIsms(destinations, routers, isms);
// assert
for (uint256 i = 0; i < destinations.length; i++) {
bytes32 actualRouter = originRouter.routers(destinations[i]);
bytes32 actualIsm = originRouter.isms(destinations[i]);
bytes32 actualRouter = originIcaRouter.routers(destinations[i]);
bytes32 actualIsm = originIcaRouter.isms(destinations[i]);
assertEq(actualRouter, routers[i]);
assertEq(actualIsm, isms[i]);
}
@ -259,13 +271,13 @@ contract InterchainAccountRouterTest is Test {
vm.assume(routerA != bytes32(0) && routerB != bytes32(0));
// act
originRouter.enrollRemoteRouterAndIsm(destination, routerA, ismA);
originIcaRouter.enrollRemoteRouterAndIsm(destination, routerA, ismA);
// assert
vm.expectRevert(
bytes("router and ISM defaults are immutable once set")
);
originRouter.enrollRemoteRouterAndIsm(destination, routerB, ismB);
originIcaRouter.enrollRemoteRouterAndIsm(destination, routerB, ismB);
}
function testFuzz_enrollRemoteRouterAndIsmNonOwner(
@ -273,14 +285,16 @@ contract InterchainAccountRouterTest is Test {
bytes32 router,
bytes32 ism
) public {
vm.assume(newOwner != address(0) && newOwner != originRouter.owner());
vm.assume(
newOwner != address(0) && newOwner != originIcaRouter.owner()
);
// act
originRouter.transferOwnership(newOwner);
originIcaRouter.transferOwnership(newOwner);
// assert
vm.expectRevert(bytes("Ownable: caller is not the owner"));
originRouter.enrollRemoteRouterAndIsm(destination, router, ism);
originIcaRouter.enrollRemoteRouterAndIsm(destination, router, ism);
}
function getCalls(
@ -300,7 +314,7 @@ contract InterchainAccountRouterTest is Test {
function assertRemoteCallReceived(bytes32 data) private {
assertEq(target.data(address(this)), bytes32(0));
vm.expectEmit(true, true, false, true, address(destinationRouter));
vm.expectEmit(true, true, false, true, address(destinationIcaRouter));
emit InterchainAccountCreated(
origin,
address(this).addressToBytes32(),
@ -325,16 +339,15 @@ contract InterchainAccountRouterTest is Test {
address owner
) public {
// act
ica = destinationRouter.getDeployedInterchainAccount(
ica = destinationIcaRouter.getDeployedInterchainAccount(
origin,
owner,
address(originRouter),
address(originIcaRouter),
address(environment.isms(destination))
);
(uint32 domain, bytes32 ownerBytes) = destinationRouter.accountOwners(
address(ica)
);
(uint32 domain, bytes32 ownerBytes) = destinationIcaRouter
.accountOwners(address(ica));
// assert
assertEq(domain, origin);
assertEq(ownerBytes, owner.addressToBytes32());
@ -342,19 +355,19 @@ contract InterchainAccountRouterTest is Test {
function test_quoteGasPayment() public {
// arrange
originRouter.enrollRemoteRouterAndIsm(
originIcaRouter.enrollRemoteRouterAndIsm(
destination,
routerOverride,
ismOverride
);
// assert
assertEq(originRouter.quoteGasPayment(destination), gasPaymentQuote);
assertEq(originIcaRouter.quoteGasPayment(destination), gasPaymentQuote);
}
function test_quoteGasPayment_gasLimitOverride() public {
// arrange
originRouter.enrollRemoteRouterAndIsm(
originIcaRouter.enrollRemoteRouterAndIsm(
destination,
routerOverride,
ismOverride
@ -362,14 +375,18 @@ contract InterchainAccountRouterTest is Test {
// assert
assertEq(
originRouter.quoteGasPayment(destination, "", GAS_LIMIT_OVERRIDE),
originIcaRouter.quoteGasPayment(
destination,
"",
GAS_LIMIT_OVERRIDE
),
igp.quoteGasPayment(destination, GAS_LIMIT_OVERRIDE)
);
}
function testFuzz_singleCallRemoteWithDefault(bytes32 data) public {
// arrange
originRouter.enrollRemoteRouterAndIsm(
originIcaRouter.enrollRemoteRouterAndIsm(
destination,
routerOverride,
ismOverride
@ -378,7 +395,7 @@ contract InterchainAccountRouterTest is Test {
// act
CallLib.Call[] memory calls = getCalls(data);
originRouter.callRemote{value: gasPaymentQuote}(
originIcaRouter.callRemote{value: gasPaymentQuote}(
destination,
TypeCasts.bytes32ToAddress(calls[0].to),
calls[0].value,
@ -393,7 +410,7 @@ contract InterchainAccountRouterTest is Test {
function testFuzz_callRemoteWithDefault(bytes32 data) public {
// arrange
originRouter.enrollRemoteRouterAndIsm(
originIcaRouter.enrollRemoteRouterAndIsm(
destination,
routerOverride,
ismOverride
@ -401,7 +418,7 @@ contract InterchainAccountRouterTest is Test {
uint256 balanceBefore = address(this).balance;
// act
originRouter.callRemote{value: gasPaymentQuote}(
originIcaRouter.callRemote{value: gasPaymentQuote}(
destination,
getCalls(data)
);
@ -414,7 +431,7 @@ contract InterchainAccountRouterTest is Test {
function testFuzz_overrideAndCallRemote(bytes32 data) public {
// arrange
originRouter.enrollRemoteRouterAndIsm(
originIcaRouter.enrollRemoteRouterAndIsm(
destination,
routerOverride,
ismOverride
@ -422,7 +439,7 @@ contract InterchainAccountRouterTest is Test {
uint256 balanceBefore = address(this).balance;
// act
originRouter.callRemote{value: gasPaymentQuote}(
originIcaRouter.callRemote{value: gasPaymentQuote}(
destination,
getCalls(data)
);
@ -439,7 +456,7 @@ contract InterchainAccountRouterTest is Test {
// assert error
CallLib.Call[] memory calls = getCalls(data);
vm.expectRevert(bytes("no router specified for destination"));
originRouter.callRemote(destination, calls);
originIcaRouter.callRemote(destination, calls);
}
function testFuzz_customMetadata_forIgp(
@ -454,7 +471,7 @@ contract InterchainAccountRouterTest is Test {
address(this),
""
);
originRouter.enrollRemoteRouterAndIsm(
originIcaRouter.enrollRemoteRouterAndIsm(
destination,
routerOverride,
ismOverride
@ -462,11 +479,9 @@ contract InterchainAccountRouterTest is Test {
uint256 balanceBefore = address(this).balance;
// act
originRouter.callRemote{value: gasLimit * igp.gasPrice() + overpayment}(
destination,
getCalls(data),
metadata
);
originIcaRouter.callRemote{
value: gasLimit * igp.gasPrice() + overpayment
}(destination, getCalls(data), metadata);
// assert
uint256 balanceAfter = address(this).balance;
@ -488,7 +503,7 @@ contract InterchainAccountRouterTest is Test {
address(this),
""
);
originRouter.enrollRemoteRouterAndIsm(
originIcaRouter.enrollRemoteRouterAndIsm(
destination,
routerOverride,
ismOverride
@ -496,7 +511,11 @@ contract InterchainAccountRouterTest is Test {
// act
vm.expectRevert("IGP: insufficient interchain gas payment");
originRouter.callRemote{value: payment}(destination, calls, metadata);
originIcaRouter.callRemote{value: payment}(
destination,
calls,
metadata
);
}
function testFuzz_callRemoteWithOverrides_default(bytes32 data) public {
@ -504,7 +523,7 @@ contract InterchainAccountRouterTest is Test {
uint256 balanceBefore = address(this).balance;
// act
originRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
originIcaRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
destination,
routerOverride,
ismOverride,
@ -531,13 +550,9 @@ contract InterchainAccountRouterTest is Test {
uint256 balanceBefore = address(this).balance;
// act
originRouter.callRemoteWithOverrides{value: gasLimit * igp.gasPrice()}(
destination,
routerOverride,
ismOverride,
getCalls(data),
metadata
);
originIcaRouter.callRemoteWithOverrides{
value: gasLimit * igp.gasPrice()
}(destination, routerOverride, ismOverride, getCalls(data), metadata);
// assert
uint256 balanceAfter = address(this).balance;
@ -553,7 +568,7 @@ contract InterchainAccountRouterTest is Test {
);
// act
originRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
originIcaRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
destination,
routerOverride,
failingIsm,
@ -573,7 +588,7 @@ contract InterchainAccountRouterTest is Test {
// act
environment.mailboxes(destination).setDefaultIsm(address(failingIsm));
originRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
originIcaRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
destination,
routerOverride,
bytes32(0),
@ -588,20 +603,20 @@ contract InterchainAccountRouterTest is Test {
function testFuzz_getLocalInterchainAccount(bytes32 data) public {
// check
OwnableMulticall destinationIca = destinationRouter
OwnableMulticall destinationIca = destinationIcaRouter
.getLocalInterchainAccount(
origin,
address(this),
address(originRouter),
address(originIcaRouter),
address(environment.isms(destination))
);
assertEq(
address(destinationIca),
address(
destinationRouter.getLocalInterchainAccount(
destinationIcaRouter.getLocalInterchainAccount(
origin,
TypeCasts.addressToBytes32(address(this)),
TypeCasts.addressToBytes32(address(originRouter)),
TypeCasts.addressToBytes32(address(originIcaRouter)),
address(environment.isms(destination))
)
)
@ -609,7 +624,7 @@ contract InterchainAccountRouterTest is Test {
assertEq(address(destinationIca).code.length, 0);
// act
originRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
originIcaRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
destination,
routerOverride,
ismOverride,
@ -631,10 +646,10 @@ contract InterchainAccountRouterTest is Test {
require(success, "transfer before deploy failed");
// receive value after deployed
destinationRouter.getDeployedInterchainAccount(
destinationIcaRouter.getDeployedInterchainAccount(
origin,
address(this),
address(originRouter),
address(originIcaRouter),
address(environment.isms(destination))
);
assert(address(ica).code.length > 0);
@ -658,7 +673,7 @@ contract InterchainAccountRouterTest is Test {
CallLib.Call[] memory calls = new CallLib.Call[](1);
calls[0] = call;
originRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
originIcaRouter.callRemoteWithOverrides{value: gasPaymentQuote}(
destination,
routerOverride,
ismOverride,
@ -668,6 +683,4 @@ contract InterchainAccountRouterTest is Test {
vm.expectCall(address(this), value, data);
environment.processNextPendingMessage();
}
receive() external payable {}
}

Loading…
Cancel
Save