-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaverage-work.js
More file actions
29 lines (16 loc) · 780 Bytes
/
average-work.js
File metadata and controls
29 lines (16 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const Blockchain = require('../blockchain');
const blockchain = new Blockchain();
blockchain.addBlock({data: 'initial'});
console.log(blockchain.chain[blockchain.chain.length-1]);
let prevTimestamp, nextTimestamp, nextBlock, timeDiff, average;
const times = [];
for (let i = 0; i < 10000; i++) {
prevTimestamp = blockchain.chain[blockchain.chain.length-1].timestamp;
blockchain.addBlock({data: `block ${i}`});
nextBlock = blockchain.chain[blockchain.chain.length-1];
nextTimestamp = nextBlock.timestamp;
timeDiff = nextTimestamp - prevTimestamp;
times.push(timeDiff);
average = times.reduce((total, num) => (total+num))/times.length;
console.log(`Time: ${timeDiff}ms, Difficulty: ${nextBlock.difficulty}, Average: ${average}ms`);
}