Skip to content

Commit

Permalink
fix: add spec service to beacon service and fetch data from db instea…
Browse files Browse the repository at this point in the history
…d of node when available
  • Loading branch information
rickimoore committed Apr 24, 2024
1 parent fe45ef6 commit 5b8d08c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 43 deletions.
8 changes: 6 additions & 2 deletions backend/src/beacon/beacon.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Controller, Get } from '@nestjs/common';
import { BeaconService } from './beacon.service';
import { SpecsService } from '../specs/specs.service';

@Controller('beacon')
export class BeaconController {
constructor(private beaconService: BeaconService) {}
constructor(
private beaconService: BeaconService,
private readonly specsService: SpecsService
) {}

@Get('spec')
async getBeaconSpec() {
return this.beaconService.fetchBeaconSpec();
return this.specsService.findOrFetch();
}

@Get('node-version')
Expand Down
7 changes: 5 additions & 2 deletions backend/src/beacon/beacon.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { Module } from '@nestjs/common';
import { BeaconService } from './beacon.service';
import { BeaconController } from './beacon.controller';
import { UtilsModule } from '../utils/utils.module';
import { SpecsService } from '../specs/specs.service';
import { SequelizeModule } from '@nestjs/sequelize';
import { Spec } from '../specs/entities/spec.entity';

@Module({
imports: [UtilsModule],
imports: [UtilsModule, SequelizeModule.forFeature([Spec])],
controllers: [BeaconController],
providers: [BeaconService],
providers: [BeaconService, SpecsService],
})
export class BeaconModule {}
54 changes: 15 additions & 39 deletions backend/src/beacon/beacon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@ import { UtilsService } from '../utils/utils.service';
import { throwServerError } from '../utilities';
import getPercentage from '../../../utilities/getPercentage';
import getStatus from '../../../utilities/getInclusionRateStatus';
import { SpecsService } from '../specs/specs.service';

@Injectable()
export class BeaconService {
constructor(private utilsService: UtilsService) {}
constructor(private utilsService: UtilsService, private readonly specsService: SpecsService) {}
private beaconUrl = process.env.BEACON_URL;

async fetchBeaconSpec() {
try {
const { data } = await this.utilsService.sendHttpRequest({
url: `${this.beaconUrl}/eth/v1/config/spec`,
});
return data.data;
} catch (e) {
console.error(e);
throwServerError('Unable to fetch beacon spec');
}
}

async fetchBeaconNodeVersion() {
try {
const { data } = await this.utilsService.sendHttpRequest({
Expand All @@ -47,11 +36,9 @@ export class BeaconService {

async fetchSyncData() {
try {
const [beaconSpec, beaconResponse, executionResponse] = await Promise.all(
const { SECONDS_PER_SLOT } = await this.specsService.findOrFetch()
const [ beaconResponse, executionResponse] = await Promise.all(
[
this.utilsService.sendHttpRequest({
url: `${this.beaconUrl}/eth/v1/config/spec`,
}),
this.utilsService.sendHttpRequest({
url: `${this.beaconUrl}/eth/v1/node/syncing`,
}),
Expand All @@ -61,7 +48,6 @@ export class BeaconService {
],
);

const { SECONDS_PER_SLOT } = beaconSpec.data.data;
const { head_slot, sync_distance, is_syncing } = beaconResponse.data.data;
const {
head_block_number,
Expand Down Expand Up @@ -101,16 +87,12 @@ export class BeaconService {

async fetchInclusionRate() {
try {
const [beaconSpec, beaconResponse] = await Promise.all([
this.utilsService.sendHttpRequest({
url: `${this.beaconUrl}/eth/v1/config/spec`,
}),
this.utilsService.sendHttpRequest({
url: `${this.beaconUrl}/eth/v1/node/syncing`,
}),
]);

const { SLOTS_PER_EPOCH } = beaconSpec.data.data;
const { SLOTS_PER_EPOCH } = await this.specsService.findOrFetch()

const beaconResponse = await this.utilsService.sendHttpRequest({
url: `${this.beaconUrl}/eth/v1/node/syncing`,
});

const { head_slot } = beaconResponse.data.data;

const epoch = Math.floor(Number(head_slot) / Number(SLOTS_PER_EPOCH)) - 1;
Expand Down Expand Up @@ -167,18 +149,12 @@ export class BeaconService {

async fetchProposerDuties() {
try {
const [beaconSpec, beaconResponse] = await Promise.all(
[
this.utilsService.sendHttpRequest({
url: `${this.beaconUrl}/eth/v1/config/spec`,
}),
this.utilsService.sendHttpRequest({
url: `${this.beaconUrl}/eth/v1/node/syncing`,
})
],
);
const { SLOTS_PER_EPOCH } = await this.specsService.findOrFetch()

const beaconResponse = await this.utilsService.sendHttpRequest({
url: `${this.beaconUrl}/eth/v1/node/syncing`,
})

const { SLOTS_PER_EPOCH } = beaconSpec.data.data;
const { head_slot } = beaconResponse.data.data;
const closestEpoch = Math.floor(Number(head_slot) / Number(SLOTS_PER_EPOCH)) + 1

Expand Down

0 comments on commit 5b8d08c

Please sign in to comment.