• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

handshake-org / hsd / 11142522588

02 Oct 2024 10:56AM UTC coverage: 70.04% (+0.007%) from 70.033%
11142522588

push

github

nodech
Merge PR #902 from 'nodech/update-types'

7761 of 12916 branches covered (60.09%)

Branch coverage included in aggregate %.

67 of 92 new or added lines in 25 files covered. (72.83%)

8 existing lines in 5 files now uncovered.

24790 of 33559 relevant lines covered (73.87%)

34019.86 hits per line

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

65.63
/lib/mining/common.js
1
/*!
2
 * common.js - mining utils
3
 * Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
4
 * https://github.com/handshake-org/hsd
5
 */
6

7
'use strict';
8

9
const assert = require('bsert');
1✔
10
const consensus = require('../protocol/consensus');
1✔
11
const BN = require('bcrypto/lib/bn.js');
1✔
12

13
/**
14
 * @exports mining/common
15
 */
16

17
const common = exports;
1✔
18

19
/*
20
 * Constants
21
 */
22

23
const DIFF = 0x00000000ffff0000000000000000000000000000000000000000000000000000;
1✔
24
const B192 = 0x1000000000000000000000000000000000000000000000000;
1✔
25
const B128 = 0x100000000000000000000000000000000;
1✔
26
const B64 = 0x10000000000000000;
1✔
27
const B0 = 0x1;
1✔
28

29
/**
30
 * Swap 32 bit endianness of uint256.
31
 * @param {Buffer} data
32
 * @returns {Buffer}
33
 */
34

35
common.swap32 = function swap32(data) {
1✔
36
  for (let i = 0; i < data.length; i += 4) {
×
NEW
37
    const field = data.readUInt32LE(i);
×
NEW
38
    data.writeUInt32BE(field, i);
×
39
  }
40

41
  return data;
×
42
};
43

44
/**
45
 * Convert a uint256be to a double.
46
 * @param {Buffer} target
47
 * @returns {Number}
48
 */
49

50
common.double256 = function double256(target) {
1✔
51
  let n = 0;
16,502✔
52
  let hi, lo;
53

54
  assert(target.length === 32);
16,502✔
55

56
  hi = target.readUInt32BE(0);
16,502✔
57
  lo = target.readUInt32BE(4);
16,502✔
58
  n += (hi * 0x100000000 + lo) * B192;
16,502✔
59

60
  hi = target.readUInt32BE(8);
16,502✔
61
  lo = target.readUInt32BE(12);
16,502✔
62
  n += (hi * 0x100000000 + lo) * B128;
16,502✔
63

64
  hi = target.readUInt32BE(16);
16,502✔
65
  lo = target.readUInt32BE(20);
16,502✔
66
  n += (hi * 0x100000000 + lo) * B64;
16,502✔
67

68
  hi = target.readUInt32BE(24);
16,502✔
69
  lo = target.readUInt32BE(28);
16,502✔
70
  n += (hi * 0x100000000 + lo) * B0;
16,502✔
71

72
  return n;
16,502✔
73
};
74

75
/**
76
 * Calculate mining difficulty
77
 * from little-endian target.
78
 * @param {Buffer} target
79
 * @returns {Number}
80
 */
81

82
common.getDifficulty = function getDifficulty(target) {
1✔
83
  const d = DIFF;
16,502✔
84
  const n = common.double256(target);
16,502✔
85

86
  if (n === 0)
16,502!
87
    return d;
×
88

89
  return Math.floor(d / n);
16,502✔
90
};
91

92
/**
93
 * Get target from bits as a uint256le.
94
 * @param {Number} bits
95
 * @returns {Buffer}
96
 */
97

98
common.getTarget = function getTarget(bits) {
1✔
99
  const target = consensus.fromCompact(bits);
16,503✔
100

101
  if (target.isNeg())
16,503!
102
    throw new Error('Target is negative.');
×
103

104
  if (target.isZero())
16,503!
105
    throw new Error('Target is zero.');
×
106

107
  if (target.bitLength() > 256)
16,503!
108
    throw new Error('Target overflow.');
×
109

110
  return target.toArrayLike(Buffer, 'be', 32);
16,503✔
111
};
112

113
/**
114
 * Get bits from target.
115
 * @param {Buffer} data
116
 * @returns {Number}
117
 */
118

119
common.getBits = function getBits(data) {
1✔
120
  const target = new BN(data, 'be');
×
121

122
  if (target.isZero())
×
123
    throw new Error('Target is zero.');
×
124

125
  if (target.bitLength() > 256)
×
126
    throw new Error('Target overflow.');
×
127

128
  return consensus.toCompact(target);
×
129
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc