From 1f4704ef0008782af4b8feef7496efc91648817b Mon Sep 17 00:00:00 2001 From: Marketen Date: Thu, 19 Sep 2024 15:14:36 +0000 Subject: [PATCH 1/3] rename getactivavevalidators & use set on proposals --- ...ts => getActiveValidatorsLoadedInBrain.ts} | 2 +- .../getBlockProposalStatusMap.ts | 24 ++++++++++++------- .../cron/trackValidatorsPerformance/index.ts | 4 ++-- 3 files changed, 19 insertions(+), 11 deletions(-) rename packages/brain/src/modules/cron/trackValidatorsPerformance/{getActiveValidators.ts => getActiveValidatorsLoadedInBrain.ts} (97%) 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..00e74499 100644 --- a/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts +++ b/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts @@ -19,41 +19,49 @@ 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; From 89d4164ee751b5721911a87a9692acbea3c53ef5 Mon Sep 17 00:00:00 2001 From: Marketen Date: Thu, 19 Sep 2024 15:20:41 +0000 Subject: [PATCH 2/3] fix lint --- .../trackValidatorsPerformance/getBlockProposalStatusMap.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts b/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts index 00e74499..c6da0ae1 100644 --- a/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts +++ b/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts @@ -19,7 +19,6 @@ 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({ @@ -68,4 +67,4 @@ export async function getBlockProposalStatusMap({ } return validatorBlockStatus; -} +} \ No newline at end of file From b346a0c229985e291832ab94a5dc42318663068d Mon Sep 17 00:00:00 2001 From: Marketen Date: Thu, 19 Sep 2024 15:22:45 +0000 Subject: [PATCH 3/3] fix lint --- .../trackValidatorsPerformance/getBlockProposalStatusMap.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts b/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts index c6da0ae1..ad3c8ae7 100644 --- a/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts +++ b/packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts @@ -67,4 +67,4 @@ export async function getBlockProposalStatusMap({ } return validatorBlockStatus; -} \ No newline at end of file +}