-
Notifications
You must be signed in to change notification settings - Fork 0
/
average-work.js
38 lines (28 loc) · 1.12 KB
/
average-work.js
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
30
31
32
33
34
35
36
37
38
const Blockchain = require('./blockchain');
const blockchain = new Blockchain();
blockchain.addBlock({ data: 'genesis data' });
const lastBlock = blockchain.chain[blockchain.chain.length - 1];
console.log(lastBlock);
let previousTimeStamp, nextTimeStamp, nextBlock, timeDifference, averageTime;
const times = [];
// mine 10,000 block
for (let i = 0; i < 1000; i++) {
// Get previous time stamp
previousTimeStamp = blockchain.chain[blockchain.chain.length - 1].timestamp;
// Add the 10,000 blocks
blockchain.addBlock({ data: `this is block ${i}` });
// get the next block
nextBlock = blockchain.chain[blockchain.chain.length - 1];
// Get the next timestamp
nextTimeStamp = nextBlock.timestamp;
// Get the time difference between each block
timeDifference = nextTimeStamp - previousTimeStamp;
times.push(timeDifference);
// Calculate the average time it takes to mine a block
averageTime = times.reduce((total, item) => total + item) / times.length;
console.log(
`Block ${i}. Time taken to mine block: ${timeDifference}ms.
Difficulty: ${nextBlock.difficulty}.
Average time: ${averageTime}ms`
);
}