Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

handshake-org / hsd / 3115375372

23 Sep 2022 - 20:17 coverage: 67.934% (+0.02%) from 67.917%
3115375372

Pull #768

github

GitHub
Merge 5e9be7902 into 6f112121a
Pull Request #768: mempool: reject TXs that exceed consensus covenant block limits

7324 of 12647 branches covered (57.91%)

Branch coverage included in aggregate %.

6 of 6 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

23355 of 32513 relevant lines covered (71.83%)

31425.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.79
/lib/protocol/consensus.js
1
/*!
2
 * consensus.js - consensus constants and helpers for hsd
3
 * Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
4
 * https://github.com/handshake-org/hsd
5
 */
6

7
'use strict';
8

9
/**
10
 * @module protocol/consensus
11
 */
12

13
const assert = require('bsert');
60×
14
const BN = require('bcrypto/lib/bn.js');
60×
15

16
/**
17
 * Coin exponent.
18
 * @const {Number}
19
 * @default
20
 */
21

22
exports.EXP = 6;
60×
23

24
/**
25
 * One handshake in dollarydoos.
26
 * @const {Amount}
27
 * @default
28
 */
29

30
exports.COIN = Math.pow(10, exports.EXP);
60×
31

32
/**
33
 * Maximum creators amount in dollarydoos (consensus).
34
 * @const {Amount}
35
 * @default
36
 */
37

38
exports.MAX_CREATORS = 102e6 * exports.COIN;
60×
39

40
/**
41
 * Maximum sponsors amount in dollarydoos (consensus).
42
 * @const {Amount}
43
 * @default
44
 */
45

46
exports.MAX_SPONSORS = 102e6 * exports.COIN;
60×
47

48
/**
49
 * Maximum TLD holder amount in dollarydoos (consensus).
50
 * @const {Amount}
51
 * @default
52
 */
53

54
exports.MAX_TLD = 51e6 * exports.COIN;
60×
55

56
/**
57
 * Maximum domain holder amount in dollarydoos (consensus).
58
 * @const {Amount}
59
 * @default
60
 */
61

62
exports.MAX_DOMAIN = 51e6 * exports.COIN;
60×
63

64
/**
65
 * Maximum CA/naming amount in dollarydoos (consensus).
66
 * @const {Amount}
67
 * @default
68
 */
69

70
exports.MAX_CA_NAMING = 102e6 * exports.COIN;
60×
71

72
/**
73
 * Maximum airdrop amount in dollarydoos (consensus).
74
 * @const {Amount}
75
 * @default
76
 */
77

78
exports.MAX_AIRDROP = 0.952e9 * exports.COIN;
60×
79

80
/**
81
 * Maximum initial supply in dollarydoos (consensus).
82
 * @const {Amount}
83
 * @default
84
 */
85

86
exports.MAX_INITIAL = 1.36e9 * exports.COIN;
60×
87

88
assert(exports.MAX_CREATORS
60×
89
     + exports.MAX_SPONSORS
90
     + exports.MAX_TLD
91
     + exports.MAX_DOMAIN
92
     + exports.MAX_CA_NAMING
93
     + exports.MAX_AIRDROP === exports.MAX_INITIAL);
94

95
/**
96
 * Maximum amount of subsidies in dollarydoos (consensus).
97
 * @const {Amount}
98
 * @default
99
 */
100

101
exports.MAX_SUBSIDY = 0.68e9 * exports.COIN;
60×
102

103
assert(exports.MAX_INITIAL / 2 === exports.MAX_SUBSIDY);
60×
104

105
/**
106
 * Maximum amount of money in dollarydoos (consensus).
107
 * @const {Amount}
108
 * @default
109
 */
110

111
exports.MAX_MONEY = 2.04e9 * exports.COIN;
60×
112

113
assert(exports.MAX_INITIAL + exports.MAX_SUBSIDY === exports.MAX_MONEY);
60×
114

115
/**
116
 * Base block subsidy (consensus).
117
 * @const {Amount}
118
 * @default
119
 */
120

121
exports.BASE_REWARD = 2000 * exports.COIN;
60×
122

123
assert(2 * exports.BASE_REWARD * 170000 === exports.MAX_SUBSIDY);
60×
124

125
/**
126
 * Block subsidy specifically for the genesis block.
127
 *
128
 * Explanation:
129
 * The max miner subsidy is 680000000, but due
130
 * to the halving interval it actually ends up
131
 * as 679999995.79, so add 2.21 coins to the
132
 * genesis reward output to make MAX_MONEY a
133
 * thoroughly true value.
134
 *
135
 * This, combined with the 3 1/4 year halving
136
 * interval, causes the supply to run dry
137
 * after about 100 years (around the year 2119,
138
 * or height=5,270,000).
139
 *
140
 * @const {Amount}
141
 * @default
142
 */
143

144
exports.GENESIS_REWARD = exports.BASE_REWARD + ((2.21 * exports.COIN) | 0);
60×
145

146
/**
147
 * Genesis key.
148
 * @const {Buffer}
149
 */
150

151
exports.GENESIS_KEY =
60×
152
  Buffer.from('f0237ae2e8f860f7d79124fc513f012e5aaa8d23', 'hex');
153

154
/**
155
 * Maximum block base size (consensus).
156
 * @const {Number}
157
 * @default
158
 */
159

160
exports.MAX_BLOCK_SIZE = 1000000;
60×
161

162
/**
163
 * Maximum block serialization size (protocol).
164
 * @const {Number}
165
 * @default
166
 */
167

168
exports.MAX_RAW_BLOCK_SIZE = 4000000;
60×
169

170
/**
171
 * Maximum block weight (consensus).
172
 * @const {Number}
173
 * @default
174
 */
175

176
exports.MAX_BLOCK_WEIGHT = 4000000;
60×
177

178
/**
179
 * Maximum block sigops cost (consensus).
180
 * @const {Number}
181
 * @default
182
 */
183

184
exports.MAX_BLOCK_SIGOPS = 80000;
60×
185

186
/**
187
 * Maximum block tree opens.
188
 * @const {Number}
189
 * @default
190
 */
191

192
exports.MAX_BLOCK_OPENS = 300;
60×
193

194
/**
195
 * Maximum block tree updates.
196
 * @const {Number}
197
 * @default
198
 */
199

200
exports.MAX_BLOCK_UPDATES = 600;
60×
201

202
/**
203
 * Maximum block tree renewals.
204
 * @const {Number}
205
 * @default
206
 */
207

208
exports.MAX_BLOCK_RENEWALS = 600;
60×
209

210
/**
211
 * Size of set to pick median time from.
212
 * @const {Number}
213
 * @default
214
 */
215

216
exports.MEDIAN_TIMESPAN = 11;
60×
217

218
/**
219
 * Amount to multiply base/non-witness sizes by.
220
 * @const {Number}
221
 * @default
222
 */
223

224
exports.WITNESS_SCALE_FACTOR = 4;
60×
225

226
/**
227
 * Maximum TX base size (consensus).
228
 * @const {Number}
229
 * @default
230
 */
231

232
exports.MAX_TX_SIZE = 1000000;
60×
233

234
/**
235
 * Maximum TX weight (consensus).
236
 * @const {Number}
237
 * @default
238
 */
239

240
exports.MAX_TX_WEIGHT = 4000000;
60×
241

242
/**
243
 * Locktime flag.
244
 * @const {Number}
245
 * @default
246
 */
247

248
exports.LOCKTIME_FLAG = (1 << 31) >>> 0;
60×
249

250
/**
251
 * Locktime mask.
252
 * @const {Number}
253
 * @default
254
 */
255

256
exports.LOCKTIME_MASK = exports.LOCKTIME_FLAG - 1;
60×
257

258
/**
259
 * Locktime granularity.
260
 * @const {Number}
261
 * @default
262
 */
263

264
exports.LOCKTIME_GRANULARITY = 9;
60×
265

266
/**
267
 * Locktime multiplier.
268
 * @const {Number}
269
 * @default
270
 */
271

272
exports.LOCKTIME_MULT = 2 ** exports.LOCKTIME_GRANULARITY;
60×
273

274
/**
275
 * Highest nSequence bit -- disables
276
 * sequence locktimes (consensus).
277
 * @const {Number}
278
 */
279

280
exports.SEQUENCE_DISABLE_FLAG = (1 << 31) >>> 0;
60×
281

282
/**
283
 * Sequence time: height or time (consensus).
284
 * @const {Number}
285
 * @default
286
 */
287

288
exports.SEQUENCE_TYPE_FLAG = 1 << 22;
60×
289

290
/**
291
 * Sequence granularity for time (consensus).
292
 * @const {Number}
293
 * @default
294
 */
295

296
exports.SEQUENCE_GRANULARITY = 9;
60×
297

298
/**
299
 * Sequence mask (consensus).
300
 * @const {Number}
301
 * @default
302
 */
303

304
exports.SEQUENCE_MASK = 0x0000ffff;
60×
305

306
/**
307
 * Max serialized script size (consensus).
308
 * @const {Number}
309
 * @default
310
 */
311

312
exports.MAX_SCRIPT_SIZE = 10000;
60×
313

314
/**
315
 * Max stack size during execution (consensus).
316
 * @const {Number}
317
 * @default
318
 */
319

320
exports.MAX_SCRIPT_STACK = 1000;
60×
321

322
/**
323
 * Max script element size (consensus).
324
 * @const {Number}
325
 * @default
326
 */
327

328
exports.MAX_SCRIPT_PUSH = 520;
60×
329

330
/**
331
 * Max opcodes executed (consensus).
332
 * @const {Number}
333
 * @default
334
 */
335

336
exports.MAX_SCRIPT_OPS = 201;
60×
337

338
/**
339
 * Max `n` value for multisig (consensus).
340
 * @const {Number}
341
 * @default
342
 */
343

344
exports.MAX_MULTISIG_PUBKEYS = 20;
60×
345

346
/**
347
 * A hash of all zeroes.
348
 * @const {Buffer}
349
 * @default
350
 */
351

352
exports.ZERO_HASH = Buffer.alloc(32, 0x00);
60×
353

354
/**
355
 * Block header size.
356
 * @const {Number}
357
 * @default
358
 */
359

360
exports.HEADER_SIZE = 236;
60×
361

362
/**
363
 * Block header nonce size.
364
 * @const {Number}
365
 * @default
366
 */
367

368
exports.NONCE_SIZE = 24;
60×
369

370
/**
371
 * Block header of all zeroes.
372
 * @const {Buffer}
373
 * @default
374
 */
375

376
exports.ZERO_HEADER = Buffer.alloc(exports.HEADER_SIZE, 0x00);
60×
377

378
/**
379
 * Block header nonce of all zeroes.
380
 * @const {Buffer}
381
 * @default
382
 */
383

384
exports.ZERO_NONCE = Buffer.alloc(exports.NONCE_SIZE, 0x00);
60×
385

386
/**
387
 * Convert a compact number to a big number.
388
 * Used for `block.bits` -> `target` conversion.
389
 * @param {Number} compact
390
 * @returns {BN}
391
 */
392

393
exports.fromCompact = function fromCompact(compact) {
60×
394
  if (compact === 0)
39,149×
395
    return new BN(0);
7×
396

397
  const exponent = compact >>> 24;
39,142×
398
  const negative = (compact >>> 23) & 1;
39,142×
399

400
  let mantissa = compact & 0x7fffff;
39,142×
401
  let num;
402

403
  if (exponent <= 3) {
Branches [[1, 0]] missed. 39,142×
404
    mantissa >>>= 8 * (3 - exponent);
!
405
    num = new BN(mantissa);
!
406
  } else {
407
    num = new BN(mantissa);
39,142×
408
    num.iushln(8 * (exponent - 3));
39,142×
409
  }
410

411
  if (negative)
Branches [[2, 0]] missed. 39,142×
412
    num.ineg();
!
413

414
  return num;
39,142×
415
};
416

417
/**
418
 * Convert a big number to a compact number.
419
 * Used for `target` -> `block.bits` conversion.
420
 * @param {BN} num
421
 * @returns {Number}
422
 */
423

424
exports.toCompact = function toCompact(num) {
60×
425
  if (num.isZero())
Branches [[3, 0]] missed. 1×
426
    return 0;
!
427

428
  let exponent = num.byteLength();
1×
429
  let mantissa;
430

431
  if (exponent <= 3) {
Branches [[4, 0]] missed. 1×
432
    mantissa = num.toNumber();
!
433
    mantissa <<= 8 * (3 - exponent);
!
434
  } else {
435
    mantissa = num.ushrn(8 * (exponent - 3)).toNumber();
1×
436
  }
437

438
  if (mantissa & 0x800000) {
Branches [[5, 1]] missed. 1×
439
    mantissa >>>= 8;
1×
440
    exponent += 1;
1×
441
  }
442

443
  let compact = (exponent << 24) | mantissa;
1×
444

445
  if (num.isNeg())
Branches [[6, 0]] missed. 1×
446
    compact |= 0x800000;
!
447

448
  compact >>>= 0;
1×
449

450
  return compact;
1×
451
};
452

453
/**
454
 * Verify proof-of-work.
455
 * @param {Hash} hash
456
 * @param {Number} bits
457
 * @returns {Boolean}
458
 */
459

460
exports.verifyPOW = function verifyPOW(hash, bits) {
60×
461
  const target = exports.fromCompact(bits);
13,588×
462

463
  if (target.isNeg() || target.isZero())
Branches [[7, 0]] missed. 13,588×
464
    return false;
!
465

466
  if (target.bitLength() > 256)
Branches [[9, 0]] missed. 13,588×
467
    return false;
!
468

469
  const num = new BN(hash, 'be');
13,588×
470

471
  if (num.gt(target))
Branches [[10, 0]] missed. 13,588×
UNCOV
472
    return false;
!
473

474
  return true;
13,588×
475
};
476

477
/**
478
 * Calculate block subsidy.
479
 * @param {Number} height - Reward era by height.
480
 * @returns {Amount}
481
 */
482

483
exports.getReward = function getReward(height, interval) {
60×
484
  assert((height >>> 0) === height, 'Bad height for reward.');
5,308,143×
485
  assert((interval >>> 0) === interval);
5,308,143×
486

487
  const halvings = Math.floor(height / interval);
5,308,143×
488

489
  if (halvings >= 52)
Branches [[11, 0]] missed. 5,308,143×
490
    return 0;
!
491

492
  return Math.floor(exports.BASE_REWARD / Math.pow(2, halvings));
5,308,143×
493
};
494

495
/**
496
 * Test version bit.
497
 * @param {Number} version
498
 * @param {Number} bit
499
 * @returns {Boolean}
500
 */
501

502
exports.hasBit = function hasBit(version, bit) {
60×
503
  return (version & (1 << bit)) !== 0;
6,521×
504
};
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc