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/optics-core/test/XAppConnectionManager.test.js

343 lines
10 KiB

const { waffle, ethers } = require('hardhat');
test: cross chain governance (#312) * fix: OpticsIdentifier as_ref casts ethereum address conditionally * feature: adds GovernanceRouter test boilerplate and fixes ConnectionManager test setup * test: adds testProcess and revert message utility * test: adds test for handle revert with unenrolled replica * test: adds test for reverting in handle on nonGovernorRouter message * rename: formattedMessage --> opticsMessage * prog: blocked on incorrect setting of governorDomain = localDomain * fix: fix after domainHash rebase * test: adds success case transfer governor test * test: adds setRouter message success case * prog: Call message success case failing due to datalen mismatches * prog: storing in dynamic struct array reverts silently * add: initial setup * add: transfer governorship test * add: signUpdate * refactor: combine bridgerouter tests * fix: cast to address, set sequence * delete: old gov router tests * Fix transfer governor test * fix: debug call storage, get sequence * fix: bad merge rust code * revert: stale changes * refactor: nits, create thirdDomain/thirdRouter vars * refactor: extract some code into formatOpticsMessage * refactor: formatOpticsMessage * refactor: deployOptics and devDeployOptics * fix: bug, clean up code * lint * fix: add numCalls (#315) * fix: initial stab at adding numCalls * debugging: numCall type conversion * fix: read _numCalls from bytes29 * use MSG_PREFIX_LEN * clean: remove unused imports * fix: data bug, refactor a bit * add: Transaction reverted silently * fix: bug in formatCalls * refactor: formatCalls * add: check ret value for testProcess from GovernorRouter * enhance: more explicit naming * rename: BYTE_LEN to NUM_ITEMS * slow down turbo * enhance: assign _numCalls variable Co-authored-by: Luke Tchang <ltchang@stanford.edu> Co-authored-by: anna-caroll <anna.s.carroll@gmail.com>
4 years ago
const { provider, deployMockContract } = waffle;
const { expect } = require('chai');
test: cross chain governance (#312) * fix: OpticsIdentifier as_ref casts ethereum address conditionally * feature: adds GovernanceRouter test boilerplate and fixes ConnectionManager test setup * test: adds testProcess and revert message utility * test: adds test for handle revert with unenrolled replica * test: adds test for reverting in handle on nonGovernorRouter message * rename: formattedMessage --> opticsMessage * prog: blocked on incorrect setting of governorDomain = localDomain * fix: fix after domainHash rebase * test: adds success case transfer governor test * test: adds setRouter message success case * prog: Call message success case failing due to datalen mismatches * prog: storing in dynamic struct array reverts silently * add: initial setup * add: transfer governorship test * add: signUpdate * refactor: combine bridgerouter tests * fix: cast to address, set sequence * delete: old gov router tests * Fix transfer governor test * fix: debug call storage, get sequence * fix: bad merge rust code * revert: stale changes * refactor: nits, create thirdDomain/thirdRouter vars * refactor: extract some code into formatOpticsMessage * refactor: formatOpticsMessage * refactor: deployOptics and devDeployOptics * fix: bug, clean up code * lint * fix: add numCalls (#315) * fix: initial stab at adding numCalls * debugging: numCall type conversion * fix: read _numCalls from bytes29 * use MSG_PREFIX_LEN * clean: remove unused imports * fix: data bug, refactor a bit * add: Transaction reverted silently * fix: bug in formatCalls * refactor: formatCalls * add: check ret value for testProcess from GovernorRouter * enhance: more explicit naming * rename: BYTE_LEN to NUM_ITEMS * slow down turbo * enhance: assign _numCalls variable Co-authored-by: Luke Tchang <ltchang@stanford.edu> Co-authored-by: anna-caroll <anna.s.carroll@gmail.com>
4 years ago
const UpdaterManager = require('../artifacts/contracts/UpdaterManager.sol/UpdaterManager.json');
const {
testCases: signedFailureTestCases,
} = require('../../../vectors/signedFailureTestCases.json');
const testUtils = require('./utils');
const ONLY_OWNER_REVERT_MSG = 'Ownable: caller is not the owner';
const localDomain = 1000;
const remoteDomain = 2000;
const optimisticSeconds = 3;
const initialCurrentRoot = ethers.utils.formatBytes32String('current');
const initialIndex = 0;
const controller = null;
const walletProvider = new testUtils.WalletProvider(provider);
describe('XAppConnectionManager', async () => {
let connectionManager,
mockUpdaterManager,
enrolledReplica,
home,
signer,
updater;
before(async () => {
[signer] = walletProvider.getWalletsPersistent(1);
updater = await optics.Updater.fromSigner(signer, remoteDomain);
});
beforeEach(async () => {
test: cross chain governance (#312) * fix: OpticsIdentifier as_ref casts ethereum address conditionally * feature: adds GovernanceRouter test boilerplate and fixes ConnectionManager test setup * test: adds testProcess and revert message utility * test: adds test for handle revert with unenrolled replica * test: adds test for reverting in handle on nonGovernorRouter message * rename: formattedMessage --> opticsMessage * prog: blocked on incorrect setting of governorDomain = localDomain * fix: fix after domainHash rebase * test: adds success case transfer governor test * test: adds setRouter message success case * prog: Call message success case failing due to datalen mismatches * prog: storing in dynamic struct array reverts silently * add: initial setup * add: transfer governorship test * add: signUpdate * refactor: combine bridgerouter tests * fix: cast to address, set sequence * delete: old gov router tests * Fix transfer governor test * fix: debug call storage, get sequence * fix: bad merge rust code * revert: stale changes * refactor: nits, create thirdDomain/thirdRouter vars * refactor: extract some code into formatOpticsMessage * refactor: formatOpticsMessage * refactor: deployOptics and devDeployOptics * fix: bug, clean up code * lint * fix: add numCalls (#315) * fix: initial stab at adding numCalls * debugging: numCall type conversion * fix: read _numCalls from bytes29 * use MSG_PREFIX_LEN * clean: remove unused imports * fix: data bug, refactor a bit * add: Transaction reverted silently * fix: bug in formatCalls * refactor: formatCalls * add: check ret value for testProcess from GovernorRouter * enhance: more explicit naming * rename: BYTE_LEN to NUM_ITEMS * slow down turbo * enhance: assign _numCalls variable Co-authored-by: Luke Tchang <ltchang@stanford.edu> Co-authored-by: anna-caroll <anna.s.carroll@gmail.com>
4 years ago
// Deploy XAppConnectionManager
connectionManager = await optics.deployImplementation(
'TestXAppConnectionManager',
[],
);
test: cross chain governance (#312) * fix: OpticsIdentifier as_ref casts ethereum address conditionally * feature: adds GovernanceRouter test boilerplate and fixes ConnectionManager test setup * test: adds testProcess and revert message utility * test: adds test for handle revert with unenrolled replica * test: adds test for reverting in handle on nonGovernorRouter message * rename: formattedMessage --> opticsMessage * prog: blocked on incorrect setting of governorDomain = localDomain * fix: fix after domainHash rebase * test: adds success case transfer governor test * test: adds setRouter message success case * prog: Call message success case failing due to datalen mismatches * prog: storing in dynamic struct array reverts silently * add: initial setup * add: transfer governorship test * add: signUpdate * refactor: combine bridgerouter tests * fix: cast to address, set sequence * delete: old gov router tests * Fix transfer governor test * fix: debug call storage, get sequence * fix: bad merge rust code * revert: stale changes * refactor: nits, create thirdDomain/thirdRouter vars * refactor: extract some code into formatOpticsMessage * refactor: formatOpticsMessage * refactor: deployOptics and devDeployOptics * fix: bug, clean up code * lint * fix: add numCalls (#315) * fix: initial stab at adding numCalls * debugging: numCall type conversion * fix: read _numCalls from bytes29 * use MSG_PREFIX_LEN * clean: remove unused imports * fix: data bug, refactor a bit * add: Transaction reverted silently * fix: bug in formatCalls * refactor: formatCalls * add: check ret value for testProcess from GovernorRouter * enhance: more explicit naming * rename: BYTE_LEN to NUM_ITEMS * slow down turbo * enhance: assign _numCalls variable Co-authored-by: Luke Tchang <ltchang@stanford.edu> Co-authored-by: anna-caroll <anna.s.carroll@gmail.com>
4 years ago
// Deploy home's mock updater manager
mockUpdaterManager = await deployMockContract(signer, UpdaterManager.abi);
test: cross chain governance (#312) * fix: OpticsIdentifier as_ref casts ethereum address conditionally * feature: adds GovernanceRouter test boilerplate and fixes ConnectionManager test setup * test: adds testProcess and revert message utility * test: adds test for handle revert with unenrolled replica * test: adds test for reverting in handle on nonGovernorRouter message * rename: formattedMessage --> opticsMessage * prog: blocked on incorrect setting of governorDomain = localDomain * fix: fix after domainHash rebase * test: adds success case transfer governor test * test: adds setRouter message success case * prog: Call message success case failing due to datalen mismatches * prog: storing in dynamic struct array reverts silently * add: initial setup * add: transfer governorship test * add: signUpdate * refactor: combine bridgerouter tests * fix: cast to address, set sequence * delete: old gov router tests * Fix transfer governor test * fix: debug call storage, get sequence * fix: bad merge rust code * revert: stale changes * refactor: nits, create thirdDomain/thirdRouter vars * refactor: extract some code into formatOpticsMessage * refactor: formatOpticsMessage * refactor: deployOptics and devDeployOptics * fix: bug, clean up code * lint * fix: add numCalls (#315) * fix: initial stab at adding numCalls * debugging: numCall type conversion * fix: read _numCalls from bytes29 * use MSG_PREFIX_LEN * clean: remove unused imports * fix: data bug, refactor a bit * add: Transaction reverted silently * fix: bug in formatCalls * refactor: formatCalls * add: check ret value for testProcess from GovernorRouter * enhance: more explicit naming * rename: BYTE_LEN to NUM_ITEMS * slow down turbo * enhance: assign _numCalls variable Co-authored-by: Luke Tchang <ltchang@stanford.edu> Co-authored-by: anna-caroll <anna.s.carroll@gmail.com>
4 years ago
await mockUpdaterManager.mock.updater.returns(signer.address);
await mockUpdaterManager.mock.slashUpdater.returns();
// Deploy home
const {
contracts: homeContracts,
} = await optics.deployUpgradeSetupAndProxy(
'TestHome',
[localDomain],
[mockUpdaterManager.address],
);
test: cross chain governance (#312) * fix: OpticsIdentifier as_ref casts ethereum address conditionally * feature: adds GovernanceRouter test boilerplate and fixes ConnectionManager test setup * test: adds testProcess and revert message utility * test: adds test for handle revert with unenrolled replica * test: adds test for reverting in handle on nonGovernorRouter message * rename: formattedMessage --> opticsMessage * prog: blocked on incorrect setting of governorDomain = localDomain * fix: fix after domainHash rebase * test: adds success case transfer governor test * test: adds setRouter message success case * prog: Call message success case failing due to datalen mismatches * prog: storing in dynamic struct array reverts silently * add: initial setup * add: transfer governorship test * add: signUpdate * refactor: combine bridgerouter tests * fix: cast to address, set sequence * delete: old gov router tests * Fix transfer governor test * fix: debug call storage, get sequence * fix: bad merge rust code * revert: stale changes * refactor: nits, create thirdDomain/thirdRouter vars * refactor: extract some code into formatOpticsMessage * refactor: formatOpticsMessage * refactor: deployOptics and devDeployOptics * fix: bug, clean up code * lint * fix: add numCalls (#315) * fix: initial stab at adding numCalls * debugging: numCall type conversion * fix: read _numCalls from bytes29 * use MSG_PREFIX_LEN * clean: remove unused imports * fix: data bug, refactor a bit * add: Transaction reverted silently * fix: bug in formatCalls * refactor: formatCalls * add: check ret value for testProcess from GovernorRouter * enhance: more explicit naming * rename: BYTE_LEN to NUM_ITEMS * slow down turbo * enhance: assign _numCalls variable Co-authored-by: Luke Tchang <ltchang@stanford.edu> Co-authored-by: anna-caroll <anna.s.carroll@gmail.com>
4 years ago
home = homeContracts.proxyWithImplementation;
// Set XAppConnectionManager's home
await connectionManager.setHome(home.address);
// Deploy single replica
const { contracts } = await optics.deployUpgradeSetupAndProxy(
'TestReplica',
[localDomain],
[
remoteDomain,
updater.signer.address,
initialCurrentRoot,
optimisticSeconds,
initialIndex,
],
controller,
'initialize(uint32, address, bytes32, uint256, uint256)',
);
enrolledReplica = contracts.proxyWithImplementation;
// Enroll replica and check that enrolling replica succeeded
await connectionManager.ownerEnrollReplica(
enrolledReplica.address,
remoteDomain,
);
});
it('Returns the local domain', async () => {
expect(await connectionManager.localDomain()).to.equal(localDomain);
});
it('onlyOwner function rejects call from non-owner', async () => {
const [nonOwner, nonHome] = walletProvider.getWalletsEphemeral(2);
await expect(
connectionManager.connect(nonOwner).setHome(nonHome.address),
).to.be.revertedWith(ONLY_OWNER_REVERT_MSG);
});
it('isOwner returns true for owner and false for non-owner', async () => {
const [newOwner, nonOwner] = walletProvider.getWalletsEphemeral(2);
await connectionManager.transferOwnership(newOwner.address);
expect(await connectionManager.isOwner(newOwner.address)).to.be.true;
expect(await connectionManager.isOwner(nonOwner.address)).to.be.false;
});
it('isReplica returns true for enrolledReplica and false for non-enrolled Replica', async () => {
const [nonEnrolledReplica] = walletProvider.getWalletsEphemeral(1);
expect(await connectionManager.isReplica(enrolledReplica.address)).to.be
.true;
expect(await connectionManager.isReplica(nonEnrolledReplica.address)).to.be
.false;
});
it('Allows owner to set the home', async () => {
const {
contracts: newHomeContracts,
} = await optics.deployUpgradeSetupAndProxy(
'TestHome',
[localDomain],
[mockUpdaterManager.address],
);
const newHome = newHomeContracts.proxyWithImplementation;
await connectionManager.setHome(newHome.address);
expect(await connectionManager.home()).to.equal(newHome.address);
});
it('Owner can enroll a new replica', async () => {
const newRemoteDomain = 3000;
const {
contracts: newReplicaContracts,
} = await optics.deployUpgradeSetupAndProxy(
'TestReplica',
[localDomain],
[
newRemoteDomain,
updater.signer.address,
initialCurrentRoot,
optimisticSeconds,
initialIndex,
],
controller,
'initialize(uint32, address, bytes32, uint256, uint256)',
);
const newReplica = newReplicaContracts.proxyWithImplementation;
// Assert new replica not considered replica before enrolled
expect(await connectionManager.isReplica(newReplica.address)).to.be.false;
await expect(
connectionManager.ownerEnrollReplica(newReplica.address, newRemoteDomain),
).to.emit(connectionManager, 'ReplicaEnrolled');
expect(await connectionManager.domainToReplica(newRemoteDomain)).to.equal(
newReplica.address,
);
expect(
await connectionManager.replicaToDomain(newReplica.address),
).to.equal(newRemoteDomain);
expect(await connectionManager.isReplica(newReplica.address)).to.be.true;
});
it('Owner can unenroll a replica', async () => {
await expect(
connectionManager.ownerUnenrollReplica(enrolledReplica.address),
).to.emit(connectionManager, 'ReplicaUnenrolled');
expect(
await connectionManager.replicaToDomain(enrolledReplica.address),
).to.equal(0);
expect(await connectionManager.domainToReplica(localDomain)).to.equal(
ethers.constants.AddressZero,
);
expect(await connectionManager.isReplica(enrolledReplica.address)).to.be
.false;
});
it('Owner can set watcher permissions', async () => {
const [watcher] = walletProvider.getWalletsEphemeral(1);
expect(
await connectionManager.watcherPermission(watcher.address, remoteDomain),
).to.be.false;
await expect(
connectionManager.setWatcherPermission(
watcher.address,
remoteDomain,
true,
),
).to.emit(connectionManager, 'WatcherPermissionSet');
expect(
await connectionManager.watcherPermission(watcher.address, remoteDomain),
).to.be.true;
});
it('Unenrolls a replica given valid SignedFailureNotification', async () => {
// Set watcher permissions for domain of currently enrolled replica
const [watcher] = walletProvider.getWalletsEphemeral(1);
await connectionManager.setWatcherPermission(
watcher.address,
remoteDomain,
true,
);
// Create signed failure notification and signature
const {
failureNotification,
signature,
} = await optics.signedFailureNotification(
watcher,
remoteDomain,
updater.signer.address,
);
// Assert new replica considered replica before unenrolled
expect(await connectionManager.isReplica(enrolledReplica.address)).to.be
.true;
// Unenroll replica using data + signature
await expect(
connectionManager.unenrollReplica(
failureNotification.domain,
failureNotification.updaterBytes32,
signature,
),
).to.emit(connectionManager, 'ReplicaUnenrolled');
expect(
await connectionManager.replicaToDomain(enrolledReplica.address),
).to.equal(0);
expect(await connectionManager.domainToReplica(localDomain)).to.equal(
ethers.constants.AddressZero,
);
expect(await connectionManager.isReplica(enrolledReplica.address)).to.be
.false;
});
it('unenrollReplica reverts if there is no replica for provided domain', async () => {
const noReplicaDomain = 3000;
// Set watcher permissions for noReplicaDomain
const [watcher] = walletProvider.getWalletsEphemeral(1);
await connectionManager.setWatcherPermission(
watcher.address,
noReplicaDomain,
true,
);
// Create signed failure notification and signature for noReplicaDomain
const {
failureNotification,
signature,
} = await optics.signedFailureNotification(
watcher,
noReplicaDomain,
updater.signer.address,
);
// Expect unenrollReplica call to revert
await expect(
connectionManager.unenrollReplica(
failureNotification.domain,
failureNotification.updaterBytes32,
signature,
),
).to.be.revertedWith('!replica exists');
});
it('unenrollReplica reverts if provided updater does not match replica updater', async () => {
const [watcher, nonUpdater] = walletProvider.getWalletsEphemeral(2);
// Set watcher permissions
await connectionManager.setWatcherPermission(
watcher.address,
remoteDomain,
true,
);
// Create signed failure notification and signature with nonUpdater
const {
failureNotification,
signature,
} = await optics.signedFailureNotification(
watcher,
remoteDomain,
nonUpdater.address,
);
// Expect unenrollReplica call to revert
await expect(
connectionManager.unenrollReplica(
failureNotification.domain,
failureNotification.updaterBytes32,
signature,
),
).to.be.revertedWith('!current updater');
});
it('unenrollReplica reverts if incorrect watcher provided', async () => {
const [watcher, nonWatcher] = walletProvider.getWalletsEphemeral(2);
// Set watcher permissions
await connectionManager.setWatcherPermission(
watcher.address,
remoteDomain,
true,
);
// Create signed failure notification and signature with nonWatcher
const {
failureNotification,
signature,
} = await optics.signedFailureNotification(
nonWatcher,
remoteDomain,
updater.signer.address,
);
test: cross chain governance (#312) * fix: OpticsIdentifier as_ref casts ethereum address conditionally * feature: adds GovernanceRouter test boilerplate and fixes ConnectionManager test setup * test: adds testProcess and revert message utility * test: adds test for handle revert with unenrolled replica * test: adds test for reverting in handle on nonGovernorRouter message * rename: formattedMessage --> opticsMessage * prog: blocked on incorrect setting of governorDomain = localDomain * fix: fix after domainHash rebase * test: adds success case transfer governor test * test: adds setRouter message success case * prog: Call message success case failing due to datalen mismatches * prog: storing in dynamic struct array reverts silently * add: initial setup * add: transfer governorship test * add: signUpdate * refactor: combine bridgerouter tests * fix: cast to address, set sequence * delete: old gov router tests * Fix transfer governor test * fix: debug call storage, get sequence * fix: bad merge rust code * revert: stale changes * refactor: nits, create thirdDomain/thirdRouter vars * refactor: extract some code into formatOpticsMessage * refactor: formatOpticsMessage * refactor: deployOptics and devDeployOptics * fix: bug, clean up code * lint * fix: add numCalls (#315) * fix: initial stab at adding numCalls * debugging: numCall type conversion * fix: read _numCalls from bytes29 * use MSG_PREFIX_LEN * clean: remove unused imports * fix: data bug, refactor a bit * add: Transaction reverted silently * fix: bug in formatCalls * refactor: formatCalls * add: check ret value for testProcess from GovernorRouter * enhance: more explicit naming * rename: BYTE_LEN to NUM_ITEMS * slow down turbo * enhance: assign _numCalls variable Co-authored-by: Luke Tchang <ltchang@stanford.edu> Co-authored-by: anna-caroll <anna.s.carroll@gmail.com>
4 years ago
// Expect unenrollReplica call to revert
await expect(
connectionManager.unenrollReplica(
failureNotification.domain,
failureNotification.updaterBytes32,
signature,
),
).to.be.revertedWith('!valid watcher');
});
it('Checks Rust-produced SignedFailureNotification', async () => {
// Compare Rust output in json file to solidity output
const testCase = signedFailureTestCases[0];
const { domain, updater, signature, signer } = testCase;
await enrolledReplica.setUpdater(updater);
await connectionManager.setWatcherPermission(signer, domain, true);
test: cross chain governance (#312) * fix: OpticsIdentifier as_ref casts ethereum address conditionally * feature: adds GovernanceRouter test boilerplate and fixes ConnectionManager test setup * test: adds testProcess and revert message utility * test: adds test for handle revert with unenrolled replica * test: adds test for reverting in handle on nonGovernorRouter message * rename: formattedMessage --> opticsMessage * prog: blocked on incorrect setting of governorDomain = localDomain * fix: fix after domainHash rebase * test: adds success case transfer governor test * test: adds setRouter message success case * prog: Call message success case failing due to datalen mismatches * prog: storing in dynamic struct array reverts silently * add: initial setup * add: transfer governorship test * add: signUpdate * refactor: combine bridgerouter tests * fix: cast to address, set sequence * delete: old gov router tests * Fix transfer governor test * fix: debug call storage, get sequence * fix: bad merge rust code * revert: stale changes * refactor: nits, create thirdDomain/thirdRouter vars * refactor: extract some code into formatOpticsMessage * refactor: formatOpticsMessage * refactor: deployOptics and devDeployOptics * fix: bug, clean up code * lint * fix: add numCalls (#315) * fix: initial stab at adding numCalls * debugging: numCall type conversion * fix: read _numCalls from bytes29 * use MSG_PREFIX_LEN * clean: remove unused imports * fix: data bug, refactor a bit * add: Transaction reverted silently * fix: bug in formatCalls * refactor: formatCalls * add: check ret value for testProcess from GovernorRouter * enhance: more explicit naming * rename: BYTE_LEN to NUM_ITEMS * slow down turbo * enhance: assign _numCalls variable Co-authored-by: Luke Tchang <ltchang@stanford.edu> Co-authored-by: anna-caroll <anna.s.carroll@gmail.com>
4 years ago
// Just performs signature recovery (not dependent on replica state, just
// tests functionality)
const watcher = await connectionManager.testRecoverWatcherFromSig(
domain,
enrolledReplica.address,
updater,
ethers.utils.joinSignature(signature),
);
expect(watcher.toLowerCase()).to.equal(signer);
});
});