/
wallet-rescan-test.js
569 lines (471 loc) · 18.1 KB
/
wallet-rescan-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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
/* eslint no-implicit-coercion: "off" */
'use strict';
const assert = require('bsert');
const FullNode = require('../lib/node/fullnode');
const MemWallet = require('./util/memwallet');
const Network = require('../lib/protocol/network');
const Address = require('../lib/primitives/address');
const rules = require('../lib/covenants/rules');
const {Resource} = require('../lib/dns/resource');
const {forValue} = require('./util/common');
const network = Network.get('regtest');
const {
treeInterval,
biddingPeriod,
revealPeriod,
transferLockup
} = network.names;
describe('Wallet rescan with namestate transitions', function() {
describe('Only sends OPEN', function() {
// Bob runs a full node with wallet plugin
const node = new FullNode({
network: network.type,
memory: true,
plugins: [require('../lib/wallet/plugin')]
});
node.on('error', (err) => {
assert(false, err);
});
const {wdb} = node.require('walletdb');
let bob, bobAddr;
// Alice is some other wallet on the network
const alice = new MemWallet({ network });
const aliceAddr = alice.getAddress();
// Connect MemWallet to chain as minimally as possible
node.chain.on('connect', (entry, block) => {
alice.addBlock(entry, block.txs);
});
alice.getNameStatus = async (nameHash) => {
assert(Buffer.isBuffer(nameHash));
const height = node.chain.height + 1;
const state = await node.chain.getNextState();
const hardened = state.hasHardening();
return node.chain.db.getNameStatus(nameHash, height, hardened);
};
const NAME = rules.grindName(4, 4, network);
// Hash of the FINALIZE transaction
let aliceFinalizeHash;
async function mineBlocks(n, addr) {
addr = addr ? addr : new Address().toString('regtest');
const blocks = [];
for (let i = 0; i < n; i++) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
blocks.push(block);
}
return blocks;
}
async function sendTXs() {
const aliceTX = await alice.send({
outputs: [{
address: aliceAddr,
value: 20000
}]
});
alice.addTX(aliceTX.toTX());
await node.mempool.addTX(aliceTX.toTX());
await bob.send({
outputs: [{
address: bobAddr,
value: 20000
}]
});
}
before(async () => {
await node.open();
bob = await wdb.create();
bobAddr = await bob.receiveAddress();
});
after(async () => {
await node.close();
});
it('should fund wallets', async () => {
const blocks = 10;
await mineBlocks(blocks, aliceAddr);
await mineBlocks(blocks, bobAddr);
const bobBal = await bob.getBalance();
assert.strictEqual(bobBal.confirmed, blocks * 2000 * 1e6);
assert.strictEqual(alice.balance, blocks * 2000 * 1e6);
});
it('should run auction', async () => {
// Poor Bob, all he does is send an OPEN but his wallet will
// watch all the other activity including TRANSFERS for this name
await bob.sendOpen(NAME, true);
// Scatter unrelated TXs throughout the test.
// This will ensure that txdb.removeBlock() removes TXs
// in the reverse order from when they were added
await sendTXs();
const openBlocks = await mineBlocks(1);
// Coinbase plus open
assert.strictEqual(openBlocks[0].txs.length, 4);
// Advance to bidding phase
await mineBlocks(treeInterval);
await forValue(alice, 'height', node.chain.height);
// Alice sends only bid
await sendTXs();
const aliceBid = await alice.createBid(NAME, 20000, 20000);
await node.mempool.addTX(aliceBid.toTX());
const bidBlocks = await mineBlocks(1);
assert.strictEqual(bidBlocks[0].txs.length, 4);
// Advance to reveal phase
await mineBlocks(biddingPeriod);
await sendTXs();
const aliceReveal = await alice.createReveal(NAME);
await node.mempool.addTX(aliceReveal.toTX());
const revealBlocks = await mineBlocks(1);
assert.strictEqual(revealBlocks[0].txs.length, 4);
// Close auction
await mineBlocks(revealPeriod);
// Alice registers
await sendTXs();
const aliceRegister = await alice.createRegister(
NAME,
Resource.fromJSON({records:[]})
);
await node.mempool.addTX(aliceRegister.toTX());
const registerBlocks = await mineBlocks(1);
assert.strictEqual(registerBlocks[0].txs.length, 4);
});
it('should get namestate', async () => {
const ns = await bob.getNameStateByName(NAME);
// Bob has the namestate
assert(ns);
// Bob is not the name owner
const {hash, index} = ns.owner;
const coin = await bob.getCoin(hash, index);
assert.strictEqual(coin, null);
// Name is not in mid-TRANSFER
assert.strictEqual(ns.transfer, 0);
});
it('should process TRANSFER', async () => {
// Alice transfers the name to her own address
await sendTXs();
const aliceTransfer = await alice.createTransfer(NAME, aliceAddr);
await node.mempool.addTX(aliceTransfer.toTX());
const transferBlocks = await mineBlocks(1);
assert.strictEqual(transferBlocks[0].txs.length, 4);
// Bob detects the TRANSFER even though it doesn't involve him at all
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.transfer, node.chain.height);
// Bob's wallet has not indexed the TRANSFER
const bobTransfer = await bob.getTX(aliceTransfer.hash());
assert.strictEqual(bobTransfer, null);
});
it('should fully rescan', async () => {
// Complete chain rescan
await wdb.rescan(0);
await forValue(wdb, 'height', node.chain.height);
// No change
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.transfer, node.chain.height);
});
it('should process FINALIZE', async () => {
await mineBlocks(transferLockup);
// Alice finalizes the name
await sendTXs();
const aliceFinalize = await alice.createFinalize(NAME);
await node.mempool.addTX(aliceFinalize.toTX());
const finalizeBlocks = await mineBlocks(1);
assert.strictEqual(finalizeBlocks[0].txs.length, 4);
aliceFinalizeHash = aliceFinalize.hash();
// Bob detects the FINALIZE even though it doesn't involve him at all
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.bufferEqual(ns.owner.hash, aliceFinalizeHash);
// Bob's wallet has not indexed the FINALIZE
const bobFinalize = await bob.getTX(aliceFinalize.hash());
assert.strictEqual(bobFinalize, null);
});
it('should fully rescan', async () => {
// Complete chain rescan
await wdb.rescan(0);
await forValue(wdb, 'height', node.chain.height);
// No change
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.bufferEqual(ns.owner.hash, aliceFinalizeHash);
});
it('should process TRANSFER (again)', async () => {
// Alice transfers the name to her own address
await sendTXs();
const aliceTransfer = await alice.createTransfer(NAME, aliceAddr);
await node.mempool.addTX(aliceTransfer.toTX());
const transferBlocks = await mineBlocks(1);
assert.strictEqual(transferBlocks[0].txs.length, 4);
// Bob detects the TRANSFER even though it doesn't involve him at all
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.transfer, node.chain.height);
// Bob's wallet has not indexed the TRANSFER
const bobTransfer = await bob.getTX(aliceTransfer.hash());
assert.strictEqual(bobTransfer, null);
});
it('should process REVOKE', async () => {
// Alice revokes the name
await sendTXs();
const aliceRevoke = await alice.createRevoke(NAME);
await node.mempool.addTX(aliceRevoke.toTX());
const revokeBlocks = await mineBlocks(1);
assert.strictEqual(revokeBlocks[0].txs.length, 4);
// Bob detects the REVOKE even though it doesn't involve him at all
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.revoked, node.chain.height);
// Bob's wallet has not indexed the REVOKE
const bobTransfer = await bob.getTX(aliceRevoke.hash());
assert.strictEqual(bobTransfer, null);
});
it('should fully rescan', async () => {
// Complete chain rescan
await wdb.rescan(0);
await forValue(wdb, 'height', node.chain.height);
// No change
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.revoked, node.chain.height);
});
});
describe('Bids, loses, shallow rescan', function() {
// Bob runs a full node with wallet plugin
const node = new FullNode({
network: network.type,
memory: true,
plugins: [require('../lib/wallet/plugin')]
});
node.on('error', (err) => {
assert(false, err);
});
const {wdb} = node.require('walletdb');
let bob, bobAddr;
// Alice is some other wallet on the network
const alice = new MemWallet({ network });
const aliceAddr = alice.getAddress();
// Connect MemWallet to chain as minimally as possible
node.chain.on('connect', (entry, block) => {
alice.addBlock(entry, block.txs);
});
alice.getNameStatus = async (nameHash) => {
assert(Buffer.isBuffer(nameHash));
const height = node.chain.height + 1;
const state = await node.chain.getNextState();
const hardened = state.hasHardening();
return node.chain.db.getNameStatus(nameHash, height, hardened);
};
const NAME = rules.grindName(4, 4, network);
// Block that confirmed the bids
let bidBlockHash;
// Hash of the FINALIZE transaction
let aliceFinalizeHash;
async function mineBlocks(n, addr) {
addr = addr ? addr : new Address().toString('regtest');
const blocks = [];
for (let i = 0; i < n; i++) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
blocks.push(block);
}
return blocks;
}
async function sendTXs() {
const aliceTX = await alice.send({
outputs: [{
address: aliceAddr,
value: 20000
}]
});
alice.addTX(aliceTX.toTX());
await node.mempool.addTX(aliceTX.toTX());
await bob.send({
outputs: [{
address: bobAddr,
value: 20000
}]
});
}
before(async () => {
await node.open();
bob = await wdb.create();
bobAddr = await bob.receiveAddress();
});
after(async () => {
await node.close();
});
it('should fund wallets', async () => {
const blocks = 10;
await mineBlocks(blocks, aliceAddr);
await mineBlocks(blocks, bobAddr);
const bobBal = await bob.getBalance();
assert.strictEqual(bobBal.confirmed, blocks * 2000 * 1e6);
assert.strictEqual(alice.balance, blocks * 2000 * 1e6);
});
it('should run auction', async () => {
// Alice opens
await sendTXs();
const aliceOpen = await alice.createOpen(NAME);
await node.mempool.addTX(aliceOpen.toTX());
const openBlocks = await mineBlocks(1);
// Coinbase plus open
assert.strictEqual(openBlocks[0].txs.length, 4);
// Advance to bidding phase
await mineBlocks(treeInterval);
await forValue(alice, 'height', node.chain.height);
// Poor Bob, all he does is send one (losing) bid but his wallet will
// watch all the other activity including TRANSFERS for this name
await bob.sendBid(NAME, 10000, 10000);
// Alice sends winning bid
await sendTXs();
const aliceBid = await alice.createBid(NAME, 20000, 20000);
await node.mempool.addTX(aliceBid.toTX());
const bidBlocks = await mineBlocks(1);
assert.strictEqual(bidBlocks[0].txs.length, 5);
bidBlockHash = bidBlocks[0].hash();
// Advance to reveal phase
await mineBlocks(biddingPeriod);
await bob.sendReveal(NAME);
await sendTXs();
const aliceReveal = await alice.createReveal(NAME);
await node.mempool.addTX(aliceReveal.toTX());
const revealBlocks = await mineBlocks(1);
assert.strictEqual(revealBlocks[0].txs.length, 5);
// Close auction
await mineBlocks(revealPeriod);
// Alice registers
await sendTXs();
const aliceRegister = await alice.createRegister(
NAME,
Resource.fromJSON({records:[]})
);
await node.mempool.addTX(aliceRegister.toTX());
const registerBlocks = await mineBlocks(1);
assert.strictEqual(registerBlocks[0].txs.length, 4);
});
it('should get namestate', async () => {
const ns = await bob.getNameStateByName(NAME);
// Bob has the namestate
assert(ns);
// Bob is not the name owner
const {hash, index} = ns.owner;
const coin = await bob.getCoin(hash, index);
assert.strictEqual(coin, null);
// Name is not in mid-TRANSFER
assert.strictEqual(ns.transfer, 0);
});
it('should process TRANSFER', async () => {
// Alice transfers the name to her own address
await sendTXs();
const aliceTransfer = await alice.createTransfer(NAME, aliceAddr);
await node.mempool.addTX(aliceTransfer.toTX());
const transferBlocks = await mineBlocks(1);
assert.strictEqual(transferBlocks[0].txs.length, 4);
// Bob detects the TRANSFER even though it doesn't involve him at all
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.transfer, node.chain.height);
// Bob's wallet has indexed the TRANSFER
const bobTransfer = await bob.getTX(aliceTransfer.hash());
assert.strictEqual(bobTransfer, null);
});
it('should fully rescan', async () => {
await wdb.rescan(0);
await forValue(wdb, 'height', node.chain.height);
// No change
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.transfer, node.chain.height);
});
it('should rescan since, but not including, the BIDs', async () => {
const bidBlock = await node.chain.getEntry(bidBlockHash);
await wdb.rescan(bidBlock.height);
await forValue(wdb, 'height', node.chain.height);
// No change
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.transfer, node.chain.height);
});
it('should process FINALIZE', async () => {
await mineBlocks(transferLockup);
// Alice finalizes the name
await sendTXs();
const aliceFinalize = await alice.createFinalize(NAME);
await node.mempool.addTX(aliceFinalize.toTX());
const finalizeBlocks = await mineBlocks(1);
assert.strictEqual(finalizeBlocks[0].txs.length, 4);
aliceFinalizeHash = aliceFinalize.hash();
// Bob detects the FINALIZE even though it doesn't involve him at all
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.bufferEqual(ns.owner.hash, aliceFinalizeHash);
// Bob's wallet has not indexed the FINALIZE
const bobFinalize = await bob.getTX(aliceFinalize.hash());
assert.strictEqual(bobFinalize, null);
});
it('should fully rescan', async () => {
await wdb.rescan(0);
await forValue(wdb, 'height', node.chain.height);
// No change
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.bufferEqual(ns.owner.hash, aliceFinalizeHash);
});
it('should rescan since, but not including, the BIDs', async () => {
const bidBlock = await node.chain.getEntry(bidBlockHash);
await wdb.rescan(bidBlock.height);
await forValue(wdb, 'height', node.chain.height);
// No change
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.bufferEqual(ns.owner.hash, aliceFinalizeHash);
});
it('should process TRANSFER (again)', async () => {
// Alice transfers the name to her own address
await sendTXs();
const aliceTransfer = await alice.createTransfer(NAME, aliceAddr);
await node.mempool.addTX(aliceTransfer.toTX());
const transferBlocks = await mineBlocks(1);
assert.strictEqual(transferBlocks[0].txs.length, 4);
// Bob detects the TRANSFER even though it doesn't involve him at all
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.transfer, node.chain.height);
// Bob's wallet has not indexed the TRANSFER
const bobTransfer = await bob.getTX(aliceTransfer.hash());
assert.strictEqual(bobTransfer, null);
});
it('should process REVOKE', async () => {
// Alice revokes the name
await sendTXs();
const aliceRevoke = await alice.createRevoke(NAME);
await node.mempool.addTX(aliceRevoke.toTX());
const revokeBlocks = await mineBlocks(1);
assert.strictEqual(revokeBlocks[0].txs.length, 4);
// Bob detects the REVOKE even though it doesn't involve him at all
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.revoked, node.chain.height);
// Bob's wallet has not indexed the REVOKE
const bobTransfer = await bob.getTX(aliceRevoke.hash());
assert.strictEqual(bobTransfer, null);
});
it('should fully rescan', async () => {
await wdb.rescan(0);
await forValue(wdb, 'height', node.chain.height);
// No change
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.revoked, node.chain.height);
});
it('should rescan since, but not including, the BIDs', async () => {
const bidBlock = await node.chain.getEntry(bidBlockHash);
await wdb.rescan(bidBlock.height);
await forValue(wdb, 'height', node.chain.height);
// No change
const ns = await bob.getNameStateByName(NAME);
assert(ns);
assert.strictEqual(ns.revoked, node.chain.height);
});
});
});