forked from gropox/randomblockchain
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (45 loc) · 1.56 KB
/
index.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var axios = require("./axios");
var global = require("./global");
var log = require("./logger").getLogger(__filename);
var BigInt = require("big-integer");
const WARN_DURATION = 1000 * 60 * 20; //post warning for 10 minutes to end of round
let prevHash = null;
let numbers = {};
let blockCount = 0;
async function run() {
while(true) {
try {
let response = await axios.get('/block?height=' + blockCount);
let hash = response.data.result.hash;
blockCount++;
if(prevHash) {
let hash_sum = prevHash+hash;
let sha3 = hash_sum.digest().toString("hex");
log.info("hash " + sha3);
let number = BigInt(sha3, 16).mod(12);
log.info("счастливое число " + number);
if(numbers[number]) {
numbers[number] ++;
} else {
numbers[number] = 1;
}
}
prevHash = hash;
let kn = Object.keys(numbers);
kn.sort((a,b) => {
a - b;
});
log.info(`Прочитанно блоков ${blockCount}`);
for(let i = 0; i < kn.length; i++) {
log.info(`Число ${kn[i]} выпало ${numbers[kn[i]]} раз`);
}
} catch(e) {
log.error("Error catched in main loop! " + e);
}
await sleep(1000*6); //sleep 1 minutes
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
run();