Skip to content

Commit

Permalink
Merge pull request #909 from privacy-scaling-explorations/refactoring…
Browse files Browse the repository at this point in the history
…/contracts-scripts

refactor(contracts): cleanup unused scripts and organize ts code
  • Loading branch information
ctrlc03 authored Dec 20, 2023
2 parents 044d8f8 + 458bc6b commit 7d05e2c
Show file tree
Hide file tree
Showing 31 changed files with 726 additions and 555 deletions.
11 changes: 8 additions & 3 deletions cli/ts/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
deployVerifier,
deployMaci,
deployTopupCredit,
getDefaultSigner,
} from "maci-contracts";
import { logError, logGreen, success, DEFAULT_INITIAL_VOICE_CREDITS, DeployedContracts } from "../utils/";

Expand All @@ -32,11 +33,14 @@ export const deploy = async (
logError("Please provide either an initialVoiceCreditProxyAddress or initialVoiceCredits, not both");
}

const signer = await getDefaultSigner();

// if we did not deploy it before, then deploy it now
let initialVoiceCreditProxyContractAddress: string;
if (!initialVoiceCreditsProxyAddress) {
const contract = await deployConstantInitialVoiceCreditProxy(
initialVoiceCredits ? initialVoiceCredits : DEFAULT_INITIAL_VOICE_CREDITS,
signer,
true,
);

Expand All @@ -46,15 +50,15 @@ export const deploy = async (
// check if we have a signupGatekeeper already deployed or passed as arg
let signupGatekeeperContractAddress = readContractAddress("SignUpGatekeeper");
if (!signupGatekeeperContractAddress && !signupGatekeeperAddress) {
const contract = await deployFreeForAllSignUpGatekeeper(true);
const contract = await deployFreeForAllSignUpGatekeeper(signer, true);
signupGatekeeperContractAddress = await contract.getAddress();
}

// deploy a verifier contract
const verifierContract = await deployVerifier(true);
const verifierContract = await deployVerifier(signer, true);

// topup credit
const topUpCredit = await deployTopupCredit(true);
const topUpCredit = await deployTopupCredit(signer, true);

const [verifierContractAddress, topUpCreditAddress] = await Promise.all([
verifierContract.getAddress(),
Expand All @@ -67,6 +71,7 @@ export const deploy = async (
initialVoiceCreditProxyContractAddress,
verifierContractAddress,
topUpCreditAddress,
signer,
stateTreeDepth,
true,
);
Expand Down
3 changes: 3 additions & 0 deletions cli/ts/commands/deployPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const deployPoll = async (
poseidonT4,
poseidonT5,
poseidonT6,
signer,
true,
);

Expand All @@ -106,6 +107,7 @@ export const deployPoll = async (
poseidonT4,
poseidonT5,
poseidonT6,
signer,
true,
);

Expand All @@ -117,6 +119,7 @@ export const deployPoll = async (
poseidonT4,
poseidonT5,
poseidonT6,
signer,
true,
);

Expand Down
4 changes: 2 additions & 2 deletions cli/ts/commands/deployVkRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deployVkRegistry } from "maci-contracts";
import { deployVkRegistry, getDefaultSigner } from "maci-contracts";
import { existsSync, renameSync } from "fs";
import { contractAddressesStore, oldContractAddressesStore } from "../utils/constants";
import { logGreen, success } from "../utils/theme";
Expand All @@ -18,7 +18,7 @@ export const deployVkRegistryContract = async (quiet = true): Promise<string> =>
}

// deploy and store the address
const vkRegistry = await deployVkRegistry(true);
const vkRegistry = await deployVkRegistry(await getDefaultSigner(), true);
const vkRegistryAddress = await vkRegistry.getAddress();
storeContractAddress("VkRegistry", vkRegistryAddress);

Expand Down
6 changes: 4 additions & 2 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "build/ts/index.js",
"scripts": {
"watch": "tsc --watch",
"hardhat": "./scripts/runHardhat.sh",
"hardhat": "hardhat node --hostname 0.0.0.0 --port 8545",
"compileSol": "./scripts/compileSol.sh $1",
"moveIntegrationArtifacts": "cp -r artifacts/ ../integrationTests/artifacts",
"prebuild": "npm run compileSol",
Expand All @@ -22,7 +22,9 @@
"test-signupGatekeeper": "hardhat test ./tests/SignUpGatekeeper.test.ts",
"test-verifier": "hardhat test ./tests/Verifier.test.ts",
"test-accQueue": "hardhat test ./tests/AccQueue.test.ts",
"test-accQueueBenchmark": "hardhat test ./tests/AccQueueBenchmark.test.ts"
"test-accQueueBenchmark": "hardhat test ./tests/AccQueueBenchmark.test.ts",
"test-maciOverflow": "hardhat test ./tests/MaciOverflow.test.ts",
"test-hasherBenchmarks": "hardhat test ./tests/HasherBenchmarks.test.ts"
},
"_moduleAliases": {
"@maci-contracts": "."
Expand Down
32 changes: 0 additions & 32 deletions contracts/scripts/runHardhat.sh

This file was deleted.

8 changes: 0 additions & 8 deletions contracts/scripts/runTestsInCi.sh

This file was deleted.

File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions contracts/tests/AccQueueBenchmark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { expect } from "chai";
import { AccQueue, NOTHING_UP_MY_SLEEVE } from "maci-crypto";

import { deployPoseidonContracts, linkPoseidonLibraries } from "../ts/deploy";
import { getDefaultSigner } from "../ts/utils";
import { AccQueue as AccQueueContract } from "../typechain-types";

require("module-alias/register");

let aqContract: AccQueueContract;

const deploy = async (contractName: string, SUB_DEPTH: number, HASH_LENGTH: number, ZERO: bigint) => {
const { PoseidonT3Contract, PoseidonT4Contract, PoseidonT5Contract, PoseidonT6Contract } =
await deployPoseidonContracts(true);
await deployPoseidonContracts(await getDefaultSigner(), true);
const [poseidonT3ContractAddress, poseidonT4ContractAddress, poseidonT5ContractAddress, poseidonT6ContractAddress] =
await Promise.all([
PoseidonT3Contract.getAddress(),
Expand All @@ -26,6 +25,7 @@ const deploy = async (contractName: string, SUB_DEPTH: number, HASH_LENGTH: numb
poseidonT4ContractAddress,
poseidonT5ContractAddress,
poseidonT6ContractAddress,
await getDefaultSigner(),
true,
);

Expand Down
4 changes: 3 additions & 1 deletion contracts/tests/Hasher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { BigNumberish } from "ethers";
import { sha256Hash, hashLeftRight, hash3, hash4, hash5, genRandomSalt } from "maci-crypto";

import { deployPoseidonContracts, linkPoseidonLibraries } from "../ts/deploy";
import { getDefaultSigner } from "../ts/utils";
import { Hasher } from "../typechain-types";

describe("Hasher", () => {
let hasherContract: Hasher;

before(async () => {
const { PoseidonT3Contract, PoseidonT4Contract, PoseidonT5Contract, PoseidonT6Contract } =
await deployPoseidonContracts(true);
await deployPoseidonContracts(await getDefaultSigner(), true);
const [poseidonT3ContractAddress, poseidonT4ContractAddress, poseidonT5ContractAddress, poseidonT6ContractAddress] =
await Promise.all([
PoseidonT3Contract.getAddress(),
Expand All @@ -25,6 +26,7 @@ describe("Hasher", () => {
poseidonT4ContractAddress,
poseidonT5ContractAddress,
poseidonT6ContractAddress,
await getDefaultSigner(),
true,
);

Expand Down
4 changes: 3 additions & 1 deletion contracts/tests/HasherBenchmarks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BigNumberish } from "ethers";
import { genRandomSalt } from "maci-crypto";

import { deployPoseidonContracts, linkPoseidonLibraries } from "../ts/deploy";
import { getDefaultSigner } from "../ts/utils";
import { HasherBenchmarks } from "../typechain-types";

require("module-alias/register");
Expand All @@ -11,7 +12,7 @@ describe("Hasher", () => {
let hasherContract: HasherBenchmarks;
before(async () => {
const { PoseidonT3Contract, PoseidonT4Contract, PoseidonT5Contract, PoseidonT6Contract } =
await deployPoseidonContracts(true);
await deployPoseidonContracts(await getDefaultSigner(), true);
const [poseidonT3ContractAddress, poseidonT4ContractAddress, poseidonT5ContractAddress, poseidonT6ContractAddress] =
await Promise.all([
PoseidonT3Contract.getAddress(),
Expand All @@ -26,6 +27,7 @@ describe("Hasher", () => {
poseidonT4ContractAddress,
poseidonT5ContractAddress,
poseidonT6ContractAddress,
await getDefaultSigner(),
true,
);

Expand Down
11 changes: 6 additions & 5 deletions contracts/tests/MACI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { VerifyingKey, Keypair, PubKey, Message } from "maci-domainobjs";

import type { IVerifyingKeyStruct } from "../ts/types";

import { parseArtifact, getDefaultSigner } from "../ts/deploy";
import { deployTestContracts } from "../ts/utils";
import { parseArtifact } from "../ts/abi";
import { getDefaultSigner } from "../ts/utils";
import { AccQueueQuinaryMaci, MACI, VkRegistry, Poll as PollContract } from "../typechain-types";

import {
Expand All @@ -22,7 +22,7 @@ import {
testTallyVk,
treeDepths,
} from "./constants";
import { compareVks, timeTravel } from "./utils";
import { compareVks, timeTravel, deployTestContracts } from "./utils";

describe("MACI", () => {
let maciContract: MACI;
Expand All @@ -42,7 +42,7 @@ describe("MACI", () => {
describe("Deployment", () => {
before(async () => {
signer = await getDefaultSigner();
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, true);
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true);

maciContract = r.maciContract;
stateAqContract = r.stateAqContract;
Expand Down Expand Up @@ -108,7 +108,8 @@ describe("MACI", () => {
it("should not allow to sign up more than the supported amount of users (5 ** stateTreeDepth)", async () => {
const stateTreeDepthTest = 1;
const maxUsers = 5 ** stateTreeDepthTest;
const maci = (await deployTestContracts(initialVoiceCreditBalance, stateTreeDepthTest, true)).maciContract;
const maci = (await deployTestContracts(initialVoiceCreditBalance, stateTreeDepthTest, signer, true))
.maciContract;
const keypair = new Keypair();
for (let i = 0; i < maxUsers; i += 1) {
// eslint-disable-next-line no-await-in-loop
Expand Down
5 changes: 3 additions & 2 deletions contracts/tests/MaciOverflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { AbiCoder, BigNumberish } from "ethers";
import { MaxValues, STATE_TREE_DEPTH, TreeDepths } from "maci-core";
import { Keypair } from "maci-domainobjs";

import { deployTestContracts } from "../ts/utils";
import { getDefaultSigner } from "../ts/utils";
import { MACI } from "../typechain-types";

import { duration, initialVoiceCreditBalance, maxValues, treeDepths } from "./constants";
import { deployTestContracts } from "./utils";

describe("Overflow testing", () => {
let maciContract: MACI;
Expand All @@ -15,7 +16,7 @@ describe("Overflow testing", () => {
const users = [new Keypair(), new Keypair(), new Keypair()];

beforeEach(async () => {
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, true);
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, await getDefaultSigner(), true);
maciContract = r.maciContract;
});

Expand Down
9 changes: 6 additions & 3 deletions contracts/tests/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { MaciState, Poll, STATE_TREE_DEPTH, packProcessMessageSmallVals } from "
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, Message, PubKey } from "maci-domainobjs";

import { IVerifyingKeyStruct, deployTestContracts, getDefaultSigner, parseArtifact } from "../ts";
import { parseArtifact } from "../ts/abi";
import { IVerifyingKeyStruct } from "../ts/types";
import { getDefaultSigner } from "../ts/utils";
import { MACI, MessageProcessor, Poll as PollContract } from "../typechain-types";

import {
Expand All @@ -18,7 +20,7 @@ import {
testTallyVk,
treeDepths,
} from "./constants";
import { timeTravel } from "./utils";
import { timeTravel, deployTestContracts } from "./utils";

describe("MessageProcessor", () => {
// contracts
Expand All @@ -40,8 +42,9 @@ describe("MessageProcessor", () => {
const users = [new Keypair(), new Keypair()];

before(async () => {
signer = await getDefaultSigner();
// deploy test contracts
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, true);
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true);
maciContract = r.maciContract;
signer = await getDefaultSigner();
mpContract = r.mpContract;
Expand Down
9 changes: 5 additions & 4 deletions contracts/tests/Poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { MaciState } from "maci-core";
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, Message, PCommand, PubKey } from "maci-domainobjs";

import { deployTestContracts, getDefaultSigner, parseArtifact } from "../ts";
import { parseArtifact } from "../ts/abi";
import { getDefaultSigner } from "../ts/utils";
import { AccQueue, MACI, Poll as PollContract } from "../typechain-types";

import {
Expand All @@ -18,7 +19,7 @@ import {
messageBatchSize,
treeDepths,
} from "./constants";
import { timeTravel } from "./utils";
import { timeTravel, deployTestContracts } from "./utils";

describe("Poll", () => {
let maciContract: MACI;
Expand All @@ -34,7 +35,7 @@ describe("Poll", () => {

before(async () => {
signer = await getDefaultSigner();
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, true);
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true);
maciContract = r.maciContract;

// deploy on chain poll
Expand Down Expand Up @@ -108,7 +109,7 @@ describe("Poll", () => {
maciState.polls[pollId].publishMessage(message, keypair.pubKey);
});

it("shold not publish a message after the voting period", async () => {
it("should not publish a message after the voting period", async () => {
const dd = await pollContract.getDeployTimeAndDuration();
await timeTravel(signer.provider as unknown as EthereumProvider, Number(dd[0]) + 1);

Expand Down
Loading

0 comments on commit 7d05e2c

Please sign in to comment.