Skip to content

Commit

Permalink
Test cleanup: before, snapshots, multisig and validator utils (#193)
Browse files Browse the repository at this point in the history
* Stop kit after each unit test suite

* Use snapshots in all unit tests

* Move all unit test setup logic to before from beforeEaches

* Move validator utils to separate file

* Remove unused function

* Move multisig submit+execute to separate utils file

* Rely on typechain and union types rather than a manually written interface

* Appease the linter
  • Loading branch information
m-chrzan authored Mar 18, 2024
1 parent 98b52a7 commit d24c74a
Show file tree
Hide file tree
Showing 20 changed files with 499 additions and 420 deletions.
24 changes: 16 additions & 8 deletions test-ts/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ import {
LOCKED_GOLD_UNLOCKING_PERIOD,
mineToNextEpoch,
randomSigner,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
REGISTRY_ADDRESS,
resetNetwork,
timeTravel,
} from "./utils";
import { registerValidatorAndAddToGroupMembers, registerValidatorGroup } from "./utils-validators";

after(() => {
hre.kit.stop();
});

describe("Account", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let snapshotId: any;

let accountsInstance: AccountsWrapper;
let lockedGold: LockedGoldWrapper;
let election: ElectionWrapper;
Expand Down Expand Up @@ -79,19 +81,25 @@ describe("Account", () => {
await registerValidatorAndAddToGroupMembers(groups[i], validators[i], validatorWallet);
}

accountsInstance = await hre.kit.contracts.getAccounts();
lockedGold = await hre.kit.contracts.getLockedGold();
election = await hre.kit.contracts.getElection();
});

beforeEach(async () => {
await hre.deployments.fixture("TestAccount");
owner = await hre.ethers.getNamedSigner("owner");
pauser = owner;
account = await hre.ethers.getContract("Account");
await account.connect(owner).setManager(manager.address);
await account.connect(owner).setPauser();
governance = await hre.ethers.getContract("MockGovernance");

accountsInstance = await hre.kit.contracts.getAccounts();
lockedGold = await hre.kit.contracts.getLockedGold();
election = await hre.kit.contracts.getElection();
});

beforeEach(async () => {
snapshotId = await hre.ethers.provider.send("evm_snapshot", []);
});

afterEach(async () => {
await hre.ethers.provider.send("evm_revert", [snapshotId]);
});

it("should create an account on the core Accounts contract", async () => {
Expand Down
13 changes: 11 additions & 2 deletions test-ts/address-sorted-linked-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ after(() => {
});

describe("AddressSortedLinkedList", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let snapshotId: any;

let addressSortedLinkedList: MockAddressSortedLinkedList;
let accounts: string[] = [];

Expand All @@ -21,9 +24,7 @@ describe("AddressSortedLinkedList", () => {
} catch (error) {
console.error(error);
}
});

beforeEach(async () => {
const Lib = await hre.ethers.getContractFactory("AddressSortedLinkedList");
const lib = await Lib.deploy();
await lib.deployed();
Expand All @@ -38,6 +39,14 @@ describe("AddressSortedLinkedList", () => {
accounts = await hre.web3.eth.getAccounts();
});

beforeEach(async () => {
snapshotId = await hre.ethers.provider.send("evm_snapshot", []);
});

afterEach(async () => {
await hre.ethers.provider.send("evm_revert", [snapshotId]);
});

describe("#insert()", () => {
let key: string;
const numerator = 2;
Expand Down
16 changes: 9 additions & 7 deletions test-ts/default-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,28 @@ import { MockVote } from "../typechain-types/MockVote";
import { SpecificGroupStrategy } from "../typechain-types/SpecificGroupStrategy";
import {
ADDRESS_ZERO,
deregisterValidatorGroup,
electGroup,
electMockValidatorGroupsAndUpdate,
getDefaultGroups,
getOrderedActiveGroups,
getUnsortedGroups,
mineToNextEpoch,
prepareOverflow,
randomSigner,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
REGISTRY_ADDRESS,
removeMembersFromGroup,
resetNetwork,
revokeElectionOnMockValidatorGroupsAndUpdate,
updateGroupCeloBasedOnProtocolStCelo,
updateGroupSlashingMultiplier,
updateMaxNumberOfGroups,
} from "./utils";
import { OrderedGroup } from "./utils-interfaces";
import {
deregisterValidatorGroup,
electGroup,
electMockValidatorGroupsAndUpdate,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
removeMembersFromGroup,
updateGroupSlashingMultiplier,
} from "./utils-validators";

after(() => {
hre.kit.stop();
Expand Down
12 changes: 7 additions & 5 deletions test-ts/end-to-end-overflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ import { MockGroupHealth } from "../typechain-types/MockGroupHealth";
import { SpecificGroupStrategy } from "../typechain-types/SpecificGroupStrategy";
import {
activateAndVoteTest,
activateValidators,
electMockValidatorGroupsAndUpdate,
prepareOverflow,
randomSigner,
rebalanceDefaultGroups,
rebalanceGroups,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
resetNetwork,
upgradeToMockGroupHealthE2E,
} from "./utils";
import {
activateValidators,
electMockValidatorGroupsAndUpdate,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
} from "./utils-validators";

after(() => {
hre.kit.stop();
Expand Down Expand Up @@ -131,7 +133,7 @@ describe("e2e overflow test", () => {
await activateValidators(
defaultStrategy,
groupHealthContract as unknown as GroupHealth,
multisigOwner0.address,
multisigOwner0,
activatedGroupAddresses
);
});
Expand Down
12 changes: 7 additions & 5 deletions test-ts/end-to-end-specific-group-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@ import { SpecificGroupStrategy } from "../typechain-types/SpecificGroupStrategy"
import { StakedCelo } from "../typechain-types/StakedCelo";
import {
activateAndVoteTest,
activateValidators,
distributeEpochRewards,
electMockValidatorGroupsAndUpdate,
getGroupsOfAllStrategies,
getRealVsExpectedCeloForGroups,
LOCKED_GOLD_UNLOCKING_PERIOD,
mineToNextEpoch,
randomSigner,
rebalanceDefaultGroups,
rebalanceGroups,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
resetNetwork,
revokeElectionOnMockValidatorGroupsAndUpdate,
timeTravel,
upgradeToMockGroupHealthE2E,
} from "./utils";
import {
activateValidators,
electMockValidatorGroupsAndUpdate,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
} from "./utils-validators";

after(() => {
hre.kit.stop();
Expand Down Expand Up @@ -154,7 +156,7 @@ describe("e2e specific group strategy voting", () => {
await activateValidators(
defaultStrategy,
groupHealthContract as unknown as GroupHealth,
multisigOwner0.address,
multisigOwner0,
activatedGroupAddresses
);
});
Expand Down
12 changes: 7 additions & 5 deletions test-ts/end-to-end-vote-proposal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ import { StakedCelo } from "../typechain-types/StakedCelo";
import { Vote } from "../typechain-types/Vote";
import {
activateAndVoteTest,
activateValidators,
distributeEpochRewards,
electMockValidatorGroupsAndUpdate,
impersonateAccount,
mineToNextEpoch,
randomSigner,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
resetNetwork,
timeTravel,
upgradeToMockGroupHealthE2E,
} from "./utils";
import {
activateValidators,
electMockValidatorGroupsAndUpdate,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
} from "./utils-validators";

after(() => {
hre.kit.stop();
Expand Down Expand Up @@ -131,7 +133,7 @@ describe("e2e governance vote", () => {
await activateValidators(
defaultStrategyContract,
groupHealthContract as unknown as GroupHealth,
multisigOwner0.address,
multisigOwner0,
activatedGroupAddresses
);
});
Expand Down
12 changes: 7 additions & 5 deletions test-ts/end-to-end.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ import { SpecificGroupStrategy } from "../typechain-types/SpecificGroupStrategy"
import { StakedCelo } from "../typechain-types/StakedCelo";
import {
activateAndVoteTest,
activateValidators,
distributeEpochRewards,
electMockValidatorGroupsAndUpdate,
LOCKED_GOLD_UNLOCKING_PERIOD,
mineToNextEpoch,
randomSigner,
rebalanceDefaultGroups,
rebalanceGroups,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
resetNetwork,
timeTravel,
upgradeToMockGroupHealthE2E,
} from "./utils";
import {
activateValidators,
electMockValidatorGroupsAndUpdate,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
} from "./utils-validators";

after(() => {
hre.kit.stop();
Expand Down Expand Up @@ -119,7 +121,7 @@ describe("e2e", () => {
await activateValidators(
defaultStrategyContract,
groupHealthContract as unknown as GroupHealth,
multisigOwner0.address,
multisigOwner0,
activatedGroupAddresses
);
});
Expand Down
14 changes: 8 additions & 6 deletions test-ts/group-health.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ import { MockRegistry } from "../typechain-types/MockRegistry";
import { MockValidators } from "../typechain-types/MockValidators";
import {
ADDRESS_ZERO,
deregisterValidatorGroup,
electMockValidatorGroupsAndUpdate,
mineToNextEpoch,
randomSigner,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
REGISTRY_ADDRESS,
removeMembersFromGroup,
resetNetwork,
revokeElectionOnMockValidatorGroupsAndUpdate,
updateGroupSlashingMultiplier,
} from "./utils";
import {
deregisterValidatorGroup,
electMockValidatorGroupsAndUpdate,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
removeMembersFromGroup,
updateGroupSlashingMultiplier,
} from "./utils-validators";

after(() => {
hre.kit.stop();
Expand Down
10 changes: 6 additions & 4 deletions test-ts/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@ import { MockVote } from "../typechain-types/MockVote";
import { SpecificGroupStrategy } from "../typechain-types/SpecificGroupStrategy";
import {
ADDRESS_ZERO,
electMockValidatorGroupsAndUpdate,
getDefaultGroups,
getImpersonatedSigner,
getSpecificGroups,
mineToNextEpoch,
prepareOverflow,
randomSigner,
rebalanceDefaultGroups,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
REGISTRY_ADDRESS,
resetNetwork,
revokeElectionOnMockValidatorGroupsAndUpdate,
updateGroupCeloBasedOnProtocolStCelo,
updateGroupSlashingMultiplier,
} from "./utils";
import {
electMockValidatorGroupsAndUpdate,
registerValidatorAndAddToGroupMembers,
registerValidatorGroup,
updateGroupSlashingMultiplier,
} from "./utils-validators";

const sum = (xs: BigNumber[]): BigNumber => xs.reduce((a, b) => a.add(b));

Expand Down
17 changes: 16 additions & 1 deletion test-ts/multisig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { PausableTest } from "../typechain-types/PausableTest";
import { ProposalTester } from "../typechain-types/ProposalTester";
import { ADDRESS_ZERO, DAY, getImpersonatedSigner, randomSigner, timeTravel } from "./utils";

after(() => {
hre.kit.stop();
});

/**
* Invokes the multisig's submitProposal, waits for the confirmation event
* and returns the generated proposalId.
Expand Down Expand Up @@ -87,6 +91,9 @@ async function multiSigInitialize(owners: string[], requiredSignatures: number)
}

describe("MultiSig", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let snapshotId: any;

let multiSig: MultiSig;
let owner1: SignerWithAddress;
let owner2: SignerWithAddress;
Expand All @@ -100,7 +107,7 @@ describe("MultiSig", () => {
const requiredSignatures = 2;
const delay = 7 * DAY;

beforeEach(async () => {
before(async () => {
await hre.deployments.fixture("TestMultiSig");
multiSig = await hre.ethers.getContract("MultiSig");
await hre.deployments.fixture("TestPausable");
Expand All @@ -120,6 +127,14 @@ describe("MultiSig", () => {
owners = [owner1.address, owner2.address];
});

beforeEach(async () => {
snapshotId = await hre.ethers.provider.send("evm_snapshot", []);
});

afterEach(async () => {
await hre.ethers.provider.send("evm_revert", [snapshotId]);
});

describe("#constructor", () => {
it("should have set the delay to 3 days", async () => {
expect(await multiSig.minDelay()).to.deep.equal(3 * DAY);
Expand Down
Loading

0 comments on commit d24c74a

Please sign in to comment.