2021-08-18 10:20:44 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 Vulcanize, Inc.
|
|
|
|
//
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
import assert from 'assert';
|
2021-08-18 10:20:44 +00:00
|
|
|
|
|
|
|
import { EthClient } from '@vulcanize/ipld-eth-client';
|
|
|
|
|
|
|
|
import { JobQueue } from './job-queue';
|
|
|
|
import { EventWatcherInterface, IndexerInterface } from './types';
|
2021-10-26 12:06:21 +00:00
|
|
|
import { processBlockByNumber } from './common';
|
2021-08-18 10:20:44 +00:00
|
|
|
|
|
|
|
export const fillBlocks = async (
|
|
|
|
jobQueue: JobQueue,
|
|
|
|
indexer: IndexerInterface,
|
|
|
|
ethClient: EthClient,
|
|
|
|
eventWatcher: EventWatcherInterface,
|
2021-10-26 12:06:21 +00:00
|
|
|
blockDelayInMilliSecs: number,
|
2021-08-18 10:20:44 +00:00
|
|
|
{ startBlock, endBlock }: { startBlock: number, endBlock: number}
|
|
|
|
): Promise<any> => {
|
2021-10-20 10:36:03 +00:00
|
|
|
assert(startBlock < endBlock, 'endBlock should be greater than startBlock');
|
|
|
|
|
2021-08-18 10:20:44 +00:00
|
|
|
await eventWatcher.initBlockProcessingOnCompleteHandler();
|
|
|
|
await eventWatcher.initEventProcessingOnCompleteHandler();
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
let currentBlockNumber = startBlock;
|
|
|
|
const syncStatus = await indexer.getSyncStatus();
|
2021-09-09 05:21:58 +00:00
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
if (syncStatus) {
|
|
|
|
if (currentBlockNumber > syncStatus.latestIndexedBlockNumber + 1) {
|
|
|
|
throw new Error(`Missing blocks between startBlock ${currentBlockNumber} and latestIndexedBlockNumber ${syncStatus.latestIndexedBlockNumber}`);
|
2021-08-18 10:20:44 +00:00
|
|
|
}
|
2021-10-20 10:36:03 +00:00
|
|
|
|
|
|
|
currentBlockNumber = syncStatus.latestIndexedBlockNumber + 1;
|
2021-08-18 10:20:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 09:18:29 +00:00
|
|
|
console.time(`time:fill#fillBlocks-process_block_${currentBlockNumber}`);
|
|
|
|
|
2021-12-03 10:53:11 +00:00
|
|
|
processBlockByNumber(jobQueue, indexer, blockDelayInMilliSecs, currentBlockNumber);
|
2021-10-20 10:36:03 +00:00
|
|
|
|
2021-08-18 10:20:44 +00:00
|
|
|
// Creating an AsyncIterable from AsyncIterator to iterate over the values.
|
|
|
|
// https://www.codementor.io/@tiagolopesferreira/asynchronous-iterators-in-javascript-jl1yg8la1#for-wait-of
|
|
|
|
const blockProgressEventIterable = {
|
|
|
|
// getBlockProgressEventIterator returns an AsyncIterator which can be used to listen to BlockProgress events.
|
|
|
|
[Symbol.asyncIterator]: eventWatcher.getBlockProgressEventIterator.bind(eventWatcher)
|
|
|
|
};
|
|
|
|
|
2021-12-14 09:18:29 +00:00
|
|
|
console.time('time:fill#fillBlocks-process_blocks');
|
|
|
|
|
2021-08-18 10:20:44 +00:00
|
|
|
// Iterate over async iterable.
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of
|
|
|
|
for await (const data of blockProgressEventIterable) {
|
|
|
|
const { onBlockProgressEvent: { blockNumber, isComplete } } = data;
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
if (blockNumber === currentBlockNumber && isComplete) {
|
2021-12-14 09:18:29 +00:00
|
|
|
console.timeEnd(`time:fill#fillBlocks-process_block_${currentBlockNumber}`);
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
if (blockNumber >= endBlock) {
|
|
|
|
// Break the async loop when blockProgress event is for the endBlock and processing is complete.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentBlockNumber++;
|
2021-12-14 09:18:29 +00:00
|
|
|
|
|
|
|
console.time(`time:fill#fillBlocks-process_block_${currentBlockNumber}`);
|
|
|
|
|
2021-12-03 10:53:11 +00:00
|
|
|
processBlockByNumber(jobQueue, indexer, blockDelayInMilliSecs, currentBlockNumber);
|
2021-08-18 10:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-14 09:18:29 +00:00
|
|
|
|
|
|
|
console.timeEnd('time:fill#fillBlocks-process_blocks');
|
2021-08-18 10:20:44 +00:00
|
|
|
};
|