Skip to content

Commit

Permalink
test(contracts): cleanup contracts tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 authored and 0xmad committed Aug 1, 2024
1 parent 313b44a commit 7f8fc25
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 155 deletions.
3 changes: 2 additions & 1 deletion contracts/tests/EASGatekeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ describe("EAS Gatekeeper", () => {
});

describe("Deployment", () => {
it("The gatekeeper should be deployed correctly", () => {
it("The gatekeeper should be deployed correctly", async () => {
expect(easGatekeeper).to.not.eq(undefined);
expect(await easGatekeeper.getAddress()).to.not.eq(ZeroAddress);
});

it("should fail to deploy when the eas contract address is not valid", async () => {
Expand Down
3 changes: 2 additions & 1 deletion contracts/tests/GitcoinPassportGatekeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ describe("GitcoinPassport Gatekeeper", () => {
});

describe("Deployment", () => {
it("The gatekeeper should be deployed correctly", () => {
it("The gatekeeper should be deployed correctly", async () => {
expect(gitcoinGatekeeper).to.not.eq(undefined);
expect(await gitcoinGatekeeper.getAddress()).to.not.eq(ZeroAddress);
});

it("should fail to deploy when the decoder contract address is not valid", async () => {
Expand Down
30 changes: 8 additions & 22 deletions contracts/tests/MACI.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-underscore-dangle */
import { expect } from "chai";
import { AbiCoder, BigNumberish, Signer } from "ethers";
import { AbiCoder, BigNumberish, Signer, ZeroAddress } from "ethers";
import { EthereumProvider } from "hardhat/types";
import { MaciState } from "maci-core";
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
Expand Down Expand Up @@ -34,7 +34,6 @@ describe("MACI", function test() {
let signer: Signer;

const maciState = new MaciState(STATE_TREE_DEPTH);
const signUpTxOpts = { gasLimit: 400000 };

describe("Deployment", () => {
before(async () => {
Expand Down Expand Up @@ -80,7 +79,6 @@ describe("MACI", function test() {
user.pubKey.asContractParam(),
AbiCoder.defaultAbiCoder().encode(["uint256"], [1]),
AbiCoder.defaultAbiCoder().encode(["uint256"], [0]),
signUpTxOpts,
);
// eslint-disable-next-line no-await-in-loop
const receipt = await tx.wait();
Expand Down Expand Up @@ -115,7 +113,6 @@ describe("MACI", function test() {
},
AbiCoder.defaultAbiCoder().encode(["uint256"], [1]),
AbiCoder.defaultAbiCoder().encode(["uint256"], [0]),
signUpTxOpts,
),
).to.be.revertedWithCustomError(maciContract, "InvalidPubKey");
});
Expand All @@ -129,7 +126,6 @@ describe("MACI", function test() {
},
AbiCoder.defaultAbiCoder().encode(["uint256"], [1]),
AbiCoder.defaultAbiCoder().encode(["uint256"], [0]),
signUpTxOpts,
),
).to.be.revertedWithCustomError(maciContract, "InvalidPubKey");
});
Expand All @@ -143,7 +139,6 @@ describe("MACI", function test() {
},
AbiCoder.defaultAbiCoder().encode(["uint256"], [1]),
AbiCoder.defaultAbiCoder().encode(["uint256"], [0]),
signUpTxOpts,
),
).to.be.revertedWithCustomError(maciContract, "InvalidPubKey");
});
Expand All @@ -157,7 +152,6 @@ describe("MACI", function test() {
},
AbiCoder.defaultAbiCoder().encode(["uint256"], [1]),
AbiCoder.defaultAbiCoder().encode(["uint256"], [0]),
signUpTxOpts,
),
).to.be.revertedWithCustomError(maciContract, "InvalidPubKey");
});
Expand Down Expand Up @@ -223,20 +217,14 @@ describe("MACI", function test() {
verifierContract,
vkRegistryContract,
EMode.QV,
{ gasLimit: 10000000 },
);
const receipt = await tx.wait();

const block = await signer.provider!.getBlock(receipt!.blockHash);
deployTime = block!.timestamp;

expect(receipt?.status).to.eq(1);
const iface = maciContract.interface;
const logs = receipt!.logs[receipt!.logs.length - 1];
const event = iface.parseLog(logs as unknown as { topics: string[]; data: string }) as unknown as {
args: { _pollId: bigint };
};
pollId = event.args._pollId;
pollId = (await maciContract.nextPollId()) - 1n;

const p = maciState.deployPoll(
BigInt(deployTime + duration),
Expand Down Expand Up @@ -268,7 +256,6 @@ describe("MACI", function test() {
verifierContract,
vkRegistryContract,
EMode.QV,
{ gasLimit: 10000000 },
);
const receipt = await tx.wait();
expect(receipt?.status).to.eq(1);
Expand All @@ -286,7 +273,6 @@ describe("MACI", function test() {
verifierContract,
vkRegistryContract,
EMode.QV,
{ gasLimit: 10000000 },
);
const receipt = await tx.wait();
expect(receipt?.status).to.eq(1);
Expand All @@ -305,9 +291,7 @@ describe("MACI", function test() {
it("should allow a Poll contract to merge the state tree (calculate the state root)", async () => {
await timeTravel(signer.provider as unknown as EthereumProvider, Number(duration) + 1);

const tx = await pollContract.mergeMaciState({
gasLimit: 3000000,
});
const tx = await pollContract.mergeMaciState();
const receipt = await tx.wait();
expect(receipt?.status).to.eq(1);
});
Expand All @@ -327,7 +311,6 @@ describe("MACI", function test() {
users[0].pubKey.asContractParam(),
AbiCoder.defaultAbiCoder().encode(["uint256"], [1]),
AbiCoder.defaultAbiCoder().encode(["uint256"], [0]),
signUpTxOpts,
);
const receipt = await tx.wait();
expect(receipt?.status).to.eq(1);
Expand All @@ -353,8 +336,11 @@ describe("MACI", function test() {
});

describe("getPoll", () => {
it("should return an address for a valid id", async () => {
expect(await maciContract.getPoll(pollId)).to.not.eq(0);
it("should return an object for a valid id", async () => {
const pollContracts = await maciContract.getPoll(pollId);
expect(pollContracts.poll).to.not.eq(ZeroAddress);
expect(pollContracts.messageProcessor).to.not.eq(ZeroAddress);
expect(pollContracts.tally).to.not.eq(ZeroAddress);
});

it("should throw when given an invalid poll id", async () => {
Expand Down
21 changes: 3 additions & 18 deletions contracts/tests/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,18 @@ describe("MessageProcessor", () => {
verifierContract,
vkRegistryContract,
EMode.QV,
{
gasLimit: 10000000,
},
);
let receipt = await tx.wait();

// extract poll id
expect(receipt?.status).to.eq(1);
const iface = maciContract.interface;
const logs = receipt!.logs[receipt!.logs.length - 1];
const event = iface.parseLog(logs as unknown as { topics: string[]; data: string }) as unknown as {
args: {
_pollId: bigint;
pollAddr: {
poll: string;
messageProcessor: string;
tally: string;
};
};
};
pollId = event.args._pollId;

pollId = (await maciContract.nextPollId()) - 1n;

const pollContracts = await maciContract.getPoll(pollId);
pollContract = PollFactory.connect(pollContracts.poll, signer);

mpContract = MessageProcessorFactory.connect(event.args.pollAddr.messageProcessor, signer);
mpContract = MessageProcessorFactory.connect(pollContracts.messageProcessor, signer);

const block = await signer.provider!.getBlock(receipt!.blockHash);
const deployTime = block!.timestamp;
Expand Down Expand Up @@ -132,7 +118,6 @@ describe("MessageProcessor", () => {
EMode.QV,
testProcessVk.asContractParam() as IVerifyingKeyStruct,
testTallyVk.asContractParam() as IVerifyingKeyStruct,
{ gasLimit: 1000000 },
);
receipt = await tx.wait();
expect(receipt?.status).to.eq(1);
Expand Down
20 changes: 4 additions & 16 deletions contracts/tests/Poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,15 @@ describe("Poll", () => {
verifierContract,
vkRegistryContract,
EMode.QV,
{
gasLimit: 10000000,
},
);
const receipt = await tx.wait();

const block = await signer.provider!.getBlock(receipt!.blockHash);
deployTime = block!.timestamp;

expect(receipt?.status).to.eq(1);
const iface = maciContract.interface;
const logs = receipt!.logs[receipt!.logs.length - 1];
const event = iface.parseLog(logs as unknown as { topics: string[]; data: string }) as unknown as {
args: { _pollId: bigint };
};
pollId = event.args._pollId;

pollId = (await maciContract.nextPollId()) - 1n;

const pollContracts = await maciContract.getPoll(pollId);
pollContract = PollFactory.connect(pollContracts.poll, signer);
Expand Down Expand Up @@ -154,9 +147,6 @@ describe("Poll", () => {
r.mockVerifierContract as Verifier,
r.vkRegistryContract,
EMode.QV,
{
gasLimit: 10000000,
},
),
).to.be.revertedWithCustomError(testMaciContract, "InvalidPubKey");
});
Expand Down Expand Up @@ -275,13 +265,11 @@ describe("Poll", () => {
});

it("should allow to merge the message AccQueue", async () => {
let tx = await pollContract.mergeMessageAqSubRoots(0, {
gasLimit: 3000000,
});
let tx = await pollContract.mergeMessageAqSubRoots(0);
let receipt = await tx.wait();
expect(receipt?.status).to.eq(1);

tx = await pollContract.mergeMessageAq({ gasLimit: 4000000 });
tx = await pollContract.mergeMessageAq();
receipt = await tx.wait();
expect(receipt?.status).to.eq(1);
});
Expand Down
78 changes: 12 additions & 66 deletions contracts/tests/Tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,37 +79,20 @@ describe("TallyVotes", () => {
verifierContract,
vkRegistryContract,
EMode.QV,
{
gasLimit: 10000000,
},
);
const receipt = await tx.wait();

const block = await signer.provider!.getBlock(receipt!.blockHash);
const deployTime = block!.timestamp;

expect(receipt?.status).to.eq(1);
const iface = maciContract.interface;
const logs = receipt!.logs[receipt!.logs.length - 1];
const event = iface.parseLog(logs as unknown as { topics: string[]; data: string }) as unknown as {
args: {
_pollId: bigint;
pollAddr: {
poll: string;
messageProcessor: string;
tally: string;
};
};
name: string;
};
expect(event.name).to.eq("DeployPoll");

pollId = event.args._pollId;

pollId = (await maciContract.nextPollId()) - 1n;

const pollContracts = await maciContract.getPoll(pollId);
pollContract = PollFactory.connect(pollContracts.poll, signer);
mpContract = MessageProcessorFactory.connect(event.args.pollAddr.messageProcessor, signer);
tallyContract = TallyFactory.connect(event.args.pollAddr.tally, signer);
mpContract = MessageProcessorFactory.connect(pollContracts.messageProcessor, signer);
tallyContract = TallyFactory.connect(pollContracts.tally, signer);

// deploy local poll
const p = maciState.deployPoll(BigInt(deployTime + duration), maxValues, treeDepths, messageBatchSize, coordinator);
Expand Down Expand Up @@ -146,7 +129,6 @@ describe("TallyVotes", () => {
EMode.QV,
testProcessVk.asContractParam() as IVerifyingKeyStruct,
testTallyVk.asContractParam() as IVerifyingKeyStruct,
{ gasLimit: 1000000 },
);
});

Expand Down Expand Up @@ -260,37 +242,20 @@ describe("TallyVotes", () => {
verifierContract,
vkRegistryContract,
EMode.QV,
{
gasLimit: 10000000,
},
);
const receipt = await tx.wait();

const block = await signer.provider!.getBlock(receipt!.blockHash);
const deployTime = block!.timestamp;

expect(receipt?.status).to.eq(1);
const iface = maciContract.interface;
const logs = receipt!.logs[receipt!.logs.length - 1];
const event = iface.parseLog(logs as unknown as { topics: string[]; data: string }) as unknown as {
args: {
_pollId: bigint;
pollAddr: {
poll: string;
messageProcessor: string;
tally: string;
};
};
name: string;
};
expect(event.name).to.eq("DeployPoll");

pollId = event.args._pollId;

pollId = (await maciContract.nextPollId()) - 1n;

const pollContracts = await maciContract.getPoll(pollId);
pollContract = PollFactory.connect(pollContracts.poll, signer);
mpContract = MessageProcessorFactory.connect(event.args.pollAddr.messageProcessor, signer);
tallyContract = TallyFactory.connect(event.args.pollAddr.tally, signer);
mpContract = MessageProcessorFactory.connect(pollContracts.messageProcessor, signer);
tallyContract = TallyFactory.connect(pollContracts.tally, signer);

// deploy local poll
const p = maciState.deployPoll(
Expand Down Expand Up @@ -333,7 +298,6 @@ describe("TallyVotes", () => {
EMode.QV,
testProcessVk.asContractParam() as IVerifyingKeyStruct,
testTallyVk.asContractParam() as IVerifyingKeyStruct,
{ gasLimit: 1000000 },
);

await timeTravel(signer.provider! as unknown as EthereumProvider, updatedDuration);
Expand Down Expand Up @@ -405,37 +369,20 @@ describe("TallyVotes", () => {
verifierContract,
vkRegistryContract,
EMode.QV,
{
gasLimit: 10000000,
},
);
const receipt = await tx.wait();

const block = await signer.provider!.getBlock(receipt!.blockHash);
const deployTime = block!.timestamp;

expect(receipt?.status).to.eq(1);
const iface = maciContract.interface;
const logs = receipt!.logs[receipt!.logs.length - 1];
const event = iface.parseLog(logs as unknown as { topics: string[]; data: string }) as unknown as {
args: {
_pollId: bigint;
pollAddr: {
poll: string;
messageProcessor: string;
tally: string;
};
};
name: string;
};
expect(event.name).to.eq("DeployPoll");

pollId = event.args._pollId;

pollId = (await maciContract.nextPollId()) - 1n;

const pollContracts = await maciContract.getPoll(pollId);
pollContract = PollFactory.connect(pollContracts.poll, signer);
mpContract = MessageProcessorFactory.connect(event.args.pollAddr.messageProcessor, signer);
tallyContract = TallyFactory.connect(event.args.pollAddr.tally, signer);
mpContract = MessageProcessorFactory.connect(pollContracts.messageProcessor, signer);
tallyContract = TallyFactory.connect(pollContracts.tally, signer);

// deploy local poll
const p = maciState.deployPoll(
Expand Down Expand Up @@ -478,7 +425,6 @@ describe("TallyVotes", () => {
EMode.QV,
testProcessVk.asContractParam() as IVerifyingKeyStruct,
testTallyVk.asContractParam() as IVerifyingKeyStruct,
{ gasLimit: 1000000 },
);

await timeTravel(signer.provider! as unknown as EthereumProvider, updatedDuration);
Expand Down
Loading

0 comments on commit 7f8fc25

Please sign in to comment.