-
Notifications
You must be signed in to change notification settings - Fork 276
/
mempool-invalidation-test.js
292 lines (219 loc) · 7.88 KB
/
mempool-invalidation-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
'use strict';
const assert = require('bsert');
const {BufferMap} = require('buffer-map');
const Network = require('../lib/protocol/network');
const FullNode = require('../lib/node/fullnode');
const ownership = require('../lib/covenants/ownership');
const rules = require('../lib/covenants/rules');
const {forEvent} = require('./util/common');
const network = Network.get('regtest');
const {
treeInterval,
claimPeriod
} = network.names;
const ACTUAL_CLAIM_PERIOD = claimPeriod;
describe('Mempool Invalidation', function() {
const NAMES = [
// roots
'nl',
// top 100
'paypal',
// custom
'cloudflare',
// other
'steamdb'
];
describe('Claim Invalidation (Integration)', function() {
this.timeout(50000);
let node, wallet;
// copy names
const TEST_CLAIMS = NAMES.slice();
before(async () => {
node = new FullNode({
memory: true,
network: network.type,
plugins: [require('../lib/wallet/plugin')]
});
await node.ensure();
await node.open();
// Ignore claim validation
ownership.ignore = true;
const walletPlugin = node.require('walletdb');
const wdb = walletPlugin.wdb;
wallet = await wdb.get('primary');
const addr = await wallet.receiveAddress('default');
node.miner.addAddress(addr.toString());
// first interval maturity
// second interval mine claim
network.names.claimPeriod = treeInterval * 3;
// third interval last block should invalidate.
});
after(async () => {
network.names.claimPeriod = ACTUAL_CLAIM_PERIOD;
await node.close();
});
it('should mine an interval', async () => {
for (let i = 0; i < treeInterval; i++)
await mineBlock(node);
});
it('should mine claims before claimPeriod timeout', async () => {
const name = TEST_CLAIMS.shift();
const claim = await wallet.makeFakeClaim(name);
let block;
await node.mempool.insertClaim(claim);
assert.strictEqual(node.mempool.claims.size, 1);
// retain claim in mempool.
[block] = await mineBlock(node, { ignoreClaims: true });
assert.strictEqual(node.mempool.claims.size, 1);
assert.strictEqual(block.txs[0].outputs.length, 1);
// Now we can mine it.
[block] = await mineBlock(node);
assert.strictEqual(node.mempool.claims.size, 0);
assert.strictEqual(block.txs[0].outputs.length, 2);
assert.strictEqual(block.txs[0].outputs[1].covenant.type, rules.types.CLAIM);
});
it('should invalidate claim after claimPeriod timeout', async () => {
const name = TEST_CLAIMS.shift();
const claim = await wallet.makeFakeClaim(name);
let block = null;
// Mempool treats txs in it as if they were mined in the next block,
// so we need next block to still be valid.
while (node.chain.tip.height < network.names.claimPeriod - 2)
await mineBlock(node);
await node.mempool.insertClaim(claim);
[block] = await mineBlock(node, { ignoreClaims: true });
// Should invalidate the claim, because next block can't have claims.
assert.strictEqual(node.mempool.claims.size, 0);
assert.strictEqual(block.txs[0].outputs.length, 1);
// Should fail to insert claim, as they can't be mined.
let err;
try {
err = await node.mempool.insertClaim(claim);
} catch (e) {
err = e;
}
assert(err);
assert.strictEqual(err.type, 'VerifyError');
assert.strictEqual(err.reason, 'invalid-covenant');
[block] = await mineBlock(node);
assert.strictEqual(node.mempool.claims.size, 0);
assert.strictEqual(block.txs[0].outputs.length, 1);
});
});
describe('Claim Invalidation on reorg (Integration)', function() {
this.timeout(50000);
let node, wallet;
// copy names
const TEST_CLAIMS = NAMES.slice();
before(async () => {
node = new FullNode({
memory: true,
network: network.type,
plugins: [require('../lib/wallet/plugin')]
});
await node.ensure();
await node.open();
// Ignore claim validation
ownership.ignore = true;
const walletPlugin = node.require('walletdb');
const wdb = walletPlugin.wdb;
wallet = await wdb.get('primary');
const addr = await wallet.receiveAddress('default');
node.miner.addAddress(addr.toString());
// first interval maturity
// second interval mine claim
network.names.claimPeriod = treeInterval * 3;
// third interval last block should invalidate.
});
after(async () => {
network.names.claimPeriod = ACTUAL_CLAIM_PERIOD;
await node.close();
});
it('should mine an interval', async () => {
for (let i = 0; i < treeInterval; i++)
await mineBlock(node);
});
it('should mine claims before claimPeriod timeout', async () => {
const name = TEST_CLAIMS.shift();
const claim = await wallet.makeFakeClaim(name);
let block;
await node.mempool.insertClaim(claim);
assert.strictEqual(node.mempool.claims.size, 1);
// retain claim in mempool.
[block] = await mineBlock(node, { ignoreClaims: true });
assert.strictEqual(node.mempool.claims.size, 1);
assert.strictEqual(block.txs[0].outputs.length, 1);
// Now we can mine it.
[block] = await mineBlock(node);
assert.strictEqual(node.mempool.claims.size, 0);
assert.strictEqual(block.txs[0].outputs.length, 2);
assert.strictEqual(block.txs[0].outputs[1].covenant.type, rules.types.CLAIM);
});
it('should invalidate claim after claimPeriod timeout', async () => {
const name = TEST_CLAIMS.shift();
const claim = await wallet.makeFakeClaim(name);
let block, entry;
// Mempool treats txs in it as if they were mined in the next block,
// so we need next block to still be valid.
while (node.chain.tip.height < network.names.claimPeriod - 2)
await mineBlock(node);
await node.mempool.insertClaim(claim);
// here we experience a reorg into the claim period.
const tip = node.chain.tip;
const prev = await node.chain.getPrevious(tip);
[block, entry] = await mineBlock(node, {
ignoreClaims: true,
tip: prev,
blockWait: false
});
assert.strictEqual(node.mempool.claims.size, 1);
assert.strictEqual(block.txs[0].outputs.length, 1);
// Now reorg.
[block, entry] = await mineBlock(node, {
ignoreClaims: true,
tip: entry
});
// Should invalidate the claim, because next block can't have claims.
assert.strictEqual(node.mempool.claims.size, 0);
assert.strictEqual(block.txs[0].outputs.length, 1);
// Should fail to insert claim, as they can't be mined.
let err;
try {
err = await node.mempool.insertClaim(claim);
} catch (e) {
err = e;
}
assert(err);
assert.strictEqual(err.type, 'VerifyError');
assert.strictEqual(err.reason, 'invalid-covenant');
[block] = await mineBlock(node);
assert.strictEqual(node.mempool.claims.size, 0);
assert.strictEqual(block.txs[0].outputs.length, 1);
});
});
});
async function mineBlock(node, opts = {}) {
assert(node);
const chain = node.chain;
const miner = node.miner;
const ignoreClaims = opts.ignoreClaims ?? false;
const tip = opts.tip || chain.tip;
const blockWait = opts.blockWait ?? true;
let forBlock = null;
if (blockWait)
forBlock = forEvent(node, 'block', 1, 2000);
let backupClaims = null;
if (ignoreClaims) {
backupClaims = node.mempool.claims;
node.mempool.claims = new BufferMap();
}
const job = await miner.cpu.createJob(tip);
job.refresh();
if (ignoreClaims)
node.mempool.claims = backupClaims;
const block = await job.mineAsync();
const entry = await chain.add(block);
if (blockWait)
await forBlock;
return [block, entry];
}