Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export const performJob = async (): Promise<void> => {
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);
Expand Down
49 changes: 48 additions & 1 deletion packages/shared/src/db/baseIndexer.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<IndexerInfo[]> {
try {
// const cachedBuilders = this.getCache<IndexerInfo[]>(this.key);
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/types/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ export interface BuilderFeeInfoResponse {
registrationCollateralFee: TokenFee[] | null;
nonRegistrationCollateralFee: TokenFee[] | null;
}

export interface IndexerStatus {
active: boolean;
address: string;
}