Skip to content

Commit

Permalink
fix(fetch): fix bugs related to logs fetching
Browse files Browse the repository at this point in the history
Ensure the start block is not overriden by the tx hash check. Also ensure we fetch logs at least for
1 block if fromBlock is equal to lastBlock

fix #1043 fix #1044
  • Loading branch information
ctrlc03 committed Jan 18, 2024
1 parent ea97aa6 commit 7c7874e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 4 additions & 3 deletions cli/ts/commands/genProofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ 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;
fromBlock = transactionHash
? await signer.provider!.getTransaction(transactionHash).then((tx) => tx?.blockNumber ?? 0)
: 0;
if (transactionHash) {
const tx = await signer.provider!.getTransaction(transactionHash);
fromBlock = tx?.blockNumber ?? 0;
}

logYellow(quiet, info(`starting to fetch logs from block ${fromBlock}`));
maciState = await genMaciStateFromContract(
Expand Down
12 changes: 7 additions & 5 deletions contracts/ts/genMaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ export const genMaciStateFromContract = async (
// if no last block is set then we fetch until the current block number
const lastBlock = endBlock || (await provider.getBlockNumber());

// Fetch event logs in batches
for (let i = fromBlock; i < lastBlock; i += blocksPerRequest + 1) {
const toBlock = i + blocksPerRequest >= lastBlock ? undefined : i + blocksPerRequest;
// Fetch event logs in batches (lastBlock inclusive)
for (let i = fromBlock; i <= lastBlock; i += blocksPerRequest + 1) {
// the last block batch will be either current iteration block + blockPerRequest
// or the end block if it is set
const toBlock = i + blocksPerRequest >= lastBlock ? lastBlock : i + blocksPerRequest;

const [tmpSignUpLogs, tmpDeployPollLogs] =
// eslint-disable-next-line no-await-in-loop
Expand Down Expand Up @@ -178,8 +180,8 @@ export const genMaciStateFromContract = async (
let mergeMessageAqSubRootsLogs: Log[] = [];
let mergeMessageAqLogs: Log[] = [];

for (let i = fromBlock; i < lastBlock; i += blocksPerRequest + 1) {
const toBlock = i + blocksPerRequest >= lastBlock ? undefined : i + blocksPerRequest;
for (let i = fromBlock; i <= lastBlock; i += blocksPerRequest + 1) {
const toBlock = i + blocksPerRequest >= lastBlock ? lastBlock : i + blocksPerRequest;

const [
tmpPublishMessageLogs,
Expand Down

0 comments on commit 7c7874e

Please sign in to comment.