@dashevo/dash-spv 1.1.4 → 1.1.7-0
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/.eslintignore +2 -0
- package/LICENSE +21 -0
- package/README.md +1 -3
- package/config/config.js +10 -0
- package/lib/consensus.js +21 -0
- package/lib/merkleproofs.js +17 -3
- package/lib/spvchain.js +144 -43
- package/package.json +10 -10
- package/test/data/merkleproofs.js +9 -23
- package/test/index.js +189 -11
- package/.travis.yml +0 -10
package/.eslintignore
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-2022 Dash Core Group, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
package/config/config.js
CHANGED
|
@@ -35,6 +35,16 @@ module.exports = {
|
|
|
35
35
|
nonce: 3861367235,
|
|
36
36
|
});
|
|
37
37
|
},
|
|
38
|
+
getRegtestGenesis() {
|
|
39
|
+
return utils.normalizeHeader({
|
|
40
|
+
version: 1,
|
|
41
|
+
previousblockhash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
42
|
+
merkleroot: 'e0028eb9648db56b1ac77cf090b99048a8007e2bb64b68f092c03c7f56a662c7',
|
|
43
|
+
time: 1390666206,
|
|
44
|
+
bits: '1e0ffff0',
|
|
45
|
+
nonce: 3861367235,
|
|
46
|
+
});
|
|
47
|
+
},
|
|
38
48
|
getLivenetGenesis() {
|
|
39
49
|
throw Error('Livenet genesis not yet implemented');
|
|
40
50
|
},
|
package/lib/consensus.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { hasValidTarget } = require('@dashevo/dark-gravity-wave');
|
|
2
|
+
const merkleProofs = require('./merkleproofs');
|
|
2
3
|
const utils = require('./utils');
|
|
3
4
|
|
|
4
5
|
const MIN_TIMESTAMP_HEADERS = 11;
|
|
@@ -39,6 +40,26 @@ function isValidBlockHeader(newHeader, previousHeaders, network = 'mainnet') {
|
|
|
39
40
|
&& hasGreaterThanMedianTimestamp(newHeader, previousHeaders);
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
/**
|
|
44
|
+
* validates an array of tx hashes or Transaction instances
|
|
45
|
+
* against a merkleblock and the local header chain
|
|
46
|
+
* @param {Transaction[]|string[]} transactions
|
|
47
|
+
* @param {MerkleBlock} merkleBlock - a MerkleBlock instance
|
|
48
|
+
* @param {SpvChain} headerChain - an instance of an SpvChain
|
|
49
|
+
* @return {boolean}
|
|
50
|
+
*/
|
|
51
|
+
async function areValidTransactions(transactions, merkleBlock, headerChain) {
|
|
52
|
+
if (!Array.isArray(transactions) || transactions.length <= 0) {
|
|
53
|
+
throw new Error('Please check that transactions parameter is a non-empty array');
|
|
54
|
+
}
|
|
55
|
+
const localHeader = await headerChain.getHeader(merkleBlock.header.hash);
|
|
56
|
+
if (!localHeader) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
return merkleProofs.validateTxProofs(merkleBlock, transactions);
|
|
60
|
+
}
|
|
61
|
+
|
|
42
62
|
module.exports = {
|
|
43
63
|
isValidBlockHeader,
|
|
64
|
+
areValidTransactions,
|
|
44
65
|
};
|
package/lib/merkleproofs.js
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
|
-
const
|
|
1
|
+
const DashUtil = require('@dashevo/dash-util');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const merkleproofs = {
|
|
4
|
+
/**
|
|
5
|
+
* validates an array of tx hashes or Transaction instances
|
|
6
|
+
* against a merkleblock
|
|
7
|
+
* @param {MerkleBlock} merkleBlock - a MerkleBlock instance
|
|
8
|
+
* @param {Transaction[]|string[]} transactions
|
|
9
|
+
* @return {boolean}
|
|
10
|
+
*/
|
|
11
|
+
validateTxProofs: (merkleBlock, transactions) => {
|
|
12
|
+
let txToFilter = transactions.slice();
|
|
13
|
+
if (typeof transactions[0] === 'string') {
|
|
14
|
+
txToFilter = txToFilter.map(tx => DashUtil.toHash(tx).toString('hex'));
|
|
15
|
+
}
|
|
16
|
+
return merkleBlock.validMerkleTree
|
|
17
|
+
&& txToFilter.filter(tx => merkleBlock.hasTransaction(tx)).length === transactions.length;
|
|
18
|
+
},
|
|
5
19
|
};
|
|
6
20
|
|
|
7
21
|
module.exports = merkleproofs;
|
package/lib/spvchain.js
CHANGED
|
@@ -32,6 +32,14 @@ const SpvChain = class {
|
|
|
32
32
|
}
|
|
33
33
|
this.root = config.getDevnetGenesis();
|
|
34
34
|
break;
|
|
35
|
+
case 'regtest':
|
|
36
|
+
this.network = 'regtest';
|
|
37
|
+
if (startBlock) {
|
|
38
|
+
this.root = startBlock;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
this.root = config.getRegtestGenesis();
|
|
42
|
+
break;
|
|
35
43
|
case 'livenet':
|
|
36
44
|
this.network = 'mainnet';
|
|
37
45
|
if (startBlock) {
|
|
@@ -69,14 +77,11 @@ const SpvChain = class {
|
|
|
69
77
|
this.setAllBranches();
|
|
70
78
|
}
|
|
71
79
|
|
|
72
|
-
|
|
73
|
-
return this.allBranches.sort((b1, b2) => b1 < b2)[0];
|
|
74
|
-
}
|
|
75
|
-
|
|
80
|
+
/** @private */
|
|
76
81
|
checkPruneBlocks() {
|
|
77
82
|
const longestChain = this.getLongestChain();
|
|
78
83
|
|
|
79
|
-
|
|
84
|
+
while (longestChain.length > this.confirmsBeforeFinal) {
|
|
80
85
|
const pruneBlock = longestChain.splice(0, 1)[0];
|
|
81
86
|
// Children discarded as stale branches
|
|
82
87
|
delete pruneBlock.orphan;
|
|
@@ -84,25 +89,7 @@ const SpvChain = class {
|
|
|
84
89
|
}
|
|
85
90
|
}
|
|
86
91
|
|
|
87
|
-
|
|
88
|
-
return this.getLongestChain().slice(-1)[0].hash;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
getTipHeader() {
|
|
92
|
-
return this.getLongestChain().slice(-1)[0];
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
getHeader(hash) {
|
|
96
|
-
return this.store.get(hash)
|
|
97
|
-
.then((blockInDB) => {
|
|
98
|
-
if (blockInDB) {
|
|
99
|
-
return blockInDB;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return this.getLongestChain().filter(h => h.hash === hash)[0];
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
92
|
+
/** @private */
|
|
106
93
|
findConnection(newHeader) {
|
|
107
94
|
const stack = [this.root];
|
|
108
95
|
while (stack.length > 0) {
|
|
@@ -115,6 +102,7 @@ const SpvChain = class {
|
|
|
115
102
|
return null;
|
|
116
103
|
}
|
|
117
104
|
|
|
105
|
+
/** @private */
|
|
118
106
|
setAllBranches(node = this.root, branch = []) {
|
|
119
107
|
this.allBranches = [];
|
|
120
108
|
branch.push(node);
|
|
@@ -128,22 +116,26 @@ const SpvChain = class {
|
|
|
128
116
|
}
|
|
129
117
|
}
|
|
130
118
|
|
|
119
|
+
/** @private */
|
|
131
120
|
appendHeadersToLongestChain(headers) {
|
|
132
121
|
const newLongestChain = this.getLongestChain().concat(headers);
|
|
133
122
|
this.allBranches = [];
|
|
134
123
|
this.allBranches.push(newLongestChain);
|
|
135
124
|
}
|
|
136
125
|
|
|
126
|
+
/** @private */
|
|
137
127
|
getAllBranches() {
|
|
138
128
|
return this.allBranches;
|
|
139
129
|
}
|
|
140
130
|
|
|
131
|
+
/** @private */
|
|
141
132
|
isDuplicate(compareHash) {
|
|
142
133
|
return this.getAllBranches().map(branch => branch.map(node => node.hash))
|
|
143
134
|
.concat(this.orphanBlocks.map(orphan => orphan.hash))
|
|
144
135
|
.filter(hash => hash === compareHash).length > 0;
|
|
145
136
|
}
|
|
146
137
|
|
|
138
|
+
/** @private */
|
|
147
139
|
orphanReconnect() {
|
|
148
140
|
for (let i = 0; i < this.orphanBlocks.length; i += 1) {
|
|
149
141
|
const connectionTip = this.findConnection(this.orphanBlocks[i]);
|
|
@@ -154,10 +146,28 @@ const SpvChain = class {
|
|
|
154
146
|
}
|
|
155
147
|
}
|
|
156
148
|
|
|
149
|
+
/** @private */
|
|
150
|
+
orphanChunksReconnect() {
|
|
151
|
+
this.orphanChunks.sort((a, b) => a[0].timestamp - b[0].timestamp);
|
|
152
|
+
this.orphanChunks.slice().forEach((chunk, index) => {
|
|
153
|
+
if (this.getTipHash() === utils.getCorrectedHash(chunk[0].prevHash)) {
|
|
154
|
+
this.appendHeadersToLongestChain(chunk);
|
|
155
|
+
this.orphanChunks.splice(index, 1);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** @private */
|
|
157
161
|
getOrphans() {
|
|
158
162
|
return this.orphanBlocks;
|
|
159
163
|
}
|
|
160
164
|
|
|
165
|
+
/** @private */
|
|
166
|
+
getOrphanChunks() {
|
|
167
|
+
return this.orphanChunks;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** @private */
|
|
161
171
|
processValidHeader(header) {
|
|
162
172
|
const connection = this.findConnection(header);
|
|
163
173
|
if (connection) {
|
|
@@ -168,20 +178,27 @@ const SpvChain = class {
|
|
|
168
178
|
}
|
|
169
179
|
}
|
|
170
180
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
return false;
|
|
181
|
+
/** @private
|
|
182
|
+
* validates a dashcore.BlockHeader object
|
|
183
|
+
*
|
|
184
|
+
* @param {Object} header
|
|
185
|
+
* @param {Object[]} previousHeaders
|
|
186
|
+
* @return {boolean}
|
|
187
|
+
*/
|
|
188
|
+
isValid(header, previousHeaders) {
|
|
189
|
+
return !!(Consensus.isValidBlockHeader(header, previousHeaders, this.network)
|
|
190
|
+
&& !this.isDuplicate(header.hash));
|
|
182
191
|
}
|
|
183
192
|
|
|
184
193
|
/* eslint-disable no-param-reassign */
|
|
194
|
+
/**
|
|
195
|
+
* verifies the parent child connection
|
|
196
|
+
* between two adjacent dashcore.BlockHeader objects
|
|
197
|
+
*
|
|
198
|
+
* @param {Object} header
|
|
199
|
+
* @param {Object} previousHeader
|
|
200
|
+
* @return {boolean}
|
|
201
|
+
*/
|
|
185
202
|
static isParentChild(header, previousHeader) {
|
|
186
203
|
if (utils.getCorrectedHash(header.prevHash) !== previousHeader.hash) {
|
|
187
204
|
return false;
|
|
@@ -197,13 +214,87 @@ const SpvChain = class {
|
|
|
197
214
|
}
|
|
198
215
|
/* eslint-enable no-param-reassign */
|
|
199
216
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
217
|
+
/**
|
|
218
|
+
* gets the longest chain
|
|
219
|
+
*
|
|
220
|
+
* @return {Object[]}
|
|
221
|
+
*/
|
|
222
|
+
getLongestChain() {
|
|
223
|
+
return this.allBranches.sort((b1, b2) => b1 < b2)[0];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* gets the block hash of the longest chain tip
|
|
228
|
+
*
|
|
229
|
+
* @return {string} hash
|
|
230
|
+
*/
|
|
231
|
+
getTipHash() {
|
|
232
|
+
return this.getLongestChain().slice(-1)[0].hash;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* gets the dashcore.BlockHeader object the longest chain tip
|
|
237
|
+
*
|
|
238
|
+
* @return {Object} header
|
|
239
|
+
*/
|
|
240
|
+
getTipHeader() {
|
|
241
|
+
return this.getLongestChain().slice(-1)[0];
|
|
203
242
|
}
|
|
204
243
|
|
|
244
|
+
/**
|
|
245
|
+
* gets the dashcore.BlockHeader object for a specific block hash
|
|
246
|
+
*
|
|
247
|
+
* @param {string} hash
|
|
248
|
+
* @return {Object} header
|
|
249
|
+
*/
|
|
250
|
+
getHeader(hash) {
|
|
251
|
+
return this.store.get(hash)
|
|
252
|
+
.then((blockInDB) => {
|
|
253
|
+
if (blockInDB) {
|
|
254
|
+
return blockInDB;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return this.getLongestChain().filter(h => h.hash === hash)[0];
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* adds a valid header to the tip of the longest spv chain.
|
|
263
|
+
* If it cannot be connected to the tip it gets temporarily
|
|
264
|
+
* added to an orphan array for possible later reconnection
|
|
265
|
+
*
|
|
266
|
+
* @param {Object[]|string[]|buffer[]} header
|
|
267
|
+
* @return {boolean}
|
|
268
|
+
*/
|
|
269
|
+
addHeader(header) {
|
|
270
|
+
const headerNormalised = utils.normalizeHeader(header);
|
|
271
|
+
|
|
272
|
+
if (this.isValid(headerNormalised, this.getLongestChain())) {
|
|
273
|
+
headerNormalised.children = [];
|
|
274
|
+
this.processValidHeader(headerNormalised);
|
|
275
|
+
this.setAllBranches();
|
|
276
|
+
this.checkPruneBlocks();
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* adds an array of valid headers to the longest spv chain.
|
|
284
|
+
* If they cannot be connected to last tip they get temporarily
|
|
285
|
+
* added to an orphan array for possible later reconnection
|
|
286
|
+
*
|
|
287
|
+
* @param {Object[]|string[]|buffer[]} headers
|
|
288
|
+
* @return {boolean}
|
|
289
|
+
*/
|
|
205
290
|
addHeaders(headers) {
|
|
206
|
-
|
|
291
|
+
if (headers.length === 1) {
|
|
292
|
+
if (!this.addHeader(headers[0])) {
|
|
293
|
+
throw new Error('Some headers are invalid');
|
|
294
|
+
} else {
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
207
298
|
const normalizedHeaders = headers.map(h => utils.normalizeHeader(h));
|
|
208
299
|
const isOrphan = !SpvChain.isParentChild(normalizedHeaders[0], this.getTipHeader());
|
|
209
300
|
|
|
@@ -212,12 +303,18 @@ const SpvChain = class {
|
|
|
212
303
|
const previousHeaders = normalizedHeaders.slice(0, index);
|
|
213
304
|
if (index !== 0) {
|
|
214
305
|
if (!SpvChain.isParentChild(header, array[index - 1])
|
|
215
|
-
|| !
|
|
306
|
+
|| !this.isValid(header, previousHeaders)) {
|
|
216
307
|
throw new Error('Some headers are invalid');
|
|
217
308
|
}
|
|
218
309
|
return acc && true;
|
|
219
310
|
}
|
|
220
|
-
if (
|
|
311
|
+
if (isOrphan) {
|
|
312
|
+
if (!this.isValid(header, previousHeaders)) {
|
|
313
|
+
throw new Error('Some headers are invalid');
|
|
314
|
+
}
|
|
315
|
+
return acc && true;
|
|
316
|
+
}
|
|
317
|
+
if (!this.isValid(header, this.getLongestChain())) {
|
|
221
318
|
throw new Error('Some headers are invalid');
|
|
222
319
|
}
|
|
223
320
|
return acc && true;
|
|
@@ -227,11 +324,15 @@ const SpvChain = class {
|
|
|
227
324
|
throw new Error('Some headers are invalid');
|
|
228
325
|
}
|
|
229
326
|
if (isOrphan) {
|
|
230
|
-
this.orphanChunks.push(
|
|
327
|
+
this.orphanChunks.push(normalizedHeaders);
|
|
231
328
|
} else {
|
|
232
|
-
|
|
329
|
+
this.appendHeadersToLongestChain(normalizedHeaders);
|
|
330
|
+
}
|
|
331
|
+
if (this.orphanChunks.length > 0) {
|
|
332
|
+
this.orphanChunksReconnect();
|
|
233
333
|
}
|
|
234
334
|
this.checkPruneBlocks();
|
|
335
|
+
return true;
|
|
235
336
|
}
|
|
236
337
|
};
|
|
237
338
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dashevo/dash-spv",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.1.7-0",
|
|
4
|
+
"description": "Repository containing SPV functions used by @dashevo",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "mocha test/ --no-timeouts --recursive",
|
|
@@ -12,16 +12,16 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@dashevo/dark-gravity-wave": "^1.1.1",
|
|
15
|
-
"@dashevo/dashcore-lib": "^0.17.4",
|
|
16
15
|
"@dashevo/dash-util": "^2.0.3",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
16
|
+
"@dashevo/dashcore-lib": "~0.19.39",
|
|
17
|
+
"levelup": "^4.4.0",
|
|
18
|
+
"memdown": "^5.1.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"eslint": "^
|
|
22
|
-
"eslint-config-airbnb-base": "^
|
|
23
|
-
"eslint-plugin-import": "^2.
|
|
24
|
-
"mocha": "^
|
|
21
|
+
"eslint": "^7.32.0",
|
|
22
|
+
"eslint-config-airbnb-base": "^14.2.1",
|
|
23
|
+
"eslint-plugin-import": "^2.24.2",
|
|
24
|
+
"mocha": "^9.1.2",
|
|
25
25
|
"should": "^13.2.3"
|
|
26
26
|
}
|
|
27
|
-
}
|
|
27
|
+
}
|
|
@@ -22,29 +22,15 @@ const merkleJSON = {
|
|
|
22
22
|
],
|
|
23
23
|
flags: [219, 63],
|
|
24
24
|
},
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
numTransactions: 12,
|
|
36
|
-
hashes: [
|
|
37
|
-
'9d0a368bc9923c6cb966135a4ceda30cc5f259f72c8843ce015056375f8a06ec',
|
|
38
|
-
'39e5cd533567ac0a8602bcc4c29e2f01a4abb0fe68ffbc7be6c393db188b72e0',
|
|
39
|
-
'cd75b421157eca03eff664bdc165730f91ef2fa52df19ff415ab5acb30045425',
|
|
40
|
-
'2ef9795147caaeecee5bc2520704bb372cde06dbd2e871750f31336fd3f02be3',
|
|
41
|
-
'2241d3448560f8b1d3a07ea5c31e79eb595632984a20f50944809a61fdd9fe0b',
|
|
42
|
-
'45afbfe270014d5593cb065562f1fed726f767fe334d8b3f4379025cfa5be8c5',
|
|
43
|
-
'198c03da0ccf871db91fe436e2795908eac5cc7d164232182e9445f7f9db1ab2',
|
|
44
|
-
'ed07c181ce5ba7cb66d205bc970f43e1ca11996d611aa8e91e305eb8608c543c',
|
|
45
|
-
],
|
|
46
|
-
flags: [219, 63],
|
|
47
|
-
},
|
|
25
|
+
// merkleblock for inserted tx hash
|
|
26
|
+
// '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b' in testnet block 10000
|
|
27
|
+
// (bloom filter: '03359be1100000000000000001')
|
|
28
|
+
rawMerkleBlock: '000000200e71587f863213690c24b8ad07d0753b38abe6ff1328ef7661689404000000000f1e7ce895614047d5c8d3e7b537b26b0482302301aa104947eb09c55521b316b10b1e5cff7d191c166b25b80500000002a8f781d45a5b12abffcf52bac3b3fc7824cc91ef4f9bb04bc97500a1d98d91305bc9439cc732e56783e896c3751f2b3eba31f2a8a3cf26629a6ba91269476272011d',
|
|
29
|
+
// merkleblock for inserted tx hashes
|
|
30
|
+
// '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b'
|
|
31
|
+
// '3f3517ee8fa95621fe8abdd81c1e0dfb50e21dd4c5a3c01eee2c47cf664821b6' in testnet block 10000
|
|
32
|
+
// (bloom filter: '0797b88e7d21b31f130000000000000001')
|
|
33
|
+
rawMerkleBlock2: '000000200e71587f863213690c24b8ad07d0753b38abe6ff1328ef7661689404000000000f1e7ce895614047d5c8d3e7b537b26b0482302301aa104947eb09c55521b316b10b1e5cff7d191c166b25b80500000004acff07e45bfb50dd8e2fb10109f1988423cbcc9423ff07b7acb09715bd1a11e89153210725b8e219b91e433180092226c7b56649843fc51bc012c587f50030a1b6214866cf472cee1ec0a3c5d41de250fb0d1e1cd8bd8afe2156a98fee17353f5bc9439cc732e56783e896c3751f2b3eba31f2a8a3cf26629a6ba9126947627202eb01',
|
|
48
34
|
};
|
|
49
35
|
|
|
50
36
|
module.exports = merkleJSON;
|
package/test/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const
|
|
1
|
+
const { MerkleBlock, Transaction } = require('@dashevo/dashcore-lib');
|
|
2
2
|
const Blockchain = require('../lib/spvchain');
|
|
3
3
|
const utils = require('../lib/utils');
|
|
4
4
|
const merkleProofs = require('../lib/merkleproofs');
|
|
5
|
-
|
|
5
|
+
const consensus = require('../lib/consensus');
|
|
6
6
|
const {
|
|
7
7
|
testnet, testnet2, testnet3, mainnet, badRawHeaders,
|
|
8
8
|
} = require('./data/rawHeaders');
|
|
@@ -10,6 +10,8 @@ const headers = require('./data/headers');
|
|
|
10
10
|
const merkleData = require('./data/merkleproofs');
|
|
11
11
|
|
|
12
12
|
let chain = null;
|
|
13
|
+
let merkleBlock = null;
|
|
14
|
+
let merkleBlock2 = null;
|
|
13
15
|
|
|
14
16
|
require('should');
|
|
15
17
|
|
|
@@ -177,9 +179,15 @@ describe('SPV-DASH (addHeaders) add many headers for testnet', () => {
|
|
|
177
179
|
chain.getLongestChain().length.should.equal(500);
|
|
178
180
|
});
|
|
179
181
|
|
|
180
|
-
it('should
|
|
182
|
+
it('should orphan and not add invalid but consistent headers', () => {
|
|
183
|
+
chain.addHeaders([badRawHeaders[0], badRawHeaders[1]]);
|
|
184
|
+
chain.getOrphanChunks().length.should.equal(1);
|
|
185
|
+
chain.getLongestChain().length.should.equal(500);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should throw an error if some of the headers are inconsistent', (done) => {
|
|
181
189
|
try {
|
|
182
|
-
chain.addHeaders([badRawHeaders[0], badRawHeaders[
|
|
190
|
+
chain.addHeaders([badRawHeaders[0], badRawHeaders[2]]);
|
|
183
191
|
done(new Error('SPV chain failed to throw an error on invalid block'));
|
|
184
192
|
} catch (e) {
|
|
185
193
|
e.message.should.equal('Some headers are invalid');
|
|
@@ -188,6 +196,52 @@ describe('SPV-DASH (addHeaders) add many headers for testnet', () => {
|
|
|
188
196
|
});
|
|
189
197
|
});
|
|
190
198
|
|
|
199
|
+
describe('SPV-DASH (addHeaders) add testnet headers out of order', () => {
|
|
200
|
+
before(() => {
|
|
201
|
+
chain = new Blockchain('testnet', 10000, utils.normalizeHeader(testnet[0]));
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('should add the 1st 100 testnet headers', () => {
|
|
205
|
+
chain.addHeaders(testnet.slice(1, 100));
|
|
206
|
+
chain.getOrphans().length.should.equal(0);
|
|
207
|
+
chain.getAllBranches().length.should.equal(1);
|
|
208
|
+
chain.getOrphanChunks().length.should.equal(0);
|
|
209
|
+
chain.getLongestChain().length.should.equal(100);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('should orphan testnet headers 200 - 300', () => {
|
|
213
|
+
chain.addHeaders(testnet.slice(200, 300));
|
|
214
|
+
chain.getOrphans().length.should.equal(0);
|
|
215
|
+
chain.getAllBranches().length.should.equal(1);
|
|
216
|
+
chain.getOrphanChunks().length.should.equal(1);
|
|
217
|
+
chain.getLongestChain().length.should.equal(100);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('should orphan testnet headers 400 - 500', () => {
|
|
221
|
+
chain.addHeaders(testnet.slice(400, 500));
|
|
222
|
+
chain.getOrphans().length.should.equal(0);
|
|
223
|
+
chain.getAllBranches().length.should.equal(1);
|
|
224
|
+
chain.getOrphanChunks().length.should.equal(2);
|
|
225
|
+
chain.getLongestChain().length.should.equal(100);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should reconnect orphaned chunks (testnet headers 1 - 100 and 200 - 300)', () => {
|
|
229
|
+
chain.addHeaders(testnet.slice(100, 200));
|
|
230
|
+
chain.getOrphans().length.should.equal(0);
|
|
231
|
+
chain.getAllBranches().length.should.equal(1);
|
|
232
|
+
chain.getOrphanChunks().length.should.equal(1);
|
|
233
|
+
chain.getLongestChain().length.should.equal(300);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('should reconnect orphaned chunks (testnet headers 1 - 300 and 400 - 500)', () => {
|
|
237
|
+
chain.addHeaders(testnet.slice(300, 400));
|
|
238
|
+
chain.getOrphans().length.should.equal(0);
|
|
239
|
+
chain.getAllBranches().length.should.equal(1);
|
|
240
|
+
chain.getOrphanChunks().length.should.equal(0);
|
|
241
|
+
chain.getLongestChain().length.should.equal(500);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
191
245
|
describe('SPV-DASH (addHeaders) add many headers for mainnet', () => {
|
|
192
246
|
before(() => {
|
|
193
247
|
chain = new Blockchain('mainnet', 10000, utils.normalizeHeader(mainnet[0]));
|
|
@@ -219,9 +273,15 @@ describe('SPV-DASH (addHeaders) add many headers for mainnet', () => {
|
|
|
219
273
|
chain.getLongestChain().length.should.equal(1500);
|
|
220
274
|
});
|
|
221
275
|
|
|
222
|
-
it('should
|
|
276
|
+
it('should orphan and not add invalid but consistent headers', () => {
|
|
277
|
+
chain.addHeaders([badRawHeaders[0], badRawHeaders[1]]);
|
|
278
|
+
chain.getOrphanChunks().length.should.equal(1);
|
|
279
|
+
chain.getLongestChain().length.should.equal(1500);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('should throw an error if some of the headers are inconsistent', (done) => {
|
|
223
283
|
try {
|
|
224
|
-
chain.addHeaders([badRawHeaders[0], badRawHeaders[
|
|
284
|
+
chain.addHeaders([badRawHeaders[0], badRawHeaders[2]]);
|
|
225
285
|
done(new Error('SPV chain failed to throw an error on invalid block'));
|
|
226
286
|
} catch (e) {
|
|
227
287
|
e.message.should.equal('Some headers are invalid');
|
|
@@ -230,6 +290,52 @@ describe('SPV-DASH (addHeaders) add many headers for mainnet', () => {
|
|
|
230
290
|
});
|
|
231
291
|
});
|
|
232
292
|
|
|
293
|
+
describe('SPV-DASH (addHeaders) add mainnet headers out of order', () => {
|
|
294
|
+
before(() => {
|
|
295
|
+
chain = new Blockchain('mainnet', 10000, utils.normalizeHeader(mainnet[0]));
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('should add the 1st 100 mainnet headers', () => {
|
|
299
|
+
chain.addHeaders(mainnet.slice(1, 100));
|
|
300
|
+
chain.getOrphans().length.should.equal(0);
|
|
301
|
+
chain.getAllBranches().length.should.equal(1);
|
|
302
|
+
chain.getOrphanChunks().length.should.equal(0);
|
|
303
|
+
chain.getLongestChain().length.should.equal(100);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('should orphan mainnet headers 200 - 300', () => {
|
|
307
|
+
chain.addHeaders(mainnet.slice(200, 300));
|
|
308
|
+
chain.getOrphans().length.should.equal(0);
|
|
309
|
+
chain.getAllBranches().length.should.equal(1);
|
|
310
|
+
chain.getOrphanChunks().length.should.equal(1);
|
|
311
|
+
chain.getLongestChain().length.should.equal(100);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('should orphan mainnet headers 400 - 500', () => {
|
|
315
|
+
chain.addHeaders(mainnet.slice(400, 500));
|
|
316
|
+
chain.getOrphans().length.should.equal(0);
|
|
317
|
+
chain.getAllBranches().length.should.equal(1);
|
|
318
|
+
chain.getOrphanChunks().length.should.equal(2);
|
|
319
|
+
chain.getLongestChain().length.should.equal(100);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it('should reconnect orphaned chunks (mainnet headers 1 - 100 and 200 - 300)', () => {
|
|
323
|
+
chain.addHeaders(mainnet.slice(100, 200));
|
|
324
|
+
chain.getOrphans().length.should.equal(0);
|
|
325
|
+
chain.getAllBranches().length.should.equal(1);
|
|
326
|
+
chain.getOrphanChunks().length.should.equal(1);
|
|
327
|
+
chain.getLongestChain().length.should.equal(300);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it('should reconnect orphaned chunks (mainnet headers 1 - 300 and 400 - 500)', () => {
|
|
331
|
+
chain.addHeaders(mainnet.slice(300, 400));
|
|
332
|
+
chain.getOrphans().length.should.equal(0);
|
|
333
|
+
chain.getAllBranches().length.should.equal(1);
|
|
334
|
+
chain.getOrphanChunks().length.should.equal(0);
|
|
335
|
+
chain.getLongestChain().length.should.equal(500);
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
233
339
|
let genesisHash = null;
|
|
234
340
|
describe('Blockstore', () => {
|
|
235
341
|
before(() => {
|
|
@@ -261,17 +367,89 @@ describe('Blockstore', () => {
|
|
|
261
367
|
});
|
|
262
368
|
|
|
263
369
|
// TODO:
|
|
264
|
-
// Create scenarios where chain splits occur to form competing
|
|
265
|
-
// Difficult with current chain provided by chainmanager as this is
|
|
370
|
+
// Create scenarios where chain splits occur to form competing branches
|
|
371
|
+
// Difficult with current chain provided by chainmanager as this is actually hardcoded
|
|
266
372
|
// Dash testnet headers which requires significant CPU power to create forked chains from
|
|
267
373
|
|
|
268
374
|
describe('MerkleProofs', () => {
|
|
269
375
|
it('should validate tx inclusion in merkleblock', () => {
|
|
270
|
-
|
|
271
|
-
const validTx = '
|
|
376
|
+
merkleBlock = new MerkleBlock(merkleData.merkleBlock);
|
|
377
|
+
const validTx = 'c5e85bfa5c0279433f8b4d33fe67f726d7fef1625506cb93554d0170e2bfaf45';
|
|
272
378
|
const invalidTx = `${validTx.substring(0, validTx.length - 1)}0`;
|
|
273
|
-
|
|
274
379
|
merkleProofs.validateTxProofs(merkleBlock, [validTx]).should.equal(true);
|
|
275
380
|
merkleProofs.validateTxProofs(merkleBlock, [invalidTx]).should.equal(false);
|
|
276
381
|
});
|
|
277
382
|
});
|
|
383
|
+
|
|
384
|
+
describe('Transaction validation', () => {
|
|
385
|
+
before(() => {
|
|
386
|
+
chain = new Blockchain('testnet', 10000, utils.normalizeHeader(testnet[0]));
|
|
387
|
+
chain.addHeaders(testnet.slice(1, 500));
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
beforeEach(() => {
|
|
391
|
+
merkleBlock = new MerkleBlock(Buffer.from(merkleData.rawMerkleBlock, 'hex'));
|
|
392
|
+
merkleBlock2 = new MerkleBlock(Buffer.from(merkleData.rawMerkleBlock2, 'hex'));
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('should throw an error if wrong type is passed', async () => {
|
|
396
|
+
const validTx = '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b';
|
|
397
|
+
const invalid = Buffer.from(validTx);
|
|
398
|
+
try {
|
|
399
|
+
await consensus.areValidTransactions(invalid, merkleBlock, chain);
|
|
400
|
+
throw new Error('Transaction validation failed to throw an error');
|
|
401
|
+
} catch (e) {
|
|
402
|
+
e.message.should.equal('Please check that transactions parameter is a non-empty array');
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it('should throw an error if empty transactions array', async () => {
|
|
407
|
+
const transactions = [];
|
|
408
|
+
try {
|
|
409
|
+
await consensus.areValidTransactions(transactions, merkleBlock, chain);
|
|
410
|
+
throw new Error('Transaction validation failed to throw an error');
|
|
411
|
+
} catch (e) {
|
|
412
|
+
e.message.should.equal('Please check that transactions parameter is a non-empty array');
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('should not validate an array of raw transactions for a merkleblock that was generated with a filter containing only one of them', async () => {
|
|
417
|
+
const validTx = new Transaction('020000000100de7192338db34fe9bb25f34122893d94f3b43bd4c881e37924c8e95a068cc8000000006b483045022100b185b4b86b613e3ffc796db90f95dc88f82561c50ba49fa610d8090f61f38ff002201473466bddee2672ed0dba75b81c07bdade734441e005c8c7fdf12a039ff9312012102bdfedbfe6ea05de8094d18442e08c98ebd695acf489f1bdf68fe1e3aff6f488effffffff010000000000000000016a00000000');
|
|
418
|
+
const validTx2 = new Transaction('0200000001ccc68ff58b7b02247f3e05440ab7fc7c8c599453de4a49e35393981890a1e984010000006b483045022100e141365c4916fa09d03aac58f215b926b777a3acec918a6becdbb03d59d28d9f02204edc1ce68d596e28e55f114c67a270302d488707e754af1261fdfc043891651c012102d5b7c0dfb2fd9591a4a98555ce806e17842f401979f2e0cc0689c91d6ca9ef87feffffff025d4c0000000000001976a9146d14b25994e4036d70eeafd4a706640337db5a5e88ac409c0000000000001976a9148b4a9da5a46c7b89b26b649bac8e34e7aa5aa63188acc2260000');
|
|
419
|
+
const transactions = [];
|
|
420
|
+
transactions.push(validTx);
|
|
421
|
+
transactions.push(validTx2);
|
|
422
|
+
const result = await consensus.areValidTransactions(transactions, merkleBlock, chain);
|
|
423
|
+
result.should.equal(false);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it('should validate an array of raw transactions for a merkleblock that was generated with a filter containing both of them', async () => {
|
|
427
|
+
const validTx = new Transaction('020000000100de7192338db34fe9bb25f34122893d94f3b43bd4c881e37924c8e95a068cc8000000006b483045022100b185b4b86b613e3ffc796db90f95dc88f82561c50ba49fa610d8090f61f38ff002201473466bddee2672ed0dba75b81c07bdade734441e005c8c7fdf12a039ff9312012102bdfedbfe6ea05de8094d18442e08c98ebd695acf489f1bdf68fe1e3aff6f488effffffff010000000000000000016a00000000');
|
|
428
|
+
const validTx2 = new Transaction('0200000001ccc68ff58b7b02247f3e05440ab7fc7c8c599453de4a49e35393981890a1e984010000006b483045022100e141365c4916fa09d03aac58f215b926b777a3acec918a6becdbb03d59d28d9f02204edc1ce68d596e28e55f114c67a270302d488707e754af1261fdfc043891651c012102d5b7c0dfb2fd9591a4a98555ce806e17842f401979f2e0cc0689c91d6ca9ef87feffffff025d4c0000000000001976a9146d14b25994e4036d70eeafd4a706640337db5a5e88ac409c0000000000001976a9148b4a9da5a46c7b89b26b649bac8e34e7aa5aa63188acc2260000');
|
|
429
|
+
const transactions = [];
|
|
430
|
+
transactions.push(validTx);
|
|
431
|
+
transactions.push(validTx2);
|
|
432
|
+
const result = await consensus.areValidTransactions(transactions, merkleBlock2, chain);
|
|
433
|
+
result.should.equal(true);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it('should not validate an array of transactions hashes for a merkleblock that was generated with a filter containing only one of them', async () => {
|
|
437
|
+
const validTxHash = '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b';
|
|
438
|
+
const validTxHash2 = '3f3517ee8fa95621fe8abdd81c1e0dfb50e21dd4c5a3c01eee2c47cf664821b6';
|
|
439
|
+
const transactions = [];
|
|
440
|
+
transactions.push(validTxHash);
|
|
441
|
+
transactions.push(validTxHash2);
|
|
442
|
+
const result = await consensus.areValidTransactions(transactions, merkleBlock, chain);
|
|
443
|
+
result.should.equal(false);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it('should validate an array of transactions hashes for a merkleblock that was generated with a filter containing both of them', async () => {
|
|
447
|
+
const validTxHash = '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b';
|
|
448
|
+
const validTxHash2 = '3f3517ee8fa95621fe8abdd81c1e0dfb50e21dd4c5a3c01eee2c47cf664821b6';
|
|
449
|
+
const transactions = [];
|
|
450
|
+
transactions.push(validTxHash);
|
|
451
|
+
transactions.push(validTxHash2);
|
|
452
|
+
const result = await consensus.areValidTransactions(transactions, merkleBlock2, chain);
|
|
453
|
+
result.should.equal(true);
|
|
454
|
+
});
|
|
455
|
+
});
|