Skip to content

Commit

Permalink
fix: fetch val info for indices and pubkeys from db instead of node
Browse files Browse the repository at this point in the history
  • Loading branch information
rickimoore committed Apr 24, 2024
1 parent e830864 commit 9f8c12a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 43 deletions.
9 changes: 7 additions & 2 deletions backend/src/validator/validator.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
49 changes: 8 additions & 41 deletions backend/src/validator/validator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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',
Expand All @@ -118,8 +97,6 @@ export class ValidatorService {
config: requestData,
});

// console.log(caches, 'caches')

return Object.fromEntries(
Object.entries(
caches.data.validators as Record<
Expand All @@ -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',
Expand Down

0 comments on commit 9f8c12a

Please sign in to comment.