Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(contracts): optimize contract tasks and scripts #1379

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
pragma solidity ^0.8.10;

import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { SignUpGatekeeper } from "../SignUpGatekeeper.sol";
import { IHats } from "../../interfaces/IHats.sol";

import { IHats } from "../interfaces/IHats.sol";
import { SignUpGatekeeper } from "./SignUpGatekeeper.sol";

/// @title HatsGatekeeperBase
/// @notice Abstract contract containing the base elements of a Hats Gatekeeper contract
Expand Down
13 changes: 7 additions & 6 deletions contracts/scripts/compileSol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,22 @@ async function main(): Promise<void> {
await Promise.all(PATHS.map((filepath) => fs.existsSync(filepath) && fs.promises.rm(filepath, { recursive: true })));

await Promise.all(
ZERO_TREES.map(({ name, zero, hashLength, comment }) => {
const text = genZerosContract({
ZERO_TREES.map(({ name, zero, hashLength, comment }) =>
genZerosContract({
name,
zeroVal: zero,
hashLength,
numZeros: NUM_ZEROS,
comment,
useSha256: false,
subDepth: 0,
});
return fs.promises.writeFile(path.resolve(__dirname, "..", "contracts/trees/zeros", `${name}.sol`), `${text}\n`);
}),
}).then((text) =>
fs.promises.writeFile(path.resolve(__dirname, "..", "contracts/trees/zeros", `${name}.sol`), `${text}\n`),
),
),
);

genEmptyBallotRootsContract();
await genEmptyBallotRootsContract();

await hre.run("compile");

Expand Down
10 changes: 6 additions & 4 deletions contracts/tasks/helpers/Deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { exit } from "process";
import type { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers";
import type { ConfigurableTaskDefinition, HardhatRuntimeEnvironment, TaskArguments } from "hardhat/types";

import { parseArtifact } from "../../ts/abi";

import { ContractStorage } from "./ContractStorage";
import { EContracts, IDeployParams, IDeployStep, IDeployStepCatalog, IGetContractParams } from "./types";

Expand Down Expand Up @@ -409,8 +407,12 @@ export class Deployment {
const deployer = signer || (await this.getDeployer());
const contractAddress = address || this.storage.mustGetAddress(name, this.hre!.network.name, key);

const [abi] = parseArtifact(name.toString());
const factory = await this.hre?.ethers.getContractAt(name.toString(), contractAddress, deployer);

if (!factory) {
throw new Error(`Contract ${name} not found`);
}

return new BaseContract(contractAddress, abi, deployer) as T;
return factory.connect(deployer) as unknown as T;
}
}
19 changes: 10 additions & 9 deletions contracts/tasks/helpers/ProofGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { ICircuitFiles, IPrepareStateParams, IProofGeneratorParams, TallyDa
import type { Proof } from "../../ts/types";
import type { BigNumberish } from "ethers";

import { genMaciStateFromContract } from "../../ts/genMaciState";
import { asHex } from "../../ts/utils";

/**
Expand Down Expand Up @@ -127,14 +126,16 @@ export class ProofGenerator {

const maciContractAddress = await maciContract.getAddress();

return genMaciStateFromContract(
signer.provider!,
maciContractAddress,
coordinatorKeypair,
BigInt(pollId),
fromBlock,
blocksPerBatch,
endBlock || defaultEndBlock,
return import("../../ts/genMaciState").then(({ genMaciStateFromContract }) =>
genMaciStateFromContract(
signer.provider!,
maciContractAddress,
coordinatorKeypair,
BigInt(pollId),
fromBlock,
blocksPerBatch,
endBlock || defaultEndBlock,
),
);
}

Expand Down
18 changes: 12 additions & 6 deletions contracts/tests/MACI.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
/* eslint-disable no-underscore-dangle */
import { expect } from "chai";
import { AbiCoder, BaseContract, BigNumberish, Contract, Signer } from "ethers";
import { AbiCoder, BigNumberish, Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";
import { MaciState } from "maci-core";
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, PubKey, Message } from "maci-domainobjs";

import { parseArtifact } from "../ts/abi";
import { EMode } from "../ts/constants";
import { getDefaultSigner, getSigners } from "../ts/utils";
import { AccQueueQuinaryMaci, MACI, Poll as PollContract, Verifier, VkRegistry } from "../typechain-types";
import {
AccQueueQuinaryBlankSl__factory as AccQueueQuinaryBlankSlFactory,
AccQueueQuinaryMaci,
MACI,
Poll as PollContract,
Poll__factory as PollFactory,
Verifier,
VkRegistry,
} from "../typechain-types";

import {
STATE_TREE_DEPTH,
Expand All @@ -29,7 +36,6 @@ describe("MACI", () => {
let pollId: bigint;

const coordinator = new Keypair();
const [pollAbi] = parseArtifact("Poll");
const users = [new Keypair(), new Keypair(), new Keypair()];

let signer: Signer;
Expand All @@ -55,7 +61,7 @@ describe("MACI", () => {

it("should be the owner of the stateAqContract", async () => {
const stateAqAddr = await maciContract.stateAq();
const stateAq = new Contract(stateAqAddr, parseArtifact("AccQueueQuinaryBlankSl")[0], signer);
const stateAq = AccQueueQuinaryBlankSlFactory.connect(stateAqAddr, signer);

expect(await stateAq.owner()).to.eq(await maciContract.getAddress());
});
Expand Down Expand Up @@ -229,7 +235,7 @@ describe("MACI", () => {

before(async () => {
const pollContractAddress = await maciContract.getPoll(pollId);
pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract;
pollContract = PollFactory.connect(pollContractAddress, signer);
});

it("should not allow the coordinator to merge the signUp AccQueue", async () => {
Expand Down
20 changes: 12 additions & 8 deletions contracts/tests/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
/* eslint-disable no-underscore-dangle */
import { expect } from "chai";
import { BaseContract, Signer } from "ethers";
import { Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";
import { MaciState, Poll, packProcessMessageSmallVals, IProcessMessagesCircuitInputs } from "maci-core";
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, Message, PubKey } from "maci-domainobjs";

import { parseArtifact } from "../ts/abi";
import { EMode } from "../ts/constants";
import { IVerifyingKeyStruct } from "../ts/types";
import { getDefaultSigner } from "../ts/utils";
import { MACI, MessageProcessor, Poll as PollContract, Verifier, VkRegistry } from "../typechain-types";
import {
MACI,
MessageProcessor,
MessageProcessor__factory as MessageProcessorFactory,
Poll as PollContract,
Poll__factory as PollFactory,
Verifier,
VkRegistry,
} from "../typechain-types";

import {
STATE_TREE_DEPTH,
Expand All @@ -32,9 +39,6 @@ describe("MessageProcessor", () => {
let vkRegistryContract: VkRegistry;
let mpContract: MessageProcessor;

const [pollAbi] = parseArtifact("Poll");
const [mpAbi] = parseArtifact("MessageProcessor");

let pollId: bigint;

// local poll and maci state
Expand Down Expand Up @@ -87,9 +91,9 @@ describe("MessageProcessor", () => {
pollId = event.args._pollId;

const pollContractAddress = await maciContract.getPoll(pollId);
pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract;
pollContract = PollFactory.connect(pollContractAddress, signer);

mpContract = new BaseContract(event.args.pollAddr.messageProcessor, mpAbi, signer) as MessageProcessor;
mpContract = MessageProcessorFactory.connect(event.args.pollAddr.messageProcessor, signer);

const block = await signer.provider!.getBlock(receipt!.blockHash);
const deployTime = block!.timestamp;
Expand Down
20 changes: 13 additions & 7 deletions contracts/tests/Poll.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
/* eslint-disable no-underscore-dangle */
import { expect } from "chai";
import { BaseContract, Signer } from "ethers";
import { Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";
import { MaciState } from "maci-core";
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, Message, PCommand, PubKey } from "maci-domainobjs";

import { parseArtifact } from "../ts/abi";
import { EMode } from "../ts/constants";
import { getDefaultSigner, getSigners } from "../ts/utils";
import { AccQueue, MACI, Poll as PollContract, TopupCredit, Verifier, VkRegistry } from "../typechain-types";
import {
AccQueue,
AccQueueQuinaryMaci__factory as AccQueueQuinaryMaciFactory,
MACI,
Poll as PollContract,
Poll__factory as PollFactory,
TopupCredit,
Verifier,
VkRegistry,
} from "../typechain-types";

import {
MESSAGE_TREE_DEPTH,
Expand All @@ -32,8 +40,6 @@ describe("Poll", () => {
let signer: Signer;
let deployTime: number;
const coordinator = new Keypair();
const [pollAbi] = parseArtifact("Poll");
const [accQueueQuinaryMaciAbi] = parseArtifact("AccQueueQuinaryMaci");

const maciState = new MaciState(STATE_TREE_DEPTH);

Expand Down Expand Up @@ -74,7 +80,7 @@ describe("Poll", () => {
pollId = event.args._pollId;

const pollContractAddress = await maciContract.getPoll(pollId);
pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract;
pollContract = PollFactory.connect(pollContractAddress, signer);

// deploy local poll
const p = maciState.deployPoll(
Expand Down Expand Up @@ -273,7 +279,7 @@ describe("Poll", () => {
const extContracts = await pollContract.extContracts();

const messageAqAddress = extContracts.messageAq;
messageAqContract = new BaseContract(messageAqAddress, accQueueQuinaryMaciAbi, signer) as AccQueue;
messageAqContract = AccQueueQuinaryMaciFactory.connect(messageAqAddress, signer);
});

it("should revert if the subtrees are not merged for StateAq", async () => {
Expand Down
37 changes: 21 additions & 16 deletions contracts/tests/Tally.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, BaseContract, Signer } from "ethers";
import { AbiCoder, Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";
import {
MaciState,
Expand All @@ -12,11 +12,20 @@ import {
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, Message, PubKey } from "maci-domainobjs";

import { parseArtifact } from "../ts/abi";
import { EMode } from "../ts/constants";
import { IVerifyingKeyStruct } from "../ts/types";
import { getDefaultSigner } from "../ts/utils";
import { Tally, MACI, Poll as PollContract, MessageProcessor, Verifier, VkRegistry } from "../typechain-types";
import {
Tally,
MACI,
Poll as PollContract,
MessageProcessor,
Verifier,
VkRegistry,
MessageProcessor__factory as MessageProcessorFactory,
Poll__factory as PollFactory,
Tally__factory as TallyFactory,
} from "../typechain-types";

import {
STATE_TREE_DEPTH,
Expand Down Expand Up @@ -44,10 +53,6 @@ describe("TallyVotes", () => {
let users: Keypair[];
let maciState: MaciState;

const [pollAbi] = parseArtifact("Poll");
const [mpAbi] = parseArtifact("MessageProcessor");
const [tallyAbi] = parseArtifact("Tally");

let pollId: bigint;
let poll: Poll;

Expand Down Expand Up @@ -102,9 +107,9 @@ describe("TallyVotes", () => {
pollId = event.args._pollId;

const pollContractAddress = await maciContract.getPoll(pollId);
pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract;
mpContract = new BaseContract(event.args.pollAddr.messageProcessor, mpAbi, signer) as MessageProcessor;
tallyContract = new BaseContract(event.args.pollAddr.tally, tallyAbi, signer) as Tally;
pollContract = PollFactory.connect(pollContractAddress, signer);
mpContract = MessageProcessorFactory.connect(event.args.pollAddr.messageProcessor, signer);
tallyContract = TallyFactory.connect(event.args.pollAddr.tally, signer);

// deploy local poll
const p = maciState.deployPoll(BigInt(deployTime + duration), maxValues, treeDepths, messageBatchSize, coordinator);
Expand Down Expand Up @@ -284,9 +289,9 @@ describe("TallyVotes", () => {
pollId = event.args._pollId;

const pollContractAddress = await maciContract.getPoll(pollId);
pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract;
mpContract = new BaseContract(event.args.pollAddr.messageProcessor, mpAbi, signer) as MessageProcessor;
tallyContract = new BaseContract(event.args.pollAddr.tally, tallyAbi, signer) as Tally;
pollContract = PollFactory.connect(pollContractAddress, signer);
mpContract = MessageProcessorFactory.connect(event.args.pollAddr.messageProcessor, signer);
tallyContract = TallyFactory.connect(event.args.pollAddr.tally, signer);

// deploy local poll
const p = maciState.deployPoll(
Expand Down Expand Up @@ -426,9 +431,9 @@ describe("TallyVotes", () => {
pollId = event.args._pollId;

const pollContractAddress = await maciContract.getPoll(pollId);
pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract;
mpContract = new BaseContract(event.args.pollAddr.messageProcessor, mpAbi, signer) as MessageProcessor;
tallyContract = new BaseContract(event.args.pollAddr.tally, tallyAbi, signer) as Tally;
pollContract = PollFactory.connect(pollContractAddress, signer);
mpContract = MessageProcessorFactory.connect(event.args.pollAddr.messageProcessor, signer);
tallyContract = TallyFactory.connect(event.args.pollAddr.tally, signer);

// deploy local poll
const p = maciState.deployPoll(
Expand Down
26 changes: 15 additions & 11 deletions contracts/tests/TallyNonQv.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 { BaseContract, Signer } from "ethers";
import { Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";
import {
MaciState,
Expand All @@ -12,12 +12,20 @@ import {
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, Message, PubKey } from "maci-domainobjs";

import type { Tally, MACI, Poll as PollContract, MessageProcessor, Verifier, VkRegistry } from "../typechain-types";

import { parseArtifact } from "../ts/abi";
import { EMode } from "../ts/constants";
import { IVerifyingKeyStruct } from "../ts/types";
import { getDefaultSigner } from "../ts/utils";
import {
Tally,
MACI,
Poll as PollContract,
MessageProcessor,
Verifier,
VkRegistry,
MessageProcessor__factory as MessageProcessorFactory,
Poll__factory as PollFactory,
Tally__factory as TallyFactory,
} from "../typechain-types";

import {
STATE_TREE_DEPTH,
Expand All @@ -44,10 +52,6 @@ describe("TallyVotesNonQv", () => {
let users: Keypair[];
let maciState: MaciState;

const [pollAbi] = parseArtifact("Poll");
const [mpAbi] = parseArtifact("MessageProcessor");
const [tallyAbi] = parseArtifact("Tally");

let pollId: bigint;
let poll: Poll;

Expand Down Expand Up @@ -102,9 +106,9 @@ describe("TallyVotesNonQv", () => {
pollId = event.args._pollId;

const pollContractAddress = await maciContract.getPoll(pollId);
pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract;
mpContract = new BaseContract(event.args.pollAddr.messageProcessor, mpAbi, signer) as MessageProcessor;
tallyContract = new BaseContract(event.args.pollAddr.tally, tallyAbi, signer) as Tally;
pollContract = PollFactory.connect(pollContractAddress, signer);
mpContract = MessageProcessorFactory.connect(event.args.pollAddr.messageProcessor, signer);
tallyContract = TallyFactory.connect(event.args.pollAddr.tally, signer);

// deploy local poll
const p = maciState.deployPoll(BigInt(deployTime + duration), maxValues, treeDepths, messageBatchSize, coordinator);
Expand Down
Loading
Loading