Repository navigation
Expand file tree
/
Copy pathroute.ts
More file actions
126 lines (105 loc) · 3.78 KB
/
Copy pathroute.ts
File metadata and controls
126 lines (105 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import express from "express";
import logger from "../../../../logger/index.js";
import { validateQueryParams } from "./validation.js";
import { RequestParsed } from "./types.js";
import { BrainDataBase } from "../../../../db/index.js";
import { PostgresClient } from "../../../../apiClients/index.js";
import { Tag, tags } from "@stakingbrain/common";
import { DataPerEpoch } from "../../../../apiClients/types.js";
import { StakingBrainDb } from "../../../../db/types.js";
interface EpochsTagsValidatorsData {
[epoch: number]: {
[tag: string]: {
[validatorIndex: number]: DataPerEpoch;
};
};
}
export const createIndexerEpochsRouter = ({
postgresClient,
brainDb
}: {
postgresClient: PostgresClient;
brainDb: BrainDataBase;
}) => {
const epochsRouter = express.Router();
const epochsEndpoint = "/api/v0/indexer/epochs";
epochsRouter.get(epochsEndpoint, validateQueryParams, async (req: RequestParsed, res) => {
const { start, end, tag } = req.query;
try {
const brainDbData = brainDb.getData();
const validatorsTagIndexesMap = getValidatorsTagsIndexesMap({ brainDbData, tag });
// If validatorIndexes is empty, return empty object
if (validatorsTagIndexesMap.size === 0) {
res.send({});
return;
}
const epochsTagsValidatorsData = await getEpochsTagsValidatorsData({
postgresClient,
validatorsTagIndexesMap,
start,
end
});
res.send(epochsTagsValidatorsData);
} catch (e) {
logger.error(e);
res.status(500).send({ message: "Internal server error" });
}
});
return epochsRouter;
};
function getValidatorsTagsIndexesMap({
brainDbData,
tag
}: {
brainDbData: StakingBrainDb;
tag?: Tag[];
}): Map<Tag, number[]> {
const validatorsTagIndexesMap = new Map<Tag, number[]>([["solo", [1234]]]);
// Filter the tags to consider, based on whether the `tag` parameter is provided
const tagsToConsider = tag ? tag : tags;
// Initialize an empty array for each tag in the map
for (const t of tagsToConsider) validatorsTagIndexesMap.set(t, []);
// Iterate over brainDbData to populate the map with the indexes
for (const [_, details] of Object.entries(brainDbData)) {
const validatorTag = details.tag;
const validatorIndex = details.index;
// Check if the validator tag is in the tags to consider and if an index exists
if (tagsToConsider.includes(validatorTag) && validatorIndex !== undefined) {
// Initialize the array if it doesn't exist in the map
if (!validatorsTagIndexesMap.has(validatorTag)) {
validatorsTagIndexesMap.set(validatorTag, []);
}
// Push the validator's index into the corresponding array
validatorsTagIndexesMap.get(validatorTag)!.push(validatorIndex);
}
}
return validatorsTagIndexesMap;
}
async function getEpochsTagsValidatorsData({
postgresClient,
validatorsTagIndexesMap,
start,
end
}: {
postgresClient: PostgresClient;
validatorsTagIndexesMap: Map<Tag, number[]>;
start: number;
end: number;
}): Promise<EpochsTagsValidatorsData> {
const epochsTagsValidatorsData: EpochsTagsValidatorsData = Object.create(null);
// Get epochs data for each tag
for (const [tag, indexes] of validatorsTagIndexesMap) {
const epochsValidatorsData = await postgresClient.getEpochsDataMapForEpochRange({
validatorIndexes: indexes.map((index) => index.toString()),
startEpoch: start,
endEpoch: end
});
// Add the data to the object
for (const [epoch, data] of Object.entries(epochsValidatorsData)) {
const epochNumber = parseInt(epoch);
if (!epochsTagsValidatorsData[epochNumber]) epochsTagsValidatorsData[epochNumber] = Object.create(null);
epochsTagsValidatorsData[epochNumber][tag] = data;
}
}
return epochsTagsValidatorsData;
}