From 9f8c12a164ee47c6967a40a5f5bf3530ebf325a1 Mon Sep 17 00:00:00 2001
From: Mavrik <mrricki.m.usmc@gmail.com>
Date: Wed, 24 Apr 2024 13:40:03 +0200
Subject: [PATCH] fix: fetch val info for indices and pubkeys from db instead
 of node

---
 backend/src/validator/validator.module.ts  |  9 +++-
 backend/src/validator/validator.service.ts | 49 ++++------------------
 2 files changed, 15 insertions(+), 43 deletions(-)

diff --git a/backend/src/validator/validator.module.ts b/backend/src/validator/validator.module.ts
index 589ab2c8..e627028b 100644
--- a/backend/src/validator/validator.module.ts
+++ b/backend/src/validator/validator.module.ts
@@ -2,10 +2,15 @@ import { Module } from '@nestjs/common';
 import { ValidatorController } from './validator.controller';
 import { ValidatorService } from './validator.service';
 import { UtilsModule } from '../utils/utils.module';
+import { ValidatorDetailService } from './validator-detail.service';
+import { SequelizeModule } from '@nestjs/sequelize';
+import { Validator } from './entities/validator.entity';
+import { SpecsService } from '../specs/specs.service';
+import { Spec } from '../specs/entities/spec.entity';
 
 @Module({
-  imports: [UtilsModule],
+  imports: [UtilsModule, SequelizeModule.forFeature([Validator, Spec])],
   controllers: [ValidatorController],
-  providers: [ValidatorService],
+  providers: [ValidatorService, ValidatorDetailService, SpecsService],
 })
 export class ValidatorModule {}
diff --git a/backend/src/validator/validator.service.ts b/backend/src/validator/validator.service.ts
index 7fa49aa0..c18b879e 100644
--- a/backend/src/validator/validator.service.ts
+++ b/backend/src/validator/validator.service.ts
@@ -3,14 +3,14 @@ import { throwServerError } from '../utilities';
 import { UtilsService } from '../utils/utils.service';
 import {
   BeaconValidatorResult,
-  LighthouseValidatorResult,
 } from '../../../src/types/validator';
 import formatDefaultValName from '../../../utilities/formatDefaultValName';
 import { formatUnits } from 'ethers/lib/utils';
+import { ValidatorDetailService } from './validator-detail.service';
 
 @Injectable()
 export class ValidatorService {
-  constructor(private utilsService: UtilsService) {}
+  constructor(private utilsService: UtilsService, private validatorDetailService: ValidatorDetailService) {}
   private validatorUrl = process.env.VALIDATOR_URL;
   private apiToken = process.env.API_TOKEN;
   private beaconUrl = process.env.BEACON_URL;
@@ -46,15 +46,9 @@ export class ValidatorService {
 
   async fetchValidatorStates() {
     try {
-      const { data } = await this.utilsService.sendHttpRequest({
-        url: `${this.validatorUrl}/lighthouse/validators`,
-        config: this.config,
-      });
-      const validatorKeys = data.data
-        .map((validator: LighthouseValidatorResult) => validator.voting_pubkey)
-        .join(',');
+      const validatorDetails = await this.validatorDetailService.findOrFetch();
       const { data: states } = await this.utilsService.sendHttpRequest({
-        url: `${this.beaconUrl}/eth/v1/beacon/states/head/validators?id=${validatorKeys}`,
+        url: `${this.beaconUrl}/eth/v1/beacon/states/head/validators?id=${validatorDetails.map(({pubkey}) => pubkey)}`,
       });
 
       const sortedStates = [...states.data].sort(
@@ -87,25 +81,10 @@ export class ValidatorService {
 
   async fetchValidatorCaches() {
     try {
-      const { data } = await this.utilsService.sendHttpRequest({
-        url: `${this.validatorUrl}/lighthouse/validators`,
-        config: this.config,
-      });
-      const validatorKeys = data.data
-        .map((validator: LighthouseValidatorResult) => validator.voting_pubkey)
-        .join(',');
-
-      // console.log(validatorKeys, 'keys')
-
-      const { data: states } = await this.utilsService.sendHttpRequest({
-        url: `${this.beaconUrl}/eth/v1/beacon/states/head/validators?id=${validatorKeys}`,
-      });
-
-      // console.log(states, 'states')
-
+      const validatorDetails = await this.validatorDetailService.findOrFetch();
       const requestData = {
         data: JSON.stringify({
-          indices: states.data.map(({ index }) => String(index)),
+          indices: validatorDetails.map(({index}) => String(index)),
         }),
         headers: {
           'Content-Type': 'application/json',
@@ -118,8 +97,6 @@ export class ValidatorService {
         config: requestData,
       });
 
-      // console.log(caches, 'caches')
-
       return Object.fromEntries(
         Object.entries(
           caches.data.validators as Record<
@@ -136,20 +113,10 @@ export class ValidatorService {
 
   async fetchMetrics() {
     try {
-      const { data } = await this.utilsService.sendHttpRequest({
-        url: `${this.validatorUrl}/lighthouse/validators`,
-        config: this.config,
-      });
-      const validatorKeys = data.data
-        .map((validator: LighthouseValidatorResult) => validator.voting_pubkey)
-        .join(',');
-      const { data: states } = await this.utilsService.sendHttpRequest({
-        url: `${this.beaconUrl}/eth/v1/beacon/states/head/validators?id=${validatorKeys}`,
-      });
-
+      const validatorDetails = await this.validatorDetailService.findOrFetch();
       const requestData = {
         data: JSON.stringify({
-          indices: states.data.map(({ index }) => Number(index)),
+          indices: validatorDetails.map(({index}) => index),
         }),
         headers: {
           'Content-Type': 'application/json',