Skip to content

Commit

Permalink
refactor(poll): put verifier and vkRegistry into extContracts
Browse files Browse the repository at this point in the history
  • Loading branch information
djanluka committed Aug 15, 2024
1 parent e70fd5b commit 74a2ea1
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 49 deletions.
10 changes: 7 additions & 3 deletions contracts/contracts/MACI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,19 @@ contract MACI is IMACI, DomainObjs, Params, Utilities {
// the owner of the message processor and tally contract will be the msg.sender
address _msgSender = msg.sender;

ExtContracts memory extContracts = ExtContracts({
maci: IMACI(address(this)),
verifier: IVerifier(_verifier),
vkRegistry: IVkRegistry(_vkRegistry)
});

address p = pollFactory.deploy(
_verifier,
_vkRegistry,
_duration,
maxVoteOptions,
_treeDepths,
_messageBatchSize,
_coordinatorPubKey,
address(this)
extContracts
);

address mp = messageProcessorFactory.deploy(_verifier, _vkRegistry, p, _msgSender, _mode);
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/MessageProcessor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ contract MessageProcessor is Ownable, SnarkCommon, Hasher, CommonUtilities, IMes
// get the message batch size from the message tree subdepth
// get the number of signups
(uint256 numSignUps, uint256 numMessages) = poll.numSignUpsAndMessages();
IMACI maci = poll.extContracts();
IMACI maci = poll.getMaciContract();

// Calculate the public input hash (a SHA256 hash of several values)
uint256 publicInputHash = genProcessMessagesPublicInputHash(
Expand Down
24 changes: 11 additions & 13 deletions contracts/contracts/Poll.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Params } from "./utilities/Params.sol";
import { SnarkCommon } from "./crypto/SnarkCommon.sol";
import { EmptyBallotRoots } from "./trees/EmptyBallotRoots.sol";
import { LazyIMTData, InternalLazyIMT } from "./trees/LazyIMT.sol";
import { IMACI } from "./interfaces/IMACI.sol";
import { IPoll } from "./interfaces/IPoll.sol";
import { IVerifier } from "./interfaces/IVerifier.sol";
import { IVkRegistry } from "./interfaces/IVkRegistry.sol";
Expand Down Expand Up @@ -71,11 +72,6 @@ contract Poll is Params, Utilities, SnarkCommon, EmptyBallotRoots, IPoll {
/// @notice The contracts used by the Poll
ExtContracts public extContracts;

/// @notice The verifier
IVerifier public immutable verifier;
/// @notice The verification registry
IVkRegistry public immutable vkRegistry;

/// @notice The array for chain hash checkpoints
uint256[] public batchHashes;

Expand Down Expand Up @@ -127,8 +123,6 @@ contract Poll is Params, Utilities, SnarkCommon, EmptyBallotRoots, IPoll {
/// @param _extContracts The external contracts

constructor(
address _verifier,
address _vkRegistry,
uint256 _duration,
uint256 _maxVoteOptions,
TreeDepths memory _treeDepths,
Expand All @@ -141,10 +135,6 @@ contract Poll is Params, Utilities, SnarkCommon, EmptyBallotRoots, IPoll {
revert InvalidPubKey();
}

// store verifier
verifier = IVerifier(_verifier);
// store vkRegistry
vkRegistry = IVkRegistry(_vkRegistry);
// store the pub key as object then calculate the hash
coordinatorPubKey = _coordinatorPubKey;
// we hash it ourselves to ensure we store the correct value
Expand Down Expand Up @@ -325,7 +315,10 @@ contract Poll is Params, Utilities, SnarkCommon, EmptyBallotRoots, IPoll {
uint256[8] memory _proof

Check warning

Code scanning / Slither

Conformance to Solidity naming conventions Warning

) internal returns (bool isValid) {
// Get the verifying key from the VkRegistry
VerifyingKey memory vk = vkRegistry.getPollVk(extContracts.maci.stateTreeDepth(), treeDepths.voteOptionTreeDepth);
VerifyingKey memory vk = extContracts.vkRegistry.getPollVk(
extContracts.maci.stateTreeDepth(),
treeDepths.voteOptionTreeDepth
);

// Generate the circuit public input
uint256[] memory input = new uint256[](5);
Expand All @@ -336,7 +329,7 @@ contract Poll is Params, Utilities, SnarkCommon, EmptyBallotRoots, IPoll {
input[4] = _pubKey.y;
uint256 publicInputHash = sha256Hash(input);

isValid = verifier.verify(_proof, vk, publicInputHash);
isValid = extContracts.verifier.verify(_proof, vk, publicInputHash);
}

/// @inheritdoc IPoll
Expand Down Expand Up @@ -384,4 +377,9 @@ contract Poll is Params, Utilities, SnarkCommon, EmptyBallotRoots, IPoll {
numSUps = numSignups;
numMsgs = numMessages;
}

/// @inheritdoc IPoll
function getMaciContract() public view returns (IMACI maci) {
return extContracts.maci;
}
}
18 changes: 2 additions & 16 deletions contracts/contracts/PollFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ contract PollFactory is Params, DomainObjs, IPollFactory {

/// @inheritdoc IPollFactory
function deploy(
address _verifier,
address _vkRegistry,
uint256 _duration,
uint256 _maxVoteOptions,
TreeDepths calldata _treeDepths,
uint8 _messageBatchSize,
PubKey calldata _coordinatorPubKey,
address _maci
ExtContracts calldata _extContracts

Check warning

Code scanning / Slither

Conformance to Solidity naming conventions Warning

) public virtual returns (address pollAddr) {
/// @notice Validate _maxVoteOptions
/// maxVoteOptions must be less than 2 ** 50 due to circuit limitations;
Expand All @@ -37,20 +35,8 @@ contract PollFactory is Params, DomainObjs, IPollFactory {
revert InvalidMaxVoteOptions();
}

/// @notice the smart contracts that a Poll would interact with
ExtContracts memory extContracts = ExtContracts({ maci: IMACI(_maci) });

// deploy the poll
Poll poll = new Poll(
_verifier,
_vkRegistry,
_duration,
_maxVoteOptions,
_treeDepths,
_messageBatchSize,
_coordinatorPubKey,
extContracts
);
Poll poll = new Poll(_duration, _maxVoteOptions, _treeDepths, _messageBatchSize, _coordinatorPubKey, _extContracts);

// init Poll
poll.init();
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/Tally.sol
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs {
) public view returns (bool isValid) {
(uint8 intStateTreeDepth, uint8 voteOptionTreeDepth) = poll.treeDepths();

IMACI maci = poll.extContracts();
IMACI maci = poll.getMaciContract();

// Get the verifying key
VerifyingKey memory vk = vkRegistry.getTallyVk(maci.stateTreeDepth(), intStateTreeDepth, voteOptionTreeDepth, mode);
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/interfaces/IPoll.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ interface IPoll {

/// @notice Get the external contracts
/// @return maci The IMACI contract
function extContracts() external view returns (IMACI maci);
function getMaciContract() external view returns (IMACI maci);
}
8 changes: 2 additions & 6 deletions contracts/contracts/interfaces/IPollFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@ import { DomainObjs } from "../utilities/DomainObjs.sol";
/// @notice PollFactory interface
interface IPollFactory {
/// @notice Deploy a new Poll contract
/// @param _verifier Verifier contract
/// @param _vkRegistry VkRegistry contract
/// @param _duration The duration of the poll
/// @param _maxVoteOptions The max vote options for the poll
/// @param _treeDepths The depths of the merkle trees
/// @param _messageBatchSize The size of message batch
/// @param _coordinatorPubKey The coordinator's public key
/// @param _maci The MACI contract interface reference
/// @param _extContract The external contract interface references
/// @return The deployed Poll contract
function deploy(
address _verifier,
address _vkRegistry,
uint256 _duration,
uint256 _maxVoteOptions,
Params.TreeDepths memory _treeDepths,
uint8 _messageBatchSize,
DomainObjs.PubKey memory _coordinatorPubKey,
address _maci
Params.ExtContracts calldata _extContract
) external returns (address);
}
4 changes: 4 additions & 0 deletions contracts/contracts/utilities/Params.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pragma solidity ^0.8.20;

import { IMACI } from "../interfaces/IMACI.sol";
import { IVerifier } from "../interfaces/IVerifier.sol";
import { IVkRegistry } from "../interfaces/IVkRegistry.sol";

/// @title Params
/// @notice This contracts contains a number of structures
Expand All @@ -20,5 +22,7 @@ contract Params {
/// deployment
struct ExtContracts {
IMACI maci;
IVerifier verifier;
IVkRegistry vkRegistry;
}
}
1 change: 0 additions & 1 deletion contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const config: HardhatUserConfig = {
enabled: true,
runs: 200,
},
viaIR: true,
},
},
defaultNetwork: "localhost",
Expand Down
19 changes: 12 additions & 7 deletions contracts/tests/PollFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ import { Keypair } from "maci-domainobjs";
import { deployPollFactory, getDefaultSigner } from "../ts";
import { MACI, PollFactory, Verifier, VkRegistry } from "../typechain-types";

import { messageBatchSize, initialVoiceCreditBalance, maxVoteOptions, STATE_TREE_DEPTH, treeDepths } from "./constants";
import {
messageBatchSize,
initialVoiceCreditBalance,
maxVoteOptions,
STATE_TREE_DEPTH,
treeDepths,
ExtContractsStruct,
} from "./constants";
import { deployTestContracts } from "./utils";

describe("pollFactory", () => {
let maciContract: MACI;
let verifierContract: Verifier;
let vkRegistryContract: VkRegistry;
let extContracts: ExtContractsStruct;
let pollFactory: PollFactory;
let signer: Signer;

Expand All @@ -23,21 +31,20 @@ describe("pollFactory", () => {
maciContract = r.maciContract;
verifierContract = r.mockVerifierContract as Verifier;
vkRegistryContract = r.vkRegistryContract;
extContracts = { maci: maciContract, verifier: verifierContract, vkRegistry: vkRegistryContract };

pollFactory = (await deployPollFactory(signer, true)) as BaseContract as PollFactory;
});

describe("deployment", () => {
it("should allow anyone to deploy a new poll", async () => {
const tx = await pollFactory.deploy(
verifierContract,
vkRegistryContract,
"100",
maxVoteOptions,
treeDepths,
messageBatchSize,
coordinatorPubKey.asContractParam(),
maciContract,
extContracts,
);
const receipt = await tx.wait();
expect(receipt?.status).to.eq(1);
Expand All @@ -47,14 +54,12 @@ describe("pollFactory", () => {
const maxVoteOptionsInvalid = 2 ** 50;
await expect(
pollFactory.deploy(
verifierContract,
vkRegistryContract,
"100",
maxVoteOptionsInvalid,
treeDepths,
messageBatchSize,
coordinatorPubKey.asContractParam(),
maciContract,
extContracts,
),
).to.be.revertedWithCustomError(pollFactory, "InvalidMaxVoteOptions");
});
Expand Down
7 changes: 7 additions & 0 deletions contracts/tests/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { AddressLike } from "ethers";
import { TreeDepths, STATE_TREE_ARITY } from "maci-core";
import { G1Point, G2Point } from "maci-crypto";
import { VerifyingKey } from "maci-domainobjs";

export interface ExtContractsStruct {
maci: AddressLike;
verifier: AddressLike;
vkRegistry: AddressLike;
}

export const duration = 2_000;

export const STATE_TREE_DEPTH = 10;
Expand Down

0 comments on commit 74a2ea1

Please sign in to comment.