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