Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename getactivavevalidators & use set on proposals #342

Merged
merged 3 commits into from
Sep 20, 2024
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 @@ -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,48 @@ export async function getBlockProposalStatusMap({
epoch: string;
validatorIndexes: string[];
}): Promise<Map<string, BlockProposalStatus>> {
// 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