The home for Hyperlane core contracts, sdk packages, and other infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hyperlane-monorepo/solidity/test/isms/DomainRoutingIsm.t.sol

139 lines
4.6 KiB

// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import {DomainRoutingIsm} from "../../contracts/isms/routing/DomainRoutingIsm.sol";
import {DefaultFallbackRoutingIsm} from "../../contracts/isms/routing/DefaultFallbackRoutingIsm.sol";
import {DomainRoutingIsmFactory} from "../../contracts/isms/routing/DomainRoutingIsmFactory.sol";
import {IInterchainSecurityModule} from "../../contracts/interfaces/IInterchainSecurityModule.sol";
import {MessageUtils, TestIsm} from "./IsmTestUtils.sol";
import {TestMailbox} from "../../contracts/test/TestMailbox.sol";
1 year ago
import {TestPostDispatchHook} from "../../contracts/test/TestPostDispatchHook.sol";
contract DomainRoutingIsmTest is Test {
address private constant NON_OWNER =
0xCAfEcAfeCAfECaFeCaFecaFecaFECafECafeCaFe;
DomainRoutingIsm internal ism;
function setUp() public virtual {
ism = new DomainRoutingIsm();
ism.initialize(address(this));
}
function deployTestIsm(
bytes32 requiredMetadata
) internal returns (TestIsm) {
return new TestIsm(abi.encode(requiredMetadata));
}
function getMetadata(uint32 domain) internal view returns (bytes memory) {
return TestIsm(address(ism.module(domain))).requiredMetadata();
}
function testSet(uint32 domain) public {
TestIsm _ism = deployTestIsm(bytes32(0));
ism.set(domain, _ism);
assertEq(address(ism.module(domain)), address(_ism));
}
function testRemove(uint32 domain) public {
vm.expectRevert();
ism.remove(domain);
TestIsm _ism = deployTestIsm(bytes32(0));
ism.set(domain, _ism);
ism.remove(domain);
}
function testSetManyViaFactory(uint8 count, uint32 domain) public {
vm.assume(domain > count);
DomainRoutingIsmFactory factory = new DomainRoutingIsmFactory();
uint32[] memory _domains = new uint32[](count);
IInterchainSecurityModule[]
memory _isms = new IInterchainSecurityModule[](count);
for (uint32 i = 0; i < count; ++i) {
_domains[i] = domain - i;
_isms[i] = deployTestIsm(bytes32(0));
}
Refactor key management in infra and key funder (#3023) ### Description * Remove the `bank` role, which we haven't used since the inception of abacus / hyperlane * Big changes to `key-utils.ts` so that there's a single source of truth on what kind of keys are used depending on the role & chain. Before this was sprinkled in a few different places * You can now get an object of `{ [chain]: { [role]: keys[] } }`, so it's super clear what kind of key relates to which chain. For example, before we would use the AWS-based relayer key for EVM chains, and then a GCP-based relayer key for non-EVM chains. But this wasn't really honored by key funder - it had no way of knowing to only fund the AWS relayer on EVM chains, and only fund the GCP relayer on non-EVM chains. Same situation for Kathy, where we want to use AWS keys for EVM chains but the GCP key for non-EVM chains * On v2 and prior to that we were using the AWS-based key for Kathy. Originally, we also launched v3 this way. However it was changed on v3 to use the GCP key for Kathy, causing us to fund both types of addresses on v3. This makes it more clear that we should be using the AWS-based key for Kathy on EVM chains ### Drive-by changes <!-- Are there any minor or drive-by changes also included? --> ### Related issues <!-- - Fixes #[issue number here] --> ### Backward compatibility <!-- Are these changes backward compatible? Are there any infrastructure implications, e.g. changes that would prohibit deploying older commits using this infra tooling? Yes/No --> ### Testing <!-- What kind of testing have these changes undergone? None/Manual/Unit Tests -->
1 year ago
ism = factory.deploy(address(this), _domains, _isms);
for (uint256 i = 0; i < count; ++i) {
assertEq(address(ism.module(_domains[i])), address(_isms[i]));
}
}
function testSetNonOwner(
uint32 domain,
IInterchainSecurityModule _ism
) public {
vm.prank(NON_OWNER);
vm.expectRevert("Ownable: caller is not the owner");
ism.set(domain, _ism);
}
function testVerify(uint32 domain, bytes32 seed) public {
ism.set(domain, deployTestIsm(seed));
bytes memory metadata = getMetadata(domain);
uint256 gasBefore = gasleft();
assertTrue(ism.verify(metadata, MessageUtils.build(domain)));
uint256 gasAfter = gasleft();
console.log("Overhead gas usage: %d", gasBefore - gasAfter);
}
function testVerifyNoIsm(uint32 domain, bytes32 seed) public virtual {
vm.assume(domain > 0);
ism.set(domain, deployTestIsm(seed));
bytes memory metadata = getMetadata(domain);
vm.expectRevert();
ism.verify(metadata, MessageUtils.build(domain - 1));
}
function testRoute(uint32 domain, bytes32 seed) public {
TestIsm testIsm = deployTestIsm(seed);
ism.set(domain, testIsm);
assertEq(
address(ism.route(MessageUtils.build(domain))),
address(testIsm)
);
}
}
contract DefaultFallbackRoutingIsmTest is DomainRoutingIsmTest {
TestIsm defaultIsm;
function setUp() public override {
defaultIsm = deployTestIsm(bytes32(0));
TestMailbox mailbox = new TestMailbox(1000);
1 year ago
TestPostDispatchHook hook = new TestPostDispatchHook();
mailbox.initialize(
address(this),
address(defaultIsm),
address(hook),
address(hook)
);
ism = new DefaultFallbackRoutingIsm(address(mailbox));
ism.initialize(address(this));
}
function testConstructorReverts() public {
vm.expectRevert("MailboxClient: invalid mailbox");
new DefaultFallbackRoutingIsm(address(0));
}
function testVerifyNoIsm(uint32 domain, bytes32 seed) public override {
vm.assume(domain > 0);
ism.set(domain, deployTestIsm(seed));
bytes memory metadata = getMetadata(domain);
bytes memory message = MessageUtils.build(domain - 1);
vm.expectCall(
address(defaultIsm),
abi.encodeCall(defaultIsm.verify, (metadata, message))
);
ism.verify(metadata, message);
}
}