Skip to content

Commit

Permalink
chore: use non-zero start and end blocks for proof generation
Browse files Browse the repository at this point in the history
- [x] Update maci-contracts task
- [x] Update maci-cli task
- [x] Add indexed params for poll events
- [x] Change deploy config example maci public key
  • Loading branch information
0xmad committed Apr 1, 2024
1 parent 23237c3 commit 7f6f059
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
19 changes: 16 additions & 3 deletions cli/ts/commands/genProofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,23 @@ export const genProofs = async ({
}
} else {
// build an off-chain representation of the MACI contract using data in the contract storage
let fromBlock = startBlock ? Number(startBlock) : 0;
const defaultStartBlockSignup = await maciContract
.queryFilter(maciContract.filters.SignUp())
.then((events) => events[0]?.blockNumber ?? 0);
const defaultStartBlockPoll = await maciContract
.queryFilter(maciContract.filters.DeployPoll())
.then((events) => events[0]?.blockNumber ?? 0);
const defaultStartBlock = Math.min(defaultStartBlockPoll, defaultStartBlockSignup);
let fromBlock = startBlock ? Number(startBlock) : defaultStartBlock;

const root = await messageAqContract.getMainRoot(messageTreeDepth);
const defaultEndBlock = await pollContract
.queryFilter(pollContract.filters.MergeMessageAq(root), fromBlock)
.then((events) => events[events.length - 1]?.blockNumber);

if (transactionHash) {
const tx = await signer.provider!.getTransaction(transactionHash);
fromBlock = tx?.blockNumber ?? 0;
fromBlock = tx?.blockNumber ?? defaultStartBlock;
}

logYellow(quiet, info(`starting to fetch logs from block ${fromBlock}`));
Expand All @@ -254,7 +267,7 @@ export const genProofs = async ({
pollId,
fromBlock,
blocksPerBatch,
endBlock,
endBlock || defaultEndBlock,
);
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/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
2 changes: 1 addition & 1 deletion contracts/deploy-config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"Poll": {
"pollDuration": 30,
"coordinatorPubkey": "macipk.ea638a3366ed91f2e955110888573861f7c0fc0bb5fb8b8dca9cd7a08d7d6b93",
"coordinatorPubkey": "macipk.9a59264310d95cfd8eb7083aebeba221b5c26e77427f12b7c0f50bc1cc35e621",
"subsidyEnabled": false,
"useQuadraticVoting": true
}
Expand Down
25 changes: 21 additions & 4 deletions contracts/tasks/helpers/ProofGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export class ProofGenerator {
* @returns {MaciState} maci state
*/
static async prepareState({
maciContractAddress,
maciContract,
pollContract,
messageAq,
pollId,
maciPrivateKey,
coordinatorKeypair,
Expand All @@ -101,23 +103,38 @@ export class ProofGenerator {
}

// build an off-chain representation of the MACI contract using data in the contract storage
let fromBlock = startBlock ? Number(startBlock) : 0;
const defaultStartBlockSignup = await maciContract
.queryFilter(maciContract.filters.SignUp())
.then((events) => events[0]?.blockNumber ?? 0);
const defaultStartBlockPoll = await maciContract
.queryFilter(maciContract.filters.DeployPoll())
.then((events) => events[0]?.blockNumber ?? 0);
const defaultStartBlock = Math.min(defaultStartBlockPoll, defaultStartBlockSignup);
let fromBlock = startBlock ? Number(startBlock) : defaultStartBlock;

const { messageTreeDepth } = await pollContract.treeDepths();
const root = await messageAq.getMainRoot(messageTreeDepth);
const defaultEndBlock = await pollContract
.queryFilter(pollContract.filters.MergeMessageAq(root), fromBlock)
.then((events) => events[events.length - 1]?.blockNumber);

if (transactionHash) {
const tx = await signer.provider!.getTransaction(transactionHash);
fromBlock = tx?.blockNumber ?? 0;
fromBlock = tx?.blockNumber ?? defaultEndBlock;
}

console.log(`starting to fetch logs from block ${fromBlock}`);

const maciContractAddress = await maciContract.getAddress();

return genMaciStateFromContract(
signer.provider!,
maciContractAddress,
coordinatorKeypair,
BigInt(pollId),
fromBlock,
blocksPerBatch,
endBlock,
endBlock || defaultEndBlock,
);
}

Expand Down
14 changes: 12 additions & 2 deletions contracts/tasks/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,19 @@ export interface ICircuitFiles {
*/
export interface IPrepareStateParams {
/**
* MACI contract address
* MACI contract
*/
maciContractAddress: string;
maciContract: MACI;

/**
* Poll contract
*/
pollContract: Poll;

/**
* MessageAq contract
*/
messageAq: AccQueue;

/**
* Poll id
Expand Down
25 changes: 16 additions & 9 deletions contracts/tasks/runner/prove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import { Keypair, PrivKey } from "maci-domainobjs";
import fs from "fs";

import type { Proof } from "../../ts/types";

import {
type VkRegistry,
type Subsidy,
type Verifier,
type MACI,
type Poll,
type AccQueue,
import type {
VkRegistry,
Subsidy,
Verifier,
MACI,
Poll,
AccQueue,
MessageProcessor,
Tally,
TallyNonQv,
} from "../../typechain-types";

import { ContractStorage } from "../helpers/ContractStorage";
import { Deployment } from "../helpers/Deployment";
import { ProofGenerator } from "../helpers/ProofGenerator";
Expand Down Expand Up @@ -102,6 +102,11 @@ task("prove", "Command to generate proof and prove the result of a poll on-chain

const pollAddress = await maciContract.polls(poll);
const pollContract = await deployment.getContract<Poll>({ name: EContracts.Poll, address: pollAddress });
const messageAqAddress = await pollContract.extContracts().then((contracts) => contracts.messageAq);
const messageAq = await deployment.getContract<AccQueue>({
name: EContracts.AccQueue,
address: messageAqAddress,
});

const [, messageAqContractAddress] = await pollContract.extContracts();
const messageAqContract = await deployment.getContract<AccQueue>({
Expand All @@ -125,7 +130,9 @@ task("prove", "Command to generate proof and prove the result of a poll on-chain
}

const maciState = await ProofGenerator.prepareState({
maciContractAddress,
maciContract,
pollContract,
messageAq,
maciPrivateKey,
coordinatorKeypair,
pollId: poll,
Expand Down

0 comments on commit 7f6f059

Please sign in to comment.