-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
55 lines (47 loc) · 1.66 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
54
55
var golos = require("./golos");
var global = require("./global");
var log = require("./logger").getLogger(__filename);
var keccak = require("keccak");
var BigInt = require("big-integer");
const WARN_DURATION = 1000 * 60 * 20; //post warning for 10 minutes to end of round
let prevSig = null;
let numbers = {};
let blockCount = 0;
async function run() {
while(true) {
try {
let act = await golos.getCurrentServerTimeAndBlock();
let sig = await golos.getBlockSignature(act.block);
blockCount++;
if(prevSig) {
let hasher = new keccak("keccak256");
hasher.update(prevSig + sig);
let sha3 = hasher.digest().toString("hex");
log.info("hash " + sha3);
let number = BigInt(sha3, 16).mod(12).value;
log.info("счастливое число " + number);
if(numbers[number]) {
numbers[number] ++;
} else {
numbers[number] = 1;
}
}
prevSig = sig;
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();