diff --git a/packages/brain/src/modules/cron/trackValidatorsPerformance/getActiveValidators.ts b/packages/brain/src/modules/cron/trackValidatorsPerformance/getActiveValidatorsLoadedInBrain.ts similarity index 97% rename from packages/brain/src/modules/cron/trackValidatorsPerformance/getActiveValidators.ts rename to packages/brain/src/modules/cron/trackValidatorsPerformance/getActiveValidatorsLoadedInBrain.ts index 8b7692a0..ac056c40 100644 --- a/packages/brain/src/modules/cron/trackValidatorsPerformance/getActiveValidators.ts +++ b/packages/brain/src/modules/cron/trackValidatorsPerformance/getActiveValidatorsLoadedInBrain.ts @@ -14,7 +14,7 @@ import { logPrefix } from "./logPrefix.js"; * @param {BrainDataBase} brainDb - Brain DB client. * @returns {string[]} - Array of active validator indexes. */ -export async function getActiveValidators({ +export async function getActiveValidatorsLoadedInBrain({ beaconchainApi, brainDb }: { diff --git a/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts b/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts index aa8c60c8..ad3c8ae7 100644 --- a/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts +++ b/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts @@ -19,41 +19,48 @@ export async function getBlockProposalStatusMap({ epoch: string; validatorIndexes: string[]; }): Promise> { + // Get the block proposal duties for the given epoch. Which validators + // are supposed to propose a block in which slot? const blockProposalsResponse = await beaconchainApi.getProposerDuties({ epoch }); + // Utilize a Set for quick lookup. We assume that the validator indexes are unique. + const validatorIndexesSet = new Set(validatorIndexes); + // Map to store the block proposal status of each validator const validatorBlockStatus = new Map(); // Initialize all validator's status to Unchosen. - validatorIndexes.forEach((validatorIndex) => { + validatorIndexesSet.forEach((validatorIndex) => { validatorBlockStatus.set(validatorIndex, BlockProposalStatus.Unchosen); }); - // Since we know block proposal duties, we can check if the validator proposed the block or missed it. + // For each slot in the epoch, determine if the validator supposed to propose + // it is one of our monitored validators. If so, check if the validator did it correctly. for (const duty of blockProposalsResponse.data) { const { validator_index, slot } = duty; - if (validatorIndexes.includes(validator_index)) { + // enter loop if one of our monitored validators had to propose in this slot + if (validatorIndexesSet.has(validator_index)) { try { + // Get the block header for the slot. It has the proposer index. const blockHeader = await beaconchainApi.getBlockHeader({ blockId: slot }); - // Update status based on whether the validator in the block header matches the one supposed to propose - // If the duty had a proposer index and it doesn't match with the header proposer index, we did something wrong, so we consider it as an error + // If the proposer index in the block header matches the validator index, the block was proposed correctly. validatorBlockStatus.set( validator_index, - blockHeader.data.header.message.proposer_index == validator_index + blockHeader.data.header.message.proposer_index === validator_index ? BlockProposalStatus.Proposed : BlockProposalStatus.Error ); } catch (error) { if (error.status === 404) { - // Consensus clients return 404 a block was missed (there is no block for the slot) + // If the block header is not found, the validator missed the block proposal validatorBlockStatus.set(validator_index, BlockProposalStatus.Missed); } else { // If consensus client doesnt return 200 or 404, something went wrong logger.error(`${logPrefix}Error retrieving block header for slot ${slot}: ${error}`); - validatorBlockStatus.set(validator_index, BlockProposalStatus.Error); // Something went wrong + validatorBlockStatus.set(validator_index, BlockProposalStatus.Error); } } } diff --git a/packages/brain/src/modules/cron/trackValidatorsPerformance/index.ts b/packages/brain/src/modules/cron/trackValidatorsPerformance/index.ts index d9a6e05f..8c5435e8 100644 --- a/packages/brain/src/modules/cron/trackValidatorsPerformance/index.ts +++ b/packages/brain/src/modules/cron/trackValidatorsPerformance/index.ts @@ -6,7 +6,7 @@ import { insertPerformanceData } from "./insertPerformanceData.js"; import { getAttestationsTotalRewards } from "./getAttestationsTotalRewards.js"; import { getBlockProposalStatusMap } from "./getBlockProposalStatusMap.js"; import { checkNodeHealth } from "./checkNodeHealth.js"; -import { getActiveValidators } from "./getActiveValidators.js"; +import { getActiveValidatorsLoadedInBrain } from "./getActiveValidatorsLoadedInBrain.js"; import { logPrefix } from "./logPrefix.js"; const MINUTE_IN_SECONDS = 60; @@ -49,7 +49,7 @@ export async function trackValidatorsPerformance({ logger.debug(`${logPrefix}Epoch finalized: ${epochFinalized}`); // active validators indexes - const activeValidatorsIndexes = await getActiveValidators({ beaconchainApi, brainDb }); + const activeValidatorsIndexes = await getActiveValidatorsLoadedInBrain({ beaconchainApi, brainDb }); if (activeValidatorsIndexes.length === 0) { logger.info(`${logPrefix}No active validators found`); return;