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 <neeraj.rtly@gmail.com>
This commit is contained in:
Nabarun Gogoi 2023-11-20 17:13:49 +05:30 committed by GitHub
parent 07c0827a2a
commit 7b19d383ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View File

@ -29,7 +29,8 @@ import {
PaymentsManager, PaymentsManager,
Consensus, Consensus,
readParty, readParty,
UpstreamConfig UpstreamConfig,
fillBlocks
} from '@cerc-io/util'; } from '@cerc-io/util';
import { TypeSource } from '@graphql-tools/utils'; import { TypeSource } from '@graphql-tools/utils';
import type { import type {
@ -44,7 +45,7 @@ import { utils } from '@cerc-io/nitro-node';
import type { Libp2p } from '@cerc-io/libp2p'; import type { Libp2p } from '@cerc-io/libp2p';
import { BaseCmd } from './base'; import { BaseCmd } from './base';
import { readPeerId } from './utils/index'; import { readPeerId, getStartBlock } from './utils/index';
const log = debug('vulcanize:server'); const log = debug('vulcanize:server');
@ -284,6 +285,22 @@ export class ServerCmd {
assert(indexer); assert(indexer);
assert(eventWatcher); 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) { if (config.server.kind === KIND_ACTIVE) {
// Delete all active and pending (before completed) jobs to prevent creating jobs after completion of processing previous block // Delete all active and pending (before completed) jobs to prevent creating jobs after completion of processing previous block
await jobQueue.deleteAllJobs('completed'); await jobQueue.deleteAllJobs('completed');

View File

@ -64,3 +64,20 @@ export const initClients = async (config: Config): Promise<{
ethProvider 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;
};