Skip to content

Commit

Permalink
use MACI v0.0.0-ci.354b9b6
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Jan 31, 2024
1 parent 8d87737 commit f86bacd
Show file tree
Hide file tree
Showing 38 changed files with 4,891 additions and 1,629 deletions.
4 changes: 2 additions & 2 deletions common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"dependencies": {
"@openzeppelin/merkle-tree": "^1.0.5",
"ethers": "^6.9.2",
"maci-crypto": "0.0.0-ci.45d1156",
"maci-domainobjs": "0.0.0-ci.45d1156"
"maci-crypto": "0.0.0-ci.354b9b6",
"maci-domainobjs": "0.0.0-ci.354b9b6"
},
"repository": {
"type": "git",
Expand Down
23 changes: 19 additions & 4 deletions contracts/contracts/ClrFund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ contract ClrFund is OwnableUpgradeable, DomainObjs, Params {
error InvalidMaciFactory();
error RecipientRegistryNotSet();
error NotInitialized();
error VoteOptionTreeDepthNotSet();


/**
Expand Down Expand Up @@ -127,6 +128,20 @@ contract ClrFund is OwnableUpgradeable, DomainObjs, Params {
_setFundingRoundFactory(_roundFactory);
}

/**
* @dev Get the maximum recipients allowed in the recipient registry
*/
function getMaxRecipients() public view returns (uint256 _maxRecipients) {
TreeDepths memory treeDepths = maciFactory.treeDepths();
if (treeDepths.voteOptionTreeDepth == 0) revert VoteOptionTreeDepthNotSet();

uint256 maxVoteOption = maciFactory.TREE_ARITY() ** treeDepths.voteOptionTreeDepth;

// -1 because the first slot of the recipients array is not used
// and maxRecipients is used to generate 0 based index to the array
_maxRecipients = maxVoteOption - 1;
}

/**
* @dev Set registry of verified users.
* @param _userRegistry Address of a user registry.
Expand All @@ -149,8 +164,8 @@ contract ClrFund is OwnableUpgradeable, DomainObjs, Params {
onlyOwner
{
recipientRegistry = _recipientRegistry;
MaxValues memory maxValues = maciFactory.maxValues();
recipientRegistry.setMaxRecipients(maxValues.maxVoteOptions);
uint256 maxRecipients = getMaxRecipients();
recipientRegistry.setMaxRecipients(maxRecipients);

emit RecipientRegistryChanged(address(_recipientRegistry));
}
Expand Down Expand Up @@ -214,8 +229,8 @@ contract ClrFund is OwnableUpgradeable, DomainObjs, Params {
if (address(recipientRegistry) == address(0)) revert RecipientRegistryNotSet();

// Make sure that the max number of recipients is set correctly
MaxValues memory maxValues = maciFactory.maxValues();
recipientRegistry.setMaxRecipients(maxValues.maxVoteOptions);
uint256 maxRecipients = getMaxRecipients();
recipientRegistry.setMaxRecipients(maxRecipients);

// Deploy funding round and MACI contracts
address newRound = roundFactory.deploy(duration, address(this));
Expand Down
6 changes: 3 additions & 3 deletions contracts/contracts/FundingRound.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ contract FundingRound is
uint256 private constant MAX_VOICE_CREDITS = 10 ** 9; // MACI allows 2 ** 32 voice credits max
uint256 private constant MAX_CONTRIBUTION_AMOUNT = 10 ** 4; // In tokens
uint256 private constant ALPHA_PRECISION = 10 ** 18; // to account for loss of precision in division
uint8 private constant LEAVES_PER_NODE = 5; // leaves per node of the tally result tree

// Structs
struct ContributorStatus {
Expand Down Expand Up @@ -181,7 +180,8 @@ contract FundingRound is
*/
function isTallied() private view returns (bool) {
(uint256 numSignUps, ) = poll.numSignUpsAndMessages();
(, uint256 tallyBatchSize, ) = poll.batchSizes();
(uint8 intStateTreeDepth, , , ) = poll.treeDepths();
uint256 tallyBatchSize = TREE_ARITY ** uint256(intStateTreeDepth);
uint256 tallyBatchNum = tally.tallyBatchNum();
uint256 totalTallied = tallyBatchNum * tallyBatchSize;

Expand Down Expand Up @@ -497,7 +497,7 @@ contract FundingRound is

// make sure we have received all the tally results
(,,, uint8 voteOptionTreeDepth) = poll.treeDepths();
uint256 totalResults = uint256(LEAVES_PER_NODE) ** uint256(voteOptionTreeDepth);
uint256 totalResults = uint256(TREE_ARITY) ** uint256(voteOptionTreeDepth);
if ( totalTallyResults != totalResults ) {
revert IncompleteTallyResults(totalResults, totalTallyResults);
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/contracts/MACICommon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pragma solidity ^0.8.10;
* @dev a contract that holds common MACI structures
*/
contract MACICommon {
// MACI tree arity
uint256 public constant TREE_ARITY = 5;

/**
* @dev These are contract factories used to deploy MACI poll processing contracts
* when creating a new ClrFund funding round.
Expand Down
23 changes: 14 additions & 9 deletions contracts/contracts/MACIFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {
// circuit parameters
uint8 public stateTreeDepth;
TreeDepths public treeDepths;
MaxValues public maxValues;
uint256 public messageBatchSize;

// Events
event MaciParametersChanged();
Expand Down Expand Up @@ -63,6 +61,14 @@ contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {
verifier = Verifier(_verifier);
}

/**
* @dev calculate the message batch size
*/
function getMessageBatchSize(uint8 messageTreeSubDepth) public pure
returns(uint256 _messageBatchSize) {
_messageBatchSize = TREE_ARITY ** messageTreeSubDepth;
}

/**
* @dev set vk registry
*/
Expand Down Expand Up @@ -117,19 +123,19 @@ contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {
*/
function setMaciParameters(
uint8 _stateTreeDepth,
TreeDepths calldata _treeDepths,
MaxValues calldata _maxValues,
uint256 _messageBatchSize
TreeDepths calldata _treeDepths
)
public
onlyOwner
{

uint256 messageBatchSize = getMessageBatchSize(_treeDepths.messageTreeSubDepth);

if (!vkRegistry.hasProcessVk(
_stateTreeDepth,
_treeDepths.messageTreeDepth,
_treeDepths.voteOptionTreeDepth,
_messageBatchSize)
messageBatchSize)
) {
revert ProcessVkNotSet();
}
Expand All @@ -144,8 +150,6 @@ contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {

stateTreeDepth = _stateTreeDepth;
treeDepths = _treeDepths;
maxValues = _maxValues;
messageBatchSize = _messageBatchSize;

emit MaciParametersChanged();
}
Expand All @@ -165,6 +169,8 @@ contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {
external
returns (MACI _maci, MACI.PollContracts memory _pollContracts)
{
uint256 messageBatchSize = getMessageBatchSize(treeDepths.messageTreeSubDepth);

if (!vkRegistry.hasProcessVk(
stateTreeDepth,
treeDepths.messageTreeDepth,
Expand Down Expand Up @@ -195,7 +201,6 @@ contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {

_pollContracts = _maci.deployPoll(
duration,
maxValues,
treeDepths,
coordinatorPubKey,
address(verifier),
Expand Down
7 changes: 5 additions & 2 deletions contracts/contracts/interfaces/IMACIFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ interface IMACIFactory {
// poll parameters
function stateTreeDepth() external view returns (uint8);
function treeDepths() external view returns (Params.TreeDepths memory);
function maxValues() external view returns (Params.MaxValues memory);
function messageBatchSize() external view returns (uint256);

function getMessageBatchSize(uint8 _messageTreeSubDepth) external pure
returns(uint256 _messageBatchSize);

function TREE_ARITY() external pure returns (uint256);

function deployMaci(
SignUpGatekeeper signUpGatekeeper,
Expand Down
5 changes: 3 additions & 2 deletions contracts/e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ethers, config } from 'hardhat'
import { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers'
import { time, mine } from '@nomicfoundation/hardhat-network-helpers'
import { expect } from 'chai'
import { Contract, toNumber } from 'ethers'
import { BaseContract, Contract, toNumber } from 'ethers'
import {
Keypair,
createMessage,
Expand Down Expand Up @@ -37,6 +37,7 @@ import { DEFAULT_CIRCUIT } from '../utils/circuits'
import { MaciParameters } from '../utils/maciParameters'
import { readFileSync, existsSync, mkdirSync } from 'fs'
import path from 'path'
import { FundingRound } from '../typechain-types'

type VoteData = { recipientIndex: number; voiceCredits: bigint }

Expand Down Expand Up @@ -412,7 +413,7 @@ describe('End-to-end Tests', function () {
const recipientTreeDepth = params.treeDepths.voteOptionTreeDepth
console.log('Adding tally result on chain in batches of', tallyBatchSize)
await addTallyResultsBatch(
fundingRound.connect(coordinator) as Contract,
fundingRound.connect(coordinator) as BaseContract as FundingRound,
recipientTreeDepth,
tally,
tallyBatchSize
Expand Down
8 changes: 4 additions & 4 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@openzeppelin/contracts": "4.9.0",
"commander": "^11.1.0",
"dotenv": "^8.2.0",
"maci-contracts": "0.0.0-ci.45d1156",
"maci-contracts": "0.0.0-ci.354b9b6",
"solidity-rlp": "2.0.8"
},
"devDependencies": {
Expand All @@ -39,9 +39,9 @@
"hardhat-contract-sizer": "^2.10.0",
"hardhat-gas-reporter": "^1.0.8",
"ipfs-only-hash": "^2.0.1",
"maci-circuits": "0.0.0-ci.45d1156",
"maci-cli": "0.0.0-ci.45d1156",
"maci-domainobjs": "0.0.0-ci.45d1156",
"maci-circuits": "0.0.0-ci.354b9b6",
"maci-cli": "0.0.0-ci.354b9b6",
"maci-domainobjs": "0.0.0-ci.354b9b6",
"mocha": "^10.2.0",
"solidity-coverage": "^0.8.1",
"ts-node": "^10.9.2",
Expand Down
12 changes: 6 additions & 6 deletions contracts/tasks/verifyAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function verifyMaciFactory(clrfund: Contract, run: any): Promise<string> {

async function verifyClrFund(clrfund: Contract, run: any): Promise<string> {
try {
await run('verify', { address: clrfund.target })
await run('verify:verify', { address: clrfund.target })
return SUCCESS
} catch (error) {
return (error as Error).message
Expand Down Expand Up @@ -202,7 +202,7 @@ task('verify-all', 'Verify all clrfund contracts')

const results: Result[] = []
let status = await verifyMaciFactory(clrfundContract, run)
results.push({ name: 'Maci factory', status })
results.push({ name: 'MACIFactory', status })

await verifyContract(
'VkRegistry',
Expand All @@ -224,22 +224,22 @@ task('verify-all', 'Verify all clrfund contracts')
status = await verifyClrFund(clrfundContract, run)
results.push({ name: 'ClrFund', status })
status = await verifyRecipientRegistry(clrfundContract, run)
results.push({ name: 'Recipient registry', status })
results.push({ name: 'RecipientRegistry', status })
status = await verifyUserRegistry(clrfundContract, run)
results.push({ name: 'User registry', status })
results.push({ name: 'UserRegistry', status })
const sponsor = await getBrightIdSponsor(clrfundContract, ethers)
if (sponsor) {
await verifyContract('Sponsor', sponsor, run, results)
}
status = await verifyFundingRoundFactory(clrfundContract, run)
results.push({ name: 'Funding Round Factory', status })
results.push({ name: 'FundingRoundFactory', status })

const roundAddress = await clrfundContract.getCurrentRound()
if (roundAddress !== ZERO_ADDRESS) {
const round = await ethers.getContractAt('FundingRound', roundAddress)
const maciAddress = await round.maci()
status = await verifyRound(roundAddress, run)
results.push({ name: 'Funding round', status })
results.push({ name: 'FundingRound', status })
status = await verifyMaci(maciAddress, run)
results.push({ name: 'MACI', status })

Expand Down
8 changes: 1 addition & 7 deletions contracts/tasks/verifyPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { Contract } from 'ethers'

async function getConstructorArguments(pollContract: Contract): Promise<any[]> {
const [, duration] = await pollContract.getDeployTimeAndDuration()
const [maxValues, treeDepths, batchSizes, coordinatorPubKey, extContracts] =
const [maxValues, treeDepths, coordinatorPubKey, extContracts] =
await Promise.all([
pollContract.maxValues(),
pollContract.treeDepths(),
pollContract.batchSizes(),
pollContract.coordinatorPubKey(),
pollContract.extContracts(),
])
Expand All @@ -24,11 +23,6 @@ async function getConstructorArguments(pollContract: Contract): Promise<any[]> {
messageTreeDepth: treeDepths.messageTreeDepth,
voteOptionTreeDepth: treeDepths.voteOptionTreeDepth,
},
{
messageBatchSize: batchSizes.messageBatchSize,
tallyBatchSize: batchSizes.tallyBatchSize,
subsidyBatchSize: batchSizes.subsidyBatchSize,
},
{
x: coordinatorPubKey.x,
y: coordinatorPubKey.y,
Expand Down
5 changes: 3 additions & 2 deletions contracts/tests/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Contract } from 'ethers'
import { genRandomSalt } from 'maci-crypto'
import { Keypair } from '@clrfund/common'

import { ZERO_ADDRESS, UNIT } from '../utils/constants'
import { TREE_ARITY, ZERO_ADDRESS, UNIT } from '../utils/constants'
import { getGasUsage, getEventArg } from '../utils/contracts'
import {
deployContract,
Expand Down Expand Up @@ -189,7 +189,8 @@ describe('Clr fund deployer', async () => {
expect(await recipientRegistry.controller()).to.equal(clrfund.target)
const params = MaciParameters.mock()
expect(await recipientRegistry.maxRecipients()).to.equal(
BigInt(5) ** BigInt(params.treeDepths.voteOptionTreeDepth)
BigInt(TREE_ARITY) ** BigInt(params.treeDepths.voteOptionTreeDepth) -
BigInt(1)
)
})

Expand Down
13 changes: 5 additions & 8 deletions contracts/tests/maciFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ describe('MACI factory', () => {
})

it('sets default MACI parameters', async () => {
const { maxMessages, maxVoteOptions } = await maciFactory.maxValues()
expect(maxMessages).to.be.greaterThan(BigInt(0))
expect(maxVoteOptions).to.be.greaterThan(BigInt(0))
expect(maxMessages).to.equal(maciParameters.maxValues.maxMessages)
expect(maxVoteOptions).to.equal(maciParameters.maxValues.maxVoteOptions)
const stateTreeDepth = await maciFactory.stateTreeDepth()
const treeDepths = await maciFactory.treeDepths()
expect(stateTreeDepth).to.be.greaterThan(BigInt(0))
expect(treeDepths.voteOptionTreeDepth).to.be.greaterThan(BigInt(0))
expect(treeDepths.messageTreeDepth).to.be.greaterThan(BigInt(0))
})

it('sets MACI parameters', async () => {
Expand All @@ -78,9 +78,6 @@ describe('MACI factory', () => {
).to.emit(maciFactory, 'MaciParametersChanged')

const { messageTreeDepth } = await maciFactory.treeDepths()
const { maxMessages, maxVoteOptions } = await maciFactory.maxValues()
expect(maxMessages).to.equal(maciParameters.maxValues.maxMessages)
expect(maxVoteOptions).to.equal(maciParameters.maxValues.maxVoteOptions)
expect(messageTreeDepth).to.equal(
maciParameters.treeDepths.messageTreeDepth
)
Expand Down
Loading

0 comments on commit f86bacd

Please sign in to comment.