From febce070acea57cb662978431f2cbcea14001404 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 10 Aug 2023 14:47:41 -0400 Subject: [PATCH] Revert "Revert "Feature/holo 1083 group multiple crosschain logs"" --- src/commands/indexer/index.ts | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/commands/indexer/index.ts b/src/commands/indexer/index.ts index 086060ef..11627ab2 100644 --- a/src/commands/indexer/index.ts +++ b/src/commands/indexer/index.ts @@ -288,6 +288,64 @@ export default class Indexer extends HealthCheck { this.log('SQS service is reachable') } + /** + * Preprocesses a list of transactions to remove duplicates based on a combination of transaction hash and the bloomId. + * Specifically, it filters out duplicate entries where the bloomId is 'CrossChainMessageSent'. + * + * How it works: + * 1. Iterates over each transaction. + * 2. Creates a unique identifier using the transaction hash and bloomId. + * 3. If the bloomId is 'CrossChainMessageSent' and the identifier is already seen, the transaction is skipped. + * 4. Otherwise, the transaction is processed: it's added to the groupedByTransactionHashAndBloomId dictionary and the updatedInterestingTransactions array. + * 5. The end result is a list of transactions with duplicates (based on the specific criteria) removed. + * + * @param interestingTransactions - The list of transactions to be preprocessed. + * @returns A new list of transactions with duplicates removed based on the described criteria. + */ + preprocessTransactions(interestingTransactions: InterestingTransaction[]): InterestingTransaction[] { + const groupedByTransactionHashAndBloomId: {[hash: string]: {[bloomId: string]: any}} = {} + const seenCombinations = new Set() + const updatedInterestingTransactions: InterestingTransaction[] = [] + + for (const item of interestingTransactions) { + const {hash} = item.transaction + const {bloomId} = item + const identifier = `${hash}-${bloomId}` + + const isCrossChainMessageSent = bloomId === 'CrossChainMessageSent' + + // If the current bloomId is "CrossChainMessageSent" and we've seen this combination before, skip the rest of this iteration. + if (isCrossChainMessageSent && seenCombinations.has(identifier)) { + continue + } + + if (isCrossChainMessageSent) { + seenCombinations.add(identifier) + } + + const {transaction, log, allLogs} = item + + // Initialize or get existing entry + const transactionGroup = + groupedByTransactionHashAndBloomId[hash] || (groupedByTransactionHashAndBloomId[hash] = {}) + const bloomGroup = + transactionGroup[bloomId] || + (transactionGroup[bloomId] = { + transaction, + log, + allLogs: [], + }) + + if (allLogs) { + bloomGroup.allLogs.push(...allLogs) + } + + updatedInterestingTransactions.push(item) + } + + return updatedInterestingTransactions + } + async processTransactions2(job: BlockJob, interestingTransactions: InterestingTransaction[]): Promise { const startTime = performance.now() @@ -295,6 +353,9 @@ export default class Indexer extends HealthCheck { return } + // Filter out duplicate transaction / bloomId combinations (only CrossChainMessageSent is considered for now) + interestingTransactions = this.preprocessTransactions(interestingTransactions) + // Map over the transactions to create an array of Promises const transactionPromises = interestingTransactions.map(interestingTransaction => this.processSingleTransaction(interestingTransaction, job),