forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpow-test.js
88 lines (75 loc) · 2.42 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
/* 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 network = Network.get('main');
function random(max) {
return Math.floor(Math.random() * max);
}
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);
}
});
});