-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathchain-prune-test.js
180 lines (145 loc) · 4.16 KB
/
chain-prune-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
'use strict';
const assert = require('bsert');
const Network = require('../lib/protocol/network');
const WorkerPool = require('../lib/workers/workerpool');
const Miner = require('../lib/mining/miner');
const Chain = require('../lib/blockchain/chain');
const BlockStore = require('../lib/blockstore/level');
const network = Network.get('regtest');
describe('Chain Prune', function() {
describe('Prune options', function() {
const chainOptions = {
memory: true,
network
};
let blocks;
beforeEach(async () => {
blocks = new BlockStore(chainOptions);
chainOptions.blocks = blocks;
await blocks.open();
});
afterEach(async () => {
await blocks.close();
});
it('should not allow retroactive prune', async () => {
const chain = new Chain(chainOptions);
await chain.open();
await chain.close();
// Retroactively enable prune.
await assert.rejects(async () => {
chain.options.prune = true;
await chain.open();
}, {
message: 'Cannot retroactively prune.'
});
await chain.close();
});
it('should not allow prune with spv', async () => {
assert.throws(() => {
new Chain({
...chainOptions,
prune: true,
spv: true
});
}, {
message: 'Can not prune in spv mode.'
});
});
it('should not allow retroactive unprune', async () => {
const chain = new Chain({
...chainOptions,
prune: true
});
await chain.open();
await chain.close();
await assert.rejects(async () => {
chain.options.prune = false;
await chain.open();
}, {
message: 'Cannot retroactively unprune.'
});
await chain.close();
});
});
describe('Prune', function() {
const PRUNE_AFTER_HEIGHT = network.block.pruneAfterHeight;
const KEEP_BLOCKS = network.block.keepBlocks;
const TEST_PRUNE_AFTER_HEIGHT = 10;
const TEST_KEEP_BLOCKS = 10;
const TEST_PRUNED_BLOCKS = 10;
const workers = new WorkerPool({
enabled: true,
size: 2
});
const chainOptions = {
memory: true,
network,
workers
};
let chain, miner, cpu, blocks;
before(async () => {
await workers.open();
});
after(async () => {
await workers.close();
});
beforeEach(async () => {
network.block.pruneAfterHeight = TEST_PRUNE_AFTER_HEIGHT;
network.block.keepBlocks = TEST_KEEP_BLOCKS;
blocks = new BlockStore({
memory: true,
network
});
chain = new Chain({
...chainOptions,
blocks
});
miner = new Miner({ chain });
cpu = miner.cpu;
await miner.open();
await blocks.open();
});
afterEach(async () => {
network.block.pruneAfterHeight = PRUNE_AFTER_HEIGHT;
network.block.keepBlocks = KEEP_BLOCKS;
if (chain.opened)
await chain.close();
await blocks.close();
await miner.close();
});
it('should prune blocks', async () => {
chain.options.prune = true;
await chain.open();
const hashes = [];
let genBlocks = TEST_PRUNE_AFTER_HEIGHT;
genBlocks += TEST_PRUNED_BLOCKS;
genBlocks += TEST_KEEP_BLOCKS;
// 10 behind height check + 10 pruned + 10 keep blocks
for (let i = 0; i < genBlocks; i++) {
const block = await cpu.mineBlock();
hashes.push(block.hash());
assert(block);
assert(await chain.add(block));
}
let i = 0;
// behind height check
let to = TEST_PRUNE_AFTER_HEIGHT;
for (; i < 10; i++) {
const block = await chain.getBlock(hashes[i]);
assert(block, 'could not get block before height check.');
}
// pruned blocks - nulls
to += TEST_PRUNED_BLOCKS;
for (; i < to; i++) {
const block = await chain.getBlock(hashes[i]);
assert.strictEqual(block, null, `block ${i} was not pruned.`);
}
// keep blocks
to += TEST_KEEP_BLOCKS;
for (; i < to; i++) {
const block = await chain.getBlock(hashes[i]);
assert(block, `block ${i} was pruned.`);
}
});
});
});