Skip to content

Commit

Permalink
return to frontend raw data
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo committed Oct 8, 2024
1 parent c17d78d commit 4d2b896
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 913 deletions.
20 changes: 4 additions & 16 deletions packages/brain/src/calls/fetchValidatorsPerformanceData.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
import { fetchAndProcessValidatorsData } from "../modules/validatorsDataIngest/index.js";
import { minGenesisTime, secondsPerSlot } from "../index.js";
import type {
ValidatorsDataProcessed,
Granularity,
NumberOfDaysToQuery
} from "../modules/validatorsDataIngest/types.js";
import type { EpochsValidatorsMap, NumberOfDaysToQuery } from "../modules/validatorsDataIngest/types.js";
import { PostgresClient } from "../modules/apiClients/index.js";

export async function fetchValidatorsPerformanceData({
postgresClient,
validatorIndexes,
numberOfDaysToQuery,
granularity
numberOfDaysToQuery
}: {
postgresClient: PostgresClient;
validatorIndexes: string[];
numberOfDaysToQuery?: NumberOfDaysToQuery;
granularity?: Granularity;
}): Promise<
Map<
number, // validatorIndex
ValidatorsDataProcessed // processed data of the validator
>
> {
}): Promise<EpochsValidatorsMap> {
return await fetchAndProcessValidatorsData({
validatorIndexes,
postgresClient,
minGenesisTime,
secondsPerSlot,
numberOfDaysToQuery,
granularity
numberOfDaysToQuery
});
}
67 changes: 41 additions & 26 deletions packages/brain/src/modules/apiClients/postgres/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import postgres from "postgres";
import logger from "../../logger/index.js";
import { BlockProposalStatus, Columns, ValidatorPerformance, ValidatorPerformancePostgres } from "./types.js";
import { BlockProposalStatus, Columns, EpochData, ValidatorPerformancePostgres } from "./types.js";
import { ConsensusClient, ExecutionClient } from "@stakingbrain/common";
import { EpochsValidatorsMap, DataPerEpoch, ValidatorsEpochMap } from "../../validatorsDataIngest/types.js";

export class PostgresClient {
private readonly tableName = "validators_performance";
Expand Down Expand Up @@ -111,7 +112,7 @@ CREATE TABLE IF NOT EXISTS ${this.tableName} (
*
* @param data - The performance data to insert.
*/
public async insertPerformanceData(data: ValidatorPerformance): Promise<void> {
public async insertPerformanceData(data: EpochData): Promise<void> {
const query = `
INSERT INTO ${this.tableName} (${Columns.validatorIndex}, ${Columns.epoch}, ${Columns.executionClient}, ${Columns.consensusClient}, ${Columns.slot}, ${Columns.liveness}, ${Columns.blockProposalStatus}, ${Columns.syncCommitteeRewards}, ${Columns.attestationsTotalRewards}, ${Columns.attestationsIdealRewards}, ${Columns.error})
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
Expand Down Expand Up @@ -151,7 +152,7 @@ DO UPDATE SET
* @param validatorIndexes - The indexes of the validators to get the data for.
* @returns The performance data for the given validators.
*/
public async getValidatorsDataFromAllEpochs(validatorIndexes: string[]): Promise<ValidatorPerformance[]> {
public async getValidatorsDataFromAllEpochs(validatorIndexes: string[]): Promise<EpochData[]> {
const query = `
SELECT * FROM ${this.tableName}
WHERE ${Columns.validatorIndex} = ANY($1)
Expand All @@ -174,23 +175,28 @@ WHERE ${Columns.validatorIndex} = ANY($1)
}

/**
* Get the validators data for the given validator indexes and an epoch start and end range. In order to improve data process
* it will return a map with the validator index as key and the performance data as value.
* Get the validators data for the given validator indexes and an epoch start and end range.
* This function will return a nested map where the outer map is indexed by epoch, and
* each entry contains another map indexed by validator index. The inner map contains the performance data
* for each validator at that epoch.
*
* The performance data returned will be organized into attestation, block, and sync committee
* sections to provide a more structured view of the data per epoch.
*
* @param validatorIndexes - The indexes of the validators to get the data for.
* @param startEpoch - The start epoch number.
* @param endEpoch - The end epoch number.
* @returns The performance data for the given validators.
* @returns A nested map with epoch as the key, validator index as the secondary key, and the performance data as value.
*/
public async getValidatorsDataMapForEpochRange({
public async getEpochsDataMapForEpochRange({
validatorIndexes,
startEpoch,
endEpoch
}: {
validatorIndexes: string[];
startEpoch: number;
endEpoch: number;
}): Promise<Map<number, ValidatorPerformance[]>> {
}): Promise<EpochsValidatorsMap> {
const query = `
SELECT * FROM ${this.tableName}
WHERE ${Columns.validatorIndex} = ANY($1)
Expand All @@ -204,28 +210,37 @@ AND ${Columns.epoch} <= $3
endEpoch
])) as ValidatorPerformancePostgres[];

return result.reduce((map: Map<number, ValidatorPerformance[]>, row) => {
const key = row.validator_index;

const performanceData = {
validatorIndex: row.validator_index,
epoch: row.epoch,
executionClient: row.execution_client,
consensusClient: row.consensus_client,
slot: row.slot,
liveness: row.liveness,
blockProposalStatus: row.block_proposal_status,
syncCommitteeRewards: row.sync_comittee_rewards,
attestationsTotalRewards: JSON.parse(row.attestations_total_rewards),
attestationsIdealRewards: JSON.parse(row.attestations_ideal_rewards),
error: JSON.parse(row.error)
return result.reduce((map: EpochsValidatorsMap, row) => {
const epoch = row.epoch;
const validatorIndex = row.validator_index;

// Define the performance data in the new format.
const epochData: DataPerEpoch = {
attestation: {
totalRewards: JSON.parse(row.attestations_total_rewards),
idealRewards: JSON.parse(row.attestations_ideal_rewards)
},
block: {
status: row.block_proposal_status, // Assuming row.block_proposal_status will provide either 'proposed' or 'missed'
slot: row.slot,
graffiti: undefined, // Assuming there's no graffiti info in the existing data
reward: undefined // Assuming there's no reward info in the existing data
},
syncCommittee: {
reward: row.sync_comittee_rewards
},
tag: "solo" // TODO fix this
};

if (map.has(key)) map.get(key)?.push(performanceData);
else map.set(key, [performanceData]);
// If the outer map doesn't have the epoch, add it.
if (!map.has(epoch)) map.set(epoch, new Map<number, DataPerEpoch>());

const validatorsEpochMap = map.get(epoch);
// Add or update the validator data for this epoch.
if (validatorsEpochMap) validatorsEpochMap.set(validatorIndex, epochData);

return map;
}, new Map<number, ValidatorPerformance[]>());
}, new Map<number, ValidatorsEpochMap>());
}

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/brain/src/modules/apiClients/postgres/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export enum BlockProposalStatus {
}

// Interface data return from Postgres client
export interface ValidatorPerformance {
export interface EpochData {
validatorIndex: number;
epoch: number;
executionClient: ExecutionClient;
Expand All @@ -49,10 +49,10 @@ export interface ValidatorPerformance {
slot?: number;
liveness?: boolean;
syncCommitteeRewards?: number;
error?: ValidatorPerformanceError;
error?: EpochError;
}

export enum ValidatorPerformanceErrorCode {
export enum EpochErrorCode {
BEACONCHAIN_API_ERROR = "BEACONCHAIN_API_ERROR",
EXECUTION_OFFLINE = "EXECUTION_OFFLINE",
CONSENSUS_SYNCING = "CONSENSUS_SYNCING",
Expand All @@ -62,7 +62,7 @@ export enum ValidatorPerformanceErrorCode {
UNKNOWN_ERROR = "UNKNOWN_ERROR"
}

export interface ValidatorPerformanceError {
code: ValidatorPerformanceErrorCode;
export interface EpochError {
code: EpochErrorCode;
message: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import { getActiveValidatorsLoadedInBrain } from "./getActiveValidatorsLoadedInB
import { logPrefix } from "./logPrefix.js";
import { ConsensusClient, ExecutionClient } from "@stakingbrain/common";
import { IdealRewards, TotalRewards } from "../../apiClients/types.js";
import {
BlockProposalStatus,
ValidatorPerformanceError,
ValidatorPerformanceErrorCode
} from "../../apiClients/postgres/types.js";
import { BlockProposalStatus, EpochError, EpochErrorCode } from "../../apiClients/postgres/types.js";
import { BeaconchainApiError } from "../../apiClients/beaconchain/error.js";
import { BrainDbError } from "../../db/error.js";
import { ExecutionOfflineError, NodeSyncingError } from "./error.js";
Expand Down Expand Up @@ -43,7 +39,7 @@ export async function fetchAndInsertValidatorsPerformanceData({
}): Promise<void> {
if (currentEpoch === lastProcessedEpoch) return;

let validatorPerformanceError: ValidatorPerformanceError | undefined;
let validatorPerformanceError: EpochError | undefined;
let activeValidatorsIndexes: string[] = [];
let validatorBlockStatusMap: Map<string, BlockProposalStatus> | undefined;
let validatorAttestationsRewards: { totalRewards: TotalRewards[]; idealRewards: IdealRewards } | undefined;
Expand Down Expand Up @@ -110,29 +106,29 @@ async function ensureNodeStatus({ beaconchainApi }: { beaconchainApi: Beaconchai
if (el_offline) throw new ExecutionOfflineError("Execution layer is offline");
}

function getValidatorPerformanceError(e: Error): ValidatorPerformanceError {
function getValidatorPerformanceError(e: Error): EpochError {
if (e instanceof BeaconchainApiError)
return {
code: ValidatorPerformanceErrorCode.BEACONCHAIN_API_ERROR,
code: EpochErrorCode.BEACONCHAIN_API_ERROR,
message: e.message
};
if (e instanceof BrainDbError)
return {
code: ValidatorPerformanceErrorCode.BRAINDDB_ERROR,
code: EpochErrorCode.BRAINDDB_ERROR,
message: e.message
};
if (e instanceof ExecutionOfflineError)
return {
code: ValidatorPerformanceErrorCode.EXECUTION_OFFLINE,
code: EpochErrorCode.EXECUTION_OFFLINE,
message: e.message
};
if (e instanceof NodeSyncingError)
return {
code: ValidatorPerformanceErrorCode.CONSENSUS_SYNCING,
code: EpochErrorCode.CONSENSUS_SYNCING,
message: e.message
};
return {
code: ValidatorPerformanceErrorCode.UNKNOWN_ERROR,
code: EpochErrorCode.UNKNOWN_ERROR,
message: e.message
};
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { ConsensusClient, ExecutionClient } from "@stakingbrain/common";
import { PostgresClient } from "../../apiClients/index.js";
import {
BlockProposalStatus,
ValidatorPerformance,
ValidatorPerformanceError,
ValidatorPerformanceErrorCode
} from "../../apiClients/postgres/types.js";
import { BlockProposalStatus, EpochData, EpochError, EpochErrorCode } from "../../apiClients/postgres/types.js";
import { IdealRewards, TotalRewards } from "../../apiClients/types.js";
import logger from "../../logger/index.js";
import { logPrefix } from "./logPrefix.js";
Expand All @@ -32,7 +27,7 @@ export async function insertPerformanceData({
activeValidatorsIndexes: string[];
validatorBlockStatusMap?: Map<string, BlockProposalStatus>;
validatorAttestationsRewards?: { totalRewards: TotalRewards[]; idealRewards: IdealRewards };
error?: ValidatorPerformanceError;
error?: EpochError;
}): Promise<void> {
for (const validatorIndex of activeValidatorsIndexes) {
if (error) {
Expand Down Expand Up @@ -61,7 +56,7 @@ export async function insertPerformanceData({
executionClient,
consensusClient,
error: {
code: ValidatorPerformanceErrorCode.MISSING_ATT_DATA,
code: EpochErrorCode.MISSING_ATT_DATA,
message: `Missing attestation data for validator ${validatorIndex}`
}
}
Expand All @@ -79,7 +74,7 @@ export async function insertPerformanceData({
executionClient,
consensusClient,
error: {
code: ValidatorPerformanceErrorCode.MISSING_BLOCK_DATA,
code: EpochErrorCode.MISSING_BLOCK_DATA,
message: `Missing block proposal data for validator ${validatorIndex}`
}
}
Expand Down Expand Up @@ -110,7 +105,7 @@ async function insertDataNotThrow({
validatorPerformance
}: {
postgresClient: PostgresClient;
validatorPerformance: ValidatorPerformance;
validatorPerformance: EpochData;
}): Promise<void> {
try {
logger.debug(`${logPrefix}Inserting data for validator ${validatorPerformance.validatorIndex}`);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4d2b896

Please sign in to comment.