Skip to content

Commit

Permalink
Start cron at the beggining pf the epoch (#331)
Browse files Browse the repository at this point in the history
* start cron at the beggining of the epoch

* add comment
  • Loading branch information
pablomendezroyo authored Sep 17, 2024
1 parent a3d31af commit 7242c89
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
21 changes: 18 additions & 3 deletions packages/brain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import { startUiServer, startLaunchpadApi } from "./modules/apiServers/index.js"
import * as dotenv from "dotenv";
import process from "node:process";
import { params } from "./params.js";
import { CronJob, reloadValidators, trackValidatorsPerformance, sendProofsOfValidation } from "./modules/cron/index.js";
import {
CronJob,
reloadValidators,
trackValidatorsPerformance,
sendProofsOfValidation,
getSecondsToNextEpoch
} from "./modules/cron/index.js";
import { PostgresClient } from "./modules/apiClients/index.js";
import { brainConfig } from "./modules/config/index.js";

Expand Down Expand Up @@ -96,12 +102,21 @@ const proofOfValidationCron = new CronJob(shareCronInterval, () =>
sendProofsOfValidation(signerApi, brainDb, dappnodeSignatureVerifierApi, shareDataWithDappnode)
);
proofOfValidationCron.start();
// TODO: start cron within the first minute of the epoch
const trackValidatorsPerformanceCron = new CronJob(slotsPerEpoch * secondsPerSlot * 1000, () =>
// once every epoch
trackValidatorsPerformance({ brainDb, postgresClient, beaconchainApi, minGenesisTime, secondsPerSlot })
);
trackValidatorsPerformanceCron.start();
const secondsToNextEpoch = getSecondsToNextEpoch({ minGenesisTime, secondsPerSlot });
// start the cron within the first minute of an epoch
// If it remains more than 1 minute + 10 seconds of margin then wait for the next epoch, so wait the whole secondsToNextEpoch
if (secondsToNextEpoch > 60)
setTimeout(
() => {
trackValidatorsPerformanceCron.start();
},
(secondsToNextEpoch + 10) * 1000
);
else trackValidatorsPerformanceCron.start();

// Graceful shutdown
function handle(signal: string): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/brain/src/modules/cron/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { CronJob } from "./cron.js";
export { reloadValidators } from "./reloadValidators.js";
export { sendProofsOfValidation } from "./sendProofsOfValidation.js";
export { trackValidatorsPerformance } from "./trackValidatorsPerformance.js";
export { trackValidatorsPerformance, getSecondsToNextEpoch } from "./trackValidatorsPerformance.js";
10 changes: 4 additions & 6 deletions packages/brain/src/modules/cron/trackValidatorsPerformance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ export async function trackValidatorsPerformance({
logger.error(`${logPrefix}Error occurred: ${error}. Updating epoch finalized and retrying in 1 minute`);
// skip if the seconds to the next epoch is less than 1 minute
const minuteInSeconds = 60;
const currentUnixTime = Math.floor(Date.now() / 1000);
const secondsToNextEpoch = getSecondsToNextEpoch({ currentUnixTime, minGenesisTime, secondsPerSlot });
const secondsToNextEpoch = getSecondsToNextEpoch({ minGenesisTime, secondsPerSlot });
if (secondsToNextEpoch < minuteInSeconds) {
logger.warn(
`${logPrefix}Seconds to the next epoch is less than 1 minute (${secondsToNextEpoch}). Skipping until next epoch`
Expand All @@ -144,19 +143,18 @@ export async function trackValidatorsPerformance({
/**
* Get the seconds to the start of the next epoch based on the current Unix time and the minimum genesis time of the chain.
*
* @param {number} currentUnixTime - Current Unix time in seconds.
* @param {number} minGenesisTime - Minimum genesis time of the chain.
* @param {number} secondsPerSlot - Seconds per slot.
* @returns {number} - Seconds to the start of the next epoch.
*/
function getSecondsToNextEpoch({
currentUnixTime,
export function getSecondsToNextEpoch({
minGenesisTime,
secondsPerSlot
}: {
currentUnixTime: number;
minGenesisTime: number;
secondsPerSlot: number;
}): number {
const currentUnixTime = Math.floor(Date.now() / 1000);
const timeDifference = currentUnixTime - minGenesisTime; // Time difference in seconds
const stlotsSinceGenesis = timeDifference / secondsPerSlot; // Slots since genesis
const currentEpoch = Math.floor(stlotsSinceGenesis / 32); // Current epoch
Expand Down

0 comments on commit 7242c89

Please sign in to comment.