@dashevo/dash-spv 6.0.1 → 6.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ultra.cache.json +1 -1
- package/lib/consensus.js +38 -68
- package/lib/pow.js +513 -0
- package/lib/spvchain.js +62 -29
- package/lib/utils.js +17 -6
- package/package.json +2 -3
- package/test/data/difficulty.js +1795 -0
- package/test/index.js +16 -13
- package/test/pow.js +310 -0
package/test/index.js
CHANGED
|
@@ -86,7 +86,7 @@ describe('SPV-DASH (forks & re-orgs) deserialized headers', () => {
|
|
|
86
86
|
describe('SPV-DASH (forks & re-orgs) serialized raw headers for mainnet', () => {
|
|
87
87
|
before(async () => {
|
|
88
88
|
chain = new Blockchain('mainnet', 10000);
|
|
89
|
-
chain.initialize(utils.normalizeHeader(mainnet[0]),
|
|
89
|
+
chain.initialize(utils.normalizeHeader(mainnet[0]), 999900);
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
it('should get 2000 mainnet headers', () => {
|
|
@@ -141,6 +141,7 @@ describe('Block header consensus validation', () => {
|
|
|
141
141
|
invalidHeader,
|
|
142
142
|
[new Blockchain('testnet').genesis],
|
|
143
143
|
'testnet',
|
|
144
|
+
1,
|
|
144
145
|
).should.equal(false);
|
|
145
146
|
});
|
|
146
147
|
|
|
@@ -148,7 +149,7 @@ describe('Block header consensus validation', () => {
|
|
|
148
149
|
const previousHeaders = mainnet.slice(0, 24).map(utils.normalizeHeader);
|
|
149
150
|
const nextHeader = utils.normalizeHeader(mainnet[24]);
|
|
150
151
|
|
|
151
|
-
consensus.isValidBlockHeader(nextHeader, previousHeaders, 'mainnet')
|
|
152
|
+
consensus.isValidBlockHeader(nextHeader, previousHeaders, 'mainnet', 999900 + 24)
|
|
152
153
|
.should.equal(true);
|
|
153
154
|
});
|
|
154
155
|
|
|
@@ -156,7 +157,7 @@ describe('Block header consensus validation', () => {
|
|
|
156
157
|
const wrongHistory = testnet.slice(0, 24).map(utils.normalizeHeader);
|
|
157
158
|
const nextHeader = utils.normalizeHeader(mainnet[24]);
|
|
158
159
|
|
|
159
|
-
consensus.isValidBlockHeader(nextHeader, wrongHistory, 'mainnet')
|
|
160
|
+
consensus.isValidBlockHeader(nextHeader, wrongHistory, 'mainnet', 999900 + 24)
|
|
160
161
|
.should.equal(false);
|
|
161
162
|
});
|
|
162
163
|
|
|
@@ -169,7 +170,7 @@ describe('Block header consensus validation', () => {
|
|
|
169
170
|
|
|
170
171
|
const nextHeader = utils.createBlock(previousHeaders.at(-1), 0x207fffff);
|
|
171
172
|
|
|
172
|
-
consensus.isValidBlockHeader(nextHeader, previousHeaders, 'regtest')
|
|
173
|
+
consensus.isValidBlockHeader(nextHeader, previousHeaders, 'regtest', 24)
|
|
173
174
|
.should.equal(true);
|
|
174
175
|
});
|
|
175
176
|
|
|
@@ -177,7 +178,7 @@ describe('Block header consensus validation', () => {
|
|
|
177
178
|
const { genesis } = new Blockchain('regtest');
|
|
178
179
|
const changedTargetHeader = utils.createBlock(genesis, 0x2070ffff);
|
|
179
180
|
|
|
180
|
-
consensus.isValidBlockHeader(changedTargetHeader, [genesis], 'regtest')
|
|
181
|
+
consensus.isValidBlockHeader(changedTargetHeader, [genesis], 'regtest', 1)
|
|
181
182
|
.should.equal(false);
|
|
182
183
|
});
|
|
183
184
|
});
|
|
@@ -185,7 +186,7 @@ describe('Block header consensus validation', () => {
|
|
|
185
186
|
describe('SPV-DASH (addHeaders) add many headers for testnet', () => {
|
|
186
187
|
before(async () => {
|
|
187
188
|
chain = new Blockchain('testnet', 10000);
|
|
188
|
-
chain.initialize(utils.normalizeHeader(testnet[0]),
|
|
189
|
+
chain.initialize(utils.normalizeHeader(testnet[0]), 10000);
|
|
189
190
|
});
|
|
190
191
|
|
|
191
192
|
it('should add the 1st 250 testnet headers', () => {
|
|
@@ -204,7 +205,7 @@ describe('SPV-DASH (addHeaders) add many headers for testnet', () => {
|
|
|
204
205
|
|
|
205
206
|
it('should add the 1st 250 testnet2 headers', () => {
|
|
206
207
|
chain = new Blockchain('testnet', 10000);
|
|
207
|
-
chain.initialize(utils.normalizeHeader(testnet2[0]),
|
|
208
|
+
chain.initialize(utils.normalizeHeader(testnet2[0]), 10000);
|
|
208
209
|
chain.addHeaders(testnet2.slice(1, 250));
|
|
209
210
|
chain.getOrphanChunks().length.should.equal(0);
|
|
210
211
|
chain.getAllBranches().length.should.equal(1);
|
|
@@ -220,7 +221,7 @@ describe('SPV-DASH (addHeaders) add many headers for testnet', () => {
|
|
|
220
221
|
|
|
221
222
|
it('should add the 1st 250 testnet3 headers', () => {
|
|
222
223
|
chain = new Blockchain('testnet', 10000);
|
|
223
|
-
chain.initialize(utils.normalizeHeader(testnet3[0]),
|
|
224
|
+
chain.initialize(utils.normalizeHeader(testnet3[0]), 103700);
|
|
224
225
|
chain.addHeaders(testnet3.slice(1, 250));
|
|
225
226
|
chain.getOrphanChunks().length.should.equal(0);
|
|
226
227
|
chain.getAllBranches().length.should.equal(1);
|
|
@@ -259,7 +260,7 @@ describe('SPV-DASH (addHeaders) add many headers for testnet', () => {
|
|
|
259
260
|
describe('SPV-DASH (addHeaders) add testnet headers out of order', () => {
|
|
260
261
|
before(() => {
|
|
261
262
|
chain = new Blockchain('testnet', 10000);
|
|
262
|
-
chain.initialize(utils.normalizeHeader(testnet[0]),
|
|
263
|
+
chain.initialize(utils.normalizeHeader(testnet[0]), 10000);
|
|
263
264
|
});
|
|
264
265
|
|
|
265
266
|
it('should add the 1st 100 testnet headers', () => {
|
|
@@ -303,7 +304,7 @@ describe('SPV-DASH (addHeaders) add testnet headers out of order', () => {
|
|
|
303
304
|
describe('SPV-DASH (addHeaders) add many headers for mainnet', () => {
|
|
304
305
|
before(() => {
|
|
305
306
|
chain = new Blockchain('mainnet', 10000);
|
|
306
|
-
chain.initialize(utils.normalizeHeader(mainnet[0]),
|
|
307
|
+
chain.initialize(utils.normalizeHeader(mainnet[0]), 999900);
|
|
307
308
|
});
|
|
308
309
|
|
|
309
310
|
it('should add the 1st 500 mainnet headers', () => {
|
|
@@ -333,7 +334,9 @@ describe('SPV-DASH (addHeaders) add many headers for mainnet', () => {
|
|
|
333
334
|
});
|
|
334
335
|
|
|
335
336
|
it('should reject orphan headers outside the network proof-of-work limit', () => {
|
|
336
|
-
|
|
337
|
+
// Easy enough for regtest, above mainnet's limit
|
|
338
|
+
const tooEasy = utils.createBlock(null, 0x207fffff);
|
|
339
|
+
should(() => chain.addHeaders([tooEasy])).throw();
|
|
337
340
|
chain.getOrphanChunks().length.should.equal(0);
|
|
338
341
|
chain.getLongestChain().length.should.equal(1500);
|
|
339
342
|
});
|
|
@@ -352,7 +355,7 @@ describe('SPV-DASH (addHeaders) add many headers for mainnet', () => {
|
|
|
352
355
|
describe('SPV-DASH (addHeaders) add mainnet headers out of order', () => {
|
|
353
356
|
before(() => {
|
|
354
357
|
chain = new Blockchain('mainnet', 10000);
|
|
355
|
-
chain.initialize(utils.normalizeHeader(mainnet[0]),
|
|
358
|
+
chain.initialize(utils.normalizeHeader(mainnet[0]), 999900);
|
|
356
359
|
});
|
|
357
360
|
|
|
358
361
|
it('should add the 1st 100 mainnet headers', () => {
|
|
@@ -440,7 +443,7 @@ describe('MerkleProofs', () => {
|
|
|
440
443
|
describe('Transaction validation', () => {
|
|
441
444
|
before(() => {
|
|
442
445
|
chain = new Blockchain('testnet', 10000);
|
|
443
|
-
chain.initialize(utils.normalizeHeader(testnet[0]),
|
|
446
|
+
chain.initialize(utils.normalizeHeader(testnet[0]), 10000);
|
|
444
447
|
chain.addHeaders(testnet.slice(1, 500));
|
|
445
448
|
});
|
|
446
449
|
|
package/test/pow.js
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/* eslint-disable no-bitwise */
|
|
2
|
+
const { expect } = require('chai');
|
|
3
|
+
const { BlockHeader } = require('@dashevo/dashcore-lib');
|
|
4
|
+
|
|
5
|
+
const SpvChain = require('../lib/spvchain');
|
|
6
|
+
const consensus = require('../lib/consensus');
|
|
7
|
+
const pow = require('../lib/pow');
|
|
8
|
+
const utils = require('../lib/utils');
|
|
9
|
+
const difficultyCases = require('./data/difficulty');
|
|
10
|
+
|
|
11
|
+
const toHeaders = (rawHeaders) => rawHeaders.map((raw) => new BlockHeader(Buffer.from(raw, 'hex')));
|
|
12
|
+
|
|
13
|
+
// toObject() also holds the hash, which must not be copied to a changed header
|
|
14
|
+
const withFields = (header, fields) => {
|
|
15
|
+
const { hash, ...object } = header.toObject();
|
|
16
|
+
return new BlockHeader({ ...object, ...fields });
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const withBits = (header, bits) => withFields(header, { bits });
|
|
20
|
+
|
|
21
|
+
// A case's headers, with genesis first if they start at block 1
|
|
22
|
+
const caseChain = ({ network, height, headers: rawHeaders }) => {
|
|
23
|
+
const headers = toHeaders(rawHeaders);
|
|
24
|
+
const firstHeight = height - headers.length + 1;
|
|
25
|
+
if (firstHeight === 1) {
|
|
26
|
+
return { headers: [new SpvChain(network).genesis, ...headers], firstHeight: 0 };
|
|
27
|
+
}
|
|
28
|
+
return { headers, firstHeight };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const findCase = (network, height) => {
|
|
32
|
+
const { headers } = caseChain(
|
|
33
|
+
difficultyCases.find((c) => c.network === network && c.height === height),
|
|
34
|
+
);
|
|
35
|
+
return {
|
|
36
|
+
header: headers[headers.length - 1],
|
|
37
|
+
history: headers.slice(0, -1),
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Scales the target, keeping the exponent: difficulty changes by 1 / factor
|
|
42
|
+
const scaleTarget = (bits, factor) => {
|
|
43
|
+
const exponent = bits >>> 24;
|
|
44
|
+
const mantissa = Math.round((bits & 0x007fffff) * factor);
|
|
45
|
+
return ((exponent << 24) | mantissa) >>> 0;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
describe('Proof of work', () => {
|
|
49
|
+
before(async () => {
|
|
50
|
+
await SpvChain.wasmX11Ready();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('compact targets', () => {
|
|
54
|
+
// Vectors from Core's arith_uint256 tests
|
|
55
|
+
[
|
|
56
|
+
[0x00123456, 0n, false, false],
|
|
57
|
+
[0x01003456, 0n, false, false],
|
|
58
|
+
[0x02000056, 0n, false, false],
|
|
59
|
+
[0x03000000, 0n, false, false],
|
|
60
|
+
[0x01123456, 0x12n, false, false],
|
|
61
|
+
[0x01fedcba, 0x7en, true, false],
|
|
62
|
+
[0x02123456, 0x1234n, false, false],
|
|
63
|
+
[0x03123456, 0x123456n, false, false],
|
|
64
|
+
[0x04123456, 0x12345600n, false, false],
|
|
65
|
+
[0x04923456, 0x12345600n, true, false],
|
|
66
|
+
[0x05009234, 0x92340000n, false, false],
|
|
67
|
+
[0x20123456, 0x1234560000000000000000000000000000000000000000000000000000000000n, false, false],
|
|
68
|
+
[0xff123456, 0n, false, true],
|
|
69
|
+
].forEach(([compact, target, negative, overflow]) => {
|
|
70
|
+
it(`should decode 0x${compact.toString(16)} like Core`, () => {
|
|
71
|
+
const decoded = pow.decodeCompact(compact);
|
|
72
|
+
expect(decoded.negative).to.equal(negative);
|
|
73
|
+
expect(decoded.overflow).to.equal(overflow);
|
|
74
|
+
if (!overflow) {
|
|
75
|
+
expect(decoded.target).to.equal(target);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
[
|
|
81
|
+
[0n, 0],
|
|
82
|
+
[0x12n, 0x01120000],
|
|
83
|
+
[0x80n, 0x02008000],
|
|
84
|
+
[0x1234n, 0x02123400],
|
|
85
|
+
[0x123456n, 0x03123456],
|
|
86
|
+
[0x12345600n, 0x04123456],
|
|
87
|
+
[0x92340000n, 0x05009234],
|
|
88
|
+
].forEach(([target, compact]) => {
|
|
89
|
+
it(`should encode ${target} as 0x${compact.toString(16)}`, () => {
|
|
90
|
+
expect(pow.encodeCompact(target)).to.equal(compact);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('difficulty rules', () => {
|
|
96
|
+
difficultyCases.forEach((difficultyCase) => {
|
|
97
|
+
const { name, network, height } = difficultyCase;
|
|
98
|
+
|
|
99
|
+
it(`should accept the real header for ${name} at ${network} ${height}`, () => {
|
|
100
|
+
const { header, history } = findCase(network, height);
|
|
101
|
+
|
|
102
|
+
expect(pow.hasValidDifficulty(header, history, height, network)).to.equal(true);
|
|
103
|
+
expect(consensus.isValidBlockHeader(header, history, network, height)).to.equal(true);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it(`should compute the mined bits of every header it can in the ${network} ${height} case`, () => {
|
|
107
|
+
const { headers, firstHeight } = caseChain(difficultyCase);
|
|
108
|
+
let checked = 0;
|
|
109
|
+
|
|
110
|
+
for (let i = 1; i < headers.length; i += 1) {
|
|
111
|
+
const headerHeight = firstHeight + i;
|
|
112
|
+
const history = headers.slice(0, i);
|
|
113
|
+
const expectedBits = pow.getExpectedBits(
|
|
114
|
+
headers[i],
|
|
115
|
+
history,
|
|
116
|
+
headerHeight,
|
|
117
|
+
network,
|
|
118
|
+
firstHeight,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
if (expectedBits !== null) {
|
|
122
|
+
checked += 1;
|
|
123
|
+
// Early DGW versions mined bits that Core only checks within 50%
|
|
124
|
+
if (network === 'mainnet' && headerHeight >= 34140 && headerHeight <= 68589) {
|
|
125
|
+
expect(pow.hasValidDifficulty(headers[i], history, headerHeight, network))
|
|
126
|
+
.to.equal(true, `height ${headerHeight}`);
|
|
127
|
+
} else {
|
|
128
|
+
expect(headers[i].bits).to.equal(expectedBits, `height ${headerHeight}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
expect(checked).to.be.greaterThan(0);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
[
|
|
138
|
+
['testnet', 1152],
|
|
139
|
+
['testnet', 4135],
|
|
140
|
+
['testnet', 4447],
|
|
141
|
+
['testnet', 1500000],
|
|
142
|
+
['mainnet', 68590],
|
|
143
|
+
['mainnet', 2069337],
|
|
144
|
+
].forEach(([network, height]) => {
|
|
145
|
+
it(`should reject other bits at ${network} ${height}`, () => {
|
|
146
|
+
const { header, history } = findCase(network, height);
|
|
147
|
+
|
|
148
|
+
expect(pow.hasValidDifficulty(withBits(header, header.bits + 1), history, height, network))
|
|
149
|
+
.to.equal(false);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should reject the bits a plain average of the DGW window gives at mainnet 2069337', () => {
|
|
154
|
+
const { header, history } = findCase('mainnet', 2069337);
|
|
155
|
+
|
|
156
|
+
expect(pow.hasValidDifficulty(withBits(header, 0x193a408b), history, 2069337, 'mainnet'))
|
|
157
|
+
.to.equal(false);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should accept bits within 50% of the expected difficulty up to mainnet 68589', () => {
|
|
161
|
+
const { header, history } = findCase('mainnet', 68589);
|
|
162
|
+
const expectedBits = pow.getExpectedBits(header, history, 68589, 'mainnet');
|
|
163
|
+
const accepts = (bits) => pow.hasValidDifficulty(
|
|
164
|
+
withBits(header, bits),
|
|
165
|
+
history,
|
|
166
|
+
68589,
|
|
167
|
+
'mainnet',
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
// Accepted when the block's difficulty is between 2/3 and 2 times the
|
|
171
|
+
// expected one
|
|
172
|
+
expect(accepts(scaleTarget(expectedBits, 1.4))).to.equal(true);
|
|
173
|
+
expect(accepts(scaleTarget(expectedBits, 1.6))).to.equal(false);
|
|
174
|
+
expect(accepts(scaleTarget(expectedBits, 0.55))).to.equal(true);
|
|
175
|
+
expect(accepts(scaleTarget(expectedBits, 0.45))).to.equal(false);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('should check difficulty exactly from mainnet 68590', () => {
|
|
179
|
+
const { header, history } = findCase('mainnet', 68590);
|
|
180
|
+
|
|
181
|
+
expect(pow.hasValidDifficulty(withBits(header, scaleTarget(header.bits, 1.01)), history, 68590, 'mainnet'))
|
|
182
|
+
.to.equal(false);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should not decide when a rule reads headers from before the chain root', () => {
|
|
186
|
+
const { header, history } = findCase('mainnet', 21000);
|
|
187
|
+
|
|
188
|
+
expect(pow.hasValidDifficulty(header, history.slice(-100), 21000, 'mainnet')).to.equal(null);
|
|
189
|
+
expect(pow.hasValidDifficulty(header, history.slice(-1), 21000, 'mainnet')).to.equal(null);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('should throw when the history leaves out a header the chain has', () => {
|
|
193
|
+
const { header, history } = findCase('mainnet', 21000);
|
|
194
|
+
|
|
195
|
+
expect(() => pow.hasValidDifficulty(header, history.slice(-100), 21000, 'mainnet', 0))
|
|
196
|
+
.to.throw('validation history leaves out');
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('should check devnet difficulty with Core\'s default devnet rules', () => {
|
|
200
|
+
// DarkGravityWave gives the same bits on a devnet with the default spacing
|
|
201
|
+
const { header, history } = findCase('testnet', 1500000);
|
|
202
|
+
|
|
203
|
+
expect(pow.hasValidDifficulty(header, history, 1500000, 'devnet')).to.equal(true);
|
|
204
|
+
expect(pow.hasValidDifficulty(withBits(header, header.bits + 1), history, 1500000, 'devnet'))
|
|
205
|
+
.to.equal(false);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('should check devnet difficulty with the settings the devnet was started with', () => {
|
|
209
|
+
const { header, history } = findCase('testnet', 1500000);
|
|
210
|
+
const fastDevnet = { network: 'devnet', powTargetSpacing: 60 };
|
|
211
|
+
const lateDevnet = { network: 'devnet', minimumDifficultyBlocks: 2000000 };
|
|
212
|
+
|
|
213
|
+
expect(pow.hasValidDifficulty(header, history, 1500000, fastDevnet)).to.equal(false);
|
|
214
|
+
expect(pow.getExpectedBits(header, history, 1500000, lateDevnet))
|
|
215
|
+
.to.equal(pow.encodeCompact(pow.getNetworkParams('devnet').powLimit));
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('should accept block spacing and minimum-difficulty settings only for devnets', () => {
|
|
219
|
+
expect(() => pow.getNetworkParams({ network: 'mainnet', powTargetSpacing: 60 }))
|
|
220
|
+
.to.throw('Only devnets');
|
|
221
|
+
expect(() => pow.getNetworkParams({ network: 'devnet', powTargetSpacing: 10 }))
|
|
222
|
+
.to.throw('Unsupported block spacing 10');
|
|
223
|
+
expect(() => new SpvChain('devnet', 100, { powTargetSpacing: 0 })).to.throw();
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe('proof of work', () => {
|
|
228
|
+
it('should check the hash of a header as it is now, not a memoized one', () => {
|
|
229
|
+
const { header } = findCase('mainnet', 2069337);
|
|
230
|
+
const changed = new BlockHeader(header.toBuffer());
|
|
231
|
+
expect(consensus.isValidBlockHeaderWithoutContext(changed, 'mainnet')).to.equal(true);
|
|
232
|
+
|
|
233
|
+
// header.hash is memoized; changing a field doesn't refresh it
|
|
234
|
+
changed.nonce += 1;
|
|
235
|
+
|
|
236
|
+
expect(consensus.isValidBlockHeaderWithoutContext(changed, 'mainnet')).to.equal(false);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('should accept previous headers in any format a header can be given in', () => {
|
|
240
|
+
const { header, history } = findCase('mainnet', 2069337);
|
|
241
|
+
const rawHistory = history.map((h) => h.toBuffer().toString('hex'));
|
|
242
|
+
|
|
243
|
+
expect(consensus.isValidBlockHeader(header, rawHistory, 'mainnet', 2069337)).to.equal(true);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe('median time past', () => {
|
|
248
|
+
it('should reject a header not later than the median time of the previous 11', () => {
|
|
249
|
+
const { genesis } = new SpvChain('regtest');
|
|
250
|
+
const history = [genesis];
|
|
251
|
+
while (history.length < 11) {
|
|
252
|
+
history.push(utils.createBlock(history[history.length - 1], 0x207fffff));
|
|
253
|
+
}
|
|
254
|
+
const median = history[5].time;
|
|
255
|
+
|
|
256
|
+
const template = utils.createBlock(history[history.length - 1], 0x207fffff);
|
|
257
|
+
let atMedian;
|
|
258
|
+
let nonce = 0;
|
|
259
|
+
do {
|
|
260
|
+
atMedian = withFields(template, { time: median, nonce });
|
|
261
|
+
nonce += 1;
|
|
262
|
+
} while (!utils.validProofOfWork(atMedian));
|
|
263
|
+
|
|
264
|
+
expect(pow.getMedianTimePast(history, 11)).to.equal(median);
|
|
265
|
+
expect(consensus.isValidBlockHeader(atMedian, history, 'regtest', 11)).to.equal(false);
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe('SpvChain', () => {
|
|
270
|
+
it('should keep its own copies of the headers it is given', () => {
|
|
271
|
+
const { history, header } = findCase('testnet', 1152);
|
|
272
|
+
const chain = new SpvChain('testnet', 10000);
|
|
273
|
+
chain.initialize(history[0], 1152 - history.length);
|
|
274
|
+
const given = [...history.slice(1), header].map((h) => new BlockHeader(h.toBuffer()));
|
|
275
|
+
|
|
276
|
+
chain.addHeaders(given);
|
|
277
|
+
given[given.length - 1].nonce += 1;
|
|
278
|
+
|
|
279
|
+
expect(chain.getTipHeader()).to.not.equal(given[given.length - 1]);
|
|
280
|
+
expect(chain.getTipHeader().nonce).to.equal(header.nonce);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('should not be changed through a raw Buffer it was given', () => {
|
|
284
|
+
const { history, header } = findCase('testnet', 1152);
|
|
285
|
+
const chain = new SpvChain('testnet', 10000);
|
|
286
|
+
const rawRoot = history[0].toBuffer();
|
|
287
|
+
chain.initialize(rawRoot, 1152 - history.length);
|
|
288
|
+
const raw = [...history.slice(1), header].map((h) => h.toBuffer());
|
|
289
|
+
|
|
290
|
+
chain.addHeaders(raw);
|
|
291
|
+
// prevHash and merkleRoot of a header read from a Buffer are slices of it
|
|
292
|
+
raw[raw.length - 1].fill(0, 4, 68);
|
|
293
|
+
rawRoot.fill(0, 4, 68);
|
|
294
|
+
|
|
295
|
+
expect(utils.getHeaderHash(chain.getTipHeader())).to.equal(header.hash);
|
|
296
|
+
expect(() => chain.validate()).to.not.throw();
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('should validate headers from a checkpoint at their own heights', () => {
|
|
300
|
+
const { history, header } = findCase('testnet', 1152);
|
|
301
|
+
const chain = new SpvChain('testnet', 10000);
|
|
302
|
+
|
|
303
|
+
chain.initialize(history[0], 1152 - history.length);
|
|
304
|
+
chain.addHeaders([...history.slice(1), header]);
|
|
305
|
+
|
|
306
|
+
expect(chain.getTipHash()).to.equal(header.hash);
|
|
307
|
+
expect(chain.hashesByHeight.get(1152)).to.equal(header.hash);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|