From c3073b2331442198034ecc556772e5353ed50393 Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Thu, 7 Oct 2021 23:32:02 +0800 Subject: [PATCH 1/9] refactor: add clearTimeout in get request --- src/services/Http.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/services/Http.ts b/src/services/Http.ts index c4d68a8..d646cb3 100644 --- a/src/services/Http.ts +++ b/src/services/Http.ts @@ -1,17 +1,25 @@ import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'; -const REQEST_TIMEOUT = 10000; +import { monitor } from '@src/config'; + +const REQEST_TIMEOUT = monitor.REQUEST_TIMEOUT; export class HTTP { static get(url: string, config?: AxiosRequestConfig | undefined): Promise> { return new Promise>((resolve, reject) => { - setTimeout(() => { + const timeout = setTimeout(() => { reject(Error(`HTTP get request failed. Timeout error`)); }, REQEST_TIMEOUT + REQEST_TIMEOUT * 0.1); axios .get(url, { timeout: REQEST_TIMEOUT, ...config }) - .then(resolve) - .catch(reject); + .then((response) => { + clearTimeout(timeout); + resolve(response); + }) + .catch((e) => { + clearTimeout(timeout); + reject(e); + }); }); } } From 6562aa4ac05d104306286f4208da324e53bf4ad1 Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Thu, 7 Oct 2021 23:56:12 +0800 Subject: [PATCH 2/9] style: remove one line code --- src/services/NodeMonitor.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/services/NodeMonitor.ts b/src/services/NodeMonitor.ts index 5cf7085..ddec76f 100644 --- a/src/services/NodeMonitor.ts +++ b/src/services/NodeMonitor.ts @@ -90,7 +90,9 @@ export class NodeMonitor { // Nested fetch node list from current nodeList[] const nodeListPromises = this.nodeList.map(async (node) => { - if (isAPIRole(node.roles)) return this.fetchNodesByURL(getNodeURL(node, monitor.API_NODE_PORT)); + if (isAPIRole(node.roles)) { + this.fetchNodesByURL(getNodeURL(node, monitor.API_NODE_PORT)); + } return []; }); @@ -160,13 +162,17 @@ export class NodeMonitor { if (hostDetail) nodeWithInfo.hostDetail = hostDetail; - if (isPeerRole(node.roles)) nodeWithInfo.peerStatus = await PeerNodeService.getStatus(node.host, node.port); + if (isPeerRole(node.roles)) { + nodeWithInfo.peerStatus = await PeerNodeService.getStatus(node.host, node.port); + } if (isAPIRole(node.roles)) { - nodeWithInfo.apiStatus = await ApiNodeService.getStatus(node.host, monitor.API_NODE_PORT); + nodeWithInfo.apiStatus = await ApiNodeService.getStatus(node.host); } - if (nodeWithInfo.publicKey) nodeWithInfo.rewardPrograms = await NodeRewards.getNodeRewardPrograms(nodeWithInfo.publicKey); + if (nodeWithInfo.publicKey) { + nodeWithInfo.rewardPrograms = await NodeRewards.getNodeRewardPrograms(nodeWithInfo.publicKey); + } } catch (e) { logger.error(`GetNodeInfo. Failed to fetch info for "${node}". ${e.message}`); } From faab5edc89fc9768e331ddc11cfb1dc757ff31dc Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Thu, 7 Oct 2021 23:59:27 +0800 Subject: [PATCH 3/9] feat: add https checker --- src/models/Node.ts | 4 ++++ src/services/ApiNodeService.ts | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/models/Node.ts b/src/models/Node.ts index 4e3e523..3c457fb 100644 --- a/src/models/Node.ts +++ b/src/models/Node.ts @@ -58,6 +58,10 @@ const NodeSchema: Schema = new Schema({ type: Boolean, required: false, }, + isHttpsEnabled: { + type: Boolean, + required: false, + }, chainHeight: { type: Number, required: false, diff --git a/src/services/ApiNodeService.ts b/src/services/ApiNodeService.ts index d9c4c52..5f71623 100644 --- a/src/services/ApiNodeService.ts +++ b/src/services/ApiNodeService.ts @@ -7,6 +7,7 @@ const logger: winston.Logger = Logger.getLogger(basename(__filename)); export interface ApiStatus { isAvailable: boolean; + isHttpsEnabled?: boolean; chainHeight?: number; finalizationHeight?: number; nodePublicKey?: string; @@ -78,4 +79,13 @@ export class ApiNodeService { return null; } }; + + static isHttpsEnabled = async (host: string, port = 3001): Promise => { + try { + await HTTP.get(`https://${host}:${port}/chain/info`); + return true; + } catch (e) { + return false; + } + }; } From 0d3072ef547f18ff53e38ef9261da087af2c3ff2 Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Fri, 8 Oct 2021 00:16:23 +0800 Subject: [PATCH 4/9] fix: split nodos before init promise. --- src/config/config.json | 3 ++- src/config/index.ts | 5 +++++ src/services/NodeMonitor.ts | 14 ++++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/config/config.json b/src/config/config.json index 1684349..dbe4f92 100644 --- a/src/config/config.json +++ b/src/config/config.json @@ -17,5 +17,6 @@ "PEER_NODE_PORT": 7900, "REQUEST_TIMEOUT": 5000, "CONTROLLER_ENDPOINT": "http://34.252.126.146:7890", - "NETWORK_IDENTIFIER": 152 + "NETWORK_IDENTIFIER": 152, + "NUMBER_OF_NODE_REQUEST_CHUNK": 10 } diff --git a/src/config/index.ts b/src/config/index.ts index bb85abf..e90149f 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -16,6 +16,7 @@ interface Symbol { interface Monitor { NODE_MONITOR_SCHEDULE_INTERVAL: number; + NUMBER_OF_NODE_REQUEST_CHUNK: number; CHAIN_HEIGHT_MONITOR_SCHEDULE_INTERVAL: number; GEOLOCATION_MONITOR_SCHEDULE_INTERVAL: number; API_NODE_PORT: number; @@ -50,6 +51,7 @@ export const symbol: Symbol = { export const monitor: Monitor = { NODE_MONITOR_SCHEDULE_INTERVAL: Number(process.env.NODE_MONITOR_SCHEDULE_INTERVAL) || config.NODE_MONITOR_SCHEDULE_INTERVAL, + NUMBER_OF_NODE_REQUEST_CHUNK: Number(process.env.NUMBER_OF_NODE_REQUEST_CHUNK) || config.NUMBER_OF_NODE_REQUEST_CHUNK, CHAIN_HEIGHT_MONITOR_SCHEDULE_INTERVAL: Number(process.env.CHAIN_HEIGHT_MONITOR_SCHEDULE_INTERVAL) || config.CHAIN_HEIGHT_MONITOR_SCHEDULE_INTERVAL, GEOLOCATION_MONITOR_SCHEDULE_INTERVAL: @@ -86,6 +88,9 @@ export const verifyConfig = (cfg: Config): boolean => { if (isNaN(cfg.monitor.NODE_MONITOR_SCHEDULE_INTERVAL) || cfg.monitor.NODE_MONITOR_SCHEDULE_INTERVAL < 0) error = 'Invalid "NODE_MONITOR_SCHEDULE_INTERVAL"'; + if (isNaN(cfg.monitor.NUMBER_OF_NODE_REQUEST_CHUNK) || cfg.monitor.NUMBER_OF_NODE_REQUEST_CHUNK < 0) + error = 'Invalid "NUMBER_OF_NODE_REQUEST_CHUNK"'; + if (isNaN(cfg.monitor.API_NODE_PORT) || cfg.monitor.API_NODE_PORT <= 0 || cfg.monitor.API_NODE_PORT >= 10000) error = 'Invalid "API_NODE_PORT"'; diff --git a/src/services/NodeMonitor.ts b/src/services/NodeMonitor.ts index ddec76f..4853b34 100644 --- a/src/services/NodeMonitor.ts +++ b/src/services/NodeMonitor.ts @@ -38,7 +38,7 @@ export class NodeMonitor { this.nodeList = []; this.isRunning = false; this.interval = _interval || 300000; - this.nodeInfoChunks = 500; + this.nodeInfoChunks = monitor.NUMBER_OF_NODE_REQUEST_CHUNK; this.nodeInfoDelay = 1000; this.networkIdentifier = 152; // default Testnet this.cacheCollection(); @@ -139,14 +139,16 @@ export class NodeMonitor { private getNodeListInfo = async () => { logger.info(`Getting node info total for ${this.nodeList.length} nodes`); - const nodeInfoPromises = [...this.nodeList].map(this.getNodeInfo); - const nodeInfoPromisesChunks = splitArray(nodeInfoPromises, this.nodeInfoChunks); + const nodeListChunks = splitArray(this.nodeList, this.nodeInfoChunks); this.nodeList = []; - for (const chunk of nodeInfoPromisesChunks) { - logger.info(`Getting node info for chunk of ${chunk.length} nodes`); - this.addNodesToList((await Promise.all(chunk)) as INode[]); + for (const nodes of nodeListChunks) { + logger.info(`Getting node info for chunk of ${nodes.length} nodes`); + + const nodeInfoPromises = [...nodes].map(this.getNodeInfo); + + this.addNodesToList((await Promise.all(nodeInfoPromises)) as INode[]); await sleep(this.nodeInfoDelay); } this.nodeList.forEach((node) => this.nodesStats.addToStats(node)); From 0e87f2e83c7a72c71a5ee5796b0ed029b80f53e1 Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Fri, 8 Oct 2021 00:18:53 +0800 Subject: [PATCH 5/9] fix: add protocol support when request node info status --- src/services/ApiNodeService.ts | 76 ++++++++++++++++++++++++------ src/services/ChainHeightMonitor.ts | 2 +- src/services/GeolocationMonitor.ts | 2 +- 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/src/services/ApiNodeService.ts b/src/services/ApiNodeService.ts index 5f71623..802ad01 100644 --- a/src/services/ApiNodeService.ts +++ b/src/services/ApiNodeService.ts @@ -39,24 +39,59 @@ export interface ChainInfo { }; } -export class ApiNodeService { - static getStatus = async (host: string, port: number): Promise => { - // logger.info(`Getting api status for: ${host}`); +export interface ServerInfo { + restVersion: string; + sdkVersion: string; + deployment: { + deploymentTool: string; + deploymentToolVersion: string; + lastUpdatedDate: string; + }; +} +export class ApiNodeService { + static getStatus = async (host: string): Promise => { try { - const nodeInfo = (await HTTP.get(`http://${host}:${port}/node/info`)).data; - const chainInfo = (await HTTP.get(`http://${host}:${port}/chain/info`)).data; - const nodeServer = (await HTTP.get(`http://${host}:${port}/node/server`)).data; + const isHttps = await ApiNodeService.isHttpsEnabled(host); + const protocol = isHttps ? 'https' : 'http'; + const port = isHttps ? 3001 : 3000; - return { + logger.info(`Getting node status for: ${protocol}://${host}:${port}`); + + const [nodeInfo, chainInfo, nodeServer] = await Promise.all([ + ApiNodeService.getNodeInfo(host, port, protocol), + ApiNodeService.getNodeChainInfo(host, port, protocol), + ApiNodeService.getNodeServer(host, port, protocol), + ]); + + let apiStatus = { isAvailable: true, - chainHeight: chainInfo.height, - finalizationHeight: chainInfo.latestFinalizedBlock.height, - nodePublicKey: nodeInfo.nodePublicKey, - restVersion: nodeServer.serverInfo.restVersion, lastStatusCheck: Date.now(), }; + + if (nodeInfo) { + Object.assign(apiStatus, { + isHttpsEnabled: isHttps, + nodePublicKey: nodeInfo.nodePublicKey, + }); + } + + if (chainInfo) { + Object.assign(apiStatus, { + chainHeight: chainInfo.height, + finalizationHeight: chainInfo.latestFinalizedBlock.height, + }); + } + + if (nodeServer) { + Object.assign(apiStatus, { + restVersion: nodeServer.restVersion, + }); + } + + return apiStatus; } catch (e) { + logger.error(`Fail to request host node status: ${host}`, e); return { isAvailable: false, lastStatusCheck: Date.now(), @@ -64,18 +99,29 @@ export class ApiNodeService { } }; - static getNodeInfo = async (host: string, port: number): Promise => { + static getNodeInfo = async (host: string, port: number, protocol = 'http'): Promise => { + try { + return (await HTTP.get(`${protocol}://${host}:${port}/node/info`)).data; + } catch (e) { + logger.error(`Fail to request /node/info: ${host}`, e); + return null; + } + }; + + static getNodeChainInfo = async (host: string, port: number, protocol = 'http'): Promise => { try { - return (await HTTP.get(`http://${host}:${port}/node/info`)).data; + return (await HTTP.get(`${protocol}://${host}:${port}/chain/info`)).data; } catch (e) { + logger.error(`Fail to request /chain/info: ${host}`, e); return null; } }; - static getNodeChainInfo = async (host: string, port: number): Promise => { + static getNodeServer = async (host: string, port: number, protocol = 'http'): Promise => { try { - return (await HTTP.get(`http://${host}:${port}/chain/info`)).data; + return (await HTTP.get(`${protocol}://${host}:${port}/node/server`)).data as ServerInfo; } catch (e) { + logger.error(`Fail to request /node/server: ${host}`, e); return null; } }; diff --git a/src/services/ChainHeightMonitor.ts b/src/services/ChainHeightMonitor.ts index 13f8433..77b9485 100644 --- a/src/services/ChainHeightMonitor.ts +++ b/src/services/ChainHeightMonitor.ts @@ -66,7 +66,7 @@ export class ChainHeightMonitor { const node = await ApiNodeService.getNodeInfo(new URL(nodeUrl).host, Number(monitor.API_NODE_PORT)); if (node) { - const status = await ApiNodeService.getStatus(node.host, monitor.API_NODE_PORT); + const status = await ApiNodeService.getStatus(node.host); if (status.isAvailable) this.nodeList.push({ ...node, rewardPrograms: [] }); } diff --git a/src/services/GeolocationMonitor.ts b/src/services/GeolocationMonitor.ts index 6410422..47f449d 100644 --- a/src/services/GeolocationMonitor.ts +++ b/src/services/GeolocationMonitor.ts @@ -63,7 +63,7 @@ export class GeolocationMonitor { const node = await ApiNodeService.getNodeInfo(new URL(nodeUrl).host, Number(monitor.API_NODE_PORT)); if (node) { - const status = await ApiNodeService.getStatus(node.host, monitor.API_NODE_PORT); + const status = await ApiNodeService.getStatus(node.host); if (status.isAvailable) this.nodeList.push({ ...node, rewardPrograms: [] }); } From 00218e61507a50d80e0c5b85c67463cfc7dd13c4 Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Sat, 9 Oct 2021 16:41:11 +0800 Subject: [PATCH 6/9] feat: add node health --- src/models/Node.ts | 13 +++++++++++++ src/services/ApiNodeService.ts | 26 +++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/models/Node.ts b/src/models/Node.ts index 3c457fb..67f9eda 100644 --- a/src/models/Node.ts +++ b/src/models/Node.ts @@ -62,6 +62,19 @@ const NodeSchema: Schema = new Schema({ type: Boolean, required: false, }, + nodeStatus: { + type: { + apiNode: { + type: String, + required: false, + }, + db: { + type: String, + required: false, + }, + }, + required: false, + }, chainHeight: { type: Number, required: false, diff --git a/src/services/ApiNodeService.ts b/src/services/ApiNodeService.ts index 802ad01..7e77caa 100644 --- a/src/services/ApiNodeService.ts +++ b/src/services/ApiNodeService.ts @@ -5,9 +5,15 @@ import { Logger } from '@src/infrastructure'; const logger: winston.Logger = Logger.getLogger(basename(__filename)); +interface NodeStatus { + apiNode: string; + db: string; +} + export interface ApiStatus { isAvailable: boolean; isHttpsEnabled?: boolean; + nodeStatus?: NodeStatus; chainHeight?: number; finalizationHeight?: number; nodePublicKey?: string; @@ -58,10 +64,11 @@ export class ApiNodeService { logger.info(`Getting node status for: ${protocol}://${host}:${port}`); - const [nodeInfo, chainInfo, nodeServer] = await Promise.all([ + const [nodeInfo, chainInfo, nodeServer, nodeHealth] = await Promise.all([ ApiNodeService.getNodeInfo(host, port, protocol), ApiNodeService.getNodeChainInfo(host, port, protocol), ApiNodeService.getNodeServer(host, port, protocol), + ApiNodeService.getNodeHealth(host, port, protocol), ]); let apiStatus = { @@ -69,6 +76,12 @@ export class ApiNodeService { lastStatusCheck: Date.now(), }; + if (nodeHealth) { + Object.assign(apiStatus, { + nodeStatus: nodeHealth, + }); + } + if (nodeInfo) { Object.assign(apiStatus, { isHttpsEnabled: isHttps, @@ -126,6 +139,17 @@ export class ApiNodeService { } }; + static getNodeHealth = async (host: string, port: number, protocol = 'http'): Promise => { + try { + const health = (await HTTP.get(`${protocol}://${host}:${port}/node/health`)).data; + + return health.status; + } catch (e) { + logger.error(`Fail to request /node/health: ${host}`, e); + return null; + } + }; + static isHttpsEnabled = async (host: string, port = 3001): Promise => { try { await HTTP.get(`https://${host}:${port}/chain/info`); From 634b9434345183454e96536e0659796a03881fe8 Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Sat, 9 Oct 2021 17:09:11 +0800 Subject: [PATCH 7/9] feat: add finalization --- src/models/Node.ts | 21 +++++++++++++++++++-- src/services/ApiNodeService.ts | 19 ++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/models/Node.ts b/src/models/Node.ts index 67f9eda..4d158f0 100644 --- a/src/models/Node.ts +++ b/src/models/Node.ts @@ -79,8 +79,25 @@ const NodeSchema: Schema = new Schema({ type: Number, required: false, }, - finalizationHeight: { - type: Number, + finalization: { + type: { + height: { + type: Number, + required: false, + }, + epoch: { + type: Number, + required: false, + }, + point: { + type: Number, + required: false, + }, + hash: { + type: String, + required: false, + }, + }, required: false, }, nodePublicKey: { diff --git a/src/services/ApiNodeService.ts b/src/services/ApiNodeService.ts index 7e77caa..95b4b94 100644 --- a/src/services/ApiNodeService.ts +++ b/src/services/ApiNodeService.ts @@ -10,12 +10,19 @@ interface NodeStatus { db: string; } +interface FinalizedBlock { + height: number; + epoch: number; + point: number; + hash: string; +} + export interface ApiStatus { isAvailable: boolean; isHttpsEnabled?: boolean; nodeStatus?: NodeStatus; chainHeight?: number; - finalizationHeight?: number; + finalization?: FinalizedBlock; nodePublicKey?: string; restVersion?: string; lastStatusCheck: number; @@ -92,7 +99,12 @@ export class ApiNodeService { if (chainInfo) { Object.assign(apiStatus, { chainHeight: chainInfo.height, - finalizationHeight: chainInfo.latestFinalizedBlock.height, + finalization: { + height: chainInfo.latestFinalizedBlock.height, + epoch: chainInfo.latestFinalizedBlock.finalizationEpoch, + point: chainInfo.latestFinalizedBlock.finalizationPoint, + hash: chainInfo.latestFinalizedBlock.hash, + }, }); } @@ -132,7 +144,8 @@ export class ApiNodeService { static getNodeServer = async (host: string, port: number, protocol = 'http'): Promise => { try { - return (await HTTP.get(`${protocol}://${host}:${port}/node/server`)).data as ServerInfo; + const nodeServerInfo = (await HTTP.get(`${protocol}://${host}:${port}/node/server`)).data; + return nodeServerInfo.serverInfo; } catch (e) { logger.error(`Fail to request /node/server: ${host}`, e); return null; From 6bcb860c63f36fe844b2875df5c3ff1831c17159 Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Sat, 9 Oct 2021 17:21:57 +0800 Subject: [PATCH 8/9] style: fix lint, and disable unuse build script --- .travis.yml | 2 +- src/services/ApiNodeService.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7ca7c7e..8f2a99a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ before_script: - VERSION="$(node_load_version)" - log_env_variables script: - - npm run build:clients + # - npm run build:clients - npm run build jobs: include: diff --git a/src/services/ApiNodeService.ts b/src/services/ApiNodeService.ts index 95b4b94..9dae09b 100644 --- a/src/services/ApiNodeService.ts +++ b/src/services/ApiNodeService.ts @@ -145,6 +145,7 @@ export class ApiNodeService { static getNodeServer = async (host: string, port: number, protocol = 'http'): Promise => { try { const nodeServerInfo = (await HTTP.get(`${protocol}://${host}:${port}/node/server`)).data; + return nodeServerInfo.serverInfo; } catch (e) { logger.error(`Fail to request /node/server: ${host}`, e); From 4f5b5613d3cd29e7abfb3ed05fa4082f6f1cb3d0 Mon Sep 17 00:00:00 2001 From: AnthonyLaw Date: Fri, 15 Oct 2021 23:28:41 +0800 Subject: [PATCH 9/9] fix: change params to required --- src/services/ApiNodeService.ts | 8 ++++---- src/services/ChainHeightMonitor.ts | 19 +++++++++++++------ src/services/GeolocationMonitor.ts | 11 ++++++----- src/services/NodeMonitor.ts | 2 +- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/services/ApiNodeService.ts b/src/services/ApiNodeService.ts index 9dae09b..9383826 100644 --- a/src/services/ApiNodeService.ts +++ b/src/services/ApiNodeService.ts @@ -124,7 +124,7 @@ export class ApiNodeService { } }; - static getNodeInfo = async (host: string, port: number, protocol = 'http'): Promise => { + static getNodeInfo = async (host: string, port: number, protocol: string): Promise => { try { return (await HTTP.get(`${protocol}://${host}:${port}/node/info`)).data; } catch (e) { @@ -133,7 +133,7 @@ export class ApiNodeService { } }; - static getNodeChainInfo = async (host: string, port: number, protocol = 'http'): Promise => { + static getNodeChainInfo = async (host: string, port: number, protocol: string): Promise => { try { return (await HTTP.get(`${protocol}://${host}:${port}/chain/info`)).data; } catch (e) { @@ -142,7 +142,7 @@ export class ApiNodeService { } }; - static getNodeServer = async (host: string, port: number, protocol = 'http'): Promise => { + static getNodeServer = async (host: string, port: number, protocol: string): Promise => { try { const nodeServerInfo = (await HTTP.get(`${protocol}://${host}:${port}/node/server`)).data; @@ -153,7 +153,7 @@ export class ApiNodeService { } }; - static getNodeHealth = async (host: string, port: number, protocol = 'http'): Promise => { + static getNodeHealth = async (host: string, port: number, protocol: string): Promise => { try { const health = (await HTTP.get(`${protocol}://${host}:${port}/node/health`)).data; diff --git a/src/services/ChainHeightMonitor.ts b/src/services/ChainHeightMonitor.ts index 77b9485..d631441 100644 --- a/src/services/ChainHeightMonitor.ts +++ b/src/services/ChainHeightMonitor.ts @@ -62,13 +62,14 @@ export class ChainHeightMonitor { this.nodeList = (await DataBase.getNodeList()).filter((node) => isAPIRole(node.roles)); } catch (e) { logger.error('Failed to get node list. Use nodes from config'); - for (const nodeUrl of symbol.NODES) { - const node = await ApiNodeService.getNodeInfo(new URL(nodeUrl).host, Number(monitor.API_NODE_PORT)); + for (const node of symbol.NODES) { + const url = new URL(node); + const nodeInfo = await ApiNodeService.getNodeInfo(url.host, Number(url.port), url.protocol); - if (node) { - const status = await ApiNodeService.getStatus(node.host); + if (nodeInfo) { + const status = await ApiNodeService.getStatus(nodeInfo.host); - if (status.isAvailable) this.nodeList.push({ ...node, rewardPrograms: [] }); + if (status.isAvailable) this.nodeList.push({ ...nodeInfo, rewardPrograms: [] }); } } } @@ -79,7 +80,13 @@ export class ChainHeightMonitor { private getNodeChainHeight = async () => { logger.info(`Getting height stats for ${this.nodeList.length} nodes`); const nodes: INode[] = this.nodeList; - const nodeChainInfoPromises = nodes.map((node) => ApiNodeService.getNodeChainInfo(node.host, monitor.API_NODE_PORT)); + const nodeChainInfoPromises = nodes.map((node) => { + const isHttps = node.apiStatus?.isHttpsEnabled; + const protocol = isHttps ? 'https' : 'http'; + const port = isHttps ? 3001 : 3000; + + return ApiNodeService.getNodeChainInfo(node.host, port, protocol); + }); const nodeChainInfoList = await Promise.all(nodeChainInfoPromises); for (let chainInfo of nodeChainInfoList) { diff --git a/src/services/GeolocationMonitor.ts b/src/services/GeolocationMonitor.ts index 47f449d..d2da9d4 100644 --- a/src/services/GeolocationMonitor.ts +++ b/src/services/GeolocationMonitor.ts @@ -59,13 +59,14 @@ export class GeolocationMonitor { try { this.nodeList = await DataBase.getNodeList(); } catch (e) { - for (const nodeUrl of symbol.NODES) { - const node = await ApiNodeService.getNodeInfo(new URL(nodeUrl).host, Number(monitor.API_NODE_PORT)); + for (const node of symbol.NODES) { + const url = new URL(node); + const nodeInfo = await ApiNodeService.getNodeInfo(url.host, Number(url.port), url.protocol); - if (node) { - const status = await ApiNodeService.getStatus(node.host); + if (nodeInfo) { + const status = await ApiNodeService.getStatus(nodeInfo.host); - if (status.isAvailable) this.nodeList.push({ ...node, rewardPrograms: [] }); + if (status.isAvailable) this.nodeList.push({ ...nodeInfo, rewardPrograms: [] }); } } } diff --git a/src/services/NodeMonitor.ts b/src/services/NodeMonitor.ts index 4853b34..c17be8e 100644 --- a/src/services/NodeMonitor.ts +++ b/src/services/NodeMonitor.ts @@ -225,7 +225,7 @@ export class NodeMonitor { for (const nodeUrl of symbol.NODES) { const url = new URL(nodeUrl); - const nodeInfo = await ApiNodeService.getNodeInfo(url.hostname, Number(url.port || monitor.API_NODE_PORT)); + const nodeInfo = await ApiNodeService.getNodeInfo(url.hostname, Number(url.port), url.protocol); if (nodeInfo) { this.networkIdentifier = nodeInfo.networkIdentifier;