From 95cf91b685400b0989e15b0c45708a3aa40a214e Mon Sep 17 00:00:00 2001 From: Cifko Date: Tue, 5 Sep 2023 08:53:37 +0200 Subject: [PATCH] fix: make universal rate computation --- package-lock.json | 17 ++--------------- routes/index.js | 31 +++++++++++++++++++------------ 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2cb5bbf..2fcb7cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,8 +14,7 @@ "express": "~4.16.1", "hbs": "^4.1.2", "http-errors": "~1.6.3", - "morgan": "~1.9.1", - "validator-node-grpc-client": "file:../tari-dan/clients/validator_node_grpc_client" + "morgan": "~1.9.1" }, "devDependencies": { "@babel/core": "^7.15.8", @@ -61,6 +60,7 @@ "../tari-dan/clients/validator_node_grpc_client": { "name": "@tari/validator-node-grpc-client", "version": "0.0.1", + "extraneous": true, "dependencies": { "@grpc/grpc-js": "^1.2.3", "@grpc/proto-loader": "^0.5.5", @@ -2882,10 +2882,6 @@ "integrity": "sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==", "dev": true }, - "node_modules/validator-node-grpc-client": { - "resolved": "../tari-dan/clients/validator_node_grpc_client", - "link": true - }, "node_modules/vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", @@ -5060,15 +5056,6 @@ "integrity": "sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==", "dev": true }, - "validator-node-grpc-client": { - "version": "file:../tari-dan/clients/validator_node_grpc_client", - "requires": { - "@grpc/grpc-js": "^1.2.3", - "@grpc/proto-loader": "^0.5.5", - "grpc-promise": "^1.4.0", - "jest": "^27.1.1" - } - }, "vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/routes/index.js b/routes/index.js index d3b8f45..b64861e 100644 --- a/routes/index.js +++ b/routes/index.js @@ -14,7 +14,7 @@ router.get("/", async function (req, res) { let limit = parseInt(req.query.limit || "20"); let version_result = await client.getVersion({}); - let version = version_result.value.slice(0,25); + let version = version_result.value.slice(0, 25); let tipInfo = await client.getTipInfo({}); @@ -86,15 +86,17 @@ router.get("/", async function (req, res) { // estimated hash rates let lastDifficulties = await client.getNetworkDifficulty({ from_tip: 100 }); - let totalHashRates = getHashRates(lastDifficulties, "estimated_hash_rate"); - let moneroHashRates = getHashRates( - lastDifficulties, - "monero_estimated_hash_rate" - ); - let shaHashRates = getHashRates( - lastDifficulties, - "sha3_estimated_hash_rate" - ); + let totalHashRates = getHashRates(lastDifficulties, [ + "estimated_hash_rate", + ]); + let moneroHashRates = getHashRates(lastDifficulties, [ + "monero_estimated_hash_rate", + "randomx_estimated_hash_rate", + ]); + let shaHashRates = getHashRates(lastDifficulties, [ + "sha3_estimated_hash_rate", + "sha3x_estimated_hash_rate", + ]); // list of active validator nodes let tipHeight = tipInfo.metadata.height_of_longest_chain; @@ -142,12 +144,17 @@ router.get("/", async function (req, res) { } }); -function getHashRates(difficulties, property) { +function getHashRates(difficulties, properties) { const end_idx = difficulties.length - 1; const start_idx = end_idx - 60; return difficulties - .map((d) => parseInt(d[property])) + .map((d) => + properties.reduce( + (sum, property) => sum + (parseInt(d[property]) || 0), + 0 + ) + ) .slice(start_idx, end_idx); }