Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExtendedStats module fixes #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/services/classes/Tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Tx extends _BcThing.BcThing {
} catch (err) {
return Promise.reject(err);
}

}

async getTx() {
Expand Down
13 changes: 7 additions & 6 deletions src/api/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ class Api extends DataCollector {
const stats = await Stats.run('getLatest')
if (!stats) return

/* const ExtendedStats = this.getModule('ExtendedStats')
if (ExtendedStats) {
const blockNumber = parseInt(stats.blockNumber)
const extendedStats = await ExtendedStats.getExtendedStats(blockNumber)
Object.assign(stats, extendedStats)
} */
const ExtendedStats = this.getModule('ExtendedStats')
if (ExtendedStats) {
const blockNumber = parseInt(stats.blockNumber)
const extendedStats = await ExtendedStats.getExtendedStats(blockNumber)
Object.assign(stats, extendedStats)
}

let circulatingSupply = stats.circulatingSupply || await this.getCirculatingSupplyFromDb()
this.circulatingSupply = circulatingSupply
this.stats = Object.assign({}, stats)
Expand Down
26 changes: 15 additions & 11 deletions src/api/lib/hashrateCalculator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigNumber } from 'bignumber.js'

export const DECIMALS = 3
export const EXA = new BigNumber('1e50')
export const EXA = new BigNumber('1e18')

export class HashrateCalculator {
difficultyPerMiner (blocks) {
Expand All @@ -12,7 +12,11 @@ export class HashrateCalculator {
diffPerMiner[block.miner] = new BigNumber(0)
}

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

Expand All @@ -35,7 +39,7 @@ export class HashrateCalculator {
return percPerMiner
}

hashratePerMiner (blocks) {
hashratePerMiner (blocks, timeSpan) {
if (!Array.isArray(blocks)) {
return {}
}
Expand All @@ -46,12 +50,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 +66,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 +92,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
30 changes: 15 additions & 15 deletions src/api/modules/ExtendedStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { HashrateCalculator } from '../lib/hashrateCalculator'
import { DifficultyCalculator } from '../lib/difficultyCalculator'

// 1 hour bucket size
const DIFFICULTY_BUCKET_SIZE = 3600000
const DIFFICULTY_BUCKET_SIZE = 3600

export const PERIODS = {
'1w': {
timeLimit: 604800000
timeSpan: 604800
},
'1d': {
timeLimit: 86400000
timeSpan: 86400
},
'1h': {
timeLimit: 3600000
timeSpan: 3600
}
}

Expand Down 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 })
.project({ _id: 0, miner: 1, timestamp: 1, difficulty: 1, cumulativeDifficulty: 1, uncles: 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 } })
.project({ _id: 0, miner: 1, difficulty: 1 })
const blocks = await this.db.find({ timestamp: { $gte: blockDate - timeSpan, $lte: blockDate } })
.project({ _id: 0, miner: 1, difficulty: 1, cumulativeDifficulty: 1, uncles: 1 })
.toArray()

hashrates[period] = this.hashrateCalculator.hashrates(blocks)
hashrates[period] = this.hashrateCalculator.hashrates(blocks, timeSpan)
}

return hashrates
Expand All @@ -178,11 +178,11 @@ 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 })
.project({ _id: 0, timestamp: 1, difficulty: 1, cumulativeDifficulty: 1, uncles: 1 })
.toArray()

difficulties[period] = this.difficultyCalculator.difficulties(blocks, start, end, DIFFICULTY_BUCKET_SIZE)
Expand Down
4 changes: 2 additions & 2 deletions src/api/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { Token } from './Token'
import { TxPending } from './TxPending'
import { Stats } from './Stats'
import { Summary } from './Summary'
// import { ExtendedStats } from './ExtendedStats'
import { ExtendedStats } from './ExtendedStats'
import { ContractVerification } from './ContractVerification'
import { getModulesNames, getEnabledModules } from '../lib/apiTools'

const apiModules = { Block, Tx, Address, Event, Token, TxPending, Stats, Summary, ContractVerification }
const apiModules = { ExtendedStats, Block, Tx, Address, Event, Token, TxPending, Stats, Summary, ContractVerification }

export const getEnabledApiModules = modules => {
const enabled = getModulesNames(getEnabledModules(modules))
Expand Down
2 changes: 1 addition & 1 deletion test/services/difficultyCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DifficultyCalculator } from '../../src/api/lib/difficultyCalculator.js'

describe('difficultyCalculator', () => {
const minutes = (m) => m * 60 * 1000
const exa = (n) => new BigNumber(`${n}e50`)
const exa = (n) => new BigNumber(`${n}e18`)
const epoch = (dateString) => +new Date(dateString)

context('difficulties, 10 minute buckets', () => {
Expand Down
Loading