-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockListener.ts
59 lines (47 loc) · 1.79 KB
/
blockListener.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { output, getLastBlockNumber } from '~utils';
import { Block, BlockWithTransactions, EthersObserverEvents } from '~types';
import provider from '~provider';
import { processNextBlock } from '~blockProcessor';
/**
* Map storing blocks that have been either picked up by the block listener
* or missed blocks tracking
* Blocks are removed once processed by a call to .delete in the blockProcessor
*/
export const blocksMap = new Map<number, Block | BlockWithTransactions>();
let latestSeenBlockNumber = 0;
export const getLatestSeenBlockNumber = (): number => latestSeenBlockNumber;
// export const blocksMap = new Map<number, boolean|Block>();
export const startBlockListener = (): void => {
provider.on(EthersObserverEvents.Block, async (blockNumber: number) => {
try {
// For now, we just track that this block exists.
latestSeenBlockNumber = Math.max(latestSeenBlockNumber, blockNumber);
output(`Block ${blockNumber} added to the queue`);
processNextBlock();
} catch (error) {
throw new Error(
`Observed block ${blockNumber} but failed to get its data: ${error}`,
);
}
});
output('Block listener started');
trackMissedBlocks();
};
/**
* Function fetching all the blocks between the last processed block and the current block
* that happened when ingestor was not actively listening
*/
const trackMissedBlocks = async (): Promise<void> => {
const lastBlockNumber = getLastBlockNumber();
const currentBlockNumber = await provider.getBlockNumber();
if (lastBlockNumber >= currentBlockNumber) {
return;
}
output(
`Will need to process blocks from block ${
lastBlockNumber + 1
} to ${currentBlockNumber}`,
);
latestSeenBlockNumber = Math.max(latestSeenBlockNumber, currentBlockNumber);
processNextBlock();
};