watcher-ts/packages/util/src/index-block.ts
prathamesh0 306bbb73ca
Use prefetching of blocks with events in watchers and codegen (#206)
* Avoid refetching block while fetching events

* Prefetch a batch of blocks with events while indexing

* Update mock indexer used in graph-node testing

* Process available blocks while prefetching

* Refactor events fetching to a method in util

* Move method to get GQL event query result to util
2022-10-20 18:46:56 +05:30

50 lines
1.5 KiB
TypeScript

//
// Copyright 2022 Vulcanize, Inc.
//
import assert from 'assert';
import { BlockProgressInterface, IndexerInterface } from './types';
import { processBatchEvents } from './common';
export const indexBlock = async (
indexer: IndexerInterface,
eventsInBatch: number,
argv: {
block: number,
}
): Promise<any> => {
let blockProgressEntities: Partial<BlockProgressInterface>[] = await indexer.getBlocksAtHeight(argv.block, false);
if (!blockProgressEntities.length) {
console.time('time:index-block#getBlocks-ipld-eth-server');
const blocks = await indexer.getBlocks({ blockNumber: argv.block });
blockProgressEntities = blocks.map((block: any): Partial<BlockProgressInterface> => {
block.blockTimestamp = block.timestamp;
return block;
});
console.timeEnd('time:index-block#getBlocks-ipld-eth-server');
}
assert(blockProgressEntities.length, `No blocks fetched for block number ${argv.block}.`);
for (const partialblockProgress of blockProgressEntities) {
let blockProgress: BlockProgressInterface;
// Check if blockProgress fetched from database.
if (!partialblockProgress.id) {
[blockProgress] = await indexer.saveBlockAndFetchEvents(partialblockProgress);
} else {
blockProgress = partialblockProgress as BlockProgressInterface;
}
assert(indexer.processBlock);
await indexer.processBlock(blockProgress);
await processBatchEvents(indexer, blockProgress, eventsInBatch);
}
};