Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Fix and speed up addHeaders() (#21)
Browse files Browse the repository at this point in the history
* add test section for adding testnet headers

* add testnet tests

* update dark gravity wave

* reactivate mainnet headers

* reactivate mainnet headers

* update dark-gravity-wave

* bump version

* update package lock

* remove bn.js from deps

* bump patch version

* update eslint-plugin-import dep

* update package lock

* revert superfluous bump

* update package lock

* simplify validation for header chunks

* disable some tests for quicker testing

* disable blockstore tests

* add fn getTipHeader()

* add fn appendHeaderToLongestChain()

* reactivate blockstore tests

* disable eslint no-param-reassign

* break reduce function on bad header

* bump version

* update package lock

* refactor to prepare for orphan chunks

* refactor & fix
  • Loading branch information
Cofresi authored May 31, 2019
1 parent f948371 commit 727ad2f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
64 changes: 58 additions & 6 deletions lib/spvchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const SpvChain = class {
this.root = null;
this.allBranches = [];
this.orphanBlocks = [];
this.orphanChunks = [];
this.confirmsBeforeFinal = confirms;
this.init(chainType, startBlock);
this.store = new BlockStore();
Expand Down Expand Up @@ -87,6 +88,10 @@ const SpvChain = class {
return this.getLongestChain().slice(-1)[0].hash;
}

getTipHeader() {
return this.getLongestChain().slice(-1)[0];
}

getHeader(hash) {
return this.store.get(hash)
.then((blockInDB) => {
Expand Down Expand Up @@ -123,6 +128,12 @@ const SpvChain = class {
}
}

appendHeadersToLongestChain(headers) {
const newLongestChain = this.getLongestChain().concat(headers);
this.allBranches = [];
this.allBranches.push(newLongestChain);
}

getAllBranches() {
return this.allBranches;
}
Expand Down Expand Up @@ -160,8 +171,7 @@ const SpvChain = class {
addHeader(header) {
const headerNormalised = utils.normalizeHeader(header);

if (Consensus.isValidBlockHeader(headerNormalised, this.getLongestChain(), this.network)
&& !this.isDuplicate(headerNormalised.hash)) {
if (this.isValid(headerNormalised, this.getLongestChain())) {
headerNormalised.children = [];
this.processValidHeader(headerNormalised);
this.setAllBranches();
Expand All @@ -171,15 +181,57 @@ const SpvChain = class {
return false;
}

/* eslint-disable no-param-reassign */
static isParentChild(header, previousHeader) {
if (utils.getCorrectedHash(header.prevHash) !== previousHeader.hash) {
return false;
}
if (!header.children) {
header.children = [];
}
if (!previousHeader.children) {
previousHeader.children = [];
}
previousHeader.children.push(header);
return true;
}
/* eslint-enable no-param-reassign */

isValid(header, previousHeaders) {
return !!(Consensus.isValidBlockHeader(header, previousHeaders, this.network)
&& !this.isDuplicate(header.hash));
}

addHeaders(headers) {
const self = this;
const allAdded = headers.reduce(
(acc, header) => acc && self.addHeader(header, this.network), true,
const normalizedHeaders = headers.map(h => utils.normalizeHeader(h));
const isOrphan = !SpvChain.isParentChild(normalizedHeaders[0], this.getTipHeader());

const allValid = normalizedHeaders.reduce(
(acc, header, index, array) => {
const previousHeaders = normalizedHeaders.slice(0, index);
if (index !== 0) {
if (!SpvChain.isParentChild(header, array[index - 1])
|| !self.isValid(header, previousHeaders)) {
throw new Error('Some headers are invalid');
}
return acc && true;
}
if (!self.isValid(header, self.getLongestChain())) {
throw new Error('Some headers are invalid');
}
return acc && true;
}, true,
);

if (!allAdded) {
if (!allValid) {
throw new Error('Some headers are invalid');
}
if (isOrphan) {
this.orphanChunks.push(headers);
} else {
self.appendHeadersToLongestChain(normalizedHeaders);
}
this.checkPruneBlocks();
}
};

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dashevo/dash-spv",
"version": "1.1.3",
"version": "1.1.4",
"description": "Temporary repo until spv functions moved into dashcore-lib",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 727ad2f

Please sign in to comment.