Skip to content

Commit

Permalink
Merge pull request #1774 from privacy-scaling-explorations/chore/refa…
Browse files Browse the repository at this point in the history
…ctor-contracts

refactor(contracts): use custom factories for maci test deploys
  • Loading branch information
0xmad authored Aug 16, 2024
2 parents ec17194 + 2e8fbca commit b8d42a3
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 81 deletions.
7 changes: 6 additions & 1 deletion packages/contracts/tests/EASGatekeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ describe("EAS Gatekeeper", () => {
let maciContract: MACI;

before(async () => {
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true, easGatekeeper);
const r = await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
gatekeeper: easGatekeeper,
});

maciContract = r.maciContract;
});
Expand Down
7 changes: 6 additions & 1 deletion packages/contracts/tests/GitcoinPassportGatekeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ describe("GitcoinPassport Gatekeeper", () => {
decoderAddress = await mockDecoder.getAddress();
gitcoinGatekeeper = await deployContract("GitcoinPassportGatekeeper", signer, true, decoderAddress, passingScore);

const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true, gitcoinGatekeeper);
const r = await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
gatekeeper: gitcoinGatekeeper,
});
maciContract = r.maciContract;
});

Expand Down
18 changes: 8 additions & 10 deletions packages/contracts/tests/HatsGatekeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ describe("HatsProtocol Gatekeeper", () => {

describe("hatsGatekeeperSingle", () => {
before(async () => {
const r = await deployTestContracts(
const r = await deployTestContracts({
initialVoiceCreditBalance,
STATE_TREE_DEPTH,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
true,
hatsGatekeeperSingle,
);
gatekeeper: hatsGatekeeperSingle,
});

maciContract = r.maciContract;
});
Expand Down Expand Up @@ -137,13 +136,12 @@ describe("HatsProtocol Gatekeeper", () => {

describe("HatsGatekeeperMultiple", () => {
before(async () => {
const r = await deployTestContracts(
const r = await deployTestContracts({
initialVoiceCreditBalance,
STATE_TREE_DEPTH,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
true,
hatsGatekeeperMultiple,
);
gatekeeper: hatsGatekeeperMultiple,
});

maciContract = r.maciContract;
});
Expand Down
29 changes: 18 additions & 11 deletions packages/contracts/tests/MACI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ describe("MACI", function test() {
describe("Deployment", () => {
before(async () => {
signer = await getDefaultSigner();
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true);
const r = await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
});

maciContract = r.maciContract;
vkRegistryContract = r.vkRegistryContract;
Expand All @@ -45,12 +49,11 @@ describe("MACI", function test() {

it("should be possible to deploy Maci contracts with different state tree depth values", async () => {
const checkStateTreeDepth = async (stateTreeDepthTest: number): Promise<void> => {
const { maciContract: testMaci } = await deployTestContracts(
const { maciContract: testMaci } = await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepthTest,
stateTreeDepth: stateTreeDepthTest,
signer,
true,
);
});
expect(await testMaci.stateTreeDepth()).to.eq(stateTreeDepthTest);
};

Expand Down Expand Up @@ -152,8 +155,13 @@ describe("MACI", function test() {
it("should not allow to sign up more than the supported amount of users (2 ** stateTreeDepth)", async () => {
const stateTreeDepthTest = 1;
const maxUsers = 2 ** stateTreeDepthTest;
const maci = (await deployTestContracts(initialVoiceCreditBalance, stateTreeDepthTest, signer, true))
.maciContract;
const maci = (
await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepth: stateTreeDepthTest,
signer,
})
).maciContract;
const keypair = new Keypair();
// start from one as we already have one signup (blank state leaf)
for (let i = 1; i < maxUsers; i += 1) {
Expand All @@ -178,12 +186,11 @@ describe("MACI", function test() {
it("should signup 2 ** 10 users", async () => {
const stateTreeDepthTest = 10;
const maxUsers = 2 ** stateTreeDepthTest;
const { maciContract: maci } = await deployTestContracts(
const { maciContract: maci } = await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepthTest,
stateTreeDepth: stateTreeDepthTest,
signer,
true,
);
});

const keypair = new Keypair();
// start from one as we already have one signup (blank state leaf)
Expand Down
6 changes: 5 additions & 1 deletion packages/contracts/tests/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ describe("MessageProcessor", () => {
before(async () => {
signer = await getDefaultSigner();
// deploy test contracts
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true);
const r = await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
});
maciContract = r.maciContract;
signer = await getDefaultSigner();
verifierContract = r.mockVerifierContract as Verifier;
Expand Down
12 changes: 10 additions & 2 deletions packages/contracts/tests/Poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ describe("Poll", () => {
describe("deployment", () => {
before(async () => {
signer = await getDefaultSigner();
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true);
const r = await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
});
maciContract = r.maciContract;
verifierContract = r.mockVerifierContract as Verifier;
vkRegistryContract = r.vkRegistryContract;
Expand Down Expand Up @@ -119,7 +123,11 @@ describe("Poll", () => {
});

it("should fail when passing an invalid coordinator public key", async () => {
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true);
const r = await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
});
const testMaciContract = r.maciContract;

// deploy on chain poll
Expand Down
9 changes: 4 additions & 5 deletions packages/contracts/tests/SemaphoreGatekeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ describe("Semaphore Gatekeeper", () => {
let maciContract: MACI;

before(async () => {
const r = await deployTestContracts(
const r = await deployTestContracts({
initialVoiceCreditBalance,
STATE_TREE_DEPTH,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
true,
semaphoreGatekeeper,
);
gatekeeper: semaphoreGatekeeper,
});

maciContract = r.maciContract;
});
Expand Down
9 changes: 4 additions & 5 deletions packages/contracts/tests/SignUpGatekeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ describe("SignUpGatekeeper", () => {
signUpToken = await deploySignupToken(signer, true);
signUpTokenGatekeeperContract = await deploySignupTokenGatekeeper(await signUpToken.getAddress(), signer, true);

const r = await deployTestContracts(
const r = await deployTestContracts({
initialVoiceCreditBalance,
STATE_TREE_DEPTH,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
true,
signUpTokenGatekeeperContract,
);
gatekeeper: signUpTokenGatekeeperContract,
});

maciContract = r.maciContract;
});
Expand Down
6 changes: 3 additions & 3 deletions packages/contracts/tests/Tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("TallyVotes", () => {

signer = await getDefaultSigner();

const r = await deployTestContracts(100, STATE_TREE_DEPTH, signer, true);
const r = await deployTestContracts({ initialVoiceCreditBalance: 100, stateTreeDepth: STATE_TREE_DEPTH, signer });
maciContract = r.maciContract;
verifierContract = r.mockVerifierContract as Verifier;
vkRegistryContract = r.vkRegistryContract;
Expand Down Expand Up @@ -196,7 +196,7 @@ describe("TallyVotes", () => {

const intStateTreeDepth = 2;

const r = await deployTestContracts(100, STATE_TREE_DEPTH, signer, true);
const r = await deployTestContracts({ initialVoiceCreditBalance: 100, stateTreeDepth: STATE_TREE_DEPTH, signer });
maciContract = r.maciContract;
verifierContract = r.mockVerifierContract as Verifier;
vkRegistryContract = r.vkRegistryContract;
Expand Down Expand Up @@ -324,7 +324,7 @@ describe("TallyVotes", () => {

const intStateTreeDepth = 2;

const r = await deployTestContracts(100, STATE_TREE_DEPTH, signer, true);
const r = await deployTestContracts({ initialVoiceCreditBalance: 100, stateTreeDepth: STATE_TREE_DEPTH, signer });
maciContract = r.maciContract;
verifierContract = r.mockVerifierContract as Verifier;
vkRegistryContract = r.vkRegistryContract;
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/tests/TallyNonQv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("TallyVotesNonQv", () => {

signer = await getDefaultSigner();

const r = await deployTestContracts(100, STATE_TREE_DEPTH, signer, true);
const r = await deployTestContracts({ initialVoiceCreditBalance: 100, stateTreeDepth: STATE_TREE_DEPTH, signer });
maciContract = r.maciContract;
verifierContract = r.mockVerifierContract as Verifier;
vkRegistryContract = r.vkRegistryContract;
Expand Down
7 changes: 6 additions & 1 deletion packages/contracts/tests/ZupassGatekeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ describe("Zupass Gatekeeper", () => {
let maciContract: MACI;

before(async () => {
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true, zupassGatekeeper);
const r = await deployTestContracts({
initialVoiceCreditBalance,
stateTreeDepth: STATE_TREE_DEPTH,
signer,
gatekeeper: zupassGatekeeper,
});

maciContract = r.maciContract;
});
Expand Down
19 changes: 10 additions & 9 deletions packages/contracts/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable import/no-extraneous-dependencies */
import { expect } from "chai";
import { BaseContract, Signer } from "ethers";
import { BaseContract } from "ethers";
import { IncrementalQuinTree, AccQueue, calcDepthFromNumLeaves, hash2, hash5 } from "maci-crypto";
import { IVkContractParams, VerifyingKey } from "maci-domainobjs";

import type { IDeployedTestContracts, IDeployedTestContractsArgs } from "../ts/types";
import type { EthereumProvider } from "hardhat/types";

import { linkPoseidonLibraries } from "../tasks/helpers/abi";
Expand All @@ -17,14 +18,12 @@ import {
deployVkRegistry,
createContractFactory,
} from "../ts/deploy";
import { IDeployedTestContracts } from "../ts/types";
import {
AccQueueBinary0__factory as AccQueueBinary0Factory,
AccQueueBinaryMaci__factory as AccQueueBinaryMaciFactory,
AccQueue as AccQueueContract,
AccQueueQuinary0__factory as AccQueueQuinary0Factory,
AccQueueQuinaryMaci__factory as AccQueueQuinaryMaciFactory,
FreeForAllGatekeeper,
} from "../typechain-types";

export const insertSubTreeGasLimit = { gasLimit: 300000 };
Expand Down Expand Up @@ -498,13 +497,14 @@ export const testMergeAgain = async (aq: AccQueue, aqContract: AccQueueContract,
* @param gatekeeper - the gatekeeper contract to use
* @returns the deployed contracts
*/
export const deployTestContracts = async (
initialVoiceCreditBalance: number,
stateTreeDepth: number,
signer?: Signer,
export const deployTestContracts = async ({
initialVoiceCreditBalance,
stateTreeDepth,
signer,
quiet = true,
gatekeeper: FreeForAllGatekeeper | undefined = undefined,
): Promise<IDeployedTestContracts> => {
factories,
gatekeeper,
}: IDeployedTestContractsArgs): Promise<IDeployedTestContracts> => {
const mockVerifierContract = await deployMockVerifier(signer, true);

let gatekeeperContract = gatekeeper;
Expand All @@ -530,6 +530,7 @@ export const deployTestContracts = async (
initialVoiceCreditBalanceAddress: constantInitialVoiceCreditProxyContractAddress,
signer,
stateTreeDepth,
factories,
quiet,
});

Expand Down
37 changes: 17 additions & 20 deletions packages/contracts/ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,31 +296,28 @@ export const deployMaci = async ({
poseidonT6,
}));

const contractsToLink = factories || [
MACIFactory,
PollFactoryFactory,
MessageProcessorFactoryFactory,
TallyFactoryFactory,
];
const contractsToLink = [MACIFactory, PollFactoryFactory, MessageProcessorFactoryFactory, TallyFactoryFactory];

// Link Poseidon contracts to MACI
const linkedContractFactories = await Promise.all(
contractsToLink.map(async (factory) =>
createContractFactory(
factory.abi,
factory.linkBytecode({
"contracts/crypto/PoseidonT3.sol:PoseidonT3": poseidonAddrs.poseidonT3,
"contracts/crypto/PoseidonT4.sol:PoseidonT4": poseidonAddrs.poseidonT4,
"contracts/crypto/PoseidonT5.sol:PoseidonT5": poseidonAddrs.poseidonT5,
"contracts/crypto/PoseidonT6.sol:PoseidonT6": poseidonAddrs.poseidonT6,
}),
signer,
const linkedContractFactories =
factories ||
(await Promise.all(
contractsToLink.map(async (factory) =>
createContractFactory(
factory.abi,
factory.linkBytecode({
"contracts/crypto/PoseidonT3.sol:PoseidonT3": poseidonAddrs.poseidonT3,
"contracts/crypto/PoseidonT4.sol:PoseidonT4": poseidonAddrs.poseidonT4,
"contracts/crypto/PoseidonT5.sol:PoseidonT5": poseidonAddrs.poseidonT5,
"contracts/crypto/PoseidonT6.sol:PoseidonT6": poseidonAddrs.poseidonT6,
}),
signer,
),
),
),
);
));

const [maciContractFactory, pollFactoryContractFactory, messageProcessorFactory, tallyFactory] =
await Promise.all(linkedContractFactories);
linkedContractFactories;

const pollFactoryContract = await deployContractWithLinkedLibraries<PollFactory>(pollFactoryContractFactory);

Expand Down
1 change: 1 addition & 0 deletions packages/contracts/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export {
type TallyData,
} from "../tasks/helpers/types";
export { linkPoseidonLibraries } from "../tasks/helpers/abi";
export { buildPoseidonT3, buildPoseidonT4, buildPoseidonT5, buildPoseidonT6 } from "./buildPoseidon";

export type { IVerifyingKeyStruct, SnarkProof, Groth16Proof, Proof } from "./types";
export * from "../typechain-types";
Loading

0 comments on commit b8d42a3

Please sign in to comment.