Skip to content

Commit

Permalink
refactor maci parameters and getEventArg
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Jan 19, 2024
1 parent 1f261e2 commit 68d5036
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 165 deletions.
11 changes: 6 additions & 5 deletions common/src/event.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { TransactionReceipt, Contract } from 'ethers'
import { Contract, TransactionResponse } from 'ethers'

/**
* Get event log argument value
* @param transactionReceipt transaction receipt
* @param transactionReceipt transaction
* @param contract Contract handle
* @param eventName event name
* @param argumentName argument name
* @returns event argument value
*/
export async function getEventArg(
transactionReceipt: TransactionReceipt,
transaction: TransactionResponse,
contract: Contract,
eventName: string,
argumentName: string
): Promise<any> {
const transactionReceipt = await transaction.wait()
const contractAddress = await contract.getAddress()
// eslint-disable-next-line
for (const log of transactionReceipt.logs || []) {
for (const log of transactionReceipt?.logs || []) {
if (log.address !== contractAddress) {
continue
}
Expand All @@ -30,6 +31,6 @@ export async function getEventArg(
}
}
throw new Error(
`Event ${eventName} from contract ${contractAddress} not found in transaction ${transactionReceipt.hash}`
`Event ${eventName} from contract ${contractAddress} not found in transaction ${transaction.hash}`
)
}
12 changes: 10 additions & 2 deletions contracts/cli/newDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DEFAULT_CIRCUIT } from '../utils/circuits'
import { JSONFile } from '../utils/JSONFile'
import { ethers, config, network } from 'hardhat'
import { program } from 'commander'
import { MaciParameters } from '../utils/maciParameters'

program
.description('Deploy a new ClrFund deployer')
Expand All @@ -41,9 +42,16 @@ async function main(args: any) {
})
console.log('Deployed Poseidons', libraries)

const maciFactory = await deployMaciFactory({ libraries, ethers })
const maciParameters = await MaciParameters.fromConfig(
args.circuit,
args.directory
)
const maciFactory = await deployMaciFactory({
libraries,
ethers,
maciParameters,
})
console.log('Deployed MaciFactory at', maciFactory.target)
await setMaciParameters(maciFactory, args.directory, args.circuit)

const clrfundTemplate = await deployContract({
name: 'ClrFund',
Expand Down
42 changes: 19 additions & 23 deletions contracts/contracts/MACIFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {DomainObjs} from 'maci-contracts/contracts/utilities/DomainObjs.sol';
import {MACICommon} from './MACICommon.sol';

contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {


// Verifying Key Registry containing circuit parameters
VkRegistry public vkRegistry;
Expand All @@ -31,6 +30,7 @@ contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {
uint8 public stateTreeDepth;
TreeDepths public treeDepths;
MaxValues public maxValues;
uint256 public messageBatchSize;

// Events
event MaciParametersChanged();
Expand Down Expand Up @@ -119,37 +119,33 @@ contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {
uint8 _stateTreeDepth,
TreeDepths calldata _treeDepths,
MaxValues calldata _maxValues,
VerifyingKey calldata _processVk,
VerifyingKey calldata _tallyVk
uint256 _messageBatchSize
)
public
onlyOwner
{

if (!vkRegistry.hasProcessVk(
_stateTreeDepth,
_treeDepths.messageTreeDepth,
_treeDepths.voteOptionTreeDepth,
_treeDepths.messageTreeSubDepth) ||
!vkRegistry.hasTallyVk(
_stateTreeDepth,
_treeDepths.intStateTreeDepth,
_treeDepths.voteOptionTreeDepth
)
_stateTreeDepth,
_treeDepths.messageTreeDepth,
_treeDepths.voteOptionTreeDepth,
_messageBatchSize)
) {
vkRegistry.setVerifyingKeys(
_stateTreeDepth,
_treeDepths.intStateTreeDepth,
_treeDepths.messageTreeDepth,
_treeDepths.voteOptionTreeDepth,
_treeDepths.messageTreeSubDepth,
_processVk,
_tallyVk
);
revert ProcessVkNotSet();
}

if (!vkRegistry.hasTallyVk(
_stateTreeDepth,
_treeDepths.intStateTreeDepth,
_treeDepths.voteOptionTreeDepth)
) {
revert TallyVkNotSet();
}

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

emit MaciParametersChanged();
}
Expand All @@ -173,7 +169,7 @@ contract MACIFactory is Ownable, Params, SnarkCommon, DomainObjs, MACICommon {
stateTreeDepth,
treeDepths.messageTreeDepth,
treeDepths.voteOptionTreeDepth,
treeDepths.messageTreeSubDepth)
messageBatchSize)
) {
revert ProcessVkNotSet();
}
Expand Down
1 change: 1 addition & 0 deletions contracts/contracts/interfaces/IMACIFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface IMACIFactory {
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 deployMaci(
SignUpGatekeeper signUpGatekeeper,
Expand Down
5 changes: 1 addition & 4 deletions contracts/e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,8 @@ describe('End-to-end Tests', function () {
libraries: poseidonLibraries,
signer: deployer,
ethers,
maciParameters: params,
})
const setMaciTx = await maciFactory.setMaciParameters(
...params.asContractParam()
)
await setMaciTx.wait()

clrfund = await deployContract({
name: 'ClrFund',
Expand Down
28 changes: 4 additions & 24 deletions contracts/tests/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ describe('Clr fund deployer', async () => {
})
}

const params = MaciParameters.mock()
maciFactory = await deployMaciFactory({
libraries: poseidonContracts,
signer: deployer,
ethers,
maciParameters: params,
})
const params = MaciParameters.mock()
await maciFactory.setMaciParameters(...params.asContractParam())

factoryTemplate = await deployContract({
name: 'ClrFund',
Expand Down Expand Up @@ -139,26 +139,6 @@ describe('Clr fund deployer', async () => {
).to.be.revertedWith('Initializable: contract is already initialized')
})

it('can register with the subgraph', async () => {
await expect(
clrFundDeployer.registerInstance(clrfund.target, '{name:dead,title:beef}')
)
.to.emit(clrFundDeployer, 'Register')
.withArgs(clrfund.target, '{name:dead,title:beef}')
})

it('cannot register with the subgraph twice', async () => {
await expect(
clrFundDeployer.registerInstance(clrfund.target, '{name:dead,title:beef}')
)
.to.emit(clrFundDeployer, 'Register')
.withArgs(clrfund.target, '{name:dead,title:beef}')

await expect(
clrFundDeployer.registerInstance(clrfund.target, '{name:dead,title:beef}')
).to.be.revertedWithCustomError(clrFundDeployer, 'ClrFundAlreadyRegistered')
})

it('initializes clrfund', async () => {
expect(await clrfund.coordinator()).to.equal(ZERO_ADDRESS)
expect(await clrfund.nativeToken()).to.equal(ZERO_ADDRESS)
Expand Down Expand Up @@ -209,7 +189,7 @@ describe('Clr fund deployer', async () => {
expect(await recipientRegistry.controller()).to.equal(clrfund.target)
const params = MaciParameters.mock()
expect(await recipientRegistry.maxRecipients()).to.equal(
5 ** params.voteOptionTreeDepth
BigInt(5) ** BigInt(params.treeDepths.voteOptionTreeDepth)
)
})

Expand Down Expand Up @@ -380,7 +360,7 @@ describe('Clr fund deployer', async () => {
await clrfund.setToken(token.target)
await expect(
clrfund.deployNewRound(roundDuration)
).to.be.revertedWithCustomError(roundInterface, 'invalidCoordinator')
).to.be.revertedWithCustomError(roundInterface, 'InvalidCoordinator')
})

it('reverts if current round is not finalized', async () => {
Expand Down
22 changes: 11 additions & 11 deletions contracts/tests/maciFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ describe('MACI factory', () => {
signer: deployer,
})
}
maciParameters = MaciParameters.mock()
maciFactory = await deployMaciFactory({
ethers,
signer: deployer,
libraries: poseidonContracts,
maciParameters,
})
const transaction = await maciFactory.deploymentTransaction()
expect(transaction).to.be.not.null
expect(await getGasUsage(transaction as TransactionResponse)).lessThan(
5600000
)

maciParameters = MaciParameters.mock()

const SignUpGatekeeperArtifact =
await artifacts.readArtifact('SignUpGatekeeper')
signUpGatekeeper = await deployMockContract(
Expand All @@ -66,8 +66,10 @@ describe('MACI factory', () => {

it('sets default MACI parameters', async () => {
const { maxMessages, maxVoteOptions } = await maciFactory.maxValues()
expect(maxMessages).to.equal(BigInt(0))
expect(maxVoteOptions).to.equal(BigInt(0))
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)
})

it('sets MACI parameters', async () => {
Expand All @@ -77,9 +79,11 @@ describe('MACI factory', () => {

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

it('allows only owner to set MACI parameters', async () => {
Expand Down Expand Up @@ -160,10 +164,6 @@ describe('MACI factory', () => {
})

it('links with PoseidonT6 correctly', async () => {
const setParamTx = await maciFactory.setMaciParameters(
...maciParameters.asContractParam()
)
await setParamTx.wait()
const deployTx = await maciFactory.deployMaci(
signUpGatekeeper.target,
initialVoiceCreditProxy.target,
Expand Down
40 changes: 34 additions & 6 deletions contracts/utils/circuits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,38 @@

import path from 'path'

// This should match MACI.TREE_ARITY in the contract
const TREE_ARITY = 5

export const DEFAULT_CIRCUIT = 'micro'

export const CIRCUITS: { [name: string]: any } = {
/**
* Information about the circuit
*/
export type CircuitInfo = {
processMessagesZkey: string
processWitness: string
processWasm: string
processDatFile: string
tallyVotesZkey: string
tallyWitness: string
tallyWasm: string
tallyDatFile: string
stateTreeDepth: number
treeDepths: {
messageTreeDepth: number
messageTreeSubDepth: number
voteOptionTreeDepth: number
intStateTreeDepth: number
}
maxValues: {
maxMessages: bigint
maxVoteOptions: bigint
}
messageBatchSize: bigint
}

export const CIRCUITS: { [name: string]: CircuitInfo } = {
micro: {
processMessagesZkey: 'processmessages_6-8-2-3_final.zkey',
processWitness: 'processMessages_6-8-2-3_test',
Expand All @@ -19,26 +46,27 @@ export const CIRCUITS: { [name: string]: any } = {
tallyWitness: 'tallyVotes_6-2-3_test',
tallyWasm: 'tallyvotes_6-2-3.wasm',
tallyDatFile: 'tallyVotes_6-2-3_test.dat',
// 1st param in processmessages_6-8-2-3
stateTreeDepth: 6,
treeDepths: {
// 1st param in processmessages_6-8-2-3
stateTreeDepth: 6,
// 2nd param in processmessages_6-8-2-3
messageTreeDepth: 8,
// 3rd param in processmessages_6-8-2-3
messageTreeSubDepth: 2,
// last param of processMessages_6-8-2-3 and tallyvotes_6-2-3
voteOptionTreeDepth: 3,
// intStateTreeDepth is the 2nd param in tallyVotes.circom
// 2nd param in tallyvotes_6-2-3
intStateTreeDepth: 2,
},
maxValues: {
// maxMessages and maxVoteOptions are calculated using treeArity = 5 as seen in the following code:
// https://github.com/privacy-scaling-explorations/maci/blob/master/contracts/contracts/Poll.sol#L115
// treeArity ** messageTreeDepth
maxMessages: TREE_ARITY ** 8,
maxMessages: BigInt(TREE_ARITY ** 8),
// treeArity ** voteOptionTreeDepth
maxVoteOptions: TREE_ARITY ** 3,
maxVoteOptions: BigInt(TREE_ARITY ** 3),
},
messageBatchSize: BigInt(TREE_ARITY ** 2),
},
}

Expand Down
Loading

0 comments on commit 68d5036

Please sign in to comment.