Skip to content

Commit

Permalink
fix: remove proveAndProcess dependency on test ordering (#317)
Browse files Browse the repository at this point in the history
* fix: optics message sender and recipients determined in test/utils.js rather than in test file

* fix: sender and mockRecipient init

* fix: uses singleton object rather than awaiting promise explicitly

* fix: rename MockRecipient test contract TestRecipient to avoid confusion w/ actual mocks
  • Loading branch information
luketchang authored May 12, 2021
1 parent 7cdd461 commit 9ebd31e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.6.11;

import {IMessageRecipient} from "../../interfaces/IMessageRecipient.sol";

contract MockRecipient is IMessageRecipient {
contract TestRecipient is IMessageRecipient {
fallback() external {
revert("Fallback");
}
Expand Down
31 changes: 12 additions & 19 deletions solidity/optics-core/test/Replica.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const { waffle, ethers } = require('hardhat');
const { provider, deployMockContract } = waffle;
const { provider } = waffle;
const { expect } = require('chai');

const testUtils = require('./utils');
const MockRecipient = require('../artifacts/contracts/test/MockRecipient.sol/MockRecipient.json');

const {
testCases: homeDomainHashTestCases,
Expand Down Expand Up @@ -305,11 +303,8 @@ describe('Replica', async () => {
});

it('Processes a proved message', async () => {
const [sender, recipient] = provider.getWallets();
const mockRecipient = await deployMockContract(
recipient,
MockRecipient.abi,
);
const sender = testUtils.opticsMessageSender;
const mockRecipient = await testUtils.opticsMessageMockRecipient.getRecipient();

const mockVal = '0x1234abcd';
await mockRecipient.mock.handle.returns(mockVal);
Expand Down Expand Up @@ -420,11 +415,8 @@ describe('Replica', async () => {
});

it('Returns false when processing message for bad handler function', async () => {
const [sender, recipient] = provider.getWallets();
const mockRecipient = await deployMockContract(
recipient,
MockRecipient.abi,
);
const sender = testUtils.opticsMessageSender;
const mockRecipient = await testUtils.opticsMessageMockRecipient.getRecipient();

// Recipient handler function reverts
await mockRecipient.mock.handle.reverts();
Expand All @@ -448,11 +440,8 @@ describe('Replica', async () => {
});

it('Proves and processes a message', async () => {
const [sender, recipient] = provider.getWallets();
const mockRecipient = await deployMockContract(
recipient,
MockRecipient.abi,
);
const sender = testUtils.opticsMessageSender;
const mockRecipient = await testUtils.opticsMessageMockRecipient.getRecipient();

const mockVal = '0x1234abcd';
await mockRecipient.mock.handle.returns(mockVal);
Expand All @@ -475,7 +464,11 @@ describe('Replica', async () => {
const messageLeaf = optics.messageToLeaf(opticsMessage);
expect(messageLeaf).to.equal(leaf);

// Set replica's current root to match root given by proof
// Set replica's current root to match newly computed root that includes
// the new leaf (normally root will have already been computed and path
// simply verifies leaf is in tree but because it is cryptographically
// impossible to find the inputs that create a pre-determined root, we
// simply recalculate root with the leaf using branchRoot)
const proofRoot = await replica.testBranchRoot(leaf, path, index);
await replica.setCurrentRoot(proofRoot);

Expand Down
12 changes: 6 additions & 6 deletions solidity/optics-core/test/cross-chain/GovernanceRouter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ describe('GovernanceRouter', async () => {

it('Accepts valid call messages', async () => {
// Create address for router to enroll and domain for router
const mockRecipient = await optics.deployImplementation('MockRecipient');
const testRecipient = await optics.deployImplementation('TestRecipient');

const MockRecipient = await ethers.getContractFactory('MockRecipient');
const TestRecipient = await ethers.getContractFactory('TestRecipient');
const string = 'String!';
const receiveStringFunction = MockRecipient.interface.getFunction(
const receiveStringFunction = TestRecipient.interface.getFunction(
'receiveString',
);
const receiveStringEncoded = MockRecipient.interface.encodeFunctionData(
const receiveStringEncoded = TestRecipient.interface.encodeFunctionData(
receiveStringFunction,
[string],
);
Expand All @@ -243,12 +243,12 @@ describe('GovernanceRouter', async () => {
);

const callData = {
to: optics.ethersAddressToBytes32(mockRecipient.address),
to: optics.ethersAddressToBytes32(testRecipient.address),
dataLen: receiveStringEncodedLength,
data: receiveStringEncoded,
};

// Create Call message to mockRecipient that calls receiveString
// Create Call message to test recipient that calls receiveString
const callMessage = optics.GovernanceRouter.formatCalls([
callData,
callData,
Expand Down
21 changes: 21 additions & 0 deletions solidity/optics-core/test/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
const { provider, deployMockContract } = waffle;
const TestRecipient = require('../artifacts/contracts/test/TestRecipient.sol/TestRecipient.json');

const [opticsMessageSender] = provider.getWallets();

class MockRecipientObject {
constructor() {
const [opticsMessageRecipient] = provider.getWallets();
this.mockRecipient = deployMockContract(
opticsMessageRecipient,
TestRecipient.abi,
);
}

async getRecipient() {
return await this.mockRecipient;
}
}

const increaseTimestampBy = async (provider, increaseTime) => {
await provider.send('evm_increaseTime', [increaseTime]);
await provider.send('evm_mine');
Expand All @@ -16,6 +35,8 @@ function getUnusedSigner(provider, numUsedSigners) {
const testUtils = {
increaseTimestampBy,
getUnusedSigner,
opticsMessageSender,
opticsMessageMockRecipient: new MockRecipientObject(),
};

module.exports = testUtils;
2 changes: 1 addition & 1 deletion vectors/proveAndProcessTestCases.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"testCases": [
{
"leaf": "0x157e45c45cd4178436a5d5671cbdb153742b7e8fbd0eb48e09989ee4c3dad8b3",
"leaf": "0x7cabd6c58bb5bb408f8ebac7ad1d2a8415ed7b345c71a5a86a52ff0aaedfe832",
"index": 0,
"path": [
"0x65ad6b7c39c687dad3edc05bec09300b742363f5c1f42db586bdce40c9fc5eef",
Expand Down

0 comments on commit 9ebd31e

Please sign in to comment.