Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add periodic updating of NodeInfo for nova #1380

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions api/src/services/nova/nodeInfoService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* eslint-disable import/no-unresolved */
/* eslint-disable @typescript-eslint/no-unsafe-return */
import { Client, InfoResponse, ProtocolParameters } from "@iota/sdk-nova";
import cron from "node-cron";
import { NodeInfoError } from "../../errors/nodeInfoError";
import { ServiceFactory } from "../../factories/serviceFactory";
import logger from "../../logger";
import { INetwork } from "../../models/db/INetwork";

// The cron interval value to update the circulating supply every 10 minutes.
const NODE_INFO_UPDATE_INTERVAL = "*/10 * * * *";
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved

/**
* Class to handle Nova protocol node info.
*/
Expand Down Expand Up @@ -34,6 +39,8 @@ export class NodeInfoService {
this._network = network;
this._nodeInfo = nodeInfo;
this._client = client;

this.setupNodeInfoUpdater();
}

public static async build(network: INetwork): Promise<NodeInfoService> {
Expand All @@ -54,4 +61,30 @@ export class NodeInfoService {
public async getProtocolParameters(): Promise<ProtocolParameters> {
return this._client.getProtocolParameters();
}

private setupNodeInfoUpdater() {
cron.schedule(NODE_INFO_UPDATE_INTERVAL, async () => {
await this.updateNodeInfo();
});
}

private async updateNodeInfo(): Promise<void> {
logger.debug("[NovaNodeInfoService] Updating node info...");
const apiClient = ServiceFactory.get<Client>(`client-${this._network.network}`);

if (apiClient) {
try {
const response = await apiClient.getNodeInfo();

if (response?.info) {
this._nodeInfo = response.info;
logger.verbose("[NovaNodeInfoService] Node info updated successfully");
}
} catch (err) {
throw new NodeInfoError(`Failed to fetch node info for "${this._network.network}" with error:\n${err}`);
}
} else {
logger.warn("[NovaNodeInfoService] Couldn't update node info");
}
}
}
Loading