mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-20 04:46:20 +00:00
306bbb73ca
* 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
50 lines
1.5 KiB
TypeScript
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);
|
|
}
|
|
};
|