Skip to content

Commit

Permalink
fix: make universal rate computation (#13)
Browse files Browse the repository at this point in the history
Description
---
The hash rate fields were renamed. So this fix now uses all names so it
should work for any version.

How Has This Been Tested?
---
Manually running the tari-explorer.

What process can a PR reviewer use to test or verify this change?
---
Run it on both version and you should see the hash rates working.

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
  • Loading branch information
leet4tari authored Sep 5, 2023
2 parents 121a862 + 95cf91b commit c8a55ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
17 changes: 2 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 19 additions & 12 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit c8a55ab

Please sign in to comment.