-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathpow-test.js
331 lines (273 loc) · 10.7 KB
/
pow-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
'use strict';
const assert = require('./util/assert');
const Chain = require('../lib/blockchain/chain');
const ChainEntry = require('../lib/blockchain/chainentry');
const Network = require('../lib/protocol/network');
const consensus = require('../lib/protocol/consensus');
const network = Network.get('main');
function random(max) {
return Math.floor(Math.random() * max);
}
function getEntry(prev, time, bits) {
const entry = new ChainEntry();
entry.height = prev.height + 1;
entry.time = prev.time + time;
entry.bits = bits;
entry.chainwork = entry.getProof().add(prev.chainwork);
return entry;
}
const chain = new Chain({
memory: true,
network
});
describe('Difficulty', function() {
it('should open chain', async () => {
await chain.open();
});
it('should get next work', async () => {
const prev = new ChainEntry();
prev.time = 1262152739;
prev.bits = 0x1d00ffff;
prev.height = 32255;
const first = new ChainEntry();
first.time = 1261130161;
assert.strictEqual(chain.retarget(prev, first), 0x1d00d86a);
});
it('should get next work pow limit', async () => {
const prev = new ChainEntry();
prev.time = 1233061996;
prev.bits = 0x1d00ffff;
prev.height = 2015;
const first = new ChainEntry();
first.time = 1231006505;
assert.strictEqual(chain.retarget(prev, first), 0x1d00ffff);
});
it('should get next work lower limit actual', async () => {
const prev = new ChainEntry();
prev.time = 1279297671;
prev.bits = 0x1c05a3f4;
prev.height = 68543;
const first = new ChainEntry();
first.time = 1279008237;
assert.strictEqual(chain.retarget(prev, first), 0x1c0168fd);
});
it('should get next work upper limit actual', async () => {
const prev = new ChainEntry();
prev.time = 1269211443;
prev.bits = 0x1c387f6f;
prev.height = 46367;
const first = new ChainEntry();
first.time = 1263163443;
assert.strictEqual(chain.retarget(prev, first), 0x1d00e1fd);
});
it('should get block proof equivalent time', async () => {
const blocks = [];
for (let i = 0; i < 10000; i++) {
const prev = new ChainEntry();
prev.height = i;
prev.time = 1269211443 + i * network.pow.targetSpacing;
prev.bits = 0x207fffff;
if (i > 0)
prev.chainwork = prev.getProof().addn(blocks[i-1].chainwork.toNumber());
blocks[i] = prev;
}
chain.tip = blocks[blocks.length - 1];
for (let j = 0; j < 1000; j++) {
const p1 = blocks[random(blocks.length)];
const p2 = blocks[random(blocks.length)];
const tdiff = chain.getProofTime(p1, p2);
assert.ok(tdiff === p1.time - p2.time);
}
});
it('should get retargeting', async () => {
let target = network.pow.limit.ushrn(1);
let bits = consensus.toCompact(target);
const blocks = {};
blocks[0] = new ChainEntry();
blocks[0].height = 0;
blocks[0].time = 1269211443;
blocks[0].bits = bits;
blocks[0].chainwork = blocks[0].getProof();
chain.getAncestor = async(entry, height) => {
return blocks[height];
};
chain.getPrevious = async(entry) => {
return blocks[entry.height-1];
};
// Pile up some blocks.
for (let i = 1; i < 100; i++) {
blocks[i] = getEntry(blocks[i-1], network.pow.targetSpacing, bits);
}
// We start getting 2h blocks time. For the first 5 blocks, it doesn't
// matter as the MTP is not affected. For the next 5 block, MTP difference
// increases but stays below 12h.
for (let i = 100; i < 110; i++) {
blocks[i] = getEntry(blocks[i-1], 2 * 3600, bits);
assert.strictEqual(bits,
await chain.getTarget(blocks[0].time, blocks[i]));
}
// Now we expect the difficulty to decrease.
blocks[110] = getEntry(blocks[109], 2 * 3600, bits);
target.iadd(target.ushrn(2));
bits = consensus.toCompact(target);
assert.strictEqual(bits,
await chain.getTarget(blocks[0].time, blocks[110]));
// As we continue with 2h blocks, difficulty continue to decrease.
blocks[111] = getEntry(blocks[110], 2 * 3600, bits);
target = consensus.fromCompact(bits);
target.iadd(target.ushrn(2));
bits = consensus.toCompact(target);
assert.strictEqual(bits,
await chain.getTarget(blocks[0].time, blocks[111]));
// We decrease again.
blocks[112] = getEntry(blocks[111], 2 * 3600, bits);
target = consensus.fromCompact(bits);
target.iadd(target.ushrn(2));
bits = consensus.toCompact(target);
assert.strictEqual(bits,
await chain.getTarget(blocks[0].time, blocks[112]));
// We check that we do not go below the minimal difficulty.
blocks[113] = getEntry(blocks[112], 2 * 3600, bits);
assert.strictEqual(network.pow.bits,
await chain.getTarget(blocks[0].time, blocks[113]));
// Once we reached the minimal difficulty, we stick with it.
blocks[114] = getEntry(blocks[113], 2 * 3600, bits);
assert.strictEqual(network.pow.bits,
await chain.getTarget(blocks[0].time, blocks[114]));
});
it('should test cash difficulty', async () => {
const target = network.pow.limit.ushrn(4);
let bits = consensus.toCompact(target);
const blocks = {};
blocks[0] = new ChainEntry();
blocks[0].height = 0;
blocks[0].time = 1269211443;
blocks[0].bits = bits;
blocks[0].chainwork = blocks[0].getProof();
chain.getAncestor = async(entry, height) => {
return blocks[height];
};
chain.getPrevious = async(entry) => {
return blocks[entry.height-1];
};
// Block counter.
let i;
// Pile up some blocks every 10 mins to establish some history.
for (i = 1; i < 2050; i++) {
blocks[i] = getEntry(blocks[i-1], 600, bits);
}
bits = await chain.getTarget(blocks[0].time, blocks[2049]);
// Difficulty stays the same as long as we produce a block every 10 mins.
for (let j = 0; j < 10; i++, j++) {
blocks[i] = getEntry(blocks[i-1], 600, bits);
assert.strictEqual(bits,
await chain.getTarget(blocks[0].time, blocks[i]));
}
// Make sure we skip over blocks that are out of wack. To do so, we produce
// a block that is far in the future, and then produce a block with the
// expected timestamp.
blocks[i] = getEntry(blocks[i-1], 6000, bits);
assert.strictEqual(bits,
await chain.getTarget(blocks[0].time, blocks[i++]));
blocks[i] = getEntry(blocks[i-1], 2 * 600 - 6000, bits);
assert.strictEqual(bits,
await chain.getTarget(blocks[0].time, blocks[i++]));
// The system should continue unaffected by the block with a bogous
// timestamps.
for (let j = 0; j < 20; i++, j++) {
blocks[i] = getEntry(blocks[i-1], 600, bits);
assert.strictEqual(bits,
await chain.getTarget(blocks[0].time, blocks[i]));
}
// We start emitting blocks slightly faster. The first block has no impact.
blocks[i] = getEntry(blocks[i-1], 550, bits);
assert.strictEqual(bits,
await chain.getTarget(blocks[0].time, blocks[i++]));
// Now we should see difficulty increase slowly.
for (let j = 0; j < 10; i++, j++) {
blocks[i] = getEntry(blocks[i-1], 550, bits);
const nextBits =
await chain.getCashTarget(blocks[0].time, blocks[i]);
const currentTarget = consensus.fromCompact(bits);
const nextTarget = consensus.fromCompact(nextBits);
// Make sure that difficulty increases very slowly.
assert.strictEqual(nextTarget.cmp(currentTarget), -1);
assert.strictEqual(
currentTarget.sub(nextTarget).cmp(currentTarget.iushrn(10)), -1);
bits = nextBits;
}
// Check the actual value.
assert.strictEqual(bits, 0x1c0fe7b1);
// If we dramatically shorten block production, difficulty increases faster.
for (let j = 0; j < 20; i++, j++) {
blocks[i] = getEntry(blocks[i-1], 10, bits);
const nextBits =
await chain.getCashTarget(blocks[0].time, blocks[i]);
const currentTarget = consensus.fromCompact(bits);
const nextTarget = consensus.fromCompact(nextBits);
// Make sure that difficulty increases very slowly.
assert.strictEqual(nextTarget.cmp(currentTarget), -1);
assert.strictEqual(
currentTarget.sub(nextTarget).cmp(currentTarget.iushrn(4)), -1);
bits = nextBits;
}
// Check the actual value.
assert.strictEqual(bits, 0x1c0db19f);
// We start to emit blocks significantly slower. The first block has no
// impact.
blocks[i] = getEntry(blocks[i-1], 6000, bits);
bits = await chain.getCashTarget(blocks[0].time, blocks[i++]);
// Check the actual value.
assert.strictEqual(bits, 0x1c0d9222);
// If we dramatically slow down block production, difficulty decreases.
for (let j = 0; j < 93; i++, j++) {
blocks[i] = getEntry(blocks[i-1], 6000, bits);
const nextBits =
await chain.getCashTarget(blocks[0].time, blocks[i]);
const currentTarget = consensus.fromCompact(bits);
const nextTarget = consensus.fromCompact(nextBits);
// Check the difficulty decreases.
assert.ok(nextTarget.lte(network.pow.limit));
assert.strictEqual(nextTarget.cmp(currentTarget), 1);
assert.strictEqual(
nextTarget.sub(currentTarget).cmp(currentTarget.iushrn(3)), -1);
bits = nextBits;
}
// Check the actual value.
assert.strictEqual(bits, 0x1c2f13b9);
// Due to the window of time being bounded, next block's difficulty actually
// gets harder.
blocks[i] = getEntry(blocks[i-1], 6000, bits);
bits = await chain.getCashTarget(blocks[0].time, blocks[i++]);
assert.strictEqual(bits, 0x1c2ee9bf);
// And goes down again. It takes a while due to the window being bounded and
// the skewed block causes 2 blocks to get out of the window.
for (let j = 0; j < 192; i++, j++) {
blocks[i] = getEntry(blocks[i-1], 6000, bits);
const nextBits =
await chain.getCashTarget(blocks[0].time, blocks[i]);
const currentTarget = consensus.fromCompact(bits);
const nextTarget = consensus.fromCompact(nextBits);
// Check the difficulty decreases.
assert.ok(nextTarget.lte(network.pow.limit));
assert.strictEqual(nextTarget.cmp(currentTarget), 1);
assert.strictEqual(
nextTarget.sub(currentTarget).cmp(currentTarget.iushrn(3)), -1);
bits = nextBits;
}
// Check the actual value.
assert.strictEqual(bits, 0x1d00ffff);
// Once the difficulty reached the minimum allowed level, it doesn't get any
// easier.
for (let j = 0; j < 5; i++, j++) {
blocks[i] = getEntry(blocks[i-1], 6000, bits);
const nextBits =
await chain.getTarget(blocks[0].time, blocks[i]);
// Check the difficulty stays constant.
assert.strictEqual(nextBits, network.pow.bits);
bits = nextBits;
}
});
});