@dashevo/dash-spv 6.0.3 → 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 CHANGED
@@ -1 +1 @@
1
- {"files":{".gitignore":"095d365a56f423d4fab2345c95c137e7c6ea45c0",".mocharc.yml":"f6d924f143da5bc22530e55dcec6922aff69d8e1","LICENSE":"95bf3c7cb642fb61c1bbbb319fd3c83e47a5af96","README.md":"3659fd1bfb44b5e6916679ae586ccbff18683e59","config/config.js":"267c3536b5fe54c88080a56241c34d7393a54ac2","eslint.config.mjs":"cdf50e57d0d206e17f37b855643f55f73717fcd2","index.js":"4413accedb71ffb73ea7ce4ad45a2035c2ce92ca","lib/blockstore.js":"1ed82a8c1cfb4bc3aa96f0669a27c94d2e76b164","lib/consensus.js":"ebe1560f51946ce43f877839f4309b2dd6848d66","lib/errors/SPVError.js":"08b7af972c146bcdf29ffaa06eb90ca2c5660914","lib/merkleproofs.js":"1bba2c774e52e3cdfd4b99973c11229e6cf2cf6d","lib/pow.js":"4e67d46ad8f781944313d6848ee3d757541081ee","lib/spvchain.js":"c9b5ea181e77894c347b3efc12ceaf6e4e178be9","lib/test/bootstrap.js":"e2b2b2b17a9bf0ebff7721d8cb1e5cb75efb0f0f","lib/utils.js":"fa8af5df5f540bbe61a6c34c223d3d8d86c40481","lib/x11.js":"d85e555bad579a8b3e2e92447331933ce9b0c4b1","package.json":"5546c214e63a4a045371aaeb70ff4b69045d695d","test/data/difficulty.js":"b90be3791894be797ec26e00ba35bde45fcc0703","test/data/headers.js":"eb9cae85ee11027338fbf1f69e373d22388286d6","test/data/merkleproofs.js":"49ea4c29fb1f0cf6378ed7ee46b0a1db3bb555c1","test/data/rawHeaders.js":"03fed6ca2f8c2d1b63cfa13743431a2d551c7ad9","test/index.js":"05d843e5fa9ec7a6a41be26fac22e5eb60b42375","test/pow.js":"08a32b2f4a0becedfee579c610af19b39a3d6933","test/spvchain.js":"f6441870e91b7faf2e6b23d034f40d007a697652"},"deps":{}}
1
+ {"files":{".gitignore":"095d365a56f423d4fab2345c95c137e7c6ea45c0",".mocharc.yml":"f6d924f143da5bc22530e55dcec6922aff69d8e1","LICENSE":"95bf3c7cb642fb61c1bbbb319fd3c83e47a5af96","README.md":"3659fd1bfb44b5e6916679ae586ccbff18683e59","config/config.js":"267c3536b5fe54c88080a56241c34d7393a54ac2","eslint.config.mjs":"cdf50e57d0d206e17f37b855643f55f73717fcd2","index.js":"4413accedb71ffb73ea7ce4ad45a2035c2ce92ca","lib/blockstore.js":"1ed82a8c1cfb4bc3aa96f0669a27c94d2e76b164","lib/consensus.js":"ebe1560f51946ce43f877839f4309b2dd6848d66","lib/errors/SPVError.js":"08b7af972c146bcdf29ffaa06eb90ca2c5660914","lib/merkleproofs.js":"1bba2c774e52e3cdfd4b99973c11229e6cf2cf6d","lib/pow.js":"0d92761dc7876fe3e27e892a796a6422f2122f30","lib/spvchain.js":"e42d3c036e3ddf30d23551b0a740ccdd58322609","lib/test/bootstrap.js":"e2b2b2b17a9bf0ebff7721d8cb1e5cb75efb0f0f","lib/utils.js":"fa8af5df5f540bbe61a6c34c223d3d8d86c40481","lib/x11.js":"d85e555bad579a8b3e2e92447331933ce9b0c4b1","package.json":"011aa588f1489fca40b18720a2dd3d07e8c3e052","test/data/difficulty.js":"b90be3791894be797ec26e00ba35bde45fcc0703","test/data/headers.js":"eb9cae85ee11027338fbf1f69e373d22388286d6","test/data/merkleproofs.js":"49ea4c29fb1f0cf6378ed7ee46b0a1db3bb555c1","test/data/rawHeaders.js":"03fed6ca2f8c2d1b63cfa13743431a2d551c7ad9","test/index.js":"7fa94ca6269a23c6bf661cc45c7639b4ace9460c","test/pow.js":"08a32b2f4a0becedfee579c610af19b39a3d6933","test/reorg.js":"d9c8f33e79ce0fdbe1b3c582c82412c7d1a0dd89","test/spvchain.js":"f6441870e91b7faf2e6b23d034f40d007a697652"},"deps":{}}
package/lib/pow.js CHANGED
@@ -165,6 +165,28 @@ function checkProofOfWork(hash, bits, network) {
165
165
  return BigInt(`0x${hash}`) <= target;
166
166
  }
167
167
 
168
+ /**
169
+ * GetBlockProof: the expected number of hashes to mine a header
170
+ * @param {number} bits
171
+ * @return {bigint}
172
+ */
173
+ function getBlockProof(bits) {
174
+ const { target, negative, overflow } = decodeCompact(bits);
175
+ if (negative || overflow || target === 0n) {
176
+ return 0n;
177
+ }
178
+ // 2**256 / (target + 1), computed as ~target / (target + 1) + 1
179
+ return ((~target & UINT256_MASK) / (target + 1n)) + 1n;
180
+ }
181
+
182
+ /**
183
+ * @param {BlockHeader[]} headers
184
+ * @return {bigint} the work of all the headers
185
+ */
186
+ function getChainWork(headers) {
187
+ return headers.reduce((work, header) => work + getBlockProof(header.bits), 0n);
188
+ }
189
+
168
190
  /**
169
191
  * ConvertBitsToDouble (difficulty as a double)
170
192
  * @param {number} bits
@@ -505,6 +527,7 @@ module.exports = {
505
527
  MAX_HISTORY,
506
528
  getNetworkParams,
507
529
  checkProofOfWork,
530
+ getChainWork,
508
531
  getExpectedBits,
509
532
  hasValidDifficulty,
510
533
  getMedianTimePast,
package/lib/spvchain.js CHANGED
@@ -1,3 +1,4 @@
1
+ const EventEmitter = require('events');
1
2
  const X11 = require('./x11');
2
3
 
3
4
  const config = require('../config/config');
@@ -29,15 +30,26 @@ const createGenesis = (chainType) => {
29
30
  }
30
31
  };
31
32
 
32
- const SpvChain = class {
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 {
33
43
  /**
34
44
  * @param {string} chainType
35
- * @param {number} [confirms]
45
+ * @param {number} [confirms] - how many of the last headers can still be
46
+ * replaced by a fork with more work
36
47
  * @param {object} [devnetOptions] - settings a devnet was started with
37
48
  * @param {number} [devnetOptions.powTargetSpacing] - Core's -powtargetspacing
38
49
  * @param {number} [devnetOptions.minimumDifficultyBlocks] - Core's -minimumdifficultyblocks
39
50
  */
40
51
  constructor(chainType, confirms = 100, devnetOptions = undefined) {
52
+ super();
41
53
  this.confirmsBeforeFinal = confirms;
42
54
  this.network = CHAIN_TYPE_NETWORKS[chainType];
43
55
  if (!this.network) {
@@ -56,9 +68,12 @@ const SpvChain = class {
56
68
  }
57
69
 
58
70
  reset() {
71
+ // The main chain's unconfirmed headers first, then forks off them
59
72
  this.allBranches = [[]];
60
73
  this.orphanChunks = [];
74
+ // Confirmed headers kept for validation; older ones are dropped
61
75
  this.prunedHeaders = [];
76
+ this.droppedHeadersCount = 0;
62
77
 
63
78
  this.hashesByHeight = new Map();
64
79
  this.heightByHash = new Map();
@@ -159,6 +174,22 @@ const SpvChain = class {
159
174
  .forEach((header) => {
160
175
  this.prunedHeaders.push(header);
161
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
+ }
162
193
  }
163
194
 
164
195
  /** @private */
@@ -173,10 +204,14 @@ const SpvChain = class {
173
204
  this.allBranches.push(branch);
174
205
  }
175
206
 
176
- /** @private */
177
- appendHeadersToLongestChain(headers) {
178
- const longestChain = this.getLongestChain();
207
+ /**
208
+ * @private
209
+ * Validates headers that continue the main chain's tip and appends them
210
+ * @param {BlockHeader[]} headers
211
+ */
212
+ extendLongestChain(headers) {
179
213
  const firstHeight = this.getTipHeight() + 1;
214
+ this.validateConnectedHeaders(headers, this.getValidationHistory(), firstHeight);
180
215
 
181
216
  headers.forEach((header, i) => {
182
217
  const height = firstHeight + i;
@@ -184,10 +219,9 @@ const SpvChain = class {
184
219
  this.heightByHash.set(header.hash, height);
185
220
  });
186
221
 
187
- const newLongestChain = longestChain.concat(headers);
222
+ this.allBranches[0] = this.getLongestChain().concat(headers);
188
223
 
189
- this.allBranches = [];
190
- this.allBranches.push(newLongestChain);
224
+ this.emit(EVENTS.CONNECTED, headers, firstHeight);
191
225
  }
192
226
 
193
227
  /** @private */
@@ -195,62 +229,251 @@ const SpvChain = class {
195
229
  return this.allBranches;
196
230
  }
197
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
+
198
290
  /** @private */
199
291
  isDuplicate(hash) {
200
- return this.heightByHash.has(hash) || this.orphansHashes.has(hash);
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;
201
393
  }
202
394
 
203
395
  /** @private */
204
- orphanChunksReconnect() {
205
- // TODO: consider optimizing with map of { [chunkHeadHash]: chunkIndex }
206
- // to get rid of sorting and make the whole function of O(n) complexity
207
- this.orphanChunks.sort((a, b) => a[0].timestamp - b[0].timestamp);
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);
412
+
413
+ this.allBranches = [longestChain, ...this.normalizeForks(this.getForks())];
414
+
415
+ this.emit(EVENTS.CONNECTED, fork, forkParentHeight + 1);
416
+ }
208
417
 
418
+ /** @private */
419
+ orphanChunksReconnect() {
209
420
  for (let i = 0; i < this.orphanChunks.length; i += 1) {
210
421
  const chunk = this.orphanChunks[i];
211
- const tipHash = this.getTipHash();
212
- const chunkPrevHash = utils.getCorrectedHash(chunk[0].prevHash);
213
422
 
214
- if (tipHash === chunkPrevHash) {
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
+
215
430
  try {
216
- this.validateConnectedHeaders(
217
- chunk,
218
- this.getValidationHistory(),
219
- this.getTipHeight() + 1,
220
- true,
221
- );
222
- this.appendHeadersToLongestChain(chunk);
223
- chunk.forEach((header) => {
224
- this.orphansHashes.delete(header.hash);
225
- });
226
- this.orphanChunks.splice(i, 1);
227
- i -= 1;
431
+ this.connectHeaders(chunk);
228
432
  } catch (e) {
229
- chunk.forEach((header) => {
230
- this.orphansHashes.delete(header.hash);
231
- });
232
- this.orphanChunks.splice(i, 1);
233
- throw e;
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
+ }
234
439
  }
440
+
441
+ // The chain may now connect a chunk checked earlier
442
+ i = -1;
235
443
  }
236
444
  }
237
445
  }
238
446
 
239
447
  /**
240
448
  * @private
241
- * The last headers of the chain, as many as difficulty validation reads
449
+ * The main chain's headers up to `lastHeight`, as many as difficulty
450
+ * validation reads
451
+ * @param {number} [lastHeight] - the tip by default
242
452
  * @return {BlockHeader[]}
243
453
  */
244
- getValidationHistory() {
454
+ getValidationHistory(lastHeight = this.getTipHeight()) {
245
455
  const longestChain = this.getLongestChain();
246
- const fromPruned = pow.MAX_HISTORY - longestChain.length;
456
+ const end = longestChain.length - (this.getTipHeight() - lastHeight);
457
+ const fromPruned = pow.MAX_HISTORY - end;
247
458
  const prunedTail = fromPruned > 0 ? this.prunedHeaders.slice(-fromPruned) : [];
248
- return prunedTail.concat(longestChain).slice(-pow.MAX_HISTORY);
459
+ return prunedTail.concat(longestChain.slice(0, end)).slice(-pow.MAX_HISTORY);
249
460
  }
250
461
 
251
- /** @private */
462
+ /**
463
+ * Height of the main chain's tip
464
+ * @return {number}
465
+ */
252
466
  getTipHeight() {
253
- return this.startBlockHeight + this.prunedHeaders.length + this.getLongestChain().length - 1;
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;
254
477
  }
255
478
 
256
479
  /**
@@ -259,9 +482,8 @@ const SpvChain = class {
259
482
  * @param {BlockHeader[]} initialHistory - headers before them, ending with
260
483
  * the parent of the first one
261
484
  * @param {number} firstHeight - height of the first header
262
- * @param {boolean} [allowKnownOrphans]
263
485
  */
264
- validateConnectedHeaders(headers, initialHistory, firstHeight, allowKnownOrphans = false) {
486
+ validateConnectedHeaders(headers, initialHistory, firstHeight) {
265
487
  const history = initialHistory.slice();
266
488
 
267
489
  headers.forEach((header, i) => {
@@ -270,9 +492,7 @@ const SpvChain = class {
270
492
  throw new SPVError(`SPV: Header ${header.hash} is not connected to verified history`);
271
493
  }
272
494
 
273
- const isKnown = this.heightByHash.has(header.hash)
274
- || (!allowKnownOrphans && this.orphansHashes.has(header.hash));
275
- if (isKnown || !Consensus.isValidBlockHeader(
495
+ if (this.isDuplicate(header.hash) || !Consensus.isValidBlockHeader(
276
496
  header,
277
497
  history,
278
498
  this.consensusNetwork,
@@ -312,15 +532,16 @@ const SpvChain = class {
312
532
  }
313
533
 
314
534
  /**
315
- * Returns the longest chain of headers
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)
316
537
  * @param options
317
- * @returns {*}
538
+ * @returns {BlockHeader[]}
318
539
  */
319
540
  getLongestChain(options = { withPruned: false }) {
320
- let longestChain = this.allBranches.sort((b1, b2) => b1 < b2)[0];
541
+ const [longestChain] = this.allBranches;
321
542
 
322
543
  if (options.withPruned) {
323
- longestChain = this.prunedHeaders.concat(longestChain);
544
+ return this.prunedHeaders.concat(longestChain);
324
545
  }
325
546
 
326
547
  return longestChain;
@@ -352,8 +573,25 @@ const SpvChain = class {
352
573
  * @return {Object} header
353
574
  */
354
575
  getHeader(hash) {
355
- // TODO: perform lookup in pruned headers?
356
- return this.getLongestChain().filter((h) => h.hash === hash)[0];
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)];
357
595
  }
358
596
 
359
597
  /**
@@ -381,20 +619,20 @@ const SpvChain = class {
381
619
 
382
620
  // Chain is initialized, root block, and it's height are known
383
621
  if (this.pendingStartBlockHeight === null) {
384
- const tip = this.getTipHeader();
385
- // Handle 1 block intersection of batches
386
- if (tip.hash === normalizedHeaders[0].hash) {
387
- normalizedHeaders.splice(0, 1);
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;
388
627
  }
628
+ normalizedHeaders.splice(0, knownCount);
389
629
 
390
630
  if (normalizedHeaders.length === 0) {
391
631
  // The batch already in the chain, do nothing
392
632
  return [];
393
633
  }
394
634
 
395
- const firstHeader = normalizedHeaders[0];
396
-
397
- isOrphan = !SpvChain.isParentChild(firstHeader, tip);
635
+ isOrphan = !this.connectHeaders(normalizedHeaders);
398
636
  } else if (batchHeadHeight === this.pendingStartBlockHeight) {
399
637
  throw new SPVError(
400
638
  'SPV chain cannot initialize from an unauthenticated remote checkpoint',
@@ -423,13 +661,6 @@ const SpvChain = class {
423
661
  this.orphansHashes.add(header.hash);
424
662
  });
425
663
  this.orphanChunks.push(normalizedHeaders);
426
- } else {
427
- this.validateConnectedHeaders(
428
- normalizedHeaders,
429
- this.getValidationHistory(),
430
- this.getTipHeight() + 1,
431
- );
432
- this.appendHeadersToLongestChain(normalizedHeaders);
433
664
  }
434
665
  if (this.orphanChunks.length > 0) {
435
666
  this.orphanChunksReconnect();
@@ -437,6 +668,8 @@ const SpvChain = class {
437
668
  this.checkPruneBlocks();
438
669
  return normalizedHeaders;
439
670
  }
440
- };
671
+ }
672
+
673
+ SpvChain.EVENTS = EVENTS;
441
674
 
442
675
  module.exports = SpvChain;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dashevo/dash-spv",
3
- "version": "6.0.3",
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": {
package/test/index.js CHANGED
@@ -328,9 +328,10 @@ describe('SPV-DASH (addHeaders) add many headers for mainnet', () => {
328
328
  chain.getLongestChain().length.should.equal(1500);
329
329
  });
330
330
 
331
- it('should not add an invalid header', () => {
332
- should(() => chain.addHeaders([mainnet[499]])).throw();
331
+ it('should ignore a header it already has', () => {
332
+ chain.addHeaders([mainnet[499]]);
333
333
  chain.getLongestChain().length.should.equal(1500);
334
+ chain.getOrphanChunks().length.should.equal(0);
334
335
  });
335
336
 
336
337
  it('should reject orphan headers outside the network proof-of-work limit', () => {
package/test/reorg.js ADDED
@@ -0,0 +1,359 @@
1
+ const { expect } = require('chai');
2
+ const { BlockHeader } = require('@dashevo/dashcore-lib');
3
+
4
+ const SpvChain = require('../lib/spvchain');
5
+ const pow = require('../lib/pow');
6
+ const utils = require('../lib/utils');
7
+
8
+ const REGTEST_BITS = 0x207fffff;
9
+
10
+ // Mines a regtest header on `prev`; `salt` makes sibling headers differ
11
+ const mine = (prev, salt = 0) => {
12
+ const merkleRoot = Buffer.alloc(32);
13
+ merkleRoot.writeUInt32LE(salt);
14
+ let nonce = 0;
15
+ let header;
16
+ do {
17
+ header = new BlockHeader({
18
+ version: 2,
19
+ prevHash: Buffer.from(prev.hash, 'hex').reverse(),
20
+ merkleRoot,
21
+ time: prev.time + 1,
22
+ bits: REGTEST_BITS,
23
+ nonce,
24
+ });
25
+ nonce += 1;
26
+ } while (!utils.validProofOfWork(header));
27
+ return header;
28
+ };
29
+
30
+ const mineChain = (from, length, salt = 0) => {
31
+ const headers = [];
32
+ let prev = from;
33
+ for (let i = 0; i < length; i += 1) {
34
+ prev = mine(prev, salt);
35
+ headers.push(prev);
36
+ }
37
+ return headers;
38
+ };
39
+
40
+ describe('SpvChain reorgs', () => {
41
+ let chain;
42
+ let main;
43
+ let connected;
44
+
45
+ before(async () => {
46
+ await SpvChain.wasmX11Ready();
47
+ });
48
+
49
+ beforeEach(() => {
50
+ chain = new SpvChain('regtest');
51
+ chain.initialize();
52
+ main = mineChain(chain.genesis, 5);
53
+ chain.addHeaders(main);
54
+
55
+ connected = [];
56
+ chain.on(SpvChain.EVENTS.CONNECTED, (headers, firstHeight) => {
57
+ connected.push({ hashes: headers.map((h) => h.hash), firstHeight });
58
+ });
59
+ });
60
+
61
+ it('should switch to a fork from below the tip once it has more work', () => {
62
+ const fork = mineChain(main[3], 2, 1); // heights 5 and 6, on height 4
63
+
64
+ chain.addHeaders([fork[0]]);
65
+ expect(chain.getTipHash()).to.equal(main[4].hash);
66
+
67
+ chain.addHeaders([fork[1]]);
68
+
69
+ expect(chain.getTipHash()).to.equal(fork[1].hash);
70
+ expect(chain.getTipHeight()).to.equal(6);
71
+ expect(chain.hashesByHeight.get(5)).to.equal(fork[0].hash);
72
+ expect(chain.heightByHash.has(main[4].hash)).to.equal(false);
73
+ expect(chain.getOrphanChunks()).to.have.lengthOf(0);
74
+ expect(() => chain.validate()).to.not.throw();
75
+ expect(connected).to.deep.equal([{ hashes: fork.map((h) => h.hash), firstHeight: 5 }]);
76
+ });
77
+
78
+ it('should keep following the new chain after a reorg', () => {
79
+ const fork = mineChain(main[3], 2, 1);
80
+ chain.addHeaders(fork);
81
+
82
+ const next = mine(fork[1]);
83
+ chain.addHeaders([next]);
84
+
85
+ expect(chain.getTipHash()).to.equal(next.hash);
86
+ expect(chain.getTipHeight()).to.equal(7);
87
+ });
88
+
89
+ it('should switch back when the old chain outgrows the fork', () => {
90
+ const fork = mineChain(main[3], 2, 1);
91
+ chain.addHeaders(fork);
92
+
93
+ const oldChainExtension = mineChain(main[4], 2); // heights 6 and 7 on the first chain
94
+ chain.addHeaders(oldChainExtension);
95
+
96
+ expect(chain.getTipHash()).to.equal(oldChainExtension[1].hash);
97
+ expect(chain.hashesByHeight.get(5)).to.equal(main[4].hash);
98
+ expect(connected[connected.length - 1]).to.deep.equal({
99
+ hashes: [main[4], ...oldChainExtension].map((h) => h.hash),
100
+ firstHeight: 5,
101
+ });
102
+ });
103
+
104
+ it('should report headers connected to the tip with their height', () => {
105
+ const next = mineChain(main[4], 2);
106
+ chain.addHeaders(next);
107
+
108
+ expect(connected).to.deep.equal([{ hashes: next.map((h) => h.hash), firstHeight: 6 }]);
109
+ });
110
+
111
+ it('should reject a fork from below the confirmed headers', () => {
112
+ chain = new SpvChain('regtest', 3);
113
+ chain.initialize();
114
+ chain.addHeaders(main);
115
+
116
+ expect(() => chain.addHeaders(mineChain(main[0], 6, 1))).to.throw();
117
+ expect(chain.getTipHash()).to.equal(main[4].hash);
118
+ });
119
+ });
120
+
121
+ describe('SpvChain memory', () => {
122
+ let chain;
123
+ let headers;
124
+
125
+ before(async () => {
126
+ await SpvChain.wasmX11Ready();
127
+ chain = new SpvChain('regtest');
128
+ chain.initialize();
129
+ headers = mineChain(chain.genesis, pow.MAX_HISTORY + 1000);
130
+ chain.addHeaders(headers);
131
+ });
132
+
133
+ it('should keep only the confirmed headers validation reads', () => {
134
+ expect(chain.getLongestChain()).to.have.lengthOf(100);
135
+ expect(chain.prunedHeaders).to.have.lengthOf(pow.MAX_HISTORY);
136
+ expect(chain.hashesByHeight.size).to.equal(pow.MAX_HISTORY + 100);
137
+ expect(chain.heightByHash.size).to.equal(pow.MAX_HISTORY + 100);
138
+ expect(() => chain.validate()).to.not.throw();
139
+ });
140
+
141
+ it('should still know the tip height', () => {
142
+ expect(chain.getTipHeight()).to.equal(headers.length);
143
+ expect(chain.hashesByHeight.get(headers.length)).to.equal(headers[headers.length - 1].hash);
144
+ });
145
+
146
+ it('should find kept headers by hash, confirmed or not', () => {
147
+ const confirmed = headers[headers.length - 200];
148
+ const unconfirmed = headers[headers.length - 50];
149
+
150
+ expect(chain.getHeader(confirmed.hash).hash).to.equal(confirmed.hash);
151
+ expect(chain.getHeader(unconfirmed.hash).hash).to.equal(unconfirmed.hash);
152
+ expect(chain.getHeader(headers[0].hash)).to.equal(undefined);
153
+ });
154
+
155
+ it('should keep validating new headers', () => {
156
+ const next = mineChain(headers[headers.length - 1], 3);
157
+ chain.addHeaders(next);
158
+
159
+ expect(chain.getTipHash()).to.equal(next[2].hash);
160
+ expect(chain.prunedHeaders).to.have.lengthOf(pow.MAX_HISTORY);
161
+ });
162
+ });
163
+
164
+ describe('SpvChain forks', () => {
165
+ let chain;
166
+ let main;
167
+
168
+ before(async () => {
169
+ await SpvChain.wasmX11Ready();
170
+ });
171
+
172
+ beforeEach(() => {
173
+ chain = new SpvChain('regtest', 3);
174
+ chain.initialize();
175
+ main = mineChain(chain.genesis, 5);
176
+ chain.addHeaders(main);
177
+ });
178
+
179
+ it('should keep the main chain first while a fork has less work', () => {
180
+ const fork = mine(main[3], 1);
181
+ chain.addHeaders([fork]);
182
+
183
+ expect(chain.getAllBranches()).to.have.lengthOf(2);
184
+ expect(chain.getLongestChain()[2].hash).to.equal(main[4].hash);
185
+ expect(chain.getTipHash()).to.equal(main[4].hash);
186
+ });
187
+
188
+ it('should ignore fork headers it already has', () => {
189
+ const fork = mineChain(main[3], 2, 1);
190
+ chain.addHeaders(fork);
191
+
192
+ const branches = chain.getAllBranches().map((branch) => branch.map((h) => h.hash));
193
+
194
+ chain.addHeaders([main[3], ...fork]);
195
+
196
+ expect(chain.getAllBranches().map((branch) => branch.map((h) => h.hash)))
197
+ .to.deep.equal(branches);
198
+ expect(chain.getTipHash()).to.equal(fork[1].hash);
199
+ });
200
+
201
+ it('should start a fork off a fork header', () => {
202
+ // Same work as the main chain's heights 4 and 5, so no reorg
203
+ const fork = mineChain(main[2], 2, 1);
204
+ chain.addHeaders(fork);
205
+ const forkOfFork = mineChain(fork[0], 2, 2);
206
+
207
+ chain.addHeaders(forkOfFork);
208
+
209
+ // Heights 4' 5'' 6'' have more work than 4 and 5
210
+ expect(chain.getTipHash()).to.equal(forkOfFork[1].hash);
211
+ expect(chain.hashesByHeight.get(4)).to.equal(fork[0].hash);
212
+ expect(chain.getTipHeight()).to.equal(6);
213
+ expect(() => chain.validate()).to.not.throw();
214
+ // The two replaced headers and the rest of the first fork remain, the
215
+ // latter now rooted at its header that joined the main chain
216
+ expect(chain.getAllBranches().slice(1).map((branch) => branch.map((h) => h.hash)))
217
+ .to.have.deep.members([[main[3].hash, main[4].hash], [fork[1].hash]]);
218
+ });
219
+
220
+ it('should keep a fork whose start joined the main chain while it can still win', () => {
221
+ // As above: main chain A1..A3 B4 C5 C6, with B5 left on the side
222
+ const fork = mineChain(main[2], 2, 1);
223
+ chain.addHeaders(fork);
224
+ const forkOfFork = mineChain(fork[0], 2, 2);
225
+ chain.addHeaders(forkOfFork);
226
+
227
+ // C7 confirms height 4 (B4); B5 still forks off it, at unconfirmed height 5
228
+ chain.addHeaders([mine(forkOfFork[1], 3)]);
229
+
230
+ const winner = mineChain(fork[1], 3, 4); // B6 B7 B8
231
+ chain.addHeaders(winner);
232
+
233
+ expect(chain.getTipHash()).to.equal(winner[2].hash);
234
+ expect(chain.hashesByHeight.get(5)).to.equal(fork[1].hash);
235
+ expect(() => chain.validate()).to.not.throw();
236
+ });
237
+
238
+ it('should drop an invalid orphan chunk without rejecting the headers that connected it', () => {
239
+ chain = new SpvChain('regtest', 100);
240
+ chain.initialize();
241
+ chain.addHeaders(main.slice(0, 3));
242
+ // A child of height 5 that is older than the median time before it
243
+ let invalid;
244
+ let nonce = 0;
245
+ do {
246
+ invalid = new BlockHeader({
247
+ version: 2,
248
+ prevHash: Buffer.from(main[4].hash, 'hex').reverse(),
249
+ merkleRoot: Buffer.alloc(32),
250
+ time: chain.genesis.time,
251
+ bits: REGTEST_BITS,
252
+ nonce,
253
+ });
254
+ nonce += 1;
255
+ } while (!utils.validProofOfWork(invalid));
256
+ chain.addHeaders([invalid]);
257
+ expect(chain.getOrphanChunks()).to.have.lengthOf(1);
258
+
259
+ chain.addHeaders(main.slice(3));
260
+
261
+ expect(chain.getTipHash()).to.equal(main[4].hash);
262
+ expect(chain.getOrphanChunks()).to.have.lengthOf(0);
263
+ expect(chain.hasHeader(invalid.hash)).to.equal(false);
264
+ });
265
+
266
+ it('should connect headers that arrived before the fork header they continue', () => {
267
+ chain = new SpvChain('regtest', 100);
268
+ chain.initialize();
269
+ chain.addHeaders(main);
270
+ const forkAt5 = mine(main[3], 1);
271
+ const [forkAt6, forkAt7] = mineChain(forkAt5, 2, 1);
272
+
273
+ chain.addHeaders([forkAt6, forkAt7]);
274
+ expect(chain.getOrphanChunks()).to.have.lengthOf(1);
275
+
276
+ chain.addHeaders([forkAt5]);
277
+
278
+ expect(chain.getOrphanChunks()).to.have.lengthOf(0);
279
+ expect(chain.getTipHash()).to.equal(forkAt7.hash);
280
+ expect(() => chain.validate()).to.not.throw();
281
+ });
282
+
283
+ it('should keep at most 10 forks, dropping the oldest', () => {
284
+ chain = new SpvChain('regtest', 100);
285
+ chain.initialize();
286
+ chain.addHeaders(main);
287
+ const forks = [];
288
+ for (let salt = 1; salt <= 11; salt += 1) {
289
+ forks.push(mine(main[3], salt));
290
+ chain.addHeaders([forks[forks.length - 1]]);
291
+ }
292
+
293
+ expect(chain.getAllBranches()).to.have.lengthOf(11);
294
+ expect(chain.hasHeader(forks[0].hash)).to.equal(false);
295
+ expect(chain.hasHeader(forks[10].hash)).to.equal(true);
296
+ });
297
+
298
+ it('should still keep at most 10 forks after a reorg', () => {
299
+ chain = new SpvChain('regtest', 100);
300
+ chain.initialize();
301
+ chain.addHeaders(main);
302
+ for (let salt = 1; salt <= 10; salt += 1) {
303
+ chain.addHeaders([mine(main[3], salt)]);
304
+ }
305
+
306
+ const winner = mineChain(main[3], 2, 11);
307
+ chain.addHeaders(winner);
308
+
309
+ expect(chain.getTipHash()).to.equal(winner[1].hash);
310
+ expect(chain.getAllBranches()).to.have.lengthOf(11);
311
+ });
312
+
313
+ it('should reorg to a fork much longer than the unconfirmed headers when it has more work', () => {
314
+ // As after a wallet slept through many blocks while its tip was replaced
315
+ const fork = mineChain(main[3], 20, 1);
316
+
317
+ chain.addHeaders(fork);
318
+
319
+ expect(chain.getTipHash()).to.equal(fork[19].hash);
320
+ expect(chain.getTipHeight()).to.equal(24);
321
+ expect(() => chain.validate()).to.not.throw();
322
+ });
323
+
324
+ it('should not keep a fork longer than twice the unconfirmed headers without more work', () => {
325
+ // Every regtest header has the same work, so make the fork's count for less
326
+ const { getChainWork } = pow;
327
+ const fork = mineChain(main[2], 7, 1);
328
+ const forkHashes = new Set(fork.map((h) => h.hash));
329
+ pow.getChainWork = (headers) => (headers.some((h) => forkHashes.has(h.hash))
330
+ ? 0n : getChainWork(headers));
331
+ try {
332
+ expect(() => chain.addHeaders(fork)).to.throw('longer than 6 headers without more work');
333
+ } finally {
334
+ pow.getChainWork = getChainWork;
335
+ }
336
+ expect(chain.getAllBranches()).to.have.lengthOf(1);
337
+ expect(chain.getTipHash()).to.equal(main[4].hash);
338
+ });
339
+
340
+ it('should not keep a fork whose first header is invalid', () => {
341
+ const { hash, ...fields } = mine(main[3], 1).toObject();
342
+ const invalid = new BlockHeader({ ...fields, bits: 0x1d00ffff });
343
+
344
+ expect(() => chain.addHeaders([invalid])).to.throw();
345
+ expect(chain.getAllBranches()).to.have.lengthOf(1);
346
+ });
347
+
348
+ it('should drop a fork once it could only replace confirmed headers', () => {
349
+ chain.addHeaders([mine(main[3], 1)]); // forks off height 4
350
+
351
+ // Heights 5 to 7 unconfirmed: the fork could still replace them
352
+ chain.addHeaders(mineChain(main[4], 2));
353
+ expect(chain.getAllBranches()).to.have.lengthOf(2);
354
+
355
+ // Height 5 confirmed
356
+ chain.addHeaders([mine(chain.getTipHeader())]);
357
+ expect(chain.getAllBranches()).to.have.lengthOf(1);
358
+ });
359
+ });