Skip to content

Commit

Permalink
Calculate hashrate based on cummulativeDifficulty (block diff + uncle…
Browse files Browse the repository at this point in the history
…s diff). If no cummulativeDifficulty field is present, use an estimation based in the block diff * uncle count
  • Loading branch information
mpicco committed Apr 21, 2020
1 parent 0af8039 commit 3258672
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 87 deletions.
6 changes: 5 additions & 1 deletion src/api/lib/hashrateCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export class HashrateCalculator {
diffPerMiner[block.miner] = new BigNumber(0)
}

const bnDiff = new BigNumber(block.difficulty)
// cummulativeDifficulty = block.diff + sum(uncle.diff)
// If no cummulativeDifficulty is present, use an estimation under the supposition that the block diff
// is similar to its uncles' difficulty.
const bnDiff = block.cummulativeDifficulty ? new BigNumber(block.cummulativeDifficulty) :
new BigNumber(block.difficulty).multipliedBy((block.uncles ? block.uncles.length : 0) + 1)
diffPerMiner[block.miner] = diffPerMiner[block.miner].plus(bnDiff)
}

Expand Down
6 changes: 3 additions & 3 deletions src/api/modules/ExtendedStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class ExtendedStats extends DataCollectorItem {
const start = end - timeSpan

const blocks = await this.db.find({ timestamp: { $gte: start, $lte: end } })
.project({ _id: 0, miner: 1, timestamp: 1, difficulty: 1 })
.project({ _id: 0, miner: 1, timestamp: 1, difficulty: 1, cummulativeDifficulty: 1, uncles: 1 })
.toArray()

extendedStats.hashrates[period] = this.hashrateCalculator.hashrates(blocks, timeSpan)
Expand All @@ -162,7 +162,7 @@ export class ExtendedStats extends DataCollectorItem {
const timeSpan = PERIODS[period].timeSpan

const blocks = await this.db.find({ timestamp: { $gte: blockDate - timeSpan, $lte: blockDate } })
.project({ _id: 0, miner: 1, difficulty: 1 })
.project({ _id: 0, miner: 1, difficulty: 1, cummulativeDifficulty: 1, uncles: 1 })
.toArray()

hashrates[period] = this.hashrateCalculator.hashrates(blocks, timeSpan)
Expand All @@ -182,7 +182,7 @@ export class ExtendedStats extends DataCollectorItem {
const start = end - timeSpan

const blocks = await this.db.find({ timestamp: { $gte: start, $lte: end } })
.project({ _id: 0, timestamp: 1, difficulty: 1 })
.project({ _id: 0, timestamp: 1, difficulty: 1, cummulativeDifficulty: 1, uncles: 1 })
.toArray()

difficulties[period] = this.difficultyCalculator.difficulties(blocks, start, end, DIFFICULTY_BUCKET_SIZE)
Expand Down
Loading

0 comments on commit 3258672

Please sign in to comment.