67
67
/** @type {Gov } */
68
68
//@ts -ignore
69
69
var DashGov = ( "object" === typeof module && exports ) || { } ;
70
- ( function ( window , GObj ) {
70
+ ( function ( window , DashGov ) {
71
71
"use strict" ;
72
72
73
73
// Adapted from
@@ -82,18 +82,18 @@ var DashGov = ("object" === typeof module && exports) || {};
82
82
83
83
let textEncoder = new TextEncoder ( ) ;
84
84
85
- GObj . _type = 0b0000010 ; // from SER_GETHASH (bitwise enum)
86
- GObj . _typeBytes = Uint8Array . from ( [ 0b0000010 ] ) ;
87
- GObj . _protocalVersion = 70231 ; // 0x00011257 (BE) => 0x57120100 (LE)
88
- GObj . _protocalVersionBytes = Uint8Array . from ( [ 0x57 , 0x12 , 0x01 , 0x00 ] ) ;
85
+ DashGov . _type = 0b0000010 ; // from SER_GETHASH (bitwise enum)
86
+ DashGov . _typeBytes = Uint8Array . from ( [ 0b0000010 ] ) ;
87
+ DashGov . _protocalVersion = 70231 ; // 0x00011257 (BE) => 0x57120100 (LE)
88
+ DashGov . _protocalVersionBytes = Uint8Array . from ( [ 0x57 , 0x12 , 0x01 , 0x00 ] ) ;
89
89
90
- GObj . utils = { } ;
90
+ DashGov . utils = { } ;
91
91
92
92
/**
93
93
* @param {Uint8Array } bytes
94
94
* @returns {String } hex
95
95
*/
96
- GObj . utils . bytesToHex = function bytesToHex ( bytes ) {
96
+ DashGov . utils . bytesToHex = function bytesToHex ( bytes ) {
97
97
let hexes = [ ] ;
98
98
for ( let i = 0 ; i < bytes . length ; i += 1 ) {
99
99
let b = bytes [ i ] ;
@@ -105,6 +105,37 @@ var DashGov = ("object" === typeof module && exports) || {};
105
105
return hex ;
106
106
} ;
107
107
108
+ /**
109
+ * @param {Number } ms
110
+ */
111
+ DashGov . utils . sleep = async function ( ms ) {
112
+ return new Promise ( function ( resolve ) {
113
+ setTimeout ( resolve , ms ) ;
114
+ } ) ;
115
+ } ;
116
+
117
+ /**
118
+ * @param {Uint8Array } bytes - i.e. serialized gobj bytes
119
+ */
120
+ DashGov . utils . doubleSha256 = async function ( bytes ) {
121
+ let hash1 = await Crypto . subtle . digest ( "SHA-256" , bytes ) ;
122
+ let hash2 = await Crypto . subtle . digest ( "SHA-256" , hash1 ) ;
123
+ let gobjHash = new Uint8Array ( hash2 ) ;
124
+
125
+ return gobjHash ;
126
+ } ;
127
+
128
+ /**
129
+ * @param {Uint8Array } hashBytes
130
+ */
131
+ DashGov . utils . hashToId = function ( hashBytes ) {
132
+ let reverseBytes = hashBytes . slice ( ) ;
133
+ reverseBytes . reverse ( ) ;
134
+
135
+ let id = DashGov . utils . bytesToHex ( reverseBytes ) ;
136
+ return id ;
137
+ } ;
138
+
108
139
/**
109
140
* Gets the number of bytes to store the number with VarInt "compression"
110
141
* - 1 byte for 0-252 (Uint8)
@@ -114,7 +145,7 @@ var DashGov = ("object" === typeof module && exports) || {};
114
145
* @param {Number } n
115
146
* @returns {1|3|5|9 }
116
147
*/
117
- GObj . utils . toVarIntSize = function ( n ) {
148
+ DashGov . utils . toVarIntSize = function ( n ) {
118
149
if ( n <= VARINT_8_MAX ) {
119
150
return 1 ;
120
151
}
@@ -161,13 +192,13 @@ var DashGov = ("object" === typeof module && exports) || {};
161
192
/**
162
193
* @param {GObject } gobj
163
194
*/
164
- GObj . serializeForCollateralTx = function ( {
195
+ DashGov . serializeForCollateralTx = function ( {
165
196
hashParent = 0 ,
166
197
revision = 1 ,
167
198
time,
168
199
dataHex,
169
200
} ) {
170
- const varIntSize = GObj . utils . toVarIntSize ( dataHex . length ) ;
201
+ const varIntSize = DashGov . utils . toVarIntSize ( dataHex . length ) ;
171
202
172
203
const dataLen =
173
204
32 + // hashParent
@@ -236,6 +267,7 @@ var DashGov = ("object" === typeof module && exports) || {};
236
267
const END_EPOCH_MS_AFTER_SUPERBLOCK = 4 * 24 * 60 * 60 * 1000 ; // after superblock
237
268
DashGov . PROPOSAL_LEAD_MS = PROPOSAL_LEAD_MS ;
238
269
DashGov . SUPERBLOCK_INTERVAL = SUPERBLOCK_INTERVAL ;
270
+ DashGov . PROPOSAL_FEE_RATE = 100000000 / 1000 ; // 1 DASH per KB
239
271
240
272
// not used because the actual average at any time is always closer to 157.5
241
273
//const SECONDS_PER_BLOCK_ESTIMATE = 155;
@@ -252,7 +284,7 @@ var DashGov = ("object" === typeof module && exports) || {};
252
284
* @param {Snapshot } root
253
285
* @returns {Float64 } - fractional seconds
254
286
*/
255
- GObj . measureSecondsPerBlock = function ( snapshot , root ) {
287
+ DashGov . measureSecondsPerBlock = function ( snapshot , root ) {
256
288
let blockDelta = snapshot . block - root . block ;
257
289
let timeDelta = snapshot . ms - root . ms ;
258
290
let msPerBlock = timeDelta / blockDelta ;
@@ -265,7 +297,7 @@ var DashGov = ("object" === typeof module && exports) || {};
265
297
* @param {Snapshot } [snapshot] - defaults to mainnet monthly superblock 61
266
298
* @returns {Float64 } - fractional seconds
267
299
*/
268
- GObj . estimateSecondsPerBlock = function ( snapshot ) {
300
+ DashGov . estimateSecondsPerBlock = function ( snapshot ) {
269
301
if ( ! snapshot ) {
270
302
snapshot = {
271
303
block : MONTHLY_SUPERBLOCK_61 ,
@@ -277,15 +309,15 @@ var DashGov = ("object" === typeof module && exports) || {};
277
309
ms : Date . parse ( MONTHLY_SUPERBLOCK_01_DATE ) ,
278
310
} ;
279
311
280
- let spb = GObj . measureSecondsPerBlock ( snapshot , root ) ;
312
+ let spb = DashGov . measureSecondsPerBlock ( snapshot , root ) ;
281
313
return spb ;
282
314
} ;
283
315
284
316
/**
285
317
* @param {Uint53 } ms - the current time
286
318
* @param {Float64 } secondsPerBlock
287
319
*/
288
- GObj . estimateBlockHeight = function ( ms , secondsPerBlock ) {
320
+ DashGov . estimateBlockHeight = function ( ms , secondsPerBlock ) {
289
321
let then = Date . parse ( MONTHLY_SUPERBLOCK_61_DATE ) ;
290
322
let delta = ms - then ;
291
323
let deltaS = delta / 1000 ;
@@ -301,7 +333,7 @@ var DashGov = ("object" === typeof module && exports) || {};
301
333
* @param {Uint32 } startPeriod
302
334
* @param {Uint32 } endPeriod
303
335
*/
304
- GObj . selectEstimates = function ( estimates , startPeriod , endPeriod ) {
336
+ DashGov . selectEstimates = function ( estimates , startPeriod , endPeriod ) {
305
337
let startEstimate ;
306
338
let endEstimate ;
307
339
@@ -332,15 +364,15 @@ var DashGov = ("object" === typeof module && exports) || {};
332
364
return { start : startEstimate , end : endEstimate } ;
333
365
} ;
334
366
335
- GObj . proposal = { } ;
367
+ DashGov . proposal = { } ;
336
368
337
369
/**
338
370
* @param {Object } selected
339
371
* @param {Estimate } selected.start
340
372
* @param {Estimate } selected.end
341
373
* @param {DashGov.GObjectData } proposalData
342
374
*/
343
- GObj . proposal . draftJson = function ( selected , proposalData ) {
375
+ DashGov . proposal . draftJson = function ( selected , proposalData ) {
344
376
let startEpoch = selected . start . startMs / 1000 ;
345
377
startEpoch = Math . round ( startEpoch ) ;
346
378
@@ -368,9 +400,9 @@ var DashGov = ("object" === typeof module && exports) || {};
368
400
* @param {GObjectData } data - will be sorted and hex-ified
369
401
* @param {GObject } [gobj] - override values
370
402
*/
371
- GObj . proposal . draft = function ( now , startEpochMs , data , gobj ) {
372
- let dataHex = gobj ?. dataHex || GObj . proposal . sortAndEncodeJson ( data ) ;
373
- let time = GObj . proposal . _selectKnownTime ( now , startEpochMs ) ;
403
+ DashGov . proposal . draft = function ( now , startEpochMs , data , gobj ) {
404
+ let dataHex = gobj ?. dataHex || DashGov . proposal . sortAndEncodeJson ( data ) ;
405
+ let time = DashGov . proposal . _selectKnownTime ( now , startEpochMs ) ;
374
406
375
407
/** @type {DashGov.GObject } */
376
408
let normalGObj = {
@@ -391,7 +423,7 @@ var DashGov = ("object" === typeof module && exports) || {};
391
423
/**
392
424
* @param {DashGov.GObjectData } normalizedData
393
425
*/
394
- GObj . proposal . sortAndEncodeJson = function ( normalizedData ) {
426
+ DashGov . proposal . sortAndEncodeJson = function ( normalizedData ) {
395
427
let keys = Object . keys ( normalizedData ) ;
396
428
keys . sort ( ) ;
397
429
@@ -418,7 +450,7 @@ var DashGov = ("object" === typeof module && exports) || {};
418
450
* @param {Uint53 } now
419
451
* @param {Uint53 } startMs
420
452
*/
421
- GObj . proposal . _selectKnownTime = function ( now , startMs ) {
453
+ DashGov . proposal . _selectKnownTime = function ( now , startMs ) {
422
454
let startEpochDate = new Date ( startMs ) ;
423
455
let today = new Date ( ) ;
424
456
if ( today < startEpochDate ) {
@@ -441,7 +473,7 @@ var DashGov = ("object" === typeof module && exports) || {};
441
473
* we just get rid of it.
442
474
* @param {Uint53 } ms
443
475
*/
444
- GObj . proposal . _roundDownToHour = function ( ms ) {
476
+ DashGov . proposal . _roundDownToHour = function ( ms ) {
445
477
let timeF = ms / msToHours ;
446
478
let time = Math . floor ( timeF ) ;
447
479
ms = time * msToHours ;
@@ -451,24 +483,13 @@ var DashGov = ("object" === typeof module && exports) || {};
451
483
/**
452
484
* @param {Uint53 } ms
453
485
*/
454
- GObj . proposal . _roundUpToHour = function ( ms ) {
486
+ DashGov . proposal . _roundUpToHour = function ( ms ) {
455
487
let timeF = ms / msToHours ;
456
488
let time = Math . ceil ( timeF ) ;
457
489
ms = time * msToHours ;
458
490
return ms ;
459
491
} ;
460
492
461
- /**
462
- * @param {Uint8Array } gobjBytes - serialized gobj bytes
463
- */
464
- GObj . proposal . doubleSha256 = async function ( gobjBytes ) {
465
- let hash1 = await Crypto . subtle . digest ( "SHA-256" , gobjBytes ) ;
466
- let hash2 = await Crypto . subtle . digest ( "SHA-256" , hash1 ) ;
467
- let gobjHash = new Uint8Array ( hash2 ) ;
468
-
469
- return gobjHash ;
470
- } ;
471
-
472
493
/**
473
494
* Note: since we're dealing with estimates that are typically reliable
474
495
* within an hour (and often even within 15 minutes), this may
@@ -479,7 +500,7 @@ var DashGov = ("object" === typeof module && exports) || {};
479
500
* @param {Float64 } [secondsPerBlock] - typically close to 157.6
480
501
* @returns {Estimates } - the last, due, and upcoming proposal cycles
481
502
*/
482
- GObj . estimateProposalCycles = function (
503
+ DashGov . estimateProposalCycles = function (
483
504
cycles = 3 ,
484
505
snapshot = null ,
485
506
secondsPerBlock = 0 ,
@@ -491,16 +512,16 @@ var DashGov = ("object" === typeof module && exports) || {};
491
512
if ( currentBlock ) {
492
513
snapshot = { block : currentBlock , ms : now } ;
493
514
}
494
- secondsPerBlock = GObj . estimateSecondsPerBlock ( snapshot ) ;
515
+ secondsPerBlock = DashGov . estimateSecondsPerBlock ( snapshot ) ;
495
516
}
496
517
if ( ! currentBlock ) {
497
- currentBlock = GObj . estimateBlockHeight ( now , secondsPerBlock ) ;
518
+ currentBlock = DashGov . estimateBlockHeight ( now , secondsPerBlock ) ;
498
519
}
499
520
500
521
/** @type {Array<Estimate> } */
501
522
let estimates = [ ] ;
502
523
for ( let i = 0 ; i <= cycles + 1 ; i += 1 ) {
503
- let estimate = GObj . estimateNthNextGovCycle (
524
+ let estimate = DashGov . estimateNthNextGovCycle (
504
525
{ block : currentBlock , ms : now } ,
505
526
secondsPerBlock ,
506
527
i ,
@@ -540,16 +561,16 @@ var DashGov = ("object" === typeof module && exports) || {};
540
561
* @param {Uint53 } [offset] - how many superblocks in the future
541
562
* @returns {Estimate } - details about the current governance cycle
542
563
*/
543
- GObj . estimateNthNextGovCycle = function (
564
+ DashGov . estimateNthNextGovCycle = function (
544
565
snapshot ,
545
566
secondsPerBlock ,
546
567
offset = 0 ,
547
568
) {
548
569
if ( ! secondsPerBlock ) {
549
- secondsPerBlock = GObj . estimateSecondsPerBlock ( snapshot ) ;
570
+ secondsPerBlock = DashGov . estimateSecondsPerBlock ( snapshot ) ;
550
571
}
551
572
552
- let superblockHeight = GObj . getNthNextSuperblock ( snapshot . block , offset ) ;
573
+ let superblockHeight = DashGov . getNthNextSuperblock ( snapshot . block , offset ) ;
553
574
554
575
let superblockDelta = superblockHeight - snapshot . block ;
555
576
let superblockDeltaMs = superblockDelta * secondsPerBlock * 1000 ;
@@ -567,11 +588,11 @@ var DashGov = ("object" === typeof module && exports) || {};
567
588
let vtts = d . toISOString ( ) ;
568
589
569
590
let startMs = vtms - START_EPOCH_MS_BEFORE_VOTE ;
570
- startMs = GObj . proposal . _roundDownToHour ( startMs ) ;
591
+ startMs = DashGov . proposal . _roundDownToHour ( startMs ) ;
571
592
let startTime = new Date ( startMs ) ;
572
593
573
594
let endMs = sbms + END_EPOCH_MS_AFTER_SUPERBLOCK ;
574
- endMs = GObj . proposal . _roundUpToHour ( endMs ) ;
595
+ endMs = DashGov . proposal . _roundUpToHour ( endMs ) ;
575
596
let endTime = new Date ( endMs ) ;
576
597
577
598
return {
@@ -598,7 +619,7 @@ var DashGov = ("object" === typeof module && exports) || {};
598
619
* @param {Uint53 } offset - 0 (current / previous), 1 (next), 2, 3, nth
599
620
* @returns {Uint53 } - the superblock after the given height
600
621
*/
601
- GObj . getNthNextSuperblock = function ( height , offset ) {
622
+ DashGov . getNthNextSuperblock = function ( height , offset ) {
602
623
let superblockCount = height / SUPERBLOCK_INTERVAL ;
603
624
superblockCount = Math . floor ( superblockCount ) ;
604
625
@@ -609,7 +630,7 @@ var DashGov = ("object" === typeof module && exports) || {};
609
630
} ;
610
631
611
632
//@ts -ignore
612
- window . DashGov = GObj ;
633
+ window . DashGov = DashGov ;
613
634
} ) ( globalThis . window || { } , DashGov ) ;
614
635
if ( "object" === typeof module ) {
615
636
module . exports = DashGov ;
0 commit comments