Skip to content

Commit 5a92786

Browse files
committed
feat: .getNextSuperblock(), .estimateNthGovCycle(), .estimateFutureBlocks()
1 parent b0b1d1a commit 5a92786

File tree

1 file changed

+72
-9
lines changed

1 file changed

+72
-9
lines changed

dashgov.js

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ var DashGov = ("object" === typeof module && exports) || {};
183183

184184
// TODO move to a nice place
185185
const SUPERBLOCK_INTERVAL = 16616; // actual
186+
DashGov.SUPERBLOCK_INTERVAL = SUPERBLOCK_INTERVAL;
186187
const SECONDS_PER_BLOCK_ESTIMATE = 155; // estimate (measured as ~157.64)
187188

188189
const VOTE_OFFSET_BLOCKS = 1662; // actual
@@ -206,6 +207,10 @@ var DashGov = ("object" === typeof module && exports) || {};
206207
* @returns {Float64} - fractional seconds
207208
*/
208209
GObj.measureSecondsPerBlock = function (snapshot) {
210+
if (!snapshot) {
211+
snapshot = { ms: 0, block: 0 };
212+
}
213+
209214
if (!snapshot.ms) {
210215
if (snapshot.block) {
211216
throw new Error(ERR_INCOMPLETE_BLOCK);
@@ -235,7 +240,9 @@ var DashGov = ("object" === typeof module && exports) || {};
235240
now = Date.now(),
236241
currentBlock = 0,
237242
secondsPerBlock = 0,
243+
cycles = 3,
238244
) {
245+
console.log("CURRENT HEIGHT:", currentBlock);
239246
if (!currentBlock) {
240247
let d = new Date(MONTHLY_SUPERBLOCK_61_DATE);
241248
let then = d.valueOf();
@@ -259,17 +266,73 @@ var DashGov = ("object" === typeof module && exports) || {};
259266
});
260267
}
261268

262-
let superblockHeight = currentBlock / SUPERBLOCK_INTERVAL;
263-
superblockHeight = Math.ceil(superblockHeight);
264-
superblockHeight *= SUPERBLOCK_INTERVAL;
269+
/** @type {Array<Estimate>} */
270+
let estimates = [];
271+
for (let i = -1; i < cycles; i += 1) {
272+
console.log("HEIGHT:", currentBlock);
273+
let estimate = GObj.estimateNthGovCycle(
274+
{ block: currentBlock, ms: now },
275+
secondsPerBlock,
276+
i,
277+
);
278+
estimates.push(estimate);
279+
}
280+
281+
return estimates;
282+
};
283+
284+
/**
285+
* @typedef Estimate
286+
* @prop {Uint53} secondsPerBlock
287+
* @prop {Uint53} voteHeight
288+
* @prop {Uint53} voteDelta
289+
* @prop {String} voteIso - date in ISO format
290+
* @prop {Uint53} voteMs
291+
* @prop {Uint53} voteDeltaMs
292+
* @prop {Uint53} superblockHeight
293+
* @prop {Uint53} superblockDelta
294+
* @prop {String} superblockIso - date in ISO format
295+
* @prop {Uint53} superblockMs
296+
* @prop {Uint53} superblockDeltaMs
297+
*/
298+
299+
/**
300+
* @param {Uint53} height
301+
* @returns {Uint53} - the superblock after the given height
302+
*/
303+
GObj.getNextSuperblock = function (height) {
304+
let isSuperblock = height % SUPERBLOCK_INTERVAL === 0;
305+
if (isSuperblock) {
306+
height += SUPERBLOCK_INTERVAL;
307+
}
308+
let superblockCount = height / SUPERBLOCK_INTERVAL;
309+
superblockCount = Math.ceil(superblockCount);
310+
let superblockHeight = superblockCount * SUPERBLOCK_INTERVAL;
311+
312+
return superblockHeight;
313+
};
314+
315+
/**
316+
* @param {Snapshot} snapshot
317+
* @param {Float64} secondsPerBlock
318+
* @param {Uint53} offset - how many superblocks in the future
319+
* @returns {Estimate} - details about the current governance cycle
320+
*/
321+
GObj.estimateNthGovCycle = function (
322+
{ block, ms },
323+
secondsPerBlock,
324+
offset = 0,
325+
) {
326+
let blockOffset = offset * SUPERBLOCK_INTERVAL;
327+
let blockTarget = block + blockOffset;
328+
let superblockHeight = GObj.getNextSuperblock(blockTarget);
265329

266-
let superblockDelta = superblockHeight - currentBlock;
330+
let superblockDelta = superblockHeight - block;
267331
// let superblockDeltaMs = superblockDelta * SECONDS_PER_BLOCK_ESTIMATE * 1000;
268332
let superblockDeltaMs = superblockDelta * secondsPerBlock * 1000;
269-
// let voteEndDelta = superblockDelta - VOTE_OFFSET;
270-
// let voteEndDeltaMs = voteEndDelta * SECONDS_PER_BLOCK_ESTIMATE * 1000;
271333

272-
let d = new Date();
334+
console.log(ms, superblockDeltaMs);
335+
let d = new Date(ms);
273336
d.setUTCMilliseconds(0);
274337

275338
d.setUTCMilliseconds(superblockDeltaMs);
@@ -286,9 +349,9 @@ var DashGov = ("object" === typeof module && exports) || {};
286349
voteIso: vtts,
287350
voteMs: vtms,
288351
voteDelta: superblockDelta - VOTE_OFFSET_BLOCKS,
289-
voteDeltaMs: superblockDelta,
352+
voteDeltaMs: superblockDeltaMs - VOTE_OFFSET_MS,
290353
superblockHeight: superblockHeight,
291-
superblockDelta: superblockDeltaMs,
354+
superblockDelta: superblockDelta,
292355
superblockIso: sbts,
293356
superblockMs: sbms,
294357
superblockDeltaMs: superblockDeltaMs,

0 commit comments

Comments
 (0)