Skip to content

Commit 99cc17f

Browse files
committed
WIP: library
1 parent f974fd3 commit 99cc17f

File tree

1 file changed

+67
-46
lines changed

1 file changed

+67
-46
lines changed

dashgov.js

Lines changed: 67 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
/** @type {Gov} */
6868
//@ts-ignore
6969
var DashGov = ("object" === typeof module && exports) || {};
70-
(function (window, GObj) {
70+
(function (window, DashGov) {
7171
"use strict";
7272

7373
// Adapted from
@@ -82,18 +82,18 @@ var DashGov = ("object" === typeof module && exports) || {};
8282

8383
let textEncoder = new TextEncoder();
8484

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]);
8989

90-
GObj.utils = {};
90+
DashGov.utils = {};
9191

9292
/**
9393
* @param {Uint8Array} bytes
9494
* @returns {String} hex
9595
*/
96-
GObj.utils.bytesToHex = function bytesToHex(bytes) {
96+
DashGov.utils.bytesToHex = function bytesToHex(bytes) {
9797
let hexes = [];
9898
for (let i = 0; i < bytes.length; i += 1) {
9999
let b = bytes[i];
@@ -105,6 +105,37 @@ var DashGov = ("object" === typeof module && exports) || {};
105105
return hex;
106106
};
107107

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+
108139
/**
109140
* Gets the number of bytes to store the number with VarInt "compression"
110141
* - 1 byte for 0-252 (Uint8)
@@ -114,7 +145,7 @@ var DashGov = ("object" === typeof module && exports) || {};
114145
* @param {Number} n
115146
* @returns {1|3|5|9}
116147
*/
117-
GObj.utils.toVarIntSize = function (n) {
148+
DashGov.utils.toVarIntSize = function (n) {
118149
if (n <= VARINT_8_MAX) {
119150
return 1;
120151
}
@@ -161,13 +192,13 @@ var DashGov = ("object" === typeof module && exports) || {};
161192
/**
162193
* @param {GObject} gobj
163194
*/
164-
GObj.serializeForCollateralTx = function ({
195+
DashGov.serializeForCollateralTx = function ({
165196
hashParent = 0,
166197
revision = 1,
167198
time,
168199
dataHex,
169200
}) {
170-
const varIntSize = GObj.utils.toVarIntSize(dataHex.length);
201+
const varIntSize = DashGov.utils.toVarIntSize(dataHex.length);
171202

172203
const dataLen =
173204
32 + // hashParent
@@ -236,6 +267,7 @@ var DashGov = ("object" === typeof module && exports) || {};
236267
const END_EPOCH_MS_AFTER_SUPERBLOCK = 4 * 24 * 60 * 60 * 1000; // after superblock
237268
DashGov.PROPOSAL_LEAD_MS = PROPOSAL_LEAD_MS;
238269
DashGov.SUPERBLOCK_INTERVAL = SUPERBLOCK_INTERVAL;
270+
DashGov.PROPOSAL_FEE_RATE = 100000000 / 1000; // 1 DASH per KB
239271

240272
// not used because the actual average at any time is always closer to 157.5
241273
//const SECONDS_PER_BLOCK_ESTIMATE = 155;
@@ -252,7 +284,7 @@ var DashGov = ("object" === typeof module && exports) || {};
252284
* @param {Snapshot} root
253285
* @returns {Float64} - fractional seconds
254286
*/
255-
GObj.measureSecondsPerBlock = function (snapshot, root) {
287+
DashGov.measureSecondsPerBlock = function (snapshot, root) {
256288
let blockDelta = snapshot.block - root.block;
257289
let timeDelta = snapshot.ms - root.ms;
258290
let msPerBlock = timeDelta / blockDelta;
@@ -265,7 +297,7 @@ var DashGov = ("object" === typeof module && exports) || {};
265297
* @param {Snapshot} [snapshot] - defaults to mainnet monthly superblock 61
266298
* @returns {Float64} - fractional seconds
267299
*/
268-
GObj.estimateSecondsPerBlock = function (snapshot) {
300+
DashGov.estimateSecondsPerBlock = function (snapshot) {
269301
if (!snapshot) {
270302
snapshot = {
271303
block: MONTHLY_SUPERBLOCK_61,
@@ -277,15 +309,15 @@ var DashGov = ("object" === typeof module && exports) || {};
277309
ms: Date.parse(MONTHLY_SUPERBLOCK_01_DATE),
278310
};
279311

280-
let spb = GObj.measureSecondsPerBlock(snapshot, root);
312+
let spb = DashGov.measureSecondsPerBlock(snapshot, root);
281313
return spb;
282314
};
283315

284316
/**
285317
* @param {Uint53} ms - the current time
286318
* @param {Float64} secondsPerBlock
287319
*/
288-
GObj.estimateBlockHeight = function (ms, secondsPerBlock) {
320+
DashGov.estimateBlockHeight = function (ms, secondsPerBlock) {
289321
let then = Date.parse(MONTHLY_SUPERBLOCK_61_DATE);
290322
let delta = ms - then;
291323
let deltaS = delta / 1000;
@@ -301,7 +333,7 @@ var DashGov = ("object" === typeof module && exports) || {};
301333
* @param {Uint32} startPeriod
302334
* @param {Uint32} endPeriod
303335
*/
304-
GObj.selectEstimates = function (estimates, startPeriod, endPeriod) {
336+
DashGov.selectEstimates = function (estimates, startPeriod, endPeriod) {
305337
let startEstimate;
306338
let endEstimate;
307339

@@ -332,15 +364,15 @@ var DashGov = ("object" === typeof module && exports) || {};
332364
return { start: startEstimate, end: endEstimate };
333365
};
334366

335-
GObj.proposal = {};
367+
DashGov.proposal = {};
336368

337369
/**
338370
* @param {Object} selected
339371
* @param {Estimate} selected.start
340372
* @param {Estimate} selected.end
341373
* @param {DashGov.GObjectData} proposalData
342374
*/
343-
GObj.proposal.draftJson = function (selected, proposalData) {
375+
DashGov.proposal.draftJson = function (selected, proposalData) {
344376
let startEpoch = selected.start.startMs / 1000;
345377
startEpoch = Math.round(startEpoch);
346378

@@ -368,9 +400,9 @@ var DashGov = ("object" === typeof module && exports) || {};
368400
* @param {GObjectData} data - will be sorted and hex-ified
369401
* @param {GObject} [gobj] - override values
370402
*/
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);
374406

375407
/** @type {DashGov.GObject} */
376408
let normalGObj = {
@@ -391,7 +423,7 @@ var DashGov = ("object" === typeof module && exports) || {};
391423
/**
392424
* @param {DashGov.GObjectData} normalizedData
393425
*/
394-
GObj.proposal.sortAndEncodeJson = function (normalizedData) {
426+
DashGov.proposal.sortAndEncodeJson = function (normalizedData) {
395427
let keys = Object.keys(normalizedData);
396428
keys.sort();
397429

@@ -418,7 +450,7 @@ var DashGov = ("object" === typeof module && exports) || {};
418450
* @param {Uint53} now
419451
* @param {Uint53} startMs
420452
*/
421-
GObj.proposal._selectKnownTime = function (now, startMs) {
453+
DashGov.proposal._selectKnownTime = function (now, startMs) {
422454
let startEpochDate = new Date(startMs);
423455
let today = new Date();
424456
if (today < startEpochDate) {
@@ -441,7 +473,7 @@ var DashGov = ("object" === typeof module && exports) || {};
441473
* we just get rid of it.
442474
* @param {Uint53} ms
443475
*/
444-
GObj.proposal._roundDownToHour = function (ms) {
476+
DashGov.proposal._roundDownToHour = function (ms) {
445477
let timeF = ms / msToHours;
446478
let time = Math.floor(timeF);
447479
ms = time * msToHours;
@@ -451,24 +483,13 @@ var DashGov = ("object" === typeof module && exports) || {};
451483
/**
452484
* @param {Uint53} ms
453485
*/
454-
GObj.proposal._roundUpToHour = function (ms) {
486+
DashGov.proposal._roundUpToHour = function (ms) {
455487
let timeF = ms / msToHours;
456488
let time = Math.ceil(timeF);
457489
ms = time * msToHours;
458490
return ms;
459491
};
460492

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-
472493
/**
473494
* Note: since we're dealing with estimates that are typically reliable
474495
* within an hour (and often even within 15 minutes), this may
@@ -479,7 +500,7 @@ var DashGov = ("object" === typeof module && exports) || {};
479500
* @param {Float64} [secondsPerBlock] - typically close to 157.6
480501
* @returns {Estimates} - the last, due, and upcoming proposal cycles
481502
*/
482-
GObj.estimateProposalCycles = function (
503+
DashGov.estimateProposalCycles = function (
483504
cycles = 3,
484505
snapshot = null,
485506
secondsPerBlock = 0,
@@ -491,16 +512,16 @@ var DashGov = ("object" === typeof module && exports) || {};
491512
if (currentBlock) {
492513
snapshot = { block: currentBlock, ms: now };
493514
}
494-
secondsPerBlock = GObj.estimateSecondsPerBlock(snapshot);
515+
secondsPerBlock = DashGov.estimateSecondsPerBlock(snapshot);
495516
}
496517
if (!currentBlock) {
497-
currentBlock = GObj.estimateBlockHeight(now, secondsPerBlock);
518+
currentBlock = DashGov.estimateBlockHeight(now, secondsPerBlock);
498519
}
499520

500521
/** @type {Array<Estimate>} */
501522
let estimates = [];
502523
for (let i = 0; i <= cycles + 1; i += 1) {
503-
let estimate = GObj.estimateNthNextGovCycle(
524+
let estimate = DashGov.estimateNthNextGovCycle(
504525
{ block: currentBlock, ms: now },
505526
secondsPerBlock,
506527
i,
@@ -540,16 +561,16 @@ var DashGov = ("object" === typeof module && exports) || {};
540561
* @param {Uint53} [offset] - how many superblocks in the future
541562
* @returns {Estimate} - details about the current governance cycle
542563
*/
543-
GObj.estimateNthNextGovCycle = function (
564+
DashGov.estimateNthNextGovCycle = function (
544565
snapshot,
545566
secondsPerBlock,
546567
offset = 0,
547568
) {
548569
if (!secondsPerBlock) {
549-
secondsPerBlock = GObj.estimateSecondsPerBlock(snapshot);
570+
secondsPerBlock = DashGov.estimateSecondsPerBlock(snapshot);
550571
}
551572

552-
let superblockHeight = GObj.getNthNextSuperblock(snapshot.block, offset);
573+
let superblockHeight = DashGov.getNthNextSuperblock(snapshot.block, offset);
553574

554575
let superblockDelta = superblockHeight - snapshot.block;
555576
let superblockDeltaMs = superblockDelta * secondsPerBlock * 1000;
@@ -567,11 +588,11 @@ var DashGov = ("object" === typeof module && exports) || {};
567588
let vtts = d.toISOString();
568589

569590
let startMs = vtms - START_EPOCH_MS_BEFORE_VOTE;
570-
startMs = GObj.proposal._roundDownToHour(startMs);
591+
startMs = DashGov.proposal._roundDownToHour(startMs);
571592
let startTime = new Date(startMs);
572593

573594
let endMs = sbms + END_EPOCH_MS_AFTER_SUPERBLOCK;
574-
endMs = GObj.proposal._roundUpToHour(endMs);
595+
endMs = DashGov.proposal._roundUpToHour(endMs);
575596
let endTime = new Date(endMs);
576597

577598
return {
@@ -598,7 +619,7 @@ var DashGov = ("object" === typeof module && exports) || {};
598619
* @param {Uint53} offset - 0 (current / previous), 1 (next), 2, 3, nth
599620
* @returns {Uint53} - the superblock after the given height
600621
*/
601-
GObj.getNthNextSuperblock = function (height, offset) {
622+
DashGov.getNthNextSuperblock = function (height, offset) {
602623
let superblockCount = height / SUPERBLOCK_INTERVAL;
603624
superblockCount = Math.floor(superblockCount);
604625

@@ -609,7 +630,7 @@ var DashGov = ("object" === typeof module && exports) || {};
609630
};
610631

611632
//@ts-ignore
612-
window.DashGov = GObj;
633+
window.DashGov = DashGov;
613634
})(globalThis.window || {}, DashGov);
614635
if ("object" === typeof module) {
615636
module.exports = DashGov;

0 commit comments

Comments
 (0)