@@ -183,6 +183,7 @@ var DashGov = ("object" === typeof module && exports) || {};
183
183
184
184
// TODO move to a nice place
185
185
const SUPERBLOCK_INTERVAL = 16616 ; // actual
186
+ DashGov . SUPERBLOCK_INTERVAL = SUPERBLOCK_INTERVAL ;
186
187
const SECONDS_PER_BLOCK_ESTIMATE = 155 ; // estimate (measured as ~157.64)
187
188
188
189
const VOTE_OFFSET_BLOCKS = 1662 ; // actual
@@ -206,6 +207,10 @@ var DashGov = ("object" === typeof module && exports) || {};
206
207
* @returns {Float64 } - fractional seconds
207
208
*/
208
209
GObj . measureSecondsPerBlock = function ( snapshot ) {
210
+ if ( ! snapshot ) {
211
+ snapshot = { ms : 0 , block : 0 } ;
212
+ }
213
+
209
214
if ( ! snapshot . ms ) {
210
215
if ( snapshot . block ) {
211
216
throw new Error ( ERR_INCOMPLETE_BLOCK ) ;
@@ -235,7 +240,9 @@ var DashGov = ("object" === typeof module && exports) || {};
235
240
now = Date . now ( ) ,
236
241
currentBlock = 0 ,
237
242
secondsPerBlock = 0 ,
243
+ cycles = 3 ,
238
244
) {
245
+ console . log ( "CURRENT HEIGHT:" , currentBlock ) ;
239
246
if ( ! currentBlock ) {
240
247
let d = new Date ( MONTHLY_SUPERBLOCK_61_DATE ) ;
241
248
let then = d . valueOf ( ) ;
@@ -259,17 +266,73 @@ var DashGov = ("object" === typeof module && exports) || {};
259
266
} ) ;
260
267
}
261
268
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 ) ;
265
329
266
- let superblockDelta = superblockHeight - currentBlock ;
330
+ let superblockDelta = superblockHeight - block ;
267
331
// let superblockDeltaMs = superblockDelta * SECONDS_PER_BLOCK_ESTIMATE * 1000;
268
332
let superblockDeltaMs = superblockDelta * secondsPerBlock * 1000 ;
269
- // let voteEndDelta = superblockDelta - VOTE_OFFSET;
270
- // let voteEndDeltaMs = voteEndDelta * SECONDS_PER_BLOCK_ESTIMATE * 1000;
271
333
272
- let d = new Date ( ) ;
334
+ console . log ( ms , superblockDeltaMs ) ;
335
+ let d = new Date ( ms ) ;
273
336
d . setUTCMilliseconds ( 0 ) ;
274
337
275
338
d . setUTCMilliseconds ( superblockDeltaMs ) ;
@@ -286,9 +349,9 @@ var DashGov = ("object" === typeof module && exports) || {};
286
349
voteIso : vtts ,
287
350
voteMs : vtms ,
288
351
voteDelta : superblockDelta - VOTE_OFFSET_BLOCKS ,
289
- voteDeltaMs : superblockDelta ,
352
+ voteDeltaMs : superblockDeltaMs - VOTE_OFFSET_MS ,
290
353
superblockHeight : superblockHeight ,
291
- superblockDelta : superblockDeltaMs ,
354
+ superblockDelta : superblockDelta ,
292
355
superblockIso : sbts ,
293
356
superblockMs : sbms ,
294
357
superblockDeltaMs : superblockDeltaMs ,
0 commit comments