Skip to content

Commit

Permalink
feat: update contracts/tests, but the deployPoll would fail due to de…
Browse files Browse the repository at this point in the history
…ploy Tally too large
  • Loading branch information
kittybest committed Jan 4, 2024
1 parent aea3f07 commit 1f5d8cb
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 241 deletions.
20 changes: 16 additions & 4 deletions contracts/contracts/MACI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { Poll } from "./Poll.sol";
import { PollFactory } from "./PollFactory.sol";
import { MessageProcessor } from "./MessageProcessor.sol";
import { MessageProcessorFactory } from "./MessageProcessorFactory.sol";
import { Tally } from "./Tally.sol";
import { TallyFactory } from "./TallyFactory.sol";
import { Subsidy } from "./Subsidy.sol";
import { SubsidyFactory } from "./SubsidyFactory.sol";
import { InitialVoiceCreditProxy } from "./initialVoiceCreditProxy/InitialVoiceCreditProxy.sol";
import { SignUpGatekeeper } from "./gatekeepers/SignUpGatekeeper.sol";
Expand Down Expand Up @@ -86,7 +88,14 @@ contract MACI is IMACI, Params, Utilities, Ownable {

// Events
event SignUp(uint256 _stateIndex, PubKey _userPubKey, uint256 _voiceCreditBalance, uint256 _timestamp);
event DeployPoll(uint256 _pollId, address _pollAddr, PubKey _pubKey);
event DeployPoll(
uint256 _pollId,
address _pollAddr,
PubKey _pubKey,
address _mpAddr,
address _tallyAddr,
address _subsidyAddr
);

/// @notice Only allow a Poll contract to call the modified function.
modifier onlyPoll(uint256 _pollId) {
Expand Down Expand Up @@ -233,15 +242,18 @@ contract MACI is IMACI, Params, Utilities, Ownable {

MessageProcessor mp = messageProcessorFactory.deploy(_verifier, _vkRegistry, p);

tallyFactory.deploy(_verifier, _vkRegistry, p, mp);
Tally tally = tallyFactory.deploy(_verifier, _vkRegistry, p, mp);

subsidyFactory.deploy(_verifier, _vkRegistry, p, mp);
Subsidy subsidy = subsidyFactory.deploy(_verifier, _vkRegistry, p, mp);

polls[pollId] = p;

pollAddr = address(p);
address mpAddr = address(mp);
address tallyAddr = address(tally);
address subsidyAddr = address(subsidy);

emit DeployPoll(pollId, pollAddr, _coordinatorPubKey);
emit DeployPoll(pollId, pollAddr, _coordinatorPubKey, mpAddr, tallyAddr, subsidyAddr);
}

/// @notice Allow Poll contracts to merge the state subroots
Expand Down
4 changes: 4 additions & 0 deletions contracts/contracts/MessageProcessorFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { VkRegistry } from "./VkRegistry.sol";
/// @notice A factory contract which deploys MessageProcessor contracts.
contract MessageProcessorFactory is Params, DomainObjs {
/// @notice Deploy a new MessageProcessor contract and return the address.
/// @param _verifier Verifier contract
/// @param _vkRegistry VkRegistry contract
/// @param _poll Poll contract
/// @return messageProcessor The deployed MessageProcessor contract
function deploy(
Verifier _verifier,
VkRegistry _vkRegistry,
Expand Down
5 changes: 5 additions & 0 deletions contracts/contracts/SubsidyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { VkRegistry } from "./VkRegistry.sol";
/// @notice A factory contract which deploys Subsidy contracts.
contract SubsidyFactory is Params, DomainObjs {
/// @notice Deploy a new Subsidy contract and return the address.
/// @param _verifier Verifier contract
/// @param _vkRegistry VkRegistry contract
/// @param _poll Poll contract
/// @param _messageProcessor MessageProcessor contract
/// @return subsidy The deployed Subsidy contract
function deploy(
Verifier _verifier,
VkRegistry _vkRegistry,
Expand Down
5 changes: 5 additions & 0 deletions contracts/contracts/TallyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { VkRegistry } from "./VkRegistry.sol";
/// @notice A factory contract which deploys Tally contracts.
contract TallyFactory is Params, DomainObjs {
/// @notice Deploy a new Tally contract and return the address.
/// @param _verifier Verifier contract
/// @param _vkRegistry VkRegistry contract
/// @param _poll Poll contract
/// @param _messageProcessor MessageProcessor contract
/// @return tally The deployed Tally contract
function deploy(
Verifier _verifier,
VkRegistry _vkRegistry,
Expand Down
20 changes: 16 additions & 4 deletions contracts/tests/MACI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { IVerifyingKeyStruct } from "../ts/types";

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

import {
duration,
Expand All @@ -28,6 +28,7 @@ describe("MACI", () => {
let maciContract: MACI;
let stateAqContract: AccQueueQuinaryMaci;
let vkRegistryContract: VkRegistry;
let verifierContract: Verifier;
let pollId: number;

const coordinator = new Keypair();
Expand All @@ -47,6 +48,7 @@ describe("MACI", () => {
maciContract = r.maciContract;
stateAqContract = r.stateAqContract;
vkRegistryContract = r.vkRegistryContract;
verifierContract = r.mockVerifierContract;
});

it("MACI.stateTreeDepth should be correct", async () => {
Expand Down Expand Up @@ -201,6 +203,8 @@ describe("MACI", () => {
maxValues,
treeDepths,
coordinator.pubKey.asContractParam() as { x: BigNumberish; y: BigNumberish },
verifierContract,
vkRegistryContract,
{ gasLimit: 8000000 },
);
receipt = await tx.wait();
Expand Down Expand Up @@ -286,9 +290,17 @@ describe("MACI", () => {

it("should prevent deploying a second poll before the first has finished", async () => {
await expect(
maciContract.deployPoll(duration, maxValues, treeDepths, coordinator.pubKey.asContractParam(), {
gasLimit: 8000000,
}),
maciContract.deployPoll(
duration,
maxValues,
treeDepths,
coordinator.pubKey.asContractParam(),
verifierContract,
vkRegistryContract,
{
gasLimit: 8000000,
},
),
)
.to.be.revertedWithCustomError(maciContract, "PreviousPollNotCompleted")
.withArgs(1);
Expand Down
10 changes: 9 additions & 1 deletion contracts/tests/MaciOverflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import { MaxValues, STATE_TREE_DEPTH, TreeDepths } from "maci-core";
import { Keypair } from "maci-domainobjs";

import { getDefaultSigner } from "../ts/utils";
import { MACI } from "../typechain-types";
import { MACI, Verifier, VkRegistry } from "../typechain-types";

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

describe("Overflow testing", () => {
let maciContract: MACI;
let verifierContract: Verifier;
let vkRigistryContract: VkRegistry;

const coordinator = new Keypair();
const users = [new Keypair(), new Keypair(), new Keypair()];

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

it("MACI.stateTreeDepth should be correct", async () => {
Expand All @@ -39,6 +43,8 @@ describe("Overflow testing", () => {
maxValues,
treeDepths,
coordinator.pubKey.asContractParam() as { x: BigNumberish; y: BigNumberish },
verifierContract,
vkRigistryContract,
);
});

Expand All @@ -60,6 +66,8 @@ describe("Overflow testing", () => {
values,
depths,
coordinator.pubKey.asContractParam() as { x: BigNumberish; y: BigNumberish },
verifierContract,
vkRigistryContract,
);
const receipt = await tx.wait();

Expand Down
50 changes: 27 additions & 23 deletions contracts/tests/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Keypair, Message, PubKey } from "maci-domainobjs";
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 { MACI, MessageProcessor, Poll as PollContract, Verifier, VkRegistry } from "../typechain-types";

import {
duration,
Expand All @@ -26,8 +26,11 @@ describe("MessageProcessor", () => {
// contracts
let maciContract: MACI;
let pollContract: PollContract;
let verifierContract: Verifier;
let vkRegistryContract: VkRegistry;
let mpContract: MessageProcessor;
const [pollAbi] = parseArtifact("Poll");
const [mpAbi] = parseArtifact("MessageProcessor");

let pollId: number;

Expand All @@ -47,26 +50,37 @@ describe("MessageProcessor", () => {
const r = await deployTestContracts(initialVoiceCreditBalance, STATE_TREE_DEPTH, signer, true);
maciContract = r.maciContract;
signer = await getDefaultSigner();
mpContract = r.mpContract;
verifierContract = r.mockVerifierContract;
vkRegistryContract = r.vkRegistryContract;

// deploy on chain poll
const tx = await maciContract.deployPoll(duration, maxValues, treeDepths, coordinator.pubKey.asContractParam(), {
gasLimit: 8000000,
});
const tx = await maciContract.deployPoll(
duration,
maxValues,
treeDepths,
coordinator.pubKey.asContractParam(),
verifierContract,
vkRegistryContract,
{
gasLimit: 8000000,
},
);
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: number };
args: { _pollId: number; _mpAddr: string };
};
pollId = event.args._pollId;

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

mpContract = new BaseContract(event.args._mpAddr, mpAbi, signer) as MessageProcessor;

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

Expand All @@ -90,8 +104,7 @@ describe("MessageProcessor", () => {
generatedInputs = poll.processMessages(pollId) as typeof generatedInputs;

// set the verification keys on the vk smart contract
const vkContract = r.vkRegistryContract;
await vkContract.setVerifyingKeys(
await vkRegistryContract.setVerifyingKeys(
STATE_TREE_DEPTH,
treeDepths.intStateTreeDepth,
treeDepths.messageTreeDepth,
Expand All @@ -111,11 +124,10 @@ describe("MessageProcessor", () => {
});

it("processMessages() should fail if the state AQ has not been merged", async () => {
const pollContractAddress = await maciContract.getPoll(pollId);

await expect(
mpContract.processMessages(pollContractAddress, 0, [0, 0, 0, 0, 0, 0, 0, 0]),
).to.be.revertedWithCustomError(mpContract, "StateAqNotMerged");
await expect(mpContract.processMessages(0, [0, 0, 0, 0, 0, 0, 0, 0])).to.be.revertedWithCustomError(
mpContract,
"StateAqNotMerged",
);
});
});

Expand All @@ -135,21 +147,13 @@ describe("MessageProcessor", () => {
0,
poll.messages.length,
);
const onChainPackedVals = BigInt(
await mpContract.genProcessMessagesPackedVals(await pollContract.getAddress(), 0, users.length),
);
const onChainPackedVals = BigInt(await mpContract.genProcessMessagesPackedVals(0, users.length));
expect(packedVals.toString(16)).to.eq(onChainPackedVals.toString(16));
});

it("processMessages() should update the state and ballot root commitment", async () => {
const pollContractAddress = await maciContract.getPoll(pollId);

// Submit the proof
const tx = await mpContract.processMessages(
pollContractAddress,
generatedInputs.newSbCommitment,
[0, 0, 0, 0, 0, 0, 0, 0],
);
const tx = await mpContract.processMessages(generatedInputs.newSbCommitment, [0, 0, 0, 0, 0, 0, 0, 0]);

const receipt = await tx.wait();
expect(receipt?.status).to.eq(1);
Expand Down
18 changes: 14 additions & 4 deletions contracts/tests/Poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Keypair, Message, PCommand, PubKey } from "maci-domainobjs";

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

import {
MESSAGE_TREE_DEPTH,
Expand All @@ -25,6 +25,8 @@ describe("Poll", () => {
let maciContract: MACI;
let pollId: number;
let pollContract: PollContract;
let verifierContract: Verifier;
let vkRegistryContract: VkRegistry;
let signer: Signer;
let deployTime: number;
const coordinator = new Keypair();
Expand All @@ -39,9 +41,17 @@ describe("Poll", () => {
maciContract = r.maciContract;

// deploy on chain poll
const tx = await maciContract.deployPoll(duration, maxValues, treeDepths, coordinator.pubKey.asContractParam(), {
gasLimit: 8000000,
});
const tx = await maciContract.deployPoll(
duration,
maxValues,
treeDepths,
coordinator.pubKey.asContractParam(),
verifierContract,
vkRegistryContract,
{
gasLimit: 8000000,
},
);
const receipt = await tx.wait();

const block = await signer.provider!.getBlock(receipt!.blockHash);
Expand Down
Loading

0 comments on commit 1f5d8cb

Please sign in to comment.