-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashgov.js
638 lines (553 loc) · 16.9 KB
/
dashgov.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
* This serialization is used exclusively for creating a hash to place in the OP_RETURN memo
* of the collateral transaction.
*
* As such, it does NOT match the MN gobject serialization.
* - NO collateral tx id (this is for that)
* - NO masternodeOutpoint (this is for that)
* - NO bls data signature (happens on MN)
*
* However, it does include all pieces of data required to verify authenticity from proposer.
* @typedef GObject
* @prop {0} [hashParent] - not implemented, Uint8Array of 0s (32 bytes)
* @prop {1} [revision] - not implemented, always 1 (4 bytes)
* @prop {BigInt|Uint53} time - seconds since epoch (8 bytes)
* @prop {String} dataHex - variable
* @prop {null} [masternodeOutpoint] - ??
* @prop {null} [collateralTxId] - 32 bytes of 0x00s
* @prop {null} [collateralTxOutputIndex] - 4 bytes of 0xffs
* @prop {null} [signature] - 0 bytes
* @returns {Uint8Array}
*/
/**
* @typedef GObjectData
* @prop {Uint53} [end_epoch] - whole seconds since epoch (like web-standard `exp`)
* @prop {String} name - kebab case (no spaces)
* @prop {String} payment_address - base58-encoded p2pkh
* @prop {Uint32} payment_amount - in whole DASH
* @prop {Uint53} [start_epoch] - whole seconds since epoch (like web-standard `iat`)
* @prop {Uint32} [type] - TODO
* @prop {String} url - conventionally dashcentral, with page the same as the 'name'
*/
/**
* @typedef Snapshot
* @prop {Uint53} block - the block to be used for calculation
* @prop {Uint53} ms - the time of that block in ms
*/
/**
* @typedef Estimate
* @prop {String} startIso
* @prop {Uint53} startMs - suggested time to make proposal visible
* @prop {Uint53} voteHeight
* @prop {Uint53} voteDelta
* @prop {String} voteIso - date in ISO format
* @prop {Uint53} voteMs
* @prop {Uint53} voteDeltaMs
* @prop {Uint53} superblockHeight
* @prop {Uint53} superblockDelta
* @prop {String} superblockIso - date in ISO format
* @prop {Uint53} superblockMs
* @prop {Uint53} superblockDeltaMs
* @prop {String} endIso
* @prop {Uint53} endMs - suggested time to make proposal non-visible
*/
/**
* @typedef Estimates
* @prop {Estimate} last - the most recent superblock
* @prop {Estimate?} lameduck - the current voting period, if close to deadline
* @prop {Array<Estimate>} upcoming - future voting periods
*/
let DashGov = {};
// Adapted from
// github.com/dashpay/dash/tree/develop/src/governance/common.cpp
let Crypto = globalThis.crypto;
const LITTLE_ENDIAN = true;
const VARINT_8_MAX = 252;
const UINT_16_MAX = 65535;
const UINT_32_MAX = 4294967295;
let textEncoder = new TextEncoder();
DashGov._type = 0b0000010; // from SER_GETHASH (bitwise enum)
DashGov._typeBytes = Uint8Array.from([0b0000010]);
DashGov._protocalVersion = 70231; // 0x00011257 (BE) => 0x57120100 (LE)
DashGov._protocalVersionBytes = Uint8Array.from([0x57, 0x12, 0x01, 0x00]);
DashGov.utils = {};
/**
* @param {Uint8Array} bytes
* @returns {String} hex
*/
DashGov.utils.bytesToHex = function bytesToHex(bytes) {
let hexes = [];
for (let i = 0; i < bytes.length; i += 1) {
let b = bytes[i];
let h = b.toString(16);
h = h.padStart(2, "0");
hexes.push(h);
}
let hex = hexes.join("");
return hex;
};
/**
* @param {Number} ms
*/
DashGov.utils.sleep = async function (ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
};
/**
* @param {Uint8Array} bytes - i.e. serialized gobj bytes
*/
DashGov.utils.doubleSha256 = async function (bytes) {
let hash1 = await Crypto.subtle.digest("SHA-256", bytes);
let hash2 = await Crypto.subtle.digest("SHA-256", hash1);
let gobjHash = new Uint8Array(hash2);
return gobjHash;
};
/**
* @param {Uint8Array} hashBytes
*/
DashGov.utils.hashToId = function (hashBytes) {
let reverseBytes = hashBytes.slice();
reverseBytes.reverse();
let id = DashGov.utils.bytesToHex(reverseBytes);
return id;
};
/**
* Gets the number of bytes to store the number with VarInt "compression"
* - 1 byte for 0-252 (Uint8)
* - 1+2 bytes for 253 + Uint16
* - 1+4 bytes for 254 + Uint32
* - 1+8 bytes for 255 + Uint64
* @param {Number} n
* @returns {1|3|5|9}
*/
DashGov.utils.toVarIntSize = function (n) {
if (n <= VARINT_8_MAX) {
return 1;
}
if (n <= UINT_16_MAX) {
return 3;
}
if (n <= UINT_32_MAX) {
return 5;
}
return 9;
};
/**
* Writes `n` to `DataView` as a VarInt ("compressed" int).
* @param {DataView} dv
* @param {Number} offset
* @param {Number} n
* @returns void
*/
function writeVarInt(dv, offset, n) {
if (n <= VARINT_8_MAX) {
dv.setUint8(offset, n);
return;
}
let size;
if (n <= UINT_16_MAX) {
size = 253;
} else if (n <= UINT_32_MAX) {
size = 254;
} else {
size = 255;
}
dv.setUint8(offset, size);
offset += 1;
let bigN = BigInt(n);
dv.setBigUint64(offset, bigN, LITTLE_ENDIAN);
}
/**
* @param {GObject} gobj
*/
DashGov.serializeForBurnTx = function ({
hashParent = 0,
revision = 1,
time,
dataHex,
}) {
const varIntSize = DashGov.utils.toVarIntSize(dataHex.length);
const dataLen =
32 + // hashParent
4 + // revision
8 + // time
varIntSize + // compacted length header for HexStr(vchData)
dataHex.length + // HexStr(vchData)
32 +
4 + // masterNodeOutpoint (not used, so these bytes are the defaults)
1 +
4 + // dummy values to match old hashing
1; // (varint) size of `vchSig` (always 1 byte to represent 0)
const bytes = new Uint8Array(dataLen);
const dv = new DataView(bytes.buffer);
let offset = 0;
if (hashParent) {
bytes.set(hashParent, offset);
}
offset += 32;
dv.setInt32(offset, revision, LITTLE_ENDIAN);
offset += 4;
let bigTime = BigInt(time);
dv.setBigInt64(offset, bigTime, LITTLE_ENDIAN);
offset += 8;
void writeVarInt(dv, offset, dataHex.length);
offset += varIntSize;
let dataHexBytes = textEncoder.encode(dataHex);
bytes.set(dataHexBytes, offset);
offset += dataHex.length;
{
// masternodeOutpointId (hash + index) is required for legacy reasons,
// but not used for collateral serialization
offset += 32;
// Write out default mastNode `n` (index)
let masternodeOutpointIndex = 0xffffffff;
dv.setUint32(offset, masternodeOutpointIndex, LITTLE_ENDIAN);
offset += 4;
// adding dummy values here to match old hashing
offset += 1;
dv.setUint32(offset, 0xffffffff, LITTLE_ENDIAN);
offset += 4;
}
// the trailing 0 byte represents the VarInt Size of the vchSig,
// which is always 0 for collateral serialization
offset += 1;
return bytes;
};
// TODO move to a nice place
const SUPERBLOCK_INTERVAL = 16616;
const VOTE_LEAD_BLOCKS = 1662;
// these are chosen by reason rather than by specification
const PROPOSAL_LEAD_MS = 6 * 24 * 60 * 60 * 1000;
const START_EPOCH_MS_BEFORE_VOTE = 23 * 24 * 60 * 60 * 1000;
const END_EPOCH_MS_AFTER_SUPERBLOCK = 4 * 24 * 60 * 60 * 1000; // after superblock
DashGov.PROPOSAL_LEAD_MS = PROPOSAL_LEAD_MS;
DashGov.SUPERBLOCK_INTERVAL = SUPERBLOCK_INTERVAL;
// not used because the actual average at any time is always closer to 157.5
//const SECONDS_PER_BLOCK_ESTIMATE = 155;
DashGov._AVG_SECS_PER_BLOCK = 157.5816652623977;
// used to calculate ~5 year (~60 month) averages
const MONTHLY_SUPERBLOCK_01_DATE = "2017-03-05T20:16:05Z";
const MONTHLY_SUPERBLOCK_01 = 631408;
const MONTHLY_SUPERBLOCK_61_DATE = "2022-02-26T03:53:02Z";
const MONTHLY_SUPERBLOCK_61 = 1628368;
/**
* @param {Snapshot} snapshot
* @param {Snapshot} root
* @returns {Float64} - fractional seconds
*/
DashGov.measureSecondsPerBlock = function (snapshot, root) {
let blockDelta = snapshot.block - root.block;
let timeDelta = snapshot.ms - root.ms;
let msPerBlock = timeDelta / blockDelta;
let sPerBlock = msPerBlock / 1000;
return sPerBlock;
};
/**
* @param {Snapshot?} [snapshot] - defaults to mainnet monthly superblock 61
* @returns {Float64} - fractional seconds
*/
DashGov.estimateSecondsPerBlock = function (snapshot) {
if (!snapshot) {
snapshot = {
block: MONTHLY_SUPERBLOCK_61,
ms: Date.parse(MONTHLY_SUPERBLOCK_61_DATE),
};
}
let root = {
block: MONTHLY_SUPERBLOCK_01,
ms: Date.parse(MONTHLY_SUPERBLOCK_01_DATE),
};
let spb = DashGov.measureSecondsPerBlock(snapshot, root);
return spb;
};
/**
* @param {Uint53} ms - the current time
* @param {Float64} secondsPerBlock
*/
DashGov.estimateBlockHeight = function (ms, secondsPerBlock) {
let then = Date.parse(MONTHLY_SUPERBLOCK_61_DATE);
let delta = ms - then;
let deltaS = delta / 1000;
let blocks = deltaS / secondsPerBlock;
blocks = Math.round(blocks);
let height = MONTHLY_SUPERBLOCK_61 + blocks;
return height;
};
/**
* @typedef Selection
* @prop {Estimate} start
* @prop {Estimate} end
*/
/**
* @param {Estimates} estimates
* @param {Uint32} startPeriod
* @param {Uint32} endPeriod
* @returns {Selection}
*/
DashGov.selectEstimates = function (estimates, startPeriod, endPeriod) {
let startEstimate;
let endEstimate;
if (startPeriod === 0) {
startEstimate = estimates.lameduck;
} else {
startPeriod -= 1;
startEstimate = estimates.upcoming[startPeriod];
}
if (!startEstimate) {
throw new Error(
`${startPeriod} is not valid ('startPeriod' might not be a number)`,
);
}
if (endPeriod === 0) {
endEstimate = estimates.lameduck;
} else {
endPeriod -= 1;
endEstimate = estimates.upcoming[endPeriod];
}
if (!endEstimate) {
throw new Error(
`${endPeriod} is not valid ('count' might not be a number)`,
);
}
return { start: startEstimate, end: endEstimate };
};
DashGov.proposal = {};
/**
* @param {Object} selected
* @param {Estimate} selected.start
* @param {Estimate} selected.end
* @param {GObjectData} proposalData
* @returns {Required<GObjectData>}
*/
DashGov.proposal.draftJson = function (selected, proposalData) {
let startEpoch = selected.start.startMs / 1000;
startEpoch = Math.round(startEpoch);
let endEpoch = selected.end.endMs / 1000;
endEpoch = Math.round(endEpoch);
let normalizedData = {
end_epoch: Math.round(endEpoch),
name: "",
payment_address: "",
payment_amount: 0,
start_epoch: Math.round(startEpoch),
type: 1,
url: "",
};
Object.assign(normalizedData, proposalData);
return normalizedData;
};
/**
* Creates a draft object with reasonable and default values
* @param {Uint53} now - use Date.now(), except in testing
* @param {Uint53} startEpochMs - used to create a deterministic gobject time
* @param {Required<GObjectData>} data - will be sorted and hex-ified
* @param {Partial<GObject>} [gobj] - override values
*/
DashGov.proposal.draft = function (now, startEpochMs, data, gobj) {
let dataHex = gobj?.dataHex || DashGov.proposal.sortAndEncodeJson(data);
let time = DashGov.proposal._selectKnownTime(now, startEpochMs);
/** @type {GObject} */
let normalGObj = {
hashParent: 0,
revision: 1,
time: time,
dataHex: "",
masternodeOutpoint: null,
collateralTxId: null,
collateralTxOutputIndex: null,
signature: null,
};
Object.assign(normalGObj, gobj, { dataHex });
return normalGObj;
};
/**
* @param {Required<GObjectData>} normalizedData
*/
DashGov.proposal.sortAndEncodeJson = function (normalizedData) {
let keys = Object.keys(normalizedData);
keys.sort();
/** @type {GObjectData} */
//@ts-ignore
let sortedData = {};
for (let key of keys) {
//@ts-ignore - this is the same type as normalData, but future-proofed
sortedData[key] = normalizedData[key];
}
let textEncoder = new TextEncoder();
let json = JSON.stringify(sortedData);
let jsonBytes = textEncoder.encode(json);
let hex = DashGov.utils.bytesToHex(jsonBytes);
return hex;
};
/**
* The arbitrary use of random times is a leading cause of lost money during
* the proposal process, so instead we use the 'start epoch' when appropriate,
* or otherwise a UTC day interval of the start epoch
* @param {Uint53} now
* @param {Uint53} startMs
*/
DashGov.proposal._selectKnownTime = function (now, startMs) {
let startEpochDate = new Date(startMs);
let today = new Date();
if (today < startEpochDate) {
let date = today.getUTCDate();
startEpochDate.setUTCFullYear(today.getUTCFullYear());
startEpochDate.setUTCMonth(today.getUTCMonth());
startEpochDate.setUTCDate(date - 1);
}
let knownTimeMs = startEpochDate.valueOf();
let knownSecs = knownTimeMs / 1000;
knownSecs = Math.floor(knownSecs);
return knownSecs;
};
let msToHours = 60 * 60 * 1000;
/**
* Since the estimates are only accurate to within 30 minutes anyway,
* and since the extra entropy on the time just makes it more difficult to read,
* we just get rid of it.
* @param {Uint53} ms
*/
DashGov.proposal._roundDownToHour = function (ms) {
let timeF = ms / msToHours;
let time = Math.floor(timeF);
ms = time * msToHours;
return ms;
};
/**
* @param {Uint53} ms
*/
DashGov.proposal._roundUpToHour = function (ms) {
let timeF = ms / msToHours;
let time = Math.ceil(timeF);
ms = time * msToHours;
return ms;
};
/**
* Note: since we're dealing with estimates that are typically reliable
* within an hour (and often even within 15 minutes), this may
* generate more results than it presents.
* @param {Uint8} [cycles] - 3 by default
* @param {Snapshot?} [snapshot]
* @param {Uint32} [proposalLeadtime] - default 3 days in ms
* @param {Float64} [secondsPerBlock] - typically close to 157.6
* @returns {Estimates} - the last, due, and upcoming proposal cycles
*/
DashGov.estimateProposalCycles = function (
cycles = 3,
snapshot = null,
secondsPerBlock = 0,
proposalLeadtime = PROPOSAL_LEAD_MS,
) {
let now = snapshot?.ms || Date.now();
let currentBlock = snapshot?.block;
if (!secondsPerBlock) {
if (currentBlock) {
snapshot = { block: currentBlock, ms: now };
}
secondsPerBlock = DashGov.estimateSecondsPerBlock(snapshot);
}
if (!currentBlock) {
currentBlock = DashGov.estimateBlockHeight(now, secondsPerBlock);
}
/** @type {Array<Estimate>} */
let estimates = [];
for (let i = 0; i <= cycles + 1; i += 1) {
let estimate = DashGov.estimateNthNextGovCycle(
{ block: currentBlock, ms: now },
secondsPerBlock,
i,
);
estimates.push(estimate);
}
{
/** @type {Estimate} */
//@ts-ignore - we know there is at least one (past) estimate
let last = estimates.shift();
/** @type {Estimate?} */
let lameduck = null;
if (estimates.length) {
if (estimates[0].voteDeltaMs < proposalLeadtime) {
//@ts-ignore - we just checked the length
lameduck = estimates.shift();
} else {
// lose the extra cycle
void estimates.pop();
}
}
let upcoming = estimates;
return {
last,
lameduck,
upcoming,
};
}
};
/**
* @param {Snapshot} snapshot
* @param {Float64} secondsPerBlock
* @param {Uint53} [offset] - how many superblocks in the future
* @returns {Estimate} - details about the current governance cycle
*/
DashGov.estimateNthNextGovCycle = function (
snapshot,
secondsPerBlock,
offset = 0,
) {
if (!secondsPerBlock) {
secondsPerBlock = DashGov.estimateSecondsPerBlock(snapshot);
}
let superblockHeight = DashGov.getNthNextSuperblock(snapshot.block, offset);
let superblockDelta = superblockHeight - snapshot.block;
let superblockDeltaMs = superblockDelta * secondsPerBlock * 1000;
let voteDeltaMs = VOTE_LEAD_BLOCKS * secondsPerBlock * 1000;
let d = new Date(snapshot.ms);
d.setUTCMilliseconds(0);
d.setUTCMilliseconds(superblockDeltaMs);
let sbms = d.valueOf();
let sbts = d.toISOString();
d.setUTCMilliseconds(-voteDeltaMs);
let vtms = d.valueOf();
let vtts = d.toISOString();
let startMs = vtms - START_EPOCH_MS_BEFORE_VOTE;
startMs = DashGov.proposal._roundDownToHour(startMs);
let startTime = new Date(startMs);
let endMs = sbms + END_EPOCH_MS_AFTER_SUPERBLOCK;
endMs = DashGov.proposal._roundUpToHour(endMs);
let endTime = new Date(endMs);
return {
// TODO split into objects
startMs: startMs,
startIso: startTime.toISOString(),
voteHeight: superblockHeight - VOTE_LEAD_BLOCKS,
voteIso: vtts,
voteMs: vtms,
voteDelta: superblockDelta - VOTE_LEAD_BLOCKS,
voteDeltaMs: superblockDeltaMs - voteDeltaMs,
superblockHeight: superblockHeight,
superblockDelta: superblockDelta,
superblockIso: sbts,
superblockMs: sbms,
superblockDeltaMs: superblockDeltaMs,
endMs: endMs,
endIso: endTime.toISOString(),
};
};
/**
* @param {Uint53} height
* @param {Uint53} offset - 0 (current / previous), 1 (next), 2, 3, nth
* @returns {Uint53} - the superblock after the given height
*/
DashGov.getNthNextSuperblock = function (height, offset) {
let superblockCount = height / SUPERBLOCK_INTERVAL;
superblockCount = Math.floor(superblockCount);
superblockCount += offset;
let superblockHeight = superblockCount * SUPERBLOCK_INTERVAL;
return superblockHeight;
};
export default DashGov;
/** @typedef {bigint} BigInt */
/** @typedef {Number} Uint8 */
/** @typedef {Number} Uint32 */
/** @typedef {Number} Uint53 */
/** @typedef {Number} Float64 */