Skip to content

Commit

Permalink
updated the contracts to use the latest maci contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
yashgo0018 committed Apr 9, 2024
1 parent 4acc456 commit 32cc637
Show file tree
Hide file tree
Showing 42 changed files with 457 additions and 1,348 deletions.
6 changes: 3 additions & 3 deletions packages/hardhat/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const intStateTreeDepth = 1;
export const messageTreeDepth = 2;
export const voteOptionTreeDepth = 2;
export const messageBatchDepth = 1;
export const processMessagesZkeyPath = "./zkeys/ProcessMessages_6-9-2-3/processMessages_6-9-2-3.zkey";
export const tallyVotesZkeyPath = "./zkeys/TallyVotes_6-2-3/tallyVotes_6-2-3.zkey";
export const subsidyZkeyPath: string | null = null;
export const processMessagesZkeyPath = "./zkeys/ProcessMessages_10-2-1-2_test/ProcessMessages_10-2-1-2_test.0.zkey";
export const tallyVotesZkeyPath = "./zkeys/TallyVotes_10-1-2_test/TallyVotes_10-1-2_test.0.zkey";
export const useQuadraticVoting = false;
26 changes: 13 additions & 13 deletions packages/hardhat/contracts/PollManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ contract PollManager is Params, DomainObjs {

struct PollData {
uint256 id;
uint256 maciPollId;
string name;
bytes encodedOptions;
string ipfsHash;
PollContracts pollContracts;
MACI.PollContracts pollContracts;
uint256 startTime;
uint256 endTime;
uint256 numOfOptions;
Expand All @@ -36,11 +37,13 @@ contract PollManager is Params, DomainObjs {
address public verifier;
address public vkRegistry;
bool public useSubsidy;
bool public isQv;

event PollCreated(
uint256 indexed pollId,
uint256 indexed maciPollId,
address indexed creator,
PollContracts pollContracts,
MACI.PollContracts pollContracts,
string name,
string[] options,
string ipfsHash,
Expand All @@ -53,8 +56,9 @@ contract PollManager is Params, DomainObjs {
_;
}

constructor(MACI _maci) {
constructor(MACI _maci, bool _isQv) {
maci = _maci;
isQv = _isQv;
}

function owner() public view returns (address) {
Expand Down Expand Up @@ -84,13 +88,13 @@ contract PollManager is Params, DomainObjs {
// TODO: check if the number of options are more than limit

// deploy the poll contracts
MACI.PollContracts memory c = maci.deployPoll(
MACI.PollContracts memory pollContracts = maci.deployPoll(
_duration,
treeDepths,
coordinatorPubKey,
verifier,
vkRegistry,
useSubsidy
isQv
);

// encode options to bytes for retrieval
Expand All @@ -99,18 +103,13 @@ contract PollManager is Params, DomainObjs {
uint256 endTime = block.timestamp + _duration;
uint256 pollId = ++totalPolls;

PollContracts memory pollContracts = PollContracts({
poll: c.poll,
messageProcessor: c.messageProcessor,
tally: c.tally,
subsidy: c.subsidy
});

pollIdByAddress[c.poll] = pollId;
pollIdByAddress[pollContracts.poll] = pollId;
uint256 maciPollId = maci.getPollId(pollContracts.poll);

// create poll
polls[pollId] = PollData({
id: pollId,
maciPollId: maciPollId,
name: _name,
encodedOptions: encodedOptions,
numOfOptions: _options.length,
Expand All @@ -123,6 +122,7 @@ contract PollManager is Params, DomainObjs {

emit PollCreated(
pollId,
maciPollId,
msg.sender,
pollContracts,
_name,
Expand Down
47 changes: 19 additions & 28 deletions packages/hardhat/contracts/maci-contracts/MACI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.10;

import { IPollFactory } from "./interfaces/IPollFactory.sol";
import { IMessageProcessorFactory } from "./interfaces/IMPFactory.sol";
import { ITallySubsidyFactory } from "./interfaces/ITallySubsidyFactory.sol";
import { ITallyFactory } from "./interfaces/ITallyFactory.sol";
import { InitialVoiceCreditProxy } from "./initialVoiceCreditProxy/InitialVoiceCreditProxy.sol";
import { SignUpGatekeeper } from "./gatekeepers/SignUpGatekeeper.sol";
import { AccQueue } from "./trees/AccQueue.sol";
Expand Down Expand Up @@ -39,6 +39,7 @@ contract MACI is IMACI, Params, Utilities, Ownable {

/// @notice A mapping of poll IDs to Poll contracts.
mapping(uint256 => address) public polls;
mapping(address => uint256) public pollIds;

/// @notice Whether the subtrees have been merged (can merge root before new signup)
bool public subtreesMerged;
Expand All @@ -56,10 +57,7 @@ contract MACI is IMACI, Params, Utilities, Ownable {
IMessageProcessorFactory public immutable messageProcessorFactory;

/// @notice Factory contract that deploy a Tally contract
ITallySubsidyFactory public immutable tallyFactory;

/// @notice Factory contract that deploy a Subsidy contract
ITallySubsidyFactory public immutable subsidyFactory;
ITallyFactory public immutable tallyFactory;

/// @notice The state AccQueue. Represents a mapping between each user's public key
/// and their voice credit balance.
Expand All @@ -83,7 +81,6 @@ contract MACI is IMACI, Params, Utilities, Ownable {
address poll;
address messageProcessor;
address tally;
address subsidy;
}

// Events
Expand Down Expand Up @@ -115,23 +112,22 @@ contract MACI is IMACI, Params, Utilities, Ownable {
error MaciPubKeyLargerThanSnarkFieldSize();
error PreviousPollNotCompleted(uint256 pollId);
error PollDoesNotExist(uint256 pollId);
error PollAddressDoesNotExist(address pollAddr);
error SignupTemporaryBlocked();
error PubKeyAlreadyRegistered();

/// @notice Create a new instance of the MACI contract.
/// @param _pollFactory The PollFactory contract
/// @param _messageProcessorFactory The MessageProcessorFactory contract
/// @param _tallyFactory The TallyFactory contract
/// @param _subsidyFactory The SubsidyFactory contract
/// @param _signUpGatekeeper The SignUpGatekeeper contract
/// @param _initialVoiceCreditProxy The InitialVoiceCreditProxy contract
/// @param _topupCredit The TopupCredit contract
/// @param _stateTreeDepth The depth of the state tree
constructor(
IPollFactory _pollFactory,
IMessageProcessorFactory _messageProcessorFactory,
ITallySubsidyFactory _tallyFactory,
ITallySubsidyFactory _subsidyFactory,
ITallyFactory _tallyFactory,
SignUpGatekeeper _signUpGatekeeper,
InitialVoiceCreditProxy _initialVoiceCreditProxy,
TopupCredit _topupCredit,
Expand All @@ -150,7 +146,6 @@ contract MACI is IMACI, Params, Utilities, Ownable {
pollFactory = _pollFactory;
messageProcessorFactory = _messageProcessorFactory;
tallyFactory = _tallyFactory;
subsidyFactory = _subsidyFactory;
topupCredit = _topupCredit;
signUpGatekeeper = _signUpGatekeeper;
initialVoiceCreditProxy = _initialVoiceCreditProxy;
Expand Down Expand Up @@ -245,16 +240,16 @@ contract MACI is IMACI, Params, Utilities, Ownable {
/// @param _coordinatorPubKey The coordinator's public key
/// @param _verifier The Verifier Contract
/// @param _vkRegistry The VkRegistry Contract
/// @param useSubsidy If true, the Poll will use the Subsidy contract
/// @param _isQv Whether to support QV or not
/// @return pollAddr a new Poll contract address
function deployPoll(
uint256 _duration,
TreeDepths memory _treeDepths,
PubKey memory _coordinatorPubKey,
address _verifier,
address _vkRegistry,
bool useSubsidy
) public virtual onlyManager returns (PollContracts memory pollAddr) {
bool _isQv
) public virtual onlyOwner returns (PollContracts memory pollAddr) {
// cache the poll to a local variable so we can increment it
uint256 pollId = nextPollId;

Expand Down Expand Up @@ -290,35 +285,26 @@ contract MACI is IMACI, Params, Utilities, Ownable {
_verifier,
_vkRegistry,
p,
_owner
_owner,
_isQv
);
address tally = tallyFactory.deploy(
_verifier,
_vkRegistry,
p,
mp,
_owner
_owner,
_isQv
);

address subsidy;
if (useSubsidy) {
subsidy = subsidyFactory.deploy(
_verifier,
_vkRegistry,
p,
mp,
_owner
);
}

polls[pollId] = p;
pollIds[p] = pollId;

// store the addresses in a struct so they can be returned
pollAddr = PollContracts({
poll: p,
messageProcessor: mp,
tally: tally,
subsidy: subsidy
tally: tally
});

emit DeployPoll(
Expand Down Expand Up @@ -364,4 +350,9 @@ contract MACI is IMACI, Params, Utilities, Ownable {
if (_pollId >= nextPollId) revert PollDoesNotExist(_pollId);
poll = polls[_pollId];
}

function getPollId(address _poll) public view returns (uint256 pollId) {
if (pollIds[_poll] >= nextPollId) revert PollAddressDoesNotExist(_poll);
pollId = pollIds[_poll];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,18 @@ contract MessageProcessor is Ownable, SnarkCommon, Hasher, CommonUtilities, IMes
IPoll public immutable poll;
IVerifier public immutable verifier;
IVkRegistry public immutable vkRegistry;
bool public immutable isQv;

/// @notice Create a new instance
/// @param _verifier The Verifier contract address
/// @param _vkRegistry The VkRegistry contract address
/// @param _poll The Poll contract address
constructor(address _verifier, address _vkRegistry, address _poll) payable {
/// @param _isQv Whether to support QV or not
constructor(address _verifier, address _vkRegistry, address _poll, bool _isQv) payable {
verifier = IVerifier(_verifier);
vkRegistry = IVkRegistry(_vkRegistry);
poll = IPoll(_poll);
isQv = _isQv;
}

/// @notice Update the Poll's currentSbCommitment if the proof is valid.
Expand Down Expand Up @@ -180,11 +183,13 @@ contract MessageProcessor is Ownable, SnarkCommon, Hasher, CommonUtilities, IMes
);

// Get the verifying key from the VkRegistry
IVkRegistry.Mode mode = isQv ? IVkRegistry.Mode.QV : IVkRegistry.Mode.NON_QV;
VerifyingKey memory vk = vkRegistry.getProcessVk(
maci.stateTreeDepth(),
_messageTreeDepth,
_voteOptionTreeDepth,
TREE_ARITY ** _messageTreeSubDepth
TREE_ARITY ** _messageTreeSubDepth,
mode
);

isValid = verifier.verify(_proof, vk, publicInputHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ contract MessageProcessorFactory is Params, DomainObjs, IMessageProcessorFactory
address _verifier,
address _vkRegistry,
address _poll,
address _owner
address _owner,
bool _isQv
) public returns (address messageProcessorAddr) {
// deploy MessageProcessor for this Poll
MessageProcessor messageProcessor = new MessageProcessor(_verifier, _vkRegistry, _poll);
MessageProcessor messageProcessor = new MessageProcessor(_verifier, _vkRegistry, _poll, _isQv);
messageProcessor.transferOwnership(_owner);
messageProcessorAddr = address(messageProcessor);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/hardhat/contracts/maci-contracts/Poll.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ contract Poll is Params, Utilities, SnarkCommon, Ownable, EmptyBallotRoots, IPol

event PublishMessage(Message _message, PubKey _encPubKey);
event TopupMessage(Message _message);
event MergeMaciStateAqSubRoots(uint256 _numSrQueueOps);
event MergeMaciStateAq(uint256 _stateRoot, uint256 _numSignups);
event MergeMessageAqSubRoots(uint256 _numSrQueueOps);
event MergeMessageAq(uint256 _messageRoot);
event MergeMaciStateAqSubRoots(uint256 indexed _numSrQueueOps);
event MergeMaciStateAq(uint256 indexed _stateRoot, uint256 indexed _numSignups);
event MergeMessageAqSubRoots(uint256 indexed _numSrQueueOps);
event MergeMessageAq(uint256 indexed _messageRoot);

/// @notice Each MACI instance can have multiple Polls.
/// When a Poll is deployed, its voting period starts immediately.
Expand Down
Loading

0 comments on commit 32cc637

Please sign in to comment.