Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use non-zero start and end blocks for proof generation #1336

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions cli/ts/commands/genProofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,28 @@ 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, defaultStartBlockPoll, stateRoot, numSignups, messageRoot] = await Promise.all([
maciContract.queryFilter(maciContract.filters.SignUp()).then((events) => events[0]?.blockNumber ?? 0),
maciContract.queryFilter(maciContract.filters.DeployPoll()).then((events) => events[0]?.blockNumber ?? 0),
maciContract.getStateAqRoot(),
maciContract.numSignUps(),
messageAqContract.getMainRoot(messageTreeDepth),
]);
const defaultStartBlock = Math.min(defaultStartBlockPoll, defaultStartBlockSignup);
let fromBlock = startBlock ? Number(startBlock) : defaultStartBlock;

const defaultEndBlock = await Promise.all([
pollContract
.queryFilter(pollContract.filters.MergeMessageAq(messageRoot), fromBlock)
.then((events) => events[events.length - 1]?.blockNumber),
pollContract
.queryFilter(pollContract.filters.MergeMaciStateAq(stateRoot, numSignups), fromBlock)
.then((events) => events[events.length - 1]?.blockNumber),
]).then((blocks) => Math.max(...blocks));

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 +272,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
31 changes: 27 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,44 @@ 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, defaultStartBlockPoll, { messageTreeDepth }, stateRoot, numSignups] =
await Promise.all([
maciContract.queryFilter(maciContract.filters.SignUp()).then((events) => events[0]?.blockNumber ?? 0),
maciContract.queryFilter(maciContract.filters.DeployPoll()).then((events) => events[0]?.blockNumber ?? 0),
pollContract.treeDepths(),
maciContract.getStateAqRoot(),
maciContract.numSignUps(),
]);
const defaultStartBlock = Math.min(defaultStartBlockPoll, defaultStartBlockSignup);
let fromBlock = startBlock ? Number(startBlock) : defaultStartBlock;

const messageRoot = await messageAq.getMainRoot(messageTreeDepth);
const defaultEndBlock = await Promise.all([
pollContract
.queryFilter(pollContract.filters.MergeMessageAq(messageRoot), fromBlock)
.then((events) => events[events.length - 1]?.blockNumber),
pollContract
.queryFilter(pollContract.filters.MergeMaciStateAq(stateRoot, numSignups), fromBlock)
.then((events) => events[events.length - 1]?.blockNumber),
]).then((blocks) => Math.max(...blocks));

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

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
Loading