@dashevo/dash-spv 6.0.1 → 6.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ultra.cache.json +1 -1
- package/lib/consensus.js +38 -68
- package/lib/pow.js +513 -0
- package/lib/spvchain.js +62 -29
- package/lib/utils.js +17 -6
- package/package.json +2 -3
- package/test/data/difficulty.js +1795 -0
- package/test/index.js +16 -13
- package/test/pow.js +310 -0
package/lib/spvchain.js
CHANGED
|
@@ -2,6 +2,7 @@ const X11 = require('./x11');
|
|
|
2
2
|
|
|
3
3
|
const config = require('../config/config');
|
|
4
4
|
const Consensus = require('./consensus');
|
|
5
|
+
const pow = require('./pow');
|
|
5
6
|
const utils = require('./utils');
|
|
6
7
|
const SPVError = require('./errors/SPVError');
|
|
7
8
|
|
|
@@ -29,12 +30,22 @@ const createGenesis = (chainType) => {
|
|
|
29
30
|
};
|
|
30
31
|
|
|
31
32
|
const SpvChain = class {
|
|
32
|
-
|
|
33
|
+
/**
|
|
34
|
+
* @param {string} chainType
|
|
35
|
+
* @param {number} [confirms]
|
|
36
|
+
* @param {object} [devnetOptions] - settings a devnet was started with
|
|
37
|
+
* @param {number} [devnetOptions.powTargetSpacing] - Core's -powtargetspacing
|
|
38
|
+
* @param {number} [devnetOptions.minimumDifficultyBlocks] - Core's -minimumdifficultyblocks
|
|
39
|
+
*/
|
|
40
|
+
constructor(chainType, confirms = 100, devnetOptions = undefined) {
|
|
33
41
|
this.confirmsBeforeFinal = confirms;
|
|
34
42
|
this.network = CHAIN_TYPE_NETWORKS[chainType];
|
|
35
43
|
if (!this.network) {
|
|
36
44
|
throw new SPVError(`Unsupported chain type "${chainType}"`);
|
|
37
45
|
}
|
|
46
|
+
// What consensus validation gets: the network, or a devnet with its settings
|
|
47
|
+
this.consensusNetwork = devnetOptions ? { ...devnetOptions, network: this.network } : this.network;
|
|
48
|
+
pow.getNetworkParams(this.consensusNetwork);
|
|
38
49
|
this.genesis = createGenesis(chainType);
|
|
39
50
|
|
|
40
51
|
this.reset();
|
|
@@ -80,7 +91,7 @@ const SpvChain = class {
|
|
|
80
91
|
|
|
81
92
|
if (startBlock && typeof height === 'number') {
|
|
82
93
|
// eslint-disable-next-line
|
|
83
|
-
startBlock = utils.
|
|
94
|
+
startBlock = utils.copyHeader(startBlock);
|
|
84
95
|
} else if (!startBlock && typeof height !== 'number') {
|
|
85
96
|
// eslint-disable-next-line
|
|
86
97
|
startBlock = this.genesis;
|
|
@@ -165,11 +176,10 @@ const SpvChain = class {
|
|
|
165
176
|
/** @private */
|
|
166
177
|
appendHeadersToLongestChain(headers) {
|
|
167
178
|
const longestChain = this.getLongestChain();
|
|
168
|
-
const
|
|
169
|
-
+ longestChain.length + this.prunedHeaders.length;
|
|
179
|
+
const firstHeight = this.getTipHeight() + 1;
|
|
170
180
|
|
|
171
181
|
headers.forEach((header, i) => {
|
|
172
|
-
const height =
|
|
182
|
+
const height = firstHeight + i;
|
|
173
183
|
this.hashesByHeight.set(height, header.hash);
|
|
174
184
|
this.heightByHash.set(header.hash, height);
|
|
175
185
|
});
|
|
@@ -203,7 +213,12 @@ const SpvChain = class {
|
|
|
203
213
|
|
|
204
214
|
if (tipHash === chunkPrevHash) {
|
|
205
215
|
try {
|
|
206
|
-
this.validateConnectedHeaders(
|
|
216
|
+
this.validateConnectedHeaders(
|
|
217
|
+
chunk,
|
|
218
|
+
this.getValidationHistory(),
|
|
219
|
+
this.getTipHeight() + 1,
|
|
220
|
+
true,
|
|
221
|
+
);
|
|
207
222
|
this.appendHeadersToLongestChain(chunk);
|
|
208
223
|
chunk.forEach((header) => {
|
|
209
224
|
this.orphansHashes.delete(header.hash);
|
|
@@ -221,16 +236,35 @@ const SpvChain = class {
|
|
|
221
236
|
}
|
|
222
237
|
}
|
|
223
238
|
|
|
224
|
-
/**
|
|
239
|
+
/**
|
|
240
|
+
* @private
|
|
241
|
+
* The last headers of the chain, as many as difficulty validation reads
|
|
242
|
+
* @return {BlockHeader[]}
|
|
243
|
+
*/
|
|
225
244
|
getValidationHistory() {
|
|
226
|
-
|
|
245
|
+
const longestChain = this.getLongestChain();
|
|
246
|
+
const fromPruned = pow.MAX_HISTORY - longestChain.length;
|
|
247
|
+
const prunedTail = fromPruned > 0 ? this.prunedHeaders.slice(-fromPruned) : [];
|
|
248
|
+
return prunedTail.concat(longestChain).slice(-pow.MAX_HISTORY);
|
|
227
249
|
}
|
|
228
250
|
|
|
229
251
|
/** @private */
|
|
230
|
-
|
|
252
|
+
getTipHeight() {
|
|
253
|
+
return this.startBlockHeight + this.prunedHeaders.length + this.getLongestChain().length - 1;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* @private
|
|
258
|
+
* @param {BlockHeader[]} headers
|
|
259
|
+
* @param {BlockHeader[]} initialHistory - headers before them, ending with
|
|
260
|
+
* the parent of the first one
|
|
261
|
+
* @param {number} firstHeight - height of the first header
|
|
262
|
+
* @param {boolean} [allowKnownOrphans]
|
|
263
|
+
*/
|
|
264
|
+
validateConnectedHeaders(headers, initialHistory, firstHeight, allowKnownOrphans = false) {
|
|
231
265
|
const history = initialHistory.slice();
|
|
232
266
|
|
|
233
|
-
headers.forEach((header) => {
|
|
267
|
+
headers.forEach((header, i) => {
|
|
234
268
|
const previousHeader = history[history.length - 1];
|
|
235
269
|
if (!previousHeader || !SpvChain.isParentChild(header, previousHeader)) {
|
|
236
270
|
throw new SPVError(`SPV: Header ${header.hash} is not connected to verified history`);
|
|
@@ -238,13 +272,20 @@ const SpvChain = class {
|
|
|
238
272
|
|
|
239
273
|
const isKnown = this.heightByHash.has(header.hash)
|
|
240
274
|
|| (!allowKnownOrphans && this.orphansHashes.has(header.hash));
|
|
241
|
-
if (isKnown || !Consensus.isValidBlockHeader(
|
|
275
|
+
if (isKnown || !Consensus.isValidBlockHeader(
|
|
276
|
+
header,
|
|
277
|
+
history,
|
|
278
|
+
this.consensusNetwork,
|
|
279
|
+
firstHeight + i,
|
|
280
|
+
this.startBlockHeight,
|
|
281
|
+
)) {
|
|
242
282
|
throw new SPVError(`SPV: Header ${header.hash} is invalid`);
|
|
243
283
|
}
|
|
244
284
|
|
|
245
285
|
history.push(header);
|
|
246
|
-
|
|
247
|
-
|
|
286
|
+
// Trim in bulk so each header costs O(1) on average
|
|
287
|
+
if (history.length > 2 * pow.MAX_HISTORY) {
|
|
288
|
+
history.splice(0, history.length - pow.MAX_HISTORY);
|
|
248
289
|
}
|
|
249
290
|
});
|
|
250
291
|
}
|
|
@@ -254,19 +295,6 @@ const SpvChain = class {
|
|
|
254
295
|
return this.orphanChunks;
|
|
255
296
|
}
|
|
256
297
|
|
|
257
|
-
/** @private
|
|
258
|
-
* validates a dashcore.BlockHeader object
|
|
259
|
-
*
|
|
260
|
-
* @param {Object} header
|
|
261
|
-
* @param {Object[]} previousHeaders
|
|
262
|
-
* @return {boolean}
|
|
263
|
-
*/
|
|
264
|
-
isValid(header, previousHeaders) {
|
|
265
|
-
const validBlockHeader = Consensus.isValidBlockHeader(header, previousHeaders, this.network);
|
|
266
|
-
const duplicate = this.isDuplicate(header.hash);
|
|
267
|
-
return !!validBlockHeader && !duplicate;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
298
|
/**
|
|
271
299
|
* verifies the parent child connection
|
|
272
300
|
* between two adjacent dashcore.BlockHeader objects
|
|
@@ -346,7 +374,8 @@ const SpvChain = class {
|
|
|
346
374
|
throw new SPVError('Chain not initialized, either call initialize() or set pendingStartBlockHeight');
|
|
347
375
|
}
|
|
348
376
|
|
|
349
|
-
|
|
377
|
+
// Copies, so later changes to the caller's objects can't alter the chain
|
|
378
|
+
const normalizedHeaders = headers.map((h) => utils.copyHeader(h));
|
|
350
379
|
|
|
351
380
|
let isOrphan = false;
|
|
352
381
|
|
|
@@ -385,7 +414,7 @@ const SpvChain = class {
|
|
|
385
414
|
);
|
|
386
415
|
}
|
|
387
416
|
if (this.isDuplicate(header.hash)
|
|
388
|
-
|| !Consensus.isValidBlockHeaderWithoutContext(header, this.
|
|
417
|
+
|| !Consensus.isValidBlockHeaderWithoutContext(header, this.consensusNetwork)) {
|
|
389
418
|
throw new SPVError(`SPV: Header ${header.hash} is invalid`);
|
|
390
419
|
}
|
|
391
420
|
});
|
|
@@ -395,7 +424,11 @@ const SpvChain = class {
|
|
|
395
424
|
});
|
|
396
425
|
this.orphanChunks.push(normalizedHeaders);
|
|
397
426
|
} else {
|
|
398
|
-
this.validateConnectedHeaders(
|
|
427
|
+
this.validateConnectedHeaders(
|
|
428
|
+
normalizedHeaders,
|
|
429
|
+
this.getValidationHistory(),
|
|
430
|
+
this.getTipHeight() + 1,
|
|
431
|
+
);
|
|
399
432
|
this.appendHeadersToLongestChain(normalizedHeaders);
|
|
400
433
|
}
|
|
401
434
|
if (this.orphanChunks.length > 0) {
|
package/lib/utils.js
CHANGED
|
@@ -4,6 +4,23 @@ const dashcore = require('@dashevo/dashcore-lib');
|
|
|
4
4
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Hash of the header's current fields, unlike the memoized header.hash
|
|
9
|
+
* @param {BlockHeader} header
|
|
10
|
+
* @return {string}
|
|
11
|
+
*/
|
|
12
|
+
getHeaderHash(header) {
|
|
13
|
+
return Buffer.from(header._getHash()).reverse().toString('hex');
|
|
14
|
+
},
|
|
15
|
+
/**
|
|
16
|
+
* A BlockHeader of its own for the header in any format normalizeHeader
|
|
17
|
+
* takes. Always re-serialized: a BlockHeader read from a Buffer keeps slices
|
|
18
|
+
* of it, so changes to the caller's Buffer would reach the copy.
|
|
19
|
+
* @return {BlockHeader}
|
|
20
|
+
*/
|
|
21
|
+
copyHeader(header) {
|
|
22
|
+
return new dashcore.BlockHeader(this.normalizeHeader(header).toBuffer());
|
|
23
|
+
},
|
|
7
24
|
getCorrectedHash(reversedHashObj) {
|
|
8
25
|
const clone = Buffer.alloc(32);
|
|
9
26
|
reversedHashObj.copy(clone);
|
|
@@ -32,12 +49,6 @@ module.exports = {
|
|
|
32
49
|
nonce: el.nonce,
|
|
33
50
|
});
|
|
34
51
|
},
|
|
35
|
-
getDgwBlock(header) {
|
|
36
|
-
return {
|
|
37
|
-
timestamp: header.timestamp,
|
|
38
|
-
target: header.bits,
|
|
39
|
-
};
|
|
40
|
-
},
|
|
41
52
|
validProofOfWork(header) {
|
|
42
53
|
const target = DashUtil.expandTarget(header.bits);
|
|
43
54
|
const hash = header._getHash().reverse();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dashevo/dash-spv",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.3",
|
|
4
4
|
"description": "Repository containing SPV functions used by @dashevo",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,9 +12,8 @@
|
|
|
12
12
|
"author": "",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@dashevo/dark-gravity-wave": "^1.1.1",
|
|
16
15
|
"@dashevo/dash-util": "^2.0.3",
|
|
17
|
-
"@dashevo/dashcore-lib": "~0.
|
|
16
|
+
"@dashevo/dashcore-lib": "~0.26.1",
|
|
18
17
|
"levelup": "^4.4.0",
|
|
19
18
|
"memdown": "^5.1.0",
|
|
20
19
|
"wasm-x11-hash": "~0.0.2"
|