From 7b19d383ec161bd906c583c208b07214cae93863 Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Mon, 20 Nov 2023 17:13:49 +0530 Subject: [PATCH] Run block fill for watcher automatically (#479) * Find minimum start block number * Perform fill if db is empty * Make getStartBlock method public in GraphWatcher * Use dataSources property * Update server template to fill block on start * Avoid passing graphWatcher from generated watcher * Ensure block filling for non-subgraph watchers as well * Remove getStartBlock method from GraphWatcher * Remove graphWatcher property from ServerCmd --------- Co-authored-by: neeraj --- packages/cli/src/server.ts | 21 +++++++++++++++++++-- packages/cli/src/utils/index.ts | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/server.ts b/packages/cli/src/server.ts index c96a8ab4..8ffe131c 100644 --- a/packages/cli/src/server.ts +++ b/packages/cli/src/server.ts @@ -29,7 +29,8 @@ import { PaymentsManager, Consensus, readParty, - UpstreamConfig + UpstreamConfig, + fillBlocks } from '@cerc-io/util'; import { TypeSource } from '@graphql-tools/utils'; import type { @@ -44,7 +45,7 @@ import { utils } from '@cerc-io/nitro-node'; import type { Libp2p } from '@cerc-io/libp2p'; import { BaseCmd } from './base'; -import { readPeerId } from './utils/index'; +import { readPeerId, getStartBlock } from './utils/index'; const log = debug('vulcanize:server'); @@ -284,6 +285,22 @@ export class ServerCmd { assert(indexer); assert(eventWatcher); + const syncStatus = await indexer.getSyncStatus(); + if (!syncStatus) { + const contracts = await this.database.getContracts(); + const startBlock = getStartBlock(contracts); + await fillBlocks( + jobQueue, + indexer, + eventWatcher, + config.jobQueue.blockDelayInMilliSecs, + { + startBlock, + endBlock: startBlock + } + ); + } + if (config.server.kind === KIND_ACTIVE) { // Delete all active and pending (before completed) jobs to prevent creating jobs after completion of processing previous block await jobQueue.deleteAllJobs('completed'); diff --git a/packages/cli/src/utils/index.ts b/packages/cli/src/utils/index.ts index ee985bc8..556e18ba 100644 --- a/packages/cli/src/utils/index.ts +++ b/packages/cli/src/utils/index.ts @@ -64,3 +64,20 @@ export const initClients = async (config: Config): Promise<{ ethProvider }; }; + +export const getStartBlock = (contracts: any[]): number => { + if (!contracts || contracts.length === 0) { + return 0; + } + + let minStartingBlock = contracts[0].startingBlock; + + for (let i = 1; i < contracts.length; i++) { + const currentStartingBlock = contracts[i].startingBlock; + if (currentStartingBlock < minStartingBlock) { + minStartingBlock = currentStartingBlock; + } + } + + return minStartingBlock; +};