mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-07-03 02:05:20 +00:00
Index start blocks to create init states of watched contracts
This commit is contained in:
parent
39af78050b
commit
0ae926224c
@ -330,6 +330,22 @@ export class Indexer {
|
|||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getBlockWithFullBlock (blockFilter: { blockNumber?: number, blockHash?: string }): Promise<{ block: DeepPartial<BlockProgressInterface>, fullBlock: EthFullBlock }> {
|
||||||
|
const [fullBlock] = await this.getBlocks(blockFilter);
|
||||||
|
assert(fullBlock);
|
||||||
|
|
||||||
|
const block = {
|
||||||
|
...fullBlock,
|
||||||
|
blockTimestamp: Number(fullBlock.timestamp),
|
||||||
|
blockNumber: Number(fullBlock.blockNumber)
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
block: block as DeepPartial<BlockProgressInterface>,
|
||||||
|
fullBlock
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async getBlockProgress (blockHash: string): Promise<BlockProgressInterface | undefined> {
|
async getBlockProgress (blockHash: string): Promise<BlockProgressInterface | undefined> {
|
||||||
return this._db.getBlockProgress(blockHash);
|
return this._db.getBlockProgress(blockHash);
|
||||||
}
|
}
|
||||||
@ -394,6 +410,17 @@ export class Indexer {
|
|||||||
|
|
||||||
const { addresses, topics } = this._createLogsFilters(eventSignaturesMap);
|
const { addresses, topics } = this._createLogsFilters(eventSignaturesMap);
|
||||||
|
|
||||||
|
// Create a set of starting blocks of watched contracts in range [fromBlock, toBlock]
|
||||||
|
// TODO: Optimize (avoid template contracts)
|
||||||
|
const watchedBlockNumbers = Object.keys(this._watchedContracts).reduce((acc: Set<number>, address: string) => {
|
||||||
|
const startBlock = this._watchedContracts[address].startingBlock;
|
||||||
|
if (startBlock >= fromBlock && startBlock <= toBlock) {
|
||||||
|
acc.add(startBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, new Set());
|
||||||
|
|
||||||
const { logs } = await this._ethClient.getLogsForBlockRange({
|
const { logs } = await this._ethClient.getLogsForBlockRange({
|
||||||
fromBlock,
|
fromBlock,
|
||||||
toBlock,
|
toBlock,
|
||||||
@ -410,19 +437,13 @@ export class Indexer {
|
|||||||
// Fetch blocks with transactions for the logs returned
|
// Fetch blocks with transactions for the logs returned
|
||||||
console.time(`time:indexer#fetchAndSaveFilteredEventsAndBlocks-fetch-blocks-txs-${fromBlock}-${toBlock}`);
|
console.time(`time:indexer#fetchAndSaveFilteredEventsAndBlocks-fetch-blocks-txs-${fromBlock}-${toBlock}`);
|
||||||
const blocksPromises = Array.from(blockLogsMap.keys()).map(async (blockHash) => {
|
const blocksPromises = Array.from(blockLogsMap.keys()).map(async (blockHash) => {
|
||||||
const [fullBlock] = await this._ethClient.getFullBlocks({ blockHash });
|
const { block, fullBlock } = await this.getBlockWithFullBlock({ blockHash });
|
||||||
assert(fullBlock);
|
|
||||||
|
|
||||||
const block = {
|
// Remove this block from watchedBlockNumbers set as it's already fetched
|
||||||
...fullBlock,
|
assert(block.blockNumber);
|
||||||
blockTimestamp: Number(fullBlock.timestamp),
|
watchedBlockNumbers.delete(block.blockNumber);
|
||||||
blockNumber: Number(fullBlock.blockNumber)
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return { block, fullBlock };
|
||||||
block: block as DeepPartial<BlockProgressInterface>,
|
|
||||||
fullBlock
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const ethFullTxPromises = txHashes.map(async txHash => {
|
const ethFullTxPromises = txHashes.map(async txHash => {
|
||||||
@ -432,6 +453,19 @@ export class Indexer {
|
|||||||
const blocks = await Promise.all(blocksPromises);
|
const blocks = await Promise.all(blocksPromises);
|
||||||
const ethFullTxs = await Promise.all(ethFullTxPromises);
|
const ethFullTxs = await Promise.all(ethFullTxPromises);
|
||||||
|
|
||||||
|
// Fetch starting blocks for watched contracts
|
||||||
|
const watchedBlocks = await Promise.all(
|
||||||
|
Array.from(watchedBlockNumbers).map(async (blockNumber) => this.getBlockWithFullBlock({ blockNumber }))
|
||||||
|
);
|
||||||
|
|
||||||
|
// Merge and sort the two block lists
|
||||||
|
const sortedBlocks = [...blocks, ...watchedBlocks].sort((b1, b2) => {
|
||||||
|
assert(b1.block.blockNumber);
|
||||||
|
assert(b2.block.blockNumber);
|
||||||
|
|
||||||
|
return b1.block.blockNumber - b2.block.blockNumber;
|
||||||
|
});
|
||||||
|
|
||||||
const ethFullTxsMap = ethFullTxs.reduce((acc: Map<string, EthFullTransaction>, ethFullTx) => {
|
const ethFullTxsMap = ethFullTxs.reduce((acc: Map<string, EthFullTransaction>, ethFullTx) => {
|
||||||
acc.set(ethFullTx.ethTransactionCidByTxHash.txHash, ethFullTx);
|
acc.set(ethFullTx.ethTransactionCidByTxHash.txHash, ethFullTx);
|
||||||
return acc;
|
return acc;
|
||||||
@ -441,7 +475,7 @@ export class Indexer {
|
|||||||
|
|
||||||
// Map db ready events according to blockhash
|
// Map db ready events according to blockhash
|
||||||
console.time(`time:indexer#fetchAndSaveFilteredEventsAndBlocks-db-save-blocks-events-${fromBlock}-${toBlock}`);
|
console.time(`time:indexer#fetchAndSaveFilteredEventsAndBlocks-db-save-blocks-events-${fromBlock}-${toBlock}`);
|
||||||
const blockWithDbEventsPromises = blocks.map(async ({ block, fullBlock }) => {
|
const blockWithDbEventsPromises = sortedBlocks.map(async ({ block, fullBlock }) => {
|
||||||
const blockHash = block.blockHash;
|
const blockHash = block.blockHash;
|
||||||
assert(blockHash);
|
assert(blockHash);
|
||||||
const logs = blockLogsMap.get(blockHash) || [];
|
const logs = blockLogsMap.get(blockHash) || [];
|
||||||
|
Loading…
Reference in New Issue
Block a user