From 0af8039a097aca31231fffc33f69e771c95422a3 Mon Sep 17 00:00:00 2001 From: mpicco Date: Thu, 6 Feb 2020 19:06:05 -0300 Subject: [PATCH] Add timeSpan as a parameter in HashrateCalculator::hashratePerMiner --- src/api/lib/hashrateCalculator.js | 18 ++++++------- src/api/modules/ExtendedStats.js | 16 ++++++------ test/services/hashrateCalculator.js | 40 ++++++++++++++++++++++++----- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/api/lib/hashrateCalculator.js b/src/api/lib/hashrateCalculator.js index 1efabf24..a10107e8 100644 --- a/src/api/lib/hashrateCalculator.js +++ b/src/api/lib/hashrateCalculator.js @@ -35,7 +35,7 @@ export class HashrateCalculator { return percPerMiner } - hashratePerMiner (blocks) { + hashratePerMiner (blocks, timeSpan) { if (!Array.isArray(blocks)) { return {} } @@ -46,12 +46,12 @@ export class HashrateCalculator { let diffPerMiner = this.difficultyPerMiner(blocks) - let hashratePerMiner = this.innerHashratePerMiner(blocks, diffPerMiner) + let hashratePerMiner = this.innerHashratePerMiner(blocks, diffPerMiner, timeSpan) return hashratePerMiner } - hashrates (blocks) { + hashrates (blocks, timeSpan) { if (!Array.isArray(blocks)) { return {} } @@ -62,7 +62,7 @@ export class HashrateCalculator { let diffPerMiner = this.difficultyPerMiner(blocks) - let hashratePerMiner = this.innerHashratePerMiner(blocks, diffPerMiner) + let hashratePerMiner = this.innerHashratePerMiner(blocks, diffPerMiner, timeSpan) let percPerMiner = this.innerHashratePercentagePerMiner(diffPerMiner) let hashrates = {} @@ -88,14 +88,14 @@ export class HashrateCalculator { return percPerMiner } - innerHashratePerMiner (blocks, diffPerMiner) { - const start = new BigNumber(blocks[0].timestamp) - const end = new BigNumber(blocks[blocks.length - 1].timestamp) - const timeDiff = end.isGreaterThan(start) ? end.minus(start) : new BigNumber(1) + innerHashratePerMiner (blocks, diffPerMiner, timeSpan) { + if (timeSpan <= 0) + timeSpan = 1; let hashratePerMiner = {} for (const m of Object.keys(diffPerMiner)) { - const val = diffPerMiner[m].dividedBy(timeDiff).dividedBy(EXA).toFixed(DECIMALS) + const val = diffPerMiner[m].dividedBy(timeSpan).dividedBy(EXA).toFixed(DECIMALS) + hashratePerMiner[m] = `${val} EHs` } diff --git a/src/api/modules/ExtendedStats.js b/src/api/modules/ExtendedStats.js index cad293e3..eb3d84ed 100644 --- a/src/api/modules/ExtendedStats.js +++ b/src/api/modules/ExtendedStats.js @@ -138,14 +138,14 @@ export class ExtendedStats extends DataCollectorItem { const end = block.timestamp for (const period of Object.keys(PERIODS)) { - const timeLimit = PERIODS[period].timeLimit - const start = end - timeLimit + const timeSpan = PERIODS[period].timeSpan + const start = end - timeSpan const blocks = await this.db.find({ timestamp: { $gte: start, $lte: end } }) .project({ _id: 0, miner: 1, timestamp: 1, difficulty: 1 }) .toArray() - extendedStats.hashrates[period] = this.hashrateCalculator.hashrates(blocks) + extendedStats.hashrates[period] = this.hashrateCalculator.hashrates(blocks, timeSpan) extendedStats.difficulties[period] = this.difficultyCalculator.difficulties(blocks, start, end, DIFFICULTY_BUCKET_SIZE) } @@ -159,13 +159,13 @@ export class ExtendedStats extends DataCollectorItem { const blockDate = block.timestamp for (const period of Object.keys(PERIODS)) { - const timeLimit = PERIODS[period].timeLimit + const timeSpan = PERIODS[period].timeSpan - const blocks = await this.db.find({ timestamp: { $gte: blockDate - timeLimit, $lte: blockDate } }) + const blocks = await this.db.find({ timestamp: { $gte: blockDate - timeSpan, $lte: blockDate } }) .project({ _id: 0, miner: 1, difficulty: 1 }) .toArray() - hashrates[period] = this.hashrateCalculator.hashrates(blocks) + hashrates[period] = this.hashrateCalculator.hashrates(blocks, timeSpan) } return hashrates @@ -178,8 +178,8 @@ export class ExtendedStats extends DataCollectorItem { const end = block.timestamp for (const period of Object.keys(PERIODS)) { - const timeLimit = PERIODS[period].timeLimit - const start = end - timeLimit + const timeSpan = PERIODS[period].timeSpan + const start = end - timeSpan const blocks = await this.db.find({ timestamp: { $gte: start, $lte: end } }) .project({ _id: 0, timestamp: 1, difficulty: 1 }) diff --git a/test/services/hashrateCalculator.js b/test/services/hashrateCalculator.js index 7451575e..8c9a1b67 100644 --- a/test/services/hashrateCalculator.js +++ b/test/services/hashrateCalculator.js @@ -4,6 +4,7 @@ import { HashrateCalculator } from '../../src/api/lib/hashrateCalculator.js' describe('hashrateCalculator', () => { const exa = (n) => new BigNumber(`${n}e18`) + const hexExa = (n) => `0x${new BigNumber(`${n}e18`).toString(16)}` context('hashratePercentagePerMiner', () => { it('returns an empty object when argument is not an array', () => { @@ -137,7 +138,7 @@ describe('hashrateCalculator', () => { it('returns an empty object when argument is not an array', () => { const calc = new HashrateCalculator() - const hashrate = calc.hashratePerMiner() + const hashrate = calc.hashratePerMiner(0) assert.deepEqual(hashrate, {}) }) @@ -145,7 +146,7 @@ describe('hashrateCalculator', () => { it('returns an empty object when no blocks', () => { const calc = new HashrateCalculator() - const hashrate = calc.hashratePerMiner([]) + const hashrate = calc.hashratePerMiner([], 0) assert.deepEqual(hashrate, {}) }) @@ -156,7 +157,7 @@ describe('hashrateCalculator', () => { const blocks = [ { miner: '0x0a', difficulty: exa(1), timestamp: START } ] - const hashrate = calc.hashratePerMiner(blocks) + const hashrate = calc.hashratePerMiner(blocks, 0) assert.deepEqual(hashrate, { '0x0a': '1.000 EHs' @@ -173,7 +174,7 @@ describe('hashrateCalculator', () => { { miner: '0x0a', difficulty: exa(1), timestamp: START + 3 }, { miner: '0x0a', difficulty: exa(1), timestamp: START + 4 } ] - const hashrate = calc.hashratePerMiner(blocks) + const hashrate = calc.hashratePerMiner(blocks, (START + 4) - START) assert.deepEqual(hashrate, { '0x0a': '1.250 EHs' @@ -196,7 +197,7 @@ describe('hashrateCalculator', () => { { miner: '0x0c', difficulty: exa(10), timestamp: START + 9 }, { miner: '0x0c', difficulty: exa(11), timestamp: START + 10 } ] - const hashrate = calc.hashratePerMiner(blocks) + const hashrate = calc.hashratePerMiner(blocks, (START + 10) - START) assert.deepEqual(hashrate, { '0x0a': '2.600 EHs', // 26 @@ -226,7 +227,34 @@ describe('hashrateCalculator', () => { { miner: '0x0c', difficulty: exa(10), timestamp: START + 9 }, { miner: '0x0c', difficulty: exa(11), timestamp: START + 10 } ] - const hashrate = calc.hashrates(blocks) + const hashrate = calc.hashrates(blocks, (START + 10) - START) + + assert.deepEqual(hashrate, { + '0x0a': { avg: '2.600 EHs', perc: 0.394 }, // 26 + '0x0b': { avg: '1.000 EHs', perc: 0.152 }, // 10 + '0x0c': { avg: '2.500 EHs', perc: 0.379 }, // 25 + '0x0d': { avg: '0.500 EHs', perc: 0.076 } // 5 + }) + }) + + it('returns the cumulative diff divided the time elapsed between first and last block for multiple miners' + + 'when the difficulties are hexadecimal strings', () => { + const calc = new HashrateCalculator() + + const blocks = [ + { miner: '0x0a', difficulty: hexExa(1), timestamp: START }, + { miner: '0x0b', difficulty: hexExa(2), timestamp: START + 1 }, + { miner: '0x0a', difficulty: hexExa(3), timestamp: START + 2 }, + { miner: '0x0c', difficulty: hexExa(4), timestamp: START + 3 }, + { miner: '0x0d', difficulty: hexExa(5), timestamp: START + 4 }, + { miner: '0x0a', difficulty: hexExa(6), timestamp: START + 5 }, + { miner: '0x0a', difficulty: hexExa(7), timestamp: START + 6 }, + { miner: '0x0b', difficulty: hexExa(8), timestamp: START + 7 }, + { miner: '0x0a', difficulty: hexExa(9), timestamp: START + 8 }, + { miner: '0x0c', difficulty: hexExa(10), timestamp: START + 9 }, + { miner: '0x0c', difficulty: hexExa(11), timestamp: START + 10 } + ] + const hashrate = calc.hashrates(blocks, (START + 10) - START) assert.deepEqual(hashrate, { '0x0a': { avg: '2.600 EHs', perc: 0.394 }, // 26