-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverage-work.js
More file actions
26 lines (16 loc) · 833 Bytes
/
average-work.js
File metadata and controls
26 lines (16 loc) · 833 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
const Blockchain = require('./blockchain');
const blockchain = new Blockchain();
blockchain.addBlock({ data: 'initial'});
let prevTimestamp, nextTimestamp, nextBlock, timeDiff, average;
console.log('first block', blockchain.chain[blockchain.chain.length-1]);
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 to mine : ${timeDiff}ms. Difficulty : ${nextBlock.difficulty}. Average time : ${average}ms.`);
}