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.
126 lines
3.8 KiB
126 lines
3.8 KiB
3 years ago
|
import { ethers } from 'hardhat';
|
||
3 years ago
|
import { expect } from 'chai';
|
||
3 years ago
|
import { types, utils } from '@abacus-network/utils';
|
||
|
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
|
||
3 years ago
|
|
||
3 years ago
|
import { TestOutbox, TestOutbox__factory } from '../types';
|
||
3 years ago
|
|
||
3 years ago
|
const destinationNonceTestCases = require('../../../vectors/destinationNonce.json');
|
||
4 years ago
|
|
||
4 years ago
|
const localDomain = 1000;
|
||
4 years ago
|
const destDomain = 2000;
|
||
4 years ago
|
|
||
3 years ago
|
describe('Outbox', async () => {
|
||
3 years ago
|
let outbox: TestOutbox,
|
||
|
signer: SignerWithAddress,
|
||
|
recipient: SignerWithAddress;
|
||
4 years ago
|
|
||
|
before(async () => {
|
||
3 years ago
|
[signer, recipient] = await ethers.getSigners();
|
||
4 years ago
|
});
|
||
|
|
||
|
beforeEach(async () => {
|
||
3 years ago
|
// redeploy the outbox before each test run
|
||
|
const outboxFactory = new TestOutbox__factory(signer);
|
||
|
outbox = await outboxFactory.deploy(localDomain);
|
||
3 years ago
|
// The ValidatorManager is unused in these tests *but* needs to be a
|
||
|
// contract.
|
||
3 years ago
|
await outbox.initialize(outbox.address);
|
||
4 years ago
|
});
|
||
|
|
||
4 years ago
|
it('Cannot be initialized twice', async () => {
|
||
3 years ago
|
await expect(outbox.initialize(outbox.address)).to.be.revertedWith(
|
||
3 years ago
|
'Initializable: contract is already initialized',
|
||
|
);
|
||
4 years ago
|
});
|
||
|
|
||
3 years ago
|
it('ValidatorManager can fail', async () => {
|
||
3 years ago
|
await outbox.testSetValidatorManager(signer.address);
|
||
|
await outbox.fail();
|
||
3 years ago
|
expect(await outbox.state()).to.equal(types.AbacusState.FAILED);
|
||
4 years ago
|
|
||
|
const message = ethers.utils.formatBytes32String('message');
|
||
|
await expect(
|
||
3 years ago
|
outbox.dispatch(
|
||
|
destDomain,
|
||
|
utils.addressToBytes32(recipient.address),
|
||
|
message,
|
||
|
),
|
||
4 years ago
|
).to.be.revertedWith('failed state');
|
||
|
});
|
||
|
|
||
3 years ago
|
it('Non ValidatorManager cannot fail', async () => {
|
||
3 years ago
|
await expect(outbox.connect(recipient).fail()).to.be.revertedWith(
|
||
3 years ago
|
'!validatorManager',
|
||
|
);
|
||
4 years ago
|
});
|
||
|
|
||
3 years ago
|
it('Does not dispatch too large messages', async () => {
|
||
4 years ago
|
const message = `0x${Buffer.alloc(3000).toString('hex')}`;
|
||
|
await expect(
|
||
3 years ago
|
outbox.dispatch(
|
||
|
destDomain,
|
||
|
utils.addressToBytes32(recipient.address),
|
||
|
message,
|
||
|
),
|
||
3 years ago
|
).to.be.revertedWith('msg too long');
|
||
4 years ago
|
});
|
||
|
|
||
3 years ago
|
it('Dispatches a message', async () => {
|
||
4 years ago
|
const message = ethers.utils.formatBytes32String('message');
|
||
3 years ago
|
const nonce = await outbox.nonces(localDomain);
|
||
4 years ago
|
|
||
|
// Format data that will be emitted from Dispatch event
|
||
3 years ago
|
const destAndNonce = utils.destinationAndNonce(destDomain, nonce);
|
||
4 years ago
|
|
||
3 years ago
|
const abacusMessage = utils.formatMessage(
|
||
4 years ago
|
localDomain,
|
||
4 years ago
|
signer.address,
|
||
3 years ago
|
nonce,
|
||
4 years ago
|
destDomain,
|
||
|
recipient.address,
|
||
|
message,
|
||
|
);
|
||
3 years ago
|
const hash = utils.messageHash(abacusMessage);
|
||
3 years ago
|
const leafIndex = await outbox.tree();
|
||
|
const [checkpointedRoot] = await outbox.latestCheckpoint();
|
||
4 years ago
|
|
||
|
// Send message with signer address as msg.sender
|
||
|
await expect(
|
||
3 years ago
|
outbox
|
||
4 years ago
|
.connect(signer)
|
||
3 years ago
|
.dispatch(
|
||
|
destDomain,
|
||
|
utils.addressToBytes32(recipient.address),
|
||
|
message,
|
||
|
),
|
||
4 years ago
|
)
|
||
3 years ago
|
.to.emit(outbox, 'Dispatch')
|
||
3 years ago
|
.withArgs(hash, leafIndex, destAndNonce, checkpointedRoot, abacusMessage);
|
||
4 years ago
|
});
|
||
|
|
||
3 years ago
|
it('Checkpoints the latest root', async () => {
|
||
4 years ago
|
const message = ethers.utils.formatBytes32String('message');
|
||
3 years ago
|
await outbox.dispatch(
|
||
4 years ago
|
destDomain,
|
||
3 years ago
|
utils.addressToBytes32(recipient.address),
|
||
4 years ago
|
message,
|
||
|
);
|
||
3 years ago
|
await outbox.checkpoint();
|
||
|
const [root, index] = await outbox.latestCheckpoint();
|
||
3 years ago
|
expect(root).to.not.equal(ethers.constants.HashZero);
|
||
3 years ago
|
expect(index).to.equal(1);
|
||
4 years ago
|
});
|
||
4 years ago
|
|
||
3 years ago
|
it('Correctly calculates destinationAndNonce', async () => {
|
||
|
for (let testCase of destinationNonceTestCases) {
|
||
|
let { destination, nonce, expectedDestinationAndNonce } = testCase;
|
||
3 years ago
|
const solidityDestinationAndNonce = await outbox.destinationAndNonce(
|
||
3 years ago
|
destination,
|
||
|
nonce,
|
||
4 years ago
|
);
|
||
3 years ago
|
expect(solidityDestinationAndNonce).to.equal(expectedDestinationAndNonce);
|
||
4 years ago
|
}
|
||
|
});
|
||
4 years ago
|
});
|