Skip to content

Commit

Permalink
refactor: nova api service (#1023)
Browse files Browse the repository at this point in the history
* refactor: nova api service

* fix: nova feed
  • Loading branch information
begonaalvarezd authored Jan 30, 2024
1 parent 70c1550 commit adc2050
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 155 deletions.
37 changes: 26 additions & 11 deletions api/src/initServices.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-unresolved */
import { MqttClient as ChrysalisMqttClient } from "@iota/mqtt.js";
import { IClientOptions, Client as StardustClient } from "@iota/sdk";
import { Client as NovaClient } from "@iota/sdk-nova";
import { IClientOptions as IStardustClientOptions, Client as StardustClient } from "@iota/sdk";
import { IClientOptions as INovaClientOptions, Client as NovaClient } from "@iota/sdk-nova";
import { ServiceFactory } from "./factories/serviceFactory";
import logger from "./logger";
import { IConfiguration } from "./models/configuration/IConfiguration";
Expand All @@ -24,6 +24,7 @@ import { LocalStorageService } from "./services/localStorageService";
import { NetworkService } from "./services/networkService";
import { NovaFeed } from "./services/nova/feed/novaFeed";
import { NodeInfoService as NodeInfoServiceNova } from "./services/nova/nodeInfoService";
import { NovaApiService } from "./services/nova/novaApiService";
import { ChronicleService } from "./services/stardust/chronicleService";
import { StardustFeed } from "./services/stardust/feed/stardustFeed";
import { InfluxDBService } from "./services/stardust/influx/influxDbService";
Expand Down Expand Up @@ -158,12 +159,14 @@ function initChrysalisServices(networkConfig: INetwork): void {
function initStardustServices(networkConfig: INetwork): void {
logger.verbose(`Initializing Stardust services for ${networkConfig.network}`);

const stardustClientParams: IClientOptions = {
const stardustClientParams: IStardustClientOptions = {
primaryNode: networkConfig.provider,
};

if (networkConfig.permaNodeEndpoint) {
stardustClientParams.nodes = [networkConfig.permaNodeEndpoint];
// Client with permanode needs the ignoreNodeHealth as chronicle is considered "not healthy" by the sdk
// Related: https://github.com/iotaledger/inx-chronicle/issues/1302
stardustClientParams.ignoreNodeHealth = true;

const chronicleService = new ChronicleService(networkConfig);
Expand Down Expand Up @@ -206,21 +209,33 @@ function initStardustServices(networkConfig: INetwork): void {
function initNovaServices(networkConfig: INetwork): void {
logger.verbose(`Initializing Nova services for ${networkConfig.network}`);

const novaClientParams: INovaClientOptions = {
primaryNode: networkConfig.provider,
};

if (networkConfig.permaNodeEndpoint) {
novaClientParams.nodes = [networkConfig.permaNodeEndpoint];
// Client with permanode needs the ignoreNodeHealth as chronicle is considered "not healthy" by the sdk
// Related: https://github.com/iotaledger/inx-chronicle/issues/1302
novaClientParams.ignoreNodeHealth = true;

const chronicleService = new ChronicleService(networkConfig);
ServiceFactory.register(`chronicle-${networkConfig.network}`, () => chronicleService);
}

// eslint-disable-next-line no-void
void NovaClient.create({
nodes: [networkConfig.provider],
brokerOptions: { useWs: true },
// Needed only for now in local development (NOT FOR PROD)
ignoreNodeHealth: true,
}).then((novaClient) => {
void NovaClient.create(novaClientParams).then((novaClient) => {
ServiceFactory.register(`client-${networkConfig.network}`, () => novaClient);

const novaApiService = new NovaApiService(networkConfig);
ServiceFactory.register(`api-service-${networkConfig.network}`, () => novaApiService);

// eslint-disable-next-line no-void
void NodeInfoServiceNova.build(networkConfig).then((nodeInfoService) => {
ServiceFactory.register(`node-info-${networkConfig.network}`, () => nodeInfoService);

const feedInstance = new NovaFeed(networkConfig.network);
ServiceFactory.register(`feed-${networkConfig.network}`, () => feedInstance);
const novaFeed = new NovaFeed(networkConfig);
ServiceFactory.register(`feed-${networkConfig.network}`, () => novaFeed);
});
});
}
Expand Down
6 changes: 3 additions & 3 deletions api/src/routes/nova/account/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IAccountResponse } from "../../../models/api/nova/IAccountResponse";
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { NOVA } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { NovaApi } from "../../../services/nova/novaApi";
import { NovaApiService } from "../../../services/nova/novaApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -24,6 +24,6 @@ export async function get(config: IConfiguration, request: IAccountRequest): Pro
if (networkConfig.protocolVersion !== NOVA) {
return {};
}

return NovaApi.accountDetails(networkConfig, request.accountId);
const novaApiService = ServiceFactory.get<NovaApiService>(`api-service-${networkConfig.network}`);
return novaApiService.accountDetails(request.accountId);
}
5 changes: 3 additions & 2 deletions api/src/routes/nova/block/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IBlockRequest } from "../../../models/api/stardust/IBlockRequest";
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { NOVA } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { NovaApi } from "../../../services/nova/novaApi";
import { NovaApiService } from "../../../services/nova/novaApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(_: IConfiguration, request: IBlockRequest): Promise<IB
return {};
}

return NovaApi.block(networkConfig, request.blockId);
const novaApiService = ServiceFactory.get<NovaApiService>(`api-service-${networkConfig.network}`);
return novaApiService.block(request.blockId);
}
5 changes: 3 additions & 2 deletions api/src/routes/nova/block/metadata/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IBlockRequest } from "../../../../models/api/stardust/IBlockRequest";
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { NOVA } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { NovaApi } from "../../../../services/nova/novaApi";
import { NovaApiService } from "../../../../services/nova/novaApiService";
import { ValidationHelper } from "../../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(_: IConfiguration, request: IBlockRequest): Promise<IB
return {};
}

return NovaApi.blockDetails(networkConfig, request.blockId);
const novaApiService = ServiceFactory.get<NovaApiService>(`api-service-${networkConfig.network}`);
return novaApiService.blockDetails(request.blockId);
}
5 changes: 3 additions & 2 deletions api/src/routes/nova/output/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IOutputDetailsResponse } from "../../../models/api/nova/IOutputDetailsR
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { NOVA } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { NovaApi } from "../../../services/nova/novaApi";
import { NovaApiService } from "../../../services/nova/novaApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(config: IConfiguration, request: IOutputDetailsRequest
return {};
}

return NovaApi.outputDetails(networkConfig, request.outputId);
const novaApiService = ServiceFactory.get<NovaApiService>(`api-service-${networkConfig.network}`);
return novaApiService.outputDetails(request.outputId);
}
33 changes: 20 additions & 13 deletions api/src/services/nova/feed/novaFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClassConstructor, plainToInstance } from "class-transformer";
import { ServiceFactory } from "../../../factories/serviceFactory";
import logger from "../../../logger";
import { IFeedUpdate } from "../../../models/api/nova/feed/IFeedUpdate";
import { INetwork } from "../../../models/db/INetwork";
import { NodeInfoService } from "../nodeInfoService";

/**
Expand All @@ -22,29 +23,35 @@ export class NovaFeed {
/**
* Mqtt service for data (upstream).
*/
private readonly _mqttClient: Client;
private _mqttClient: Client;

/**
* The network in context.
*/
private readonly network: string;
private readonly networkId: string;

/**
* Creates a new instance of NovaFeed.
* @param networkId The network id.
* @param network The network config.
*/
constructor(networkId: string) {
constructor(network: INetwork) {
logger.debug("[NovaFeed] Constructing a Nova Feed");
this.blockSubscribers = {};
this.network = networkId;
this._mqttClient = ServiceFactory.get<Client>(`client-${networkId}`);
const nodeInfoService = ServiceFactory.get<NodeInfoService>(`node-info-${networkId}`);
this.networkId = network.network;

if (this._mqttClient && nodeInfoService) {
this.connect();
} else {
throw new Error(`Failed to build novaFeed instance for ${networkId}`);
}
// eslint-disable-next-line no-void
void Client.create({
nodes: [network.provider],
brokerOptions: { useWs: true },
}).then((_mqttClient) => {
this._mqttClient = _mqttClient;
const nodeInfoService = ServiceFactory.get<NodeInfoService>(`node-info-${this.networkId}`);
if (this._mqttClient && nodeInfoService) {
this.connect();
} else {
throw new Error(`Failed to build novaFeed instance for ${this.networkId}`);
}
});
}

/**
Expand All @@ -61,7 +68,7 @@ export class NovaFeed {
* @param subscriptionId The id to unsubscribe.
*/
public unsubscribeBlocks(subscriptionId: string): void {
logger.debug(`[NovaFeed] Removing subscriber ${subscriptionId} from blocks (${this.network})`);
logger.debug(`[NovaFeed] Removing subscriber ${subscriptionId} from blocks (${this.networkId})`);
delete this.blockSubscribers[subscriptionId];
}

Expand Down
122 changes: 0 additions & 122 deletions api/src/services/nova/novaApi.ts

This file was deleted.

Loading

0 comments on commit adc2050

Please sign in to comment.