Skip to content

Commit

Permalink
rename getactivavevalidators & use set on proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
Marketen committed Sep 19, 2024
1 parent f162d7d commit 1f4704e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,49 @@ export async function getBlockProposalStatusMap({
epoch: string;
validatorIndexes: string[];
}): Promise<Map<string, BlockProposalStatus>> {

Check failure on line 22 in packages/brain/src/modules/cron/trackValidatorsPerformance/getBlockProposalStatusMap.ts

View workflow job for this annotation

GitHub Actions / build

Delete `⏎`
// 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<string, BlockProposalStatus>();

// 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1f4704e

Please sign in to comment.