From 829a7ba754fd8cd02e458ae393747f69036ce82d Mon Sep 17 00:00:00 2001 From: signature18632 Date: Tue, 19 Aug 2025 18:06:52 +0700 Subject: [PATCH] update indexer state --- .../src/service/job.service.ts | 7 ++- packages/shared/src/db/baseIndexer.ts | 49 ++++++++++++++++++- packages/shared/src/types/indexer.ts | 5 ++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/packages/indexer-cache-validator/src/service/job.service.ts b/packages/indexer-cache-validator/src/service/job.service.ts index 773bdbd..3e993cf 100644 --- a/packages/indexer-cache-validator/src/service/job.service.ts +++ b/packages/indexer-cache-validator/src/service/job.service.ts @@ -23,7 +23,12 @@ export const performJob = async (): Promise => { addresses: indexerInfo.map(({ address }) => address), }); const activeIndexers = await processMonitor(indexers); - await indexerInstance.syncIndexerActiveStates(activeIndexers.map((indexer) => indexer.address)); + + const indexerStates = indexerInfo.map((indexer) => ({ + address: indexer.address, + active: activeIndexers.some((activeIndexer) => activeIndexer.address === indexer.address), + })); + await indexerInstance.updateIndexerStates(indexerStates); if (activeIndexers.length === 0) { await cache.del(CACHE_KEYS.BLOCK_BUILDER_INDEXER_LIST); diff --git a/packages/shared/src/db/baseIndexer.ts b/packages/shared/src/db/baseIndexer.ts index 3dbc43c..d9487ee 100644 --- a/packages/shared/src/db/baseIndexer.ts +++ b/packages/shared/src/db/baseIndexer.ts @@ -1,7 +1,7 @@ import type { DocumentReference, Query } from "@google-cloud/firestore"; import { FIRESTORE_COLLECTIONS, FIRESTORE_MAX_BATCH_SIZE } from "../constants"; import { AppError, ErrorCode, logger } from "../lib"; -import type { FirestoreDocumentKey, IndexerFilter, IndexerInfo } from "../types"; +import type { FirestoreDocumentKey, IndexerFilter, IndexerInfo, IndexerStatus } from "../types"; import { db } from "./firestore"; export class BaseIndexer { @@ -88,6 +88,53 @@ export class BaseIndexer { } } + async updateIndexerStates(indexerStates: IndexerStatus[]) { + const indexerCollection = this.indexerDocRef.collection("addresses"); + + try { + await this.db.runTransaction(async (transaction) => { + const addressesToUpdate = indexerStates.map((state) => state.address); + + if (addressesToUpdate.length === 0) { + logger.info("No indexers to update"); + return; + } + + const updatePromises = indexerStates.map(async (state) => { + const docRef = indexerCollection.doc(state.address); + const docSnapshot = await transaction.get(docRef); + + if (docSnapshot.exists) { + transaction.update(docRef, { + active: state.active, + updatedAt: new Date(), + }); + } else { + transaction.set(docRef, { + active: state.active, + address: state.address, + updatedAt: new Date(), + }); + } + }); + + await Promise.all(updatePromises); + + const activeCount = indexerStates.filter((state) => state.active).length; + logger.info( + `Updated ${indexerStates.length} indexers' status. Set ${activeCount} to active.`, + ); + }); + + // this.invalidateCache(); + } catch (error) { + logger.error( + `Failed to update indexer states: ${error instanceof Error ? error.message : error}`, + ); + throw new AppError(500, ErrorCode.INTERNAL_SERVER_ERROR, "Failed to update indexer states"); + } + } + async listIndexers(): Promise { try { // const cachedBuilders = this.getCache(this.key); diff --git a/packages/shared/src/types/indexer.ts b/packages/shared/src/types/indexer.ts index ac7e3cc..2c33aa8 100644 --- a/packages/shared/src/types/indexer.ts +++ b/packages/shared/src/types/indexer.ts @@ -29,3 +29,8 @@ export interface BuilderFeeInfoResponse { registrationCollateralFee: TokenFee[] | null; nonRegistrationCollateralFee: TokenFee[] | null; } + +export interface IndexerStatus { + active: boolean; + address: string; +}