Skip to content

Commit a6a6e9f

Browse files
committed
feat(scripts): add ./scripts/estimate.js for estimating future proposal cycles
1 parent 597a81a commit a6a6e9f

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

scripts/estimate.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env node
2+
"use strict";
3+
4+
/** @typedef {Number} Float64 */
5+
/** @typedef {Number} Uint53 */
6+
7+
const MS_PER_DAY = 24 * 60 * 60 * 1000;
8+
9+
let DashGov = require("../");
10+
11+
let Logger = module.exports;
12+
13+
Logger.logEstimate = function (estimate, index) {
14+
console.info(`Until next (${index})...`);
15+
16+
let voteDeltaDays = estimate.voteDeltaMs / MS_PER_DAY;
17+
console.info(` Vote (${estimate.voteHeight}):`);
18+
console.info(" Blocks:", estimate.voteDelta);
19+
console.info(" Days:", voteDeltaDays);
20+
console.info(" Date:", estimate.voteIso);
21+
22+
let superblockDeltaDays = estimate.superblockDeltaMs / MS_PER_DAY;
23+
console.info(` Superblock (${estimate.superblockHeight}):`);
24+
console.info(" Blocks:", estimate.superblockDelta);
25+
console.info(" Days:", superblockDeltaDays);
26+
console.info(" Date:", estimate.superblockIso);
27+
28+
console.info("");
29+
};
30+
31+
Logger.logEstimates = function (estimates, secondsPerBlock) {
32+
if (!secondsPerBlock) {
33+
secondsPerBlock = DashGov.estimateSecondsPerBlock();
34+
}
35+
console.info("Measured Seconds per Block", secondsPerBlock);
36+
console.info("");
37+
console.info(estimates);
38+
39+
console.info("LAST:");
40+
let index = -1;
41+
Logger.logEstimate(estimates.last, index);
42+
43+
index += 1;
44+
if (estimates.lameduck) {
45+
console.info("LAME DUCK:");
46+
Logger.logEstimate(estimates.lameduck, 0);
47+
}
48+
49+
console.info("UPCOMING:");
50+
for (let estimate of estimates.upcoming) {
51+
index += 1;
52+
Logger.logEstimate(estimate, index);
53+
}
54+
};
55+
56+
function help() {
57+
console.info(`USAGE`);
58+
console.info(
59+
` ./scripts/estimate.js <cycle-count> [block-height] [block-time]`,
60+
);
61+
console.info(``);
62+
63+
console.info(`EXAMPLE`);
64+
console.info(` ./scripts/estimate.js '3'`);
65+
console.info(` ./scripts/estimate.js '3' '2114623' '2024-08-01T22:01:00Z'`);
66+
console.info(` ./scripts/estimate.js '3' '2106925' '2024-07-19T02:56:54Z'`);
67+
console.info(``);
68+
}
69+
70+
function main() {
71+
let cycleCountStr = process.argv[2] || "0";
72+
let cycleCount = parseInt(cycleCountStr, 10);
73+
let blockHeightStr = process.argv[3] || "0";
74+
let blockHeight = parseInt(blockHeightStr, 10);
75+
let blockTime = process.argv[4];
76+
let blockMs = 0;
77+
let secondsPerBlock = 0;
78+
let proposalLeadtime = 0;
79+
80+
if (!cycleCount) {
81+
console.error(`ERROR`);
82+
console.error(` missing arguments`);
83+
console.error(``);
84+
help();
85+
process.exit(1);
86+
return;
87+
}
88+
89+
if (blockTime) {
90+
blockMs = Date.parse(blockTime);
91+
} else {
92+
blockMs = Date.now();
93+
}
94+
95+
if (!blockHeight) {
96+
secondsPerBlock = DashGov.estimateSecondsPerBlock();
97+
blockHeight = DashGov.estimateBlockHeight(blockMs, secondsPerBlock);
98+
}
99+
100+
let snapshot = {
101+
ms: blockMs,
102+
block: blockHeight,
103+
};
104+
if (!secondsPerBlock) {
105+
secondsPerBlock = DashGov.measureSecondsPerBlock(snapshot);
106+
}
107+
let estimates = DashGov.estimateProposalCycles(
108+
cycleCount,
109+
snapshot,
110+
secondsPerBlock,
111+
// DashGov.PROPOSAL_LEAD_MS,
112+
);
113+
114+
Logger.logEstimates(estimates, secondsPerBlock);
115+
}
116+
117+
Logger.main = main;
118+
119+
if (require.main === module) {
120+
main();
121+
}

0 commit comments

Comments
 (0)