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(genlocalstate): add start block calculation on genLocalState cli command #1338

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
1 change: 0 additions & 1 deletion cli/tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,6 @@ describe("e2e tests", function test() {
await mergeSignups({ ...mergeSignupsArgs, signer });
await genLocalState({
outputPath: stateOutPath,
startBlock: 0,
coordinatorPrivateKey: coordinatorPrivKey,
blockPerBatch: 50,
pollId: 0n,
Expand Down
50 changes: 46 additions & 4 deletions cli/ts/commands/genLocalState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { JsonRpcProvider } from "ethers";
import { genMaciStateFromContract } from "maci-contracts";
import {
MACI__factory as MACIFactory,
AccQueue__factory as AccQueueFactory,
Poll__factory as PollFactory,
genMaciStateFromContract,
} from "maci-contracts";
import { Keypair, PrivKey } from "maci-domainobjs";

import fs from "fs";
Expand Down Expand Up @@ -59,13 +64,50 @@ export const genLocalState = async ({
const coordinatorMaciPrivKey = PrivKey.deserialize(coordPrivKey);
const coordinatorKeypair = new Keypair(coordinatorMaciPrivKey);

const maciContract = MACIFactory.connect(maciAddress, signer);
const pollAddr = await maciContract.polls(pollId);

if (!(await contractExists(signer.provider!, pollAddr))) {
logError("Poll contract does not exist");
}
const pollContract = PollFactory.connect(pollAddr, signer);

const [{ messageAq }, { messageTreeDepth }] = await Promise.all([
pollContract.extContracts(),
pollContract.treeDepths(),
]);
const messageAqContract = AccQueueFactory.connect(messageAq, signer);

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 ?? defaultStartBlock;
}

// calculate the end block number
const endBlockNumber = endBlock || (await signer.provider!.getBlockNumber());
const endBlockNumber = endBlock || defaultEndBlock;

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

const provider = ethereumProvider ? new JsonRpcProvider(ethereumProvider) : signer.provider;
Expand Down
Loading