Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
🐛 Fix redundant pririoty scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Jun 23, 2023
1 parent a5d4801 commit 7bb5e62
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions services/blockchain-indexer/shared/indexer/blockchainIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ const INDEX_VERIFIED_HEIGHT = 'indexVerifiedHeight';

const validateBlock = (block) => !!block && block.height >= 0;

let lastRescheduledBlockHeightWithPriority = -1;

const indexBlock = async job => {
const { block } = job.data;
if (!validateBlock(block)) throw new Error(`Invalid block ${block.id} at height ${block.height}.`);

logger.info(block.height);

const blocksTable = await getBlocksTable();

// Check if previous block is indexed, index previous block if not indexed
// Check if the previous blocks are indexed, if not schedule them with priority
if (block.height !== await getGenesisHeight()) {
const [lastIndexedBlock = {}] = await blocksTable.find(
{
Expand All @@ -110,24 +110,30 @@ const indexBlock = async job => {
// Skip the indexing process if the last indexed block's height is
// higher than or equal to current block's height and is already finalized
if (block.height <= lastIndexedBlock.height && lastIndexedBlock.isFinal) {
logger.trace(`Skipping block indexing at height ${block.height}. Last final block indexed is ${lastIndexedBlock.height}.`);
return;
}

const heightsToIndex = range(
(lastIndexedBlock.height || await getGenesisHeight()) + 1,
block.height + 1, // '+ 1' as 'to' is non-inclusive
1,
);

if (heightsToIndex.length > 1) {
await BluebirdPromise.map(
heightsToIndex,
// eslint-disable-next-line no-use-before-define
async (height) => addBlockToQueue(height, true),
{ concurrency: 1 },
if (block.height >= lastRescheduledBlockHeightWithPriority) {
const heightsToIndex = range(
Math.max((lastIndexedBlock.height || await getGenesisHeight()) + 1, lastRescheduledBlockHeightWithPriority),
block.height + 1, // '+ 1' as 'to' is non-inclusive
1,
);

return;
if (heightsToIndex.length > 1) {
logger.trace(`Adding ${heightsToIndex.length} blocks. Current block height: ${block.height}. Scheduled blocks between heights ${heightsToIndex.at(0)} - ${heightsToIndex.at(-1)}.`);
await BluebirdPromise.map(
heightsToIndex,
// eslint-disable-next-line no-use-before-define
async (height) => addBlockToQueue(height, true),
{ concurrency: 1 },
);

lastRescheduledBlockHeightWithPriority = Math.max(...heightsToIndex);

return;
}
}
}

Expand Down

0 comments on commit 7bb5e62

Please sign in to comment.