fix: Relates to #9. Reuse calculation of average block time

Issues:

* Unable to import `blockAverage` into Chain.ts or Node.ts due to Issue #21.
* TypeScript says `[ts] Module '"/Users/Me/code/blockchain/clones/paritytech/dotstats/packages/common/build/index"' has no exported member 'blockAverage'.`
This commit is contained in:
Luke Schoen
2018-07-25 08:13:06 +02:00
parent 20d8b21adc
commit ee8b76233a
4 changed files with 26 additions and 29 deletions
+2 -12
View File
@@ -2,7 +2,7 @@ import * as EventEmitter from 'events';
import Node from './Node';
import Feed from './Feed';
import FeedSet from './FeedSet';
import { Maybe, Types, FeedMessage } from '@dotstats/common';
import { Maybe, Types, FeedMessage, blockAverage } from '@dotstats/common';
const BLOCK_TIME_HISTORY = 10;
@@ -112,17 +112,7 @@ export default class Chain {
private updateAverageBlockTime(height: Types.BlockNumber, now: Types.Timestamp) {
this.blockTimes[height % BLOCK_TIME_HISTORY] = now - this.blockTimestamp;
let sum = 0;
let count = 0;
for (const time of this.blockTimes) {
if (time) {
sum += time;
count += 1;
}
}
// We are guaranteed that count > 0
this.averageBlockTime = (sum / count) as Types.Milliseconds;
this.averageBlockTime = blockAverage(this.blockTimes);
}
}