-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverage-work.js
More file actions
29 lines (21 loc) · 1.04 KB
/
average-work.js
File metadata and controls
29 lines (21 loc) · 1.04 KB
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();
// console.log(JSON.stringify(blockchain.chain[blockchain.chain.length-1]));
let prevTimestamp, nextTimestamp,nextBlock,timeDiff, average;
// console.log('the firstblock', blockchain.chain[blockchain.chain.length-1]);
blockchain.addBlock({data:"initial"});
const times= [];
for (let i = 0; i < 10000; i++) {
prevBlock=blockchain.chain[blockchain.chain.length-1]
prevTimestamp=prevBlock.timestamp;
prevDiff=prevBlock.difficulty;
blockchain.addBlock({data:`block ${i}`});
nextBlock=blockchain.chain[blockchain.chain.length-1];
nextTimestamp=nextBlock.timestamp;
timeDiff=nextTimestamp-prevTimestamp;
times.push(timeDiff);
// calculate the avarage
average=times.reduce((total, num)=>(total+num))/times.length;
// console.log(`the diffculty is ${JSON.stringify(nextBlock)}`);
console.log(`Time to mine block: ${timeDiff}ms. difficulty: ${nextBlock.difficulty}. average:${average}ms`);
}