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.
234 lines
7.7 KiB
234 lines
7.7 KiB
3 years ago
|
/* eslint-disable @typescript-eslint/no-floating-promises */
|
||
3 years ago
|
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
|
||
3 years ago
|
import { expect } from 'chai';
|
||
3 years ago
|
import { ContractTransaction } from 'ethers';
|
||
3 years ago
|
import { ethers } from 'hardhat';
|
||
|
|
||
2 years ago
|
import { utils } from '@hyperlane-xyz/utils';
|
||
|
|
||
3 years ago
|
import {
|
||
3 years ago
|
AbacusConnectionManager,
|
||
|
AbacusConnectionManager__factory,
|
||
3 years ago
|
InterchainGasPaymaster,
|
||
|
InterchainGasPaymaster__factory,
|
||
|
Outbox,
|
||
3 years ago
|
Outbox__factory,
|
||
2 years ago
|
TestInbox,
|
||
|
TestInbox__factory,
|
||
|
TestMultisigValidatorManager__factory,
|
||
2 years ago
|
} from '../types';
|
||
3 years ago
|
import { TestRouter, TestRouter__factory } from '../types';
|
||
|
|
||
|
const ONLY_OWNER_REVERT_MSG = 'Ownable: caller is not the owner';
|
||
|
const origin = 1;
|
||
|
const destination = 2;
|
||
3 years ago
|
const destinationWithoutRouter = 3;
|
||
3 years ago
|
const message = '0xdeadbeef';
|
||
|
|
||
|
describe('Router', async () => {
|
||
|
let router: TestRouter,
|
||
3 years ago
|
outbox: Outbox,
|
||
3 years ago
|
connectionManager: AbacusConnectionManager,
|
||
3 years ago
|
signer: SignerWithAddress,
|
||
|
nonOwner: SignerWithAddress;
|
||
|
|
||
|
before(async () => {
|
||
|
[signer, nonOwner] = await ethers.getSigners();
|
||
|
});
|
||
|
|
||
|
beforeEach(async () => {
|
||
3 years ago
|
const connectionManagerFactory = new AbacusConnectionManager__factory(
|
||
|
signer,
|
||
|
);
|
||
3 years ago
|
connectionManager = await connectionManagerFactory.deploy();
|
||
|
|
||
3 years ago
|
const outboxFactory = new Outbox__factory(signer);
|
||
|
outbox = await outboxFactory.deploy(origin);
|
||
|
// dispatch dummy message
|
||
|
await outbox.dispatch(
|
||
|
destination,
|
||
|
utils.addressToBytes32(outbox.address),
|
||
|
'0x',
|
||
|
);
|
||
|
await connectionManager.setOutbox(outbox.address);
|
||
|
|
||
2 years ago
|
router = await new TestRouter__factory(signer).deploy();
|
||
3 years ago
|
});
|
||
|
|
||
2 years ago
|
describe('#initialize', () => {
|
||
|
it('should set the abacus connection manager', async () => {
|
||
|
await router.initialize(connectionManager.address);
|
||
|
expect(await router.abacusConnectionManager()).to.equal(
|
||
|
connectionManager.address,
|
||
|
);
|
||
|
});
|
||
3 years ago
|
|
||
2 years ago
|
it('should transfer owner to deployer', async () => {
|
||
|
await router.initialize(connectionManager.address);
|
||
|
expect(await router.owner()).to.equal(signer.address);
|
||
|
});
|
||
3 years ago
|
|
||
2 years ago
|
it('should use overloaded initialize', async () => {
|
||
|
await expect(router.initialize(connectionManager.address)).to.emit(
|
||
|
router,
|
||
|
'InitializeOverload',
|
||
|
);
|
||
|
});
|
||
3 years ago
|
|
||
2 years ago
|
it('cannot be initialized twice', async () => {
|
||
|
await router.initialize(ethers.constants.AddressZero);
|
||
|
await expect(
|
||
|
router.initialize(ethers.constants.AddressZero),
|
||
|
).to.be.revertedWith('Initializable: contract is already initialized');
|
||
|
});
|
||
3 years ago
|
});
|
||
|
|
||
2 years ago
|
describe('when initialized', () => {
|
||
2 years ago
|
let inbox: TestInbox;
|
||
|
|
||
2 years ago
|
beforeEach(async () => {
|
||
|
await router.initialize(connectionManager.address);
|
||
2 years ago
|
const validatorManger = await new TestMultisigValidatorManager__factory(
|
||
|
signer,
|
||
|
).deploy(origin, [signer.address], 1);
|
||
|
inbox = await new TestInbox__factory(signer).deploy(destination);
|
||
|
await inbox.initialize(origin, validatorManger.address);
|
||
2 years ago
|
});
|
||
3 years ago
|
|
||
2 years ago
|
it('accepts message from enrolled inbox and router', async () => {
|
||
2 years ago
|
await connectionManager.enrollInbox(origin, inbox.address);
|
||
2 years ago
|
const remote = utils.addressToBytes32(nonOwner.address);
|
||
|
await router.enrollRemoteRouter(origin, remote);
|
||
2 years ago
|
const recipient = utils.addressToBytes32(router.address);
|
||
2 years ago
|
// Does not revert.
|
||
2 years ago
|
await inbox.testHandle(origin, remote, recipient, message);
|
||
2 years ago
|
});
|
||
3 years ago
|
|
||
2 years ago
|
it('rejects message from unenrolled inbox', async () => {
|
||
|
await expect(
|
||
|
router.handle(
|
||
|
origin,
|
||
|
utils.addressToBytes32(nonOwner.address),
|
||
|
message,
|
||
|
),
|
||
|
).to.be.revertedWith('!inbox');
|
||
|
});
|
||
|
|
||
|
it('rejects message from unenrolled router', async () => {
|
||
2 years ago
|
await connectionManager.enrollInbox(origin, inbox.address);
|
||
|
const recipient = utils.addressToBytes32(router.address);
|
||
|
const sender = utils.addressToBytes32(nonOwner.address);
|
||
2 years ago
|
await expect(
|
||
2 years ago
|
inbox.testHandle(origin, sender, recipient, message),
|
||
2 years ago
|
).to.be.revertedWith(
|
||
|
`No router enrolled for domain. Did you specify the right domain ID?`,
|
||
|
);
|
||
2 years ago
|
});
|
||
|
|
||
|
it('owner can enroll remote router', async () => {
|
||
|
const remote = nonOwner.address;
|
||
|
const remoteBytes = utils.addressToBytes32(nonOwner.address);
|
||
|
expect(await router.isRemoteRouter(origin, remoteBytes)).to.equal(false);
|
||
|
await expect(router.mustHaveRemoteRouter(origin)).to.be.revertedWith(
|
||
2 years ago
|
`No router enrolled for domain. Did you specify the right domain ID?`,
|
||
3 years ago
|
);
|
||
2 years ago
|
await router.enrollRemoteRouter(origin, utils.addressToBytes32(remote));
|
||
|
expect(await router.isRemoteRouter(origin, remoteBytes)).to.equal(true);
|
||
|
expect(await router.mustHaveRemoteRouter(origin)).to.equal(remoteBytes);
|
||
3 years ago
|
});
|
||
|
|
||
2 years ago
|
it('non-owner cannot enroll remote router', async () => {
|
||
|
await expect(
|
||
|
router
|
||
|
.connect(nonOwner)
|
||
|
.enrollRemoteRouter(origin, utils.addressToBytes32(nonOwner.address)),
|
||
|
).to.be.revertedWith(ONLY_OWNER_REVERT_MSG);
|
||
|
});
|
||
3 years ago
|
|
||
2 years ago
|
describe('dispatch functions', () => {
|
||
|
let interchainGasPaymaster: InterchainGasPaymaster;
|
||
|
beforeEach(async () => {
|
||
|
const interchainGasPaymasterFactory =
|
||
|
new InterchainGasPaymaster__factory(signer);
|
||
|
interchainGasPaymaster = await interchainGasPaymasterFactory.deploy();
|
||
|
await router.setInterchainGasPaymaster(interchainGasPaymaster.address);
|
||
|
|
||
|
// Enroll a remote router on the destination domain.
|
||
|
// The address is arbitrary because no messages will actually be processed.
|
||
|
await router.enrollRemoteRouter(
|
||
|
destination,
|
||
|
utils.addressToBytes32(nonOwner.address),
|
||
|
);
|
||
3 years ago
|
});
|
||
|
|
||
2 years ago
|
// Helper for testing different variations of dispatch functions
|
||
|
const runDispatchFunctionTests = async (
|
||
|
dispatchFunction: (
|
||
|
destinationDomain: number,
|
||
|
interchainGasPayment?: number,
|
||
|
) => Promise<ContractTransaction>,
|
||
|
expectGasPayment: boolean,
|
||
|
) => {
|
||
|
// Allows a Chai Assertion to be programmatically negated
|
||
|
const expectAssertion = (
|
||
|
assertion: Chai.Assertion,
|
||
|
expected: boolean,
|
||
|
) => {
|
||
|
return expected ? assertion : assertion.not;
|
||
|
};
|
||
|
|
||
|
it('dispatches a message', async () => {
|
||
|
await expect(dispatchFunction(destination)).to.emit(
|
||
|
outbox,
|
||
|
'Dispatch',
|
||
|
);
|
||
|
});
|
||
|
|
||
|
it(`${
|
||
|
expectGasPayment ? 'pays' : 'does not pay'
|
||
|
} interchain gas`, async () => {
|
||
|
const testInterchainGasPayment = 1234;
|
||
|
const leafIndex = await outbox.count();
|
||
|
const assertion = expectAssertion(
|
||
|
expect(dispatchFunction(destination, testInterchainGasPayment)).to,
|
||
|
expectGasPayment,
|
||
|
);
|
||
|
await assertion
|
||
|
.emit(interchainGasPaymaster, 'GasPayment')
|
||
|
.withArgs(outbox.address, leafIndex, testInterchainGasPayment);
|
||
|
});
|
||
|
|
||
|
it('reverts when dispatching a message to an unenrolled remote router', async () => {
|
||
|
await expect(
|
||
|
dispatchFunction(destinationWithoutRouter),
|
||
2 years ago
|
).to.be.revertedWith(
|
||
|
`No router enrolled for domain. Did you specify the right domain ID?`,
|
||
|
);
|
||
2 years ago
|
});
|
||
|
};
|
||
|
|
||
|
describe('#dispatch', () => {
|
||
|
runDispatchFunctionTests(
|
||
|
(destinationDomain) => router.dispatch(destinationDomain, '0x'),
|
||
|
false,
|
||
3 years ago
|
);
|
||
|
});
|
||
|
|
||
2 years ago
|
describe('#dispatchWithGas', () => {
|
||
|
runDispatchFunctionTests(
|
||
|
(destinationDomain, interchainGasPayment = 0) =>
|
||
|
router.dispatchWithGas(
|
||
|
destinationDomain,
|
||
|
'0x',
|
||
|
interchainGasPayment,
|
||
|
{
|
||
|
value: interchainGasPayment,
|
||
|
},
|
||
|
),
|
||
|
true,
|
||
|
);
|
||
3 years ago
|
});
|
||
|
});
|
||
3 years ago
|
});
|
||
|
});
|