Skip to content

Commit

Permalink
chore(genlocalstate): add start block calculation on genLocalState cl…
Browse files Browse the repository at this point in the history
…i command
  • Loading branch information
ctrlc03 committed Apr 2, 2024
1 parent da5ba64 commit f4c4c26
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
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 extContracts = await pollContract.extContracts();
const messageAqContractAddr = extContracts.messageAq;
const messageAqContract = AccQueueFactory.connect(messageAqContractAddr, signer);

const messageTreeDepth = Number((await pollContract.treeDepths()).messageTreeDepth);

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

0 comments on commit f4c4c26

Please sign in to comment.