-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
114 lines (102 loc) · 3.8 KB
/
main.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { CosmWasmClient } from "npm:@cosmjs/cosmwasm-stargate";
import { timeOfRound } from "./drand.ts";
import { drandAddress, rpcEndpoint } from "./env.ts";
import { BeaconsResponse, ConfigResponse } from "./oracle.ts";
import { bigIntMax, bigIntMin } from "./stats.ts";
function warnLog(msg: string) {
console.warn(`%c${msg}`, "color: orange");
}
function nsToSec(ns: bigint): number {
// truncate last 3 digits to make to make value fit in safe integer range
return Number(ns / BigInt(1000)) / 1000000;
}
if (import.meta.main) {
const client = await CosmWasmClient.connect(rpcEndpoint);
const { min_round: min }: ConfigResponse = await client.queryContractSmart(
drandAddress,
{ config: {} },
);
// console.log(min);
const { beacons: [beaconLow] }: BeaconsResponse = await client.queryContractSmart(
drandAddress,
{
beacons_asc: { start_after: null, limit: 1 },
},
);
// console.log(beaconLow);
const { beacons: [beaconHigh] }: BeaconsResponse = await client.queryContractSmart(
drandAddress,
{
beacons_desc: { start_after: null, limit: 1 },
},
);
// console.log(beaconHigh);
const low = min; // beaconLow.round;
const high = beaconHigh.round;
const count = high - low + 1;
console.log(
`Verification range [${low}, ${high}] (${count} round between ${
timeOfRound(low).toISOString()
} and ${timeOfRound(high).toISOString()})`,
);
let pos = low; // lowest checked round
const lowDiff = BigInt(beaconLow.verified) - BigInt(beaconLow.published);
const diffs = [lowDiff];
while (pos < beaconHigh.round) {
const { beacons }: BeaconsResponse = await client.queryContractSmart(
drandAddress,
{ beacons_asc: { start_after: pos, limit: 400 } },
);
const rounds = beacons.map((b) => b.round);
diffs.push(...beacons.map((b) => BigInt(b.verified) - BigInt(b.published)));
// console.info(`Rounds in latest request ${rounds}`);
for (const round of rounds) {
const expected = pos + 1;
if (round != expected) {
warnLog(`Missing round ${expected}.`);
}
pos += 1;
}
const processed = pos - beaconLow.round + 1;
const done = 100 * (processed / count);
console.log(`Processed rounds ${processed} (${done.toFixed(2)}%)`);
}
console.log(`Verification times processed: ${diffs.length}`);
console.log(`Verification time minimum: ${nsToSec(bigIntMin(diffs)).toFixed(2)}s`);
console.log(`Verification time maximum: ${nsToSec(bigIntMax(diffs)).toFixed(2)}s`);
const m = new Map<string, number>();
for (const d of diffs) {
let slotLowEnd = null;
const dMs = d / 1000000n;
if (dMs > -11_000n) slotLowEnd = -11_000n;
if (dMs > -10_000n) slotLowEnd = -10_000n;
if (dMs > -9_000n) slotLowEnd = -9_000n;
if (dMs > -8_000n) slotLowEnd = -8_000n;
if (dMs > -7_000n) slotLowEnd = -7_000n;
if (dMs > -6_000n) slotLowEnd = -6_000n;
if (dMs > -5_000n) slotLowEnd = -5_000n;
if (dMs > -4_000n) slotLowEnd = -4_000n;
if (dMs > -3_000n) slotLowEnd = -3_000n;
if (dMs > -2_000n) slotLowEnd = -2_000n;
if (dMs > -1_000n) slotLowEnd = -1_000n;
if (dMs > -0n) slotLowEnd = 0n;
if (dMs > 1_000n) slotLowEnd = 1_000n;
if (dMs > 2_000n) slotLowEnd = 2_000n;
if (dMs > 3_000n) slotLowEnd = 3_000n;
if (dMs > 4_000n) slotLowEnd = 4_000n;
if (dMs > 5_000n) slotLowEnd = 5_000n;
if (dMs > 6_000n) slotLowEnd = 6_000n;
if (dMs > 7_000n) slotLowEnd = 7_000n;
if (dMs > 8_000n) slotLowEnd = 8_000n;
if (dMs > 9_000n) slotLowEnd = 9_000n;
if (dMs > 10_000n) slotLowEnd = 10_000n;
if (dMs > 11_000n) slotLowEnd = 11_000n;
if (dMs > 12_000n) slotLowEnd = null;
const key = `${slotLowEnd}`;
m.set(key, (m.get(key) ?? 0) + 1);
}
// print csv
for (const [key, val] of m.entries()) {
console.log(`${key},${val}`);
}
}