Skip to content

Commit

Permalink
Add timeSpan as a parameter in HashrateCalculator::hashratePerMiner
Browse files Browse the repository at this point in the history
  • Loading branch information
mpicco committed Apr 21, 2020
1 parent 27e6d1a commit 0af8039
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
18 changes: 9 additions & 9 deletions src/api/lib/hashrateCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class HashrateCalculator {
return percPerMiner
}

hashratePerMiner (blocks) {
hashratePerMiner (blocks, timeSpan) {
if (!Array.isArray(blocks)) {
return {}
}
Expand All @@ -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 {}
}
Expand All @@ -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 = {}
Expand All @@ -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`
}

Expand Down
16 changes: 8 additions & 8 deletions src/api/modules/ExtendedStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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 })
Expand Down
40 changes: 34 additions & 6 deletions test/services/hashrateCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -137,15 +138,15 @@ 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, {})
})

it('returns an empty object when no blocks', () => {
const calc = new HashrateCalculator()

const hashrate = calc.hashratePerMiner([])
const hashrate = calc.hashratePerMiner([], 0)

assert.deepEqual(hashrate, {})
})
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0af8039

Please sign in to comment.