@dashevo/dash-spv 6.0.2 → 6.1.0-dev.1
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 +536 -0
- package/lib/spvchain.js +341 -75
- package/lib/utils.js +17 -6
- package/package.json +1 -2
- package/test/data/difficulty.js +1795 -0
- package/test/index.js +19 -15
- package/test/pow.js +310 -0
- package/test/reorg.js +359 -0
package/lib/spvchain.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
const EventEmitter = require('events');
|
|
1
2
|
const X11 = require('./x11');
|
|
2
3
|
|
|
3
4
|
const config = require('../config/config');
|
|
4
5
|
const Consensus = require('./consensus');
|
|
6
|
+
const pow = require('./pow');
|
|
5
7
|
const utils = require('./utils');
|
|
6
8
|
const SPVError = require('./errors/SPVError');
|
|
7
9
|
|
|
@@ -28,13 +30,34 @@ const createGenesis = (chainType) => {
|
|
|
28
30
|
}
|
|
29
31
|
};
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
// Limits for headers that don't extend the main chain
|
|
34
|
+
const MAX_FORKS = 10;
|
|
35
|
+
|
|
36
|
+
const EVENTS = {
|
|
37
|
+
// (headers, firstHeight): headers that became the end of the main chain,
|
|
38
|
+
// from firstHeight on. After a reorg firstHeight is at or below the old tip.
|
|
39
|
+
CONNECTED: 'connected',
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
class SpvChain extends EventEmitter {
|
|
43
|
+
/**
|
|
44
|
+
* @param {string} chainType
|
|
45
|
+
* @param {number} [confirms] - how many of the last headers can still be
|
|
46
|
+
* replaced by a fork with more work
|
|
47
|
+
* @param {object} [devnetOptions] - settings a devnet was started with
|
|
48
|
+
* @param {number} [devnetOptions.powTargetSpacing] - Core's -powtargetspacing
|
|
49
|
+
* @param {number} [devnetOptions.minimumDifficultyBlocks] - Core's -minimumdifficultyblocks
|
|
50
|
+
*/
|
|
51
|
+
constructor(chainType, confirms = 100, devnetOptions = undefined) {
|
|
52
|
+
super();
|
|
33
53
|
this.confirmsBeforeFinal = confirms;
|
|
34
54
|
this.network = CHAIN_TYPE_NETWORKS[chainType];
|
|
35
55
|
if (!this.network) {
|
|
36
56
|
throw new SPVError(`Unsupported chain type "${chainType}"`);
|
|
37
57
|
}
|
|
58
|
+
// What consensus validation gets: the network, or a devnet with its settings
|
|
59
|
+
this.consensusNetwork = devnetOptions ? { ...devnetOptions, network: this.network } : this.network;
|
|
60
|
+
pow.getNetworkParams(this.consensusNetwork);
|
|
38
61
|
this.genesis = createGenesis(chainType);
|
|
39
62
|
|
|
40
63
|
this.reset();
|
|
@@ -45,9 +68,12 @@ const SpvChain = class {
|
|
|
45
68
|
}
|
|
46
69
|
|
|
47
70
|
reset() {
|
|
71
|
+
// The main chain's unconfirmed headers first, then forks off them
|
|
48
72
|
this.allBranches = [[]];
|
|
49
73
|
this.orphanChunks = [];
|
|
74
|
+
// Confirmed headers kept for validation; older ones are dropped
|
|
50
75
|
this.prunedHeaders = [];
|
|
76
|
+
this.droppedHeadersCount = 0;
|
|
51
77
|
|
|
52
78
|
this.hashesByHeight = new Map();
|
|
53
79
|
this.heightByHash = new Map();
|
|
@@ -80,7 +106,7 @@ const SpvChain = class {
|
|
|
80
106
|
|
|
81
107
|
if (startBlock && typeof height === 'number') {
|
|
82
108
|
// eslint-disable-next-line
|
|
83
|
-
startBlock = utils.
|
|
109
|
+
startBlock = utils.copyHeader(startBlock);
|
|
84
110
|
} else if (!startBlock && typeof height !== 'number') {
|
|
85
111
|
// eslint-disable-next-line
|
|
86
112
|
startBlock = this.genesis;
|
|
@@ -148,6 +174,22 @@ const SpvChain = class {
|
|
|
148
174
|
.forEach((header) => {
|
|
149
175
|
this.prunedHeaders.push(header);
|
|
150
176
|
});
|
|
177
|
+
|
|
178
|
+
// Forks off confirmed headers can't win any more
|
|
179
|
+
const firstUnconfirmedHeight = this.getFirstUnconfirmedHeight();
|
|
180
|
+
this.allBranches = [longestChain, ...this.normalizeForks(this.getForks()).filter(
|
|
181
|
+
(fork) => this.heightByHash.get(SpvChain.getParentHash(fork)) >= firstUnconfirmedHeight - 1,
|
|
182
|
+
)];
|
|
183
|
+
|
|
184
|
+
// Keep only the confirmed headers validation can read
|
|
185
|
+
const dropCount = this.prunedHeaders.length - pow.MAX_HISTORY;
|
|
186
|
+
if (dropCount > 0) {
|
|
187
|
+
this.prunedHeaders.splice(0, dropCount).forEach((header) => {
|
|
188
|
+
this.hashesByHeight.delete(this.heightByHash.get(header.hash));
|
|
189
|
+
this.heightByHash.delete(header.hash);
|
|
190
|
+
});
|
|
191
|
+
this.droppedHeadersCount += dropCount;
|
|
192
|
+
}
|
|
151
193
|
}
|
|
152
194
|
|
|
153
195
|
/** @private */
|
|
@@ -162,22 +204,24 @@ const SpvChain = class {
|
|
|
162
204
|
this.allBranches.push(branch);
|
|
163
205
|
}
|
|
164
206
|
|
|
165
|
-
/**
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
207
|
+
/**
|
|
208
|
+
* @private
|
|
209
|
+
* Validates headers that continue the main chain's tip and appends them
|
|
210
|
+
* @param {BlockHeader[]} headers
|
|
211
|
+
*/
|
|
212
|
+
extendLongestChain(headers) {
|
|
213
|
+
const firstHeight = this.getTipHeight() + 1;
|
|
214
|
+
this.validateConnectedHeaders(headers, this.getValidationHistory(), firstHeight);
|
|
170
215
|
|
|
171
216
|
headers.forEach((header, i) => {
|
|
172
|
-
const height =
|
|
217
|
+
const height = firstHeight + i;
|
|
173
218
|
this.hashesByHeight.set(height, header.hash);
|
|
174
219
|
this.heightByHash.set(header.hash, height);
|
|
175
220
|
});
|
|
176
221
|
|
|
177
|
-
|
|
222
|
+
this.allBranches[0] = this.getLongestChain().concat(headers);
|
|
178
223
|
|
|
179
|
-
this.
|
|
180
|
-
this.allBranches.push(newLongestChain);
|
|
224
|
+
this.emit(EVENTS.CONNECTED, headers, firstHeight);
|
|
181
225
|
}
|
|
182
226
|
|
|
183
227
|
/** @private */
|
|
@@ -185,66 +229,283 @@ const SpvChain = class {
|
|
|
185
229
|
return this.allBranches;
|
|
186
230
|
}
|
|
187
231
|
|
|
232
|
+
/** @private */
|
|
233
|
+
getForks() {
|
|
234
|
+
return this.allBranches.slice(1);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @private
|
|
239
|
+
* Each fork as the headers it has off the main chain, rooted at a main-chain
|
|
240
|
+
* header: drops a start that became part of the main chain, and roots a fork
|
|
241
|
+
* off another fork (such as the headers a reorg replaced) at that fork's root
|
|
242
|
+
* @param {BlockHeader[][]} forks
|
|
243
|
+
* @return {BlockHeader[][]}
|
|
244
|
+
*/
|
|
245
|
+
normalizeForks(forks) {
|
|
246
|
+
const isOnMainChain = (header) => this.heightByHash.has(header.hash);
|
|
247
|
+
let pending = forks
|
|
248
|
+
.map((fork) => fork.slice(fork.findIndex((header) => !isOnMainChain(header))))
|
|
249
|
+
.filter((fork) => fork.length && !isOnMainChain(fork[0]));
|
|
250
|
+
const normalized = [];
|
|
251
|
+
|
|
252
|
+
// A fork off another fork is rooted once that fork is
|
|
253
|
+
const root = (fork) => {
|
|
254
|
+
const parentHash = SpvChain.getParentHash(fork);
|
|
255
|
+
if (this.heightByHash.has(parentHash)) {
|
|
256
|
+
return fork;
|
|
257
|
+
}
|
|
258
|
+
const base = normalized.find((other) => other.some((header) => header.hash === parentHash));
|
|
259
|
+
return base
|
|
260
|
+
? base.slice(0, base.findIndex((header) => header.hash === parentHash) + 1).concat(fork)
|
|
261
|
+
: null;
|
|
262
|
+
};
|
|
263
|
+
let previousCount;
|
|
264
|
+
do {
|
|
265
|
+
previousCount = pending.length;
|
|
266
|
+
const unresolved = [];
|
|
267
|
+
pending.forEach((fork) => {
|
|
268
|
+
const rooted = root(fork);
|
|
269
|
+
if (rooted) {
|
|
270
|
+
normalized.push(rooted);
|
|
271
|
+
} else {
|
|
272
|
+
unresolved.push(fork);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
pending = unresolved;
|
|
276
|
+
} while (pending.length && pending.length < previousCount);
|
|
277
|
+
|
|
278
|
+
// Drop forks another one already contains
|
|
279
|
+
return normalized.filter((fork, i) => !normalized.some((other, j) => j !== i
|
|
280
|
+
&& other.length >= fork.length
|
|
281
|
+
&& (other.length > fork.length || j < i)
|
|
282
|
+
&& fork.every((header, k) => other[k].hash === header.hash)));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** @private */
|
|
286
|
+
static getParentHash(headers) {
|
|
287
|
+
return utils.getCorrectedHash(headers[0].prevHash);
|
|
288
|
+
}
|
|
289
|
+
|
|
188
290
|
/** @private */
|
|
189
291
|
isDuplicate(hash) {
|
|
190
|
-
return this.
|
|
292
|
+
return this.hasHeader(hash) || this.orphansHashes.has(hash);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* @private
|
|
297
|
+
* @param {string} hash
|
|
298
|
+
* @return {boolean} whether the main chain or a fork has the header
|
|
299
|
+
*/
|
|
300
|
+
hasHeader(hash) {
|
|
301
|
+
return this.heightByHash.has(hash) || this.findInForks(hash) !== null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* @private
|
|
306
|
+
* @param {string} hash
|
|
307
|
+
* @return {{ forkIndex: number, index: number }|null} position in allBranches
|
|
308
|
+
*/
|
|
309
|
+
findInForks(hash) {
|
|
310
|
+
for (let forkIndex = 1; forkIndex < this.allBranches.length; forkIndex += 1) {
|
|
311
|
+
const index = this.allBranches[forkIndex].findIndex((header) => header.hash === hash);
|
|
312
|
+
if (index >= 0) {
|
|
313
|
+
return { forkIndex, index };
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Drops headers that don't connect to anything known yet
|
|
321
|
+
*/
|
|
322
|
+
dropOrphans() {
|
|
323
|
+
this.orphanChunks = [];
|
|
324
|
+
this.orphansHashes.clear();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* @private
|
|
329
|
+
* Validates headers that continue the chain at `parentHash` and either
|
|
330
|
+
* extends the main chain, starts or extends a fork, or reorgs to a fork
|
|
331
|
+
* that now has more work.
|
|
332
|
+
* @param {BlockHeader[]} headers
|
|
333
|
+
* @return {boolean} false if they connect to nothing known
|
|
334
|
+
*/
|
|
335
|
+
connectHeaders(headers) {
|
|
336
|
+
const parentHash = SpvChain.getParentHash(headers);
|
|
337
|
+
|
|
338
|
+
if (parentHash === this.getTipHash()) {
|
|
339
|
+
this.extendLongestChain(headers);
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const longestChain = this.getLongestChain();
|
|
344
|
+
const firstUnconfirmedHeight = this.getFirstUnconfirmedHeight();
|
|
345
|
+
const inFork = this.findInForks(parentHash);
|
|
346
|
+
let fork;
|
|
347
|
+
if (inFork) {
|
|
348
|
+
const branch = this.allBranches[inFork.forkIndex];
|
|
349
|
+
// Extends the fork, or starts a new one sharing the start of it
|
|
350
|
+
fork = inFork.index === branch.length - 1 ? branch : branch.slice(0, inFork.index + 1);
|
|
351
|
+
} else if (this.heightByHash.has(parentHash)) {
|
|
352
|
+
if (this.heightByHash.get(parentHash) < firstUnconfirmedHeight - 1) {
|
|
353
|
+
throw new SPVError(`SPV: Header ${headers[0].hash} forks off confirmed header ${parentHash}`);
|
|
354
|
+
}
|
|
355
|
+
// A new fork off the main chain
|
|
356
|
+
fork = [];
|
|
357
|
+
} else {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const forkParentHash = fork.length ? SpvChain.getParentHash(fork) : parentHash;
|
|
362
|
+
const forkParentHeight = this.heightByHash.get(forkParentHash);
|
|
363
|
+
this.validateConnectedHeaders(
|
|
364
|
+
headers,
|
|
365
|
+
this.getValidationHistory(forkParentHeight).concat(fork),
|
|
366
|
+
forkParentHeight + fork.length + 1,
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
// Like Core, switch only to a fork with strictly more work
|
|
370
|
+
const extended = fork.concat(headers);
|
|
371
|
+
const replaced = longestChain.slice(forkParentHeight - firstUnconfirmedHeight + 1);
|
|
372
|
+
const wins = pow.getChainWork(extended) > pow.getChainWork(replaced);
|
|
373
|
+
|
|
374
|
+
// Forks kept on the side are bounded: in length, and in number by
|
|
375
|
+
// dropping the oldest
|
|
376
|
+
if (!wins && extended.length > 2 * this.confirmsBeforeFinal) {
|
|
377
|
+
throw new SPVError(`SPV: Fork at ${headers[0].hash} is longer than ${2 * this.confirmsBeforeFinal} headers without more work`);
|
|
378
|
+
}
|
|
379
|
+
fork.push(...headers);
|
|
380
|
+
if (!this.allBranches.includes(fork)) {
|
|
381
|
+
this.allBranches.push(fork);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (wins) {
|
|
385
|
+
// The replaced headers become a fork too
|
|
386
|
+
this.reorganize(this.allBranches.indexOf(fork), forkParentHeight);
|
|
387
|
+
}
|
|
388
|
+
if (this.allBranches.length > MAX_FORKS + 1) {
|
|
389
|
+
this.allBranches.splice(1, this.allBranches.length - MAX_FORKS - 1);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return true;
|
|
191
393
|
}
|
|
192
394
|
|
|
193
395
|
/** @private */
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
396
|
+
reorganize(forkIndex, forkParentHeight) {
|
|
397
|
+
const longestChain = this.getLongestChain();
|
|
398
|
+
const fork = this.allBranches[forkIndex];
|
|
399
|
+
|
|
400
|
+
const replaced = longestChain.splice(forkParentHeight - this.getFirstUnconfirmedHeight() + 1);
|
|
401
|
+
replaced.forEach((header) => {
|
|
402
|
+
this.hashesByHeight.delete(this.heightByHash.get(header.hash));
|
|
403
|
+
this.heightByHash.delete(header.hash);
|
|
404
|
+
});
|
|
405
|
+
this.allBranches.splice(forkIndex, 1, replaced);
|
|
406
|
+
|
|
407
|
+
fork.forEach((header, i) => {
|
|
408
|
+
this.hashesByHeight.set(forkParentHeight + 1 + i, header.hash);
|
|
409
|
+
this.heightByHash.set(header.hash, forkParentHeight + 1 + i);
|
|
410
|
+
});
|
|
411
|
+
longestChain.push(...fork);
|
|
198
412
|
|
|
413
|
+
this.allBranches = [longestChain, ...this.normalizeForks(this.getForks())];
|
|
414
|
+
|
|
415
|
+
this.emit(EVENTS.CONNECTED, fork, forkParentHeight + 1);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** @private */
|
|
419
|
+
orphanChunksReconnect() {
|
|
199
420
|
for (let i = 0; i < this.orphanChunks.length; i += 1) {
|
|
200
421
|
const chunk = this.orphanChunks[i];
|
|
201
|
-
const tipHash = this.getTipHash();
|
|
202
|
-
const chunkPrevHash = utils.getCorrectedHash(chunk[0].prevHash);
|
|
203
422
|
|
|
204
|
-
|
|
423
|
+
// Its parent joined the main chain or a fork
|
|
424
|
+
if (this.hasHeader(SpvChain.getParentHash(chunk))) {
|
|
425
|
+
chunk.forEach((header) => {
|
|
426
|
+
this.orphansHashes.delete(header.hash);
|
|
427
|
+
});
|
|
428
|
+
this.orphanChunks.splice(i, 1);
|
|
429
|
+
|
|
205
430
|
try {
|
|
206
|
-
this.
|
|
207
|
-
this.appendHeadersToLongestChain(chunk);
|
|
208
|
-
chunk.forEach((header) => {
|
|
209
|
-
this.orphansHashes.delete(header.hash);
|
|
210
|
-
});
|
|
211
|
-
this.orphanChunks.splice(i, 1);
|
|
212
|
-
i -= 1;
|
|
431
|
+
this.connectHeaders(chunk);
|
|
213
432
|
} catch (e) {
|
|
214
|
-
chunk
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
433
|
+
// The chunk is dropped; the headers that connected its parent
|
|
434
|
+
// were fine. If the chain needed it, it stays short and validate()
|
|
435
|
+
// or the historical read reports that.
|
|
436
|
+
if (!(e instanceof SPVError)) {
|
|
437
|
+
throw e;
|
|
438
|
+
}
|
|
219
439
|
}
|
|
440
|
+
|
|
441
|
+
// The chain may now connect a chunk checked earlier
|
|
442
|
+
i = -1;
|
|
220
443
|
}
|
|
221
444
|
}
|
|
222
445
|
}
|
|
223
446
|
|
|
224
|
-
/**
|
|
225
|
-
|
|
226
|
-
|
|
447
|
+
/**
|
|
448
|
+
* @private
|
|
449
|
+
* The main chain's headers up to `lastHeight`, as many as difficulty
|
|
450
|
+
* validation reads
|
|
451
|
+
* @param {number} [lastHeight] - the tip by default
|
|
452
|
+
* @return {BlockHeader[]}
|
|
453
|
+
*/
|
|
454
|
+
getValidationHistory(lastHeight = this.getTipHeight()) {
|
|
455
|
+
const longestChain = this.getLongestChain();
|
|
456
|
+
const end = longestChain.length - (this.getTipHeight() - lastHeight);
|
|
457
|
+
const fromPruned = pow.MAX_HISTORY - end;
|
|
458
|
+
const prunedTail = fromPruned > 0 ? this.prunedHeaders.slice(-fromPruned) : [];
|
|
459
|
+
return prunedTail.concat(longestChain.slice(0, end)).slice(-pow.MAX_HISTORY);
|
|
227
460
|
}
|
|
228
461
|
|
|
229
|
-
/**
|
|
230
|
-
|
|
462
|
+
/**
|
|
463
|
+
* Height of the main chain's tip
|
|
464
|
+
* @return {number}
|
|
465
|
+
*/
|
|
466
|
+
getTipHeight() {
|
|
467
|
+
return this.startBlockHeight + this.droppedHeadersCount + this.prunedHeaders.length
|
|
468
|
+
+ this.getLongestChain().length - 1;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* @private
|
|
473
|
+
* @return {number} height of the first header in the main chain's unconfirmed part
|
|
474
|
+
*/
|
|
475
|
+
getFirstUnconfirmedHeight() {
|
|
476
|
+
return this.getTipHeight() - this.getLongestChain().length + 1;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* @private
|
|
481
|
+
* @param {BlockHeader[]} headers
|
|
482
|
+
* @param {BlockHeader[]} initialHistory - headers before them, ending with
|
|
483
|
+
* the parent of the first one
|
|
484
|
+
* @param {number} firstHeight - height of the first header
|
|
485
|
+
*/
|
|
486
|
+
validateConnectedHeaders(headers, initialHistory, firstHeight) {
|
|
231
487
|
const history = initialHistory.slice();
|
|
232
488
|
|
|
233
|
-
headers.forEach((header) => {
|
|
489
|
+
headers.forEach((header, i) => {
|
|
234
490
|
const previousHeader = history[history.length - 1];
|
|
235
491
|
if (!previousHeader || !SpvChain.isParentChild(header, previousHeader)) {
|
|
236
492
|
throw new SPVError(`SPV: Header ${header.hash} is not connected to verified history`);
|
|
237
493
|
}
|
|
238
494
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
495
|
+
if (this.isDuplicate(header.hash) || !Consensus.isValidBlockHeader(
|
|
496
|
+
header,
|
|
497
|
+
history,
|
|
498
|
+
this.consensusNetwork,
|
|
499
|
+
firstHeight + i,
|
|
500
|
+
this.startBlockHeight,
|
|
501
|
+
)) {
|
|
242
502
|
throw new SPVError(`SPV: Header ${header.hash} is invalid`);
|
|
243
503
|
}
|
|
244
504
|
|
|
245
505
|
history.push(header);
|
|
246
|
-
|
|
247
|
-
|
|
506
|
+
// Trim in bulk so each header costs O(1) on average
|
|
507
|
+
if (history.length > 2 * pow.MAX_HISTORY) {
|
|
508
|
+
history.splice(0, history.length - pow.MAX_HISTORY);
|
|
248
509
|
}
|
|
249
510
|
});
|
|
250
511
|
}
|
|
@@ -254,19 +515,6 @@ const SpvChain = class {
|
|
|
254
515
|
return this.orphanChunks;
|
|
255
516
|
}
|
|
256
517
|
|
|
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
518
|
/**
|
|
271
519
|
* verifies the parent child connection
|
|
272
520
|
* between two adjacent dashcore.BlockHeader objects
|
|
@@ -284,15 +532,16 @@ const SpvChain = class {
|
|
|
284
532
|
}
|
|
285
533
|
|
|
286
534
|
/**
|
|
287
|
-
* Returns the
|
|
535
|
+
* Returns the main chain's unconfirmed headers, or with `withPruned` all the
|
|
536
|
+
* headers the chain still keeps (at least the last pow.MAX_HISTORY)
|
|
288
537
|
* @param options
|
|
289
|
-
* @returns {
|
|
538
|
+
* @returns {BlockHeader[]}
|
|
290
539
|
*/
|
|
291
540
|
getLongestChain(options = { withPruned: false }) {
|
|
292
|
-
|
|
541
|
+
const [longestChain] = this.allBranches;
|
|
293
542
|
|
|
294
543
|
if (options.withPruned) {
|
|
295
|
-
|
|
544
|
+
return this.prunedHeaders.concat(longestChain);
|
|
296
545
|
}
|
|
297
546
|
|
|
298
547
|
return longestChain;
|
|
@@ -324,8 +573,25 @@ const SpvChain = class {
|
|
|
324
573
|
* @return {Object} header
|
|
325
574
|
*/
|
|
326
575
|
getHeader(hash) {
|
|
327
|
-
|
|
328
|
-
return
|
|
576
|
+
const height = this.heightByHash.get(hash);
|
|
577
|
+
return height === undefined ? undefined : this.getHeaderAt(height);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* gets the main chain's header at a height, if the chain still keeps it
|
|
582
|
+
*
|
|
583
|
+
* @param {number} height
|
|
584
|
+
* @return {BlockHeader|undefined}
|
|
585
|
+
*/
|
|
586
|
+
getHeaderAt(height) {
|
|
587
|
+
const longestChain = this.getLongestChain();
|
|
588
|
+
const fromTip = this.getTipHeight() - height;
|
|
589
|
+
if (fromTip < 0) {
|
|
590
|
+
return undefined;
|
|
591
|
+
}
|
|
592
|
+
return fromTip < longestChain.length
|
|
593
|
+
? longestChain[longestChain.length - 1 - fromTip]
|
|
594
|
+
: this.prunedHeaders[this.prunedHeaders.length - 1 - (fromTip - longestChain.length)];
|
|
329
595
|
}
|
|
330
596
|
|
|
331
597
|
/**
|
|
@@ -346,26 +612,27 @@ const SpvChain = class {
|
|
|
346
612
|
throw new SPVError('Chain not initialized, either call initialize() or set pendingStartBlockHeight');
|
|
347
613
|
}
|
|
348
614
|
|
|
349
|
-
|
|
615
|
+
// Copies, so later changes to the caller's objects can't alter the chain
|
|
616
|
+
const normalizedHeaders = headers.map((h) => utils.copyHeader(h));
|
|
350
617
|
|
|
351
618
|
let isOrphan = false;
|
|
352
619
|
|
|
353
620
|
// Chain is initialized, root block, and it's height are known
|
|
354
621
|
if (this.pendingStartBlockHeight === null) {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
normalizedHeaders.
|
|
622
|
+
// Skip headers the chain already has, such as an overlap with the last batch
|
|
623
|
+
let knownCount = 0;
|
|
624
|
+
while (knownCount < normalizedHeaders.length
|
|
625
|
+
&& this.hasHeader(normalizedHeaders[knownCount].hash)) {
|
|
626
|
+
knownCount += 1;
|
|
359
627
|
}
|
|
628
|
+
normalizedHeaders.splice(0, knownCount);
|
|
360
629
|
|
|
361
630
|
if (normalizedHeaders.length === 0) {
|
|
362
631
|
// The batch already in the chain, do nothing
|
|
363
632
|
return [];
|
|
364
633
|
}
|
|
365
634
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
isOrphan = !SpvChain.isParentChild(firstHeader, tip);
|
|
635
|
+
isOrphan = !this.connectHeaders(normalizedHeaders);
|
|
369
636
|
} else if (batchHeadHeight === this.pendingStartBlockHeight) {
|
|
370
637
|
throw new SPVError(
|
|
371
638
|
'SPV chain cannot initialize from an unauthenticated remote checkpoint',
|
|
@@ -385,7 +652,7 @@ const SpvChain = class {
|
|
|
385
652
|
);
|
|
386
653
|
}
|
|
387
654
|
if (this.isDuplicate(header.hash)
|
|
388
|
-
|| !Consensus.isValidBlockHeaderWithoutContext(header, this.
|
|
655
|
+
|| !Consensus.isValidBlockHeaderWithoutContext(header, this.consensusNetwork)) {
|
|
389
656
|
throw new SPVError(`SPV: Header ${header.hash} is invalid`);
|
|
390
657
|
}
|
|
391
658
|
});
|
|
@@ -394,9 +661,6 @@ const SpvChain = class {
|
|
|
394
661
|
this.orphansHashes.add(header.hash);
|
|
395
662
|
});
|
|
396
663
|
this.orphanChunks.push(normalizedHeaders);
|
|
397
|
-
} else {
|
|
398
|
-
this.validateConnectedHeaders(normalizedHeaders, this.getValidationHistory());
|
|
399
|
-
this.appendHeadersToLongestChain(normalizedHeaders);
|
|
400
664
|
}
|
|
401
665
|
if (this.orphanChunks.length > 0) {
|
|
402
666
|
this.orphanChunksReconnect();
|
|
@@ -404,6 +668,8 @@ const SpvChain = class {
|
|
|
404
668
|
this.checkPruneBlocks();
|
|
405
669
|
return normalizedHeaders;
|
|
406
670
|
}
|
|
407
|
-
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
SpvChain.EVENTS = EVENTS;
|
|
408
674
|
|
|
409
675
|
module.exports = SpvChain;
|
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.1.0-dev.1",
|
|
4
4
|
"description": "Repository containing SPV functions used by @dashevo",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,7 +12,6 @@
|
|
|
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
16
|
"@dashevo/dashcore-lib": "~0.26.1",
|
|
18
17
|
"levelup": "^4.4.0",
|