-
Notifications
You must be signed in to change notification settings - Fork 8
/
tests.js
808 lines (750 loc) · 26.3 KB
/
tests.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/* global describe it */
/* eslint import/no-extraneous-dependencies: 0 */
/* eslint no-console: 0 */
/* eslint no-unused-expressions: 0 */
const deasync = require('deasync');
const chai = require('chai');
const async = require('async');
const { Node, BitcoinNet, Transaction, BitcoinGraph } = require('bitcointest');
const API = require('./api');
const bitcoin = require('./bitcoin');
const db = require('./db');
const scanner = require('./scanner');
const payment = require('./payment');
const expect = chai.expect;
const INVOICE_CONTENT = 'Test content';
let net;
let graph;
let nodes;
let grp1;
let grp2;
let n1;
let n2;
let n3;
let n4;
let runtask1 = true;
let runtask2 = true;
let runtask3 = true;
let runall = true;
if (process.env.TASK) {
runall = runtask1 = runtask2 = runtask3 = false;
const t = process.env.TASK;
if (''+t === '1') runtask1 = true;
else if (''+t === '2') runtask2 = true;
else if (''+t === '3') runtask3 = true;
else {
console.log(`unknown task id ${t}; expected 1, 2, or 3; falling back to regular execution`);
runall = runtask1 = runtask2 = runtask3 = true;
}
}
const verbose = process.env.V === '1';
const log = verbose ? (...args) => console.log(...args) : (...args) => {};
const bcgraph = verbose ? (g1, g2) => graph.printBlockChainsS(g1, g2) : (g1, g2) => {};
BitcoinNet.setVerbose(verbose);
const waitS = deasync((time, cb) => setTimeout(cb, time));
const finalize = (err, done) => {
if (err) {
console.log(`err=${JSON.stringify(err)}`);
graph.printConnectionMatrix(net.nodes);
graph.printBlockChainsS(grp1, grp2);
expect(err).to.be.null;
done();
} else {
expect(err).to.be.null;
done();
}
};
const historyString = (history) => {
let s = '\n';
for (const h of history) {
s += `\tinvoice=${h.invoice}; payment=${h.payment}; what(action=${h.what.action}): ${h.content}\n`;
}
return s;
};
const actionExistsInHistory = (history, action) => {
for (const h of history) {
if (h.what && h.what.action === action) return true;
}
return false;
};
const orderedActionsInHistory = (history, actions, historyStart = 0) => {
let actionIter = 0;
// console.log(`checking ${historyString(history)} for ordered actions
// ${JSON.stringify(actions)}`);
for (let i = historyStart; i < history.length; i++) {
let h = history[i];
const a = actions[actionIter];
if (Array.isArray(a)) {
// any order resolution
// console.log(`any order: ${JSON.stringify(a)}`);
while (a.length > 0) {
// console.log(`- i=${i}: alternatives=${JSON.stringify(a)}`);
let found = false;
let aindex = -1;
for (const aa of a) {
aindex++;
// console.log(`- checking ${h.what ? h.what.action : null} vs ${aa}`);
if (h.what && h.what.action == aa) {
found = true;
a.splice(aindex, 1);
// console.log(`- poppped ${aa} -> ${JSON.stringify(a)}`);
if (a.length === 0) {
actionIter++;
if (actionIter === actions.length) return true;
}
break;
}
}
// console.log(`- looped; a.length = ${a.length}`);
if (a.length === 0) break;
i++;
if (i >= history.length) return false;
h = history[i];
}
// console.log(`- continuing`);
continue;
}
// console.log(`got ${JSON.stringify(h.what)}; expecting action ${a}`);
if (h.what && h.what.action === a) {
actionIter++;
// console.log(`actionIter++ = ${actionIter}; aiming for ${actions.length}`);
if (actionIter === actions.length) return true;
}
}
// console.log(`exhausted history with actionIter = ${actionIter}; FAILURE`);
return false;
};
describe('db', () => {
it('can be connected to', (done) => {
db.connect((err) => {
expect(err).to.be.null;
done();
});
});
})
describe('bitcoind', () => {
it('can be found', (done) => {
const which = require('which');
which('bitcoind', (whichErr, whichPath) => {
which('Bitcoin-Qt', (whichErrQT, whichPathQT) => {
const bitcoinPath =
process.env.BITCOIN_PATH ||
(!whichErr && whichPath
? whichPath.substr(0, whichPath.length - 8)
: (!whichErrQT && whichPathQT
? whichPathQT.substr(0, whichPathQT.length - 10)
: '../bitcoin/src/'));
net = new BitcoinNet(bitcoinPath, '/tmp/bitcointest/', 22001, 22002);
graph = new BitcoinGraph(net);
// TODO: below checks absolutely nothing
expect(net).to.not.be.null;
done();
});
});
});
});
let bitcointestOK = false;
describe('bitcointest', () => {
it('configures', function(done) {
this.timeout(50000);
nodes = net.launchBatchS(4);
expect(nodes.length).to.equal(4);
[ n1, n2, n3, n4 ] = nodes;
net.waitForNodesS(nodes, 50000);
net.connectNodesS(nodes);
bitcointestOK = true;
done();
});
it('works', function(done) {
if (!bitcointestOK) {
console.log('\n\n*** ERROR : bitcoind cannot be started!\n*** ERROR : please do:\n killall -9 bitcoind\n*** ERROR : and try again!\n\n'.red);
process.exit(1);
}
done();
});
});
//==============================================================================
//==============================================================================
//==============================================================================
// BLOCKS: none
// LAYOUT: all nodes connected
describe('utxo generation', () => {
it('obtains spendable outputs', function(done) {
// switch to one of our nodes, rather than the default one
bitcoin.switchNode(nodes[0]);
this.timeout(10000);
nodes[0].generateBlocksS(1);
const blocks = nodes[1].generateBlocksS(110);
net.syncS(net.nodes);
API.waitForChangeS(blocks[109], 10000);
const balance = nodes[1].getBalanceS();
expect(balance).to.be.above(1);
done();
});
});
if (runtask1 || runall)
describe('invoice', function() {
it('can be created', function(done) {
this.timeout(20000);
const result = API.createS(1.0, INVOICE_CONTENT);
expect(result).to.exist;
expect(result.insertedIds).to.exist;
expect(result.insertedIds.length).to.equal(1);
const [ invoiceid ] = result.insertedIds;
expect(invoiceid).to.exist;
const { invoice, state } = API.infoS(invoiceid);
expect(invoice).to.exist;
expect(state).to.exist;
expect(invoice.addr).to.exist;
expect(typeof(invoice.addr)).to.equal('string');
const addrinfo = bitcoin.validateAddressS(invoice.addr).result;
expect(addrinfo).to.exist;
expect(addrinfo.isvalid);
expect(addrinfo.ismine);
expect(addrinfo.pubkey).to.exist;
done();
});
});
if (runtask2 || runall)
describe('payment', function() {
let invoiceid;
let txid;
// confirmations < 6のpaymentはpendingである
it('updates status to pending', function(done) {
this.timeout(20000);
net.partitionS(nodes, 2);
const result = API.createS(1, INVOICE_CONTENT);
invoiceid = result.insertedIds[0];
const startingInvoice = API.infoS(invoiceid).invoice;
txid = n2.sendToAddressS(startingInvoice.addr, 1);
const blocks = n2.generateBlocksS(1);
API.waitForChangeS(blocks[0], 15000);
const { invoice, state } = API.infoS(invoiceid);
expect(state.payments.length).to.be.equal(1);
const [ payment ] = state.payments;
expect(payment.status).to.equal('pending');
done();
});
// confirmations >= 6のpaymentはconfirmedである
it('updates status to confirmed', function(done) {
this.timeout(20000);
const blocks = n2.generateBlocksS(5);
API.waitForChangeS(blocks[4], 15000);
const { invoice, state } = API.infoS(invoiceid);
expect(state.payments.length).to.be.equal(1);
const [ payment ] = state.payments;
expect(payment.status).to.equal('confirmed');
done();
});
let inState;
// Payment that is dropped from the Block is changed into Reorg status
it('updates status to reorg', function(done) {
this.timeout(20000);
const blocks = n3.generateBlocksS(7);
net.mergeS(nodes);
API.waitForChangeS(blocks[6], 15000);
const { invoice, state } = API.infoS(invoiceid);
inState = state;
expect(state.payments.length).to.be.equal(1);
const [ payment ] = state.payments;
expect(payment.status).to.equal('reorg');
const history = API.historyS(invoiceid);
lastHistory = history;
found = orderedActionsInHistory(history, ['create', 'receive', 'reorg']);
expect(found).to.be.true;
done();
});
it('sets disabledAmount for reorg', function(done) {
// amounts are slightly off sometimes (e.g. by 1 satoshi)
expect(inState.disabledAmount).to.be.above( 90000000);
expect(inState.disabledAmount).to.be.below(110000000);
done();
});
// When returning to the blockchain, becomes pending
it('restores status to pending', function(done) {
this.timeout(20000);
const blocks = n2.generateBlocksS(1);
API.waitForChangeS(blocks[1], 15000);
const { invoice, state } = API.infoS(invoiceid);
expect(state.payments.length).to.be.equal(1);
const [ payment ] = state.payments;
expect(payment.status).to.equal('pending');
done();
});
// When confirmations >= 6, become confirmed again
it('restores status to confirmed', function(done) {
this.timeout(20000);
const blocks = n2.generateBlocksS(5);
API.waitForChangeS(blocks[5], 15000);
const { invoice, state } = API.infoS(invoiceid);
expect(state.payments.length).to.be.equal(1);
const [ payment ] = state.payments;
expect(payment.status).to.equal('confirmed');
done();
});
});
if (runtask3 || runall) {
describe('invoice status', function() {
const make_and_pay = (self, invamount, payments, blockgen = 1) => {
self.timeout(20000);
const result = API.createS(invamount, INVOICE_CONTENT);
const [ invoiceid ] = result.insertedIds;
invoice = API.infoS(invoiceid).invoice;
for (const p of payments)
n2.sendToAddressS(invoice.addr, p);
const blocks = n2.generateBlocksS(blockgen);
API.waitForChangeS(blocks[blockgen-1], 15000);
return API.infoS(invoiceid).invoice;
};
let invoice;
it('is unpaid', function(done) {
invoice = make_and_pay(this, 1, [], 6);
expect(invoice.status).to.be.equal('unpaid');
done();
});
it('becomes paid', function(done) {
invoice = make_and_pay(this, 1, [1], 6);
expect(invoice.status).to.be.equal('paid');
done();
});
it('becomes overpaid', function(done) {
invoice = make_and_pay(this, 1, [1.1], 6);
expect(invoice.status).to.be.equal('overpaid');
done();
});
it('becomes pending_overpaid', function(done) {
invoice = make_and_pay(this, 1, [1.1]);
expect(invoice.status).to.be.equal('pending_overpaid');
done();
});
it('becomes pending_paid', function(done) {
invoice = make_and_pay(this, 1, [1]);
expect(invoice.status).to.be.equal('pending_paid');
done();
});
it('becomes partial', function(done) {
invoice = make_and_pay(this, 1, [0.5], 6);
expect(invoice.status).to.be.equal('partial');
done();
});
it('becomes pending_partial', function(done) {
invoice = make_and_pay(this, 1, [0.5]);
expect(invoice.status).to.be.equal('pending_partial');
done();
});
});
let invoice1;
describe('reorg unconfirm then reconfirm into another block:', function() {
it('partitions', (done) => {
const nodeGroups = net.partitionS(nodes, 2);
expect(nodeGroups.length).to.equal(2);
[ grp1, grp2 ] = nodeGroups;
done();
});
it('can create an invoice', function(done) {
const result = API.createS(1, INVOICE_CONTENT);
expect(result).to.not.be.null;
expect(result.insertedIds).to.not.be.null;
expect(result.insertedIds.length).to.equal(1);
[ invoice1 ] = result.insertedIds;
const history = API.historyS(invoice1);
expect(actionExistsInHistory(history, 'create')).to.be.true;
done();
});
it('can pay an invoice', function(done) {
this.timeout(20000);
const { invoice, state } = API.infoS(invoice1);
expect(invoice).to.exist;
expect(state).to.exist;
expect(invoice.addr).to.exist;
log(`[PAY ${invoice.addr} from node 2]`);
const txid = n2.sendToAddressS(invoice.addr, 1);
expect(txid).to.exist;
expect(Array.isArray(txid)).to.be.false;
expect(n1.isConnected(n2, true)).to.be.true;
const result = n1.waitForTransactionS(txid, 15000);
if (!result) {
finalize('result is false', () => done());
} else {
expect(result).to.not.be.false;
done();
}
});
it('populates history with observed transaction', function(done) {
this.timeout(15000);
log('[GENERATE blocks on node 1]');
const blocks = n1.generateBlocksS(6);
expect(blocks.length).to.equal(6);
log('block chain after generation (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
net.syncS([n1, n2]);
API.waitForChangeS(blocks[5], 10000);
// HISTORY: invoice1, <some payment>, { action: 'receive' }
const history = API.historyS(invoice1);
log(`history = ${historyString(history)}`);
expect(orderedActionsInHistory(history, ['create', 'receive'])).to.be.true;
API.waitForStatusS(invoice1, 'paid');
n3.generateBlocksS(6);
waitS(1000);
log('block chain after n3.generateBlocks (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
done();
});
it('puts payment as unconfirmed after reorg', function(done) {
this.timeout(12000);
log('[Reconnect partitions]');
const nodes = net.mergeS(net.nodes);
log('[GENERATE block on node 3 to trigger reorg. Payment should now be unconfirmed.]');
const blocks = n3.generateBlocksS(1);
net.syncS(net.nodes);
API.waitForChangeS(blocks[0], 6000);
// HISTORY: invoice1, <same payment>, { action: 'adjust' }
log('block chain after merge + n3.generateBlocks (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
const { invoice, state } = API.infoS(invoice1);
// the invoice should be unpaid again
log(`this should be unpaid again:\ninvoice = ${JSON.stringify(invoice)}\nstate = ${JSON.stringify(state)}`);
const { payments } = state;
expect(payments.length).to.equal(1);
expect(invoice.status).to.equal('unpaid');
const history = API.historyS(invoice1);
if (!orderedActionsInHistory(history, ['create', 'receive', 'reorg'])) {
console.log(`history failure (create, receive, reorg): ${historyString(history)}`);
}
expect(orderedActionsInHistory(history, ['create', 'receive', 'reorg'])).to.be.true;
done();
});
it('detects reconfirm', function(done) {
this.timeout(10000);
log('[GENERATE block on node 1 to reconfirm transaction.]');
const blocks = n1.generateBlocksS(6);
net.syncS(net.nodes);
API.waitForChangeS(blocks[5], 2000);
const { invoice, state } = API.infoS(invoice1);
// the invoice should have seen a reorg, but it should now be properly paid
const { payments } = state;
expect(payments.length).to.equal(1);
expect(invoice.status).to.equal('paid');
done();
});
});
let invoice2;
describe('double spend (replace payment to other address)', () => {
it('partitions', (done) => {
const nodeGroups = net.partitionS(nodes, 2);
expect(nodeGroups.length).to.equal(2);
[ grp1, grp2 ] = nodeGroups;
[ n1, n2 ] = grp1;
[ n3, n4 ] = grp2;
log('block chain after partitioning (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
done();
});
it('submits invoice', (done) => {
const result = API.createS(1, INVOICE_CONTENT);
expect(result).to.not.be.null;
expect(result.insertedIds).to.not.be.null;
expect(result.insertedIds.length).to.equal(1);
[ invoice2 ] = result.insertedIds;
waitS(250);
const history = API.historyS(invoice2);
log(`expect 'create' in history = ${historyString(history)}`);
expect(actionExistsInHistory(history, 'create')).to.be.true;
done();
});
it('pays invoice', function(done) {
this.timeout(30000);
const utxo = n2.findSpendableOutputS(1);
expect(utxo).to.exist;
expect(utxo.amount).to.be.above(0.99);
const { invoice, state } = API.infoS(invoice2);
expect(invoice).to.exist;
expect(state).to.exist;
expect(invoice.addr).to.exist;
log(`[PAY ${invoice.addr} from node 2]`);
const txid = n2.spendUTXOS(utxo, invoice.addr, 1);
expect(txid).to.exist;
log(`- payment txid = ${txid}`);
expect(n1.isConnected(n2, true)).to.be.true;
const result = n1.waitForTransactionS(txid, 20000);
if (!result) {
finalize('!result', () => {
expect(result).to.not.be.false;
n2.shareAddressWithNodeS(n3, utxo.address, true);
});
} else {
expect(result).to.not.be.false;
n2.shareAddressWithNodeS(n3, utxo.address, true);
}
const selfAddr = n3.getNewAddressS();
log('[Double Spend from node 3]');
const txid2 = n3.spendUTXOS(utxo, selfAddr, 1);
log(`- double spend txid = ${txid2}`);
done();
});
// at this point, we have two partitions (n1-n2, n3-n4), where the former
// group thinks the transaction has gone to the invoice address, and the
// latter group thinks it's been sent to n3's own address
// i.e. a double spend to two separate addresses
// NOTE: the rpc-cli API is hooked up to n1, so it thinks the invoice has been
// paid at this point
it('populates history with observed transaction', function(done) {
this.timeout(20000);
log('[GENERATE six blocks on node 1]');
const blocks = n1.generateBlocksS(6);
net.syncS([n1, n2]);
API.waitForChangeS(blocks[5], 10000);
log('block chain after gen (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
// HISTORY: invoice2, <some payment>, { action: 'receive' }
const history = API.historyS(invoice2);
expect(orderedActionsInHistory(history, ['create', 'receive'])).to.be.true;
API.waitForStatusS(invoice2, 'paid');
done();
});
it('detects double spend', function(done) {
this.timeout(30000);
log('[GENERATE six blocks on node 4]');
const blocks = n4.generateBlocksS(6);
net.syncS([n3, n4]);
log('block chain after partitioned gen (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
log('[Reconnect partitions]');
net.mergeS(net.nodes);
log('[GENERATE block on node 4 to trigger reorg]');
n4.generateBlocksS(1);
net.syncS(net.nodes);
API.waitForChangeS(blocks[0], 10000);
log('block chain after n4 gen (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
let found = false;
let lastHistory = null;
const expiry = new Date().getTime() + 3000;
while (!found && expiry > new Date().getTime()) {
const history = API.historyS(invoice2);
lastHistory = history;
found = orderedActionsInHistory(history, ['create', 'receive', 'reorg']);
if (!found) {
try {
API.waitForChangeS(invoice2, 'reorg');
} catch (e) {
// ignore errors; probably timeout anyway
}
}
}
expect(found).to.be.true;
log(`expect create, receive, reorg in ${historyString(lastHistory)}`);
expect(orderedActionsInHistory(lastHistory, ['create', 'receive', 'reorg'])).to.be.true;
// also expect history to not be bloated
expect(lastHistory.length < 6); // create, receive, adjust, reorg, with room for a few others if they ever occur
log('block chain after n1.generateBlocks (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
done();
});
});
let invoice3;
describe('double spend replace (payment to same address)', () => {
it('partitions', (done) => {
const nodeGroups = net.partitionS(nodes, 2);
expect(nodeGroups.length).to.equal(2);
[ grp1, grp2 ] = nodeGroups;
[ n1, n2 ] = grp1;
[ n3, n4 ] = grp2;
done();
});
it('submits invoice', (done) => {
const result = API.createS(1, INVOICE_CONTENT);
expect(result).to.not.be.null;
expect(result.insertedIds).to.not.be.null;
expect(result.insertedIds.length).to.equal(1);
[ invoice3 ] = result.insertedIds;
waitS(250);
const history = API.historyS(invoice3);
log(`expect 'create' in history = ${historyString(history)}`);
expect(actionExistsInHistory(history, 'create')).to.be.true;
done();
});
let firsttx;
let secondtx;
it('pays invoice', function(done) {
this.timeout(40000);
const utxo = n2.findSpendableOutputS(1);
expect(utxo).to.exist;
expect(utxo.amount).to.be.above(0.99);
const { invoice, state } = API.infoS(invoice3);
expect(invoice).to.exist;
expect(state).to.exist;
expect(invoice.addr).to.exist;
log(`[PAY ${invoice.addr} from node 2]`);
const txid = firsttx = n2.spendUTXOS(utxo, invoice.addr, 1);
expect(txid).to.exist;
log(`- payment txid = ${txid}`);
expect(n1.isConnected(n2, true)).to.be.true;
n2.generateBlocksS(1);
const result = n1.waitForTransactionS(txid, 5000);
if (!result) {
finalize('!result', () => {
expect(result).to.not.be.false;
});
} else {
expect(result).to.not.be.false;
}
n2.shareAddressWithNodeS(n3, utxo.address, true);
log('[Double Spend from node 3]');
const txid2 = secondtx = n3.spendUTXOS(utxo, invoice.addr, 1);
log(`- double spend txid = ${txid2}`);
expect(n3.isConnected(n4, true)).to.be.true;
const result2 = n4.waitForTransactionS(txid2, 15000);
log(`- n4.waitForTX(${txid2}) = ${result2}`);
if (!result2) {
finalize('!result', () => {
expect(result2).to.not.be.false;
});
} else {
expect(result2).to.not.be.false;
}
done();
});
it('populates history with observed transaction', function(done) {
this.timeout(20000);
log('[GENERATE six blocks on node 1]');
const blocks = n1.generateBlocksS(5);
net.syncS([n1, n2]);
API.waitForChangeS(blocks[4], 10000);
log('block chain after gen (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
// HISTORY: invoice3, <some payment>, { action: 'receive' }
const history = API.historyS(invoice3);
expect(orderedActionsInHistory(history, ['create', 'receive'])).to.be.true;
API.waitForStatusS(invoice3, 'paid');
done();
});
it('detects double spend', function(done) {
this.timeout(30000);
log('[GENERATE six blocks on node 4]');
const blocks = n4.generateBlocksS(6);
net.syncS([n3, n4]);
log('block chain after partitioned gen (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
log('[Reconnect partitions]');
net.mergeS(net.nodes);
log('[GENERATE block on node 4 to trigger reorg]');
n4.generateBlocksS(1);
// try {
// const stxi = n4.getTransactionS(secondtx, true);
// log(`stxi=${JSON.stringify(stxi)}`);
// } catch (e) {
// log('*** n4 does NOOOOOOT know about tx2!!!!');
// throw e;
// }
net.syncS(net.nodes);
log(`Waiting for second tx ${secondtx}`)
const result = n1.waitForTransactionS(secondtx, 15000);
expect(result).to.not.be.false;
API.waitForChangeS(blocks[5], 10000);
// console.log(`first transaction ${firsttx} state:`);
// try {
// console.log(JSON.stringify(bitcoin.getTransactionS(firsttx)));
// } catch(e) {
// console.log('[unavailable]');
// }
log('block chain after n4 gen (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
let found = false;
let lastHistory = null;
const expiry = new Date().getTime() + 3000;
while (!found && expiry > new Date().getTime()) {
const history = API.historyS(invoice3);
lastHistory = history;
found = orderedActionsInHistory(history, ['create', 'receive', ['receive', 'reorg']]);
if (!found) {
try {
API.waitForChangeS(invoice3, 'receive');
} catch (e) {
// ignore errors; probably timeout anyway
}
}
}
if (!found) {
console.log(`not found; history = ${historyString(lastHistory)}`);
const { invoice, state } = API.infoS(invoice3);
for (const payment of state.payments) {
console.log(`- payment ${payment._id} [${payment.txid}]: ${payment.amount} (${payment.status})`);
}
console.log(`first transaction ${firsttx} state:`);
try {
console.log(JSON.stringify(bitcoin.getTransactionS(firsttx)));
} catch(e) {
console.log('[unavailable]');
}
console.log(`second transaction ${secondtx} state:`);
try {
console.log(JSON.stringify(bitcoin.getTransactionS(secondtx)));
} catch(e) {
console.log('[unavailable]');
}
}
expect(found).to.be.true;
log(`expect create, receive, reorg, receive in ${historyString(lastHistory)}`);
expect(orderedActionsInHistory(lastHistory, ['create', 'receive', ['receive', 'reorg']])).to.be.true;
// also expect history to not be bloated
expect(lastHistory.length < 7); // create, receive, adjust, reorg, receive, with room for a few others if they ever occur
log('block chain after n1.generateBlocks (n1,n2 n3,n4):');
bcgraph(grp1, grp2);
// finally expect invoice to be in 'paid' state
const { invoice, state } = API.infoS(invoice3);
expect(invoice.status).to.be.equal('paid');
done();
});
});
} // if (runtask3 || runall)
//==============================================================================
//==============================================================================
//==============================================================================
describe('cleanup', () => {
let invoices = null;
it('finds relevant invoices', (done) => {
db.find('invoice', { content: INVOICE_CONTENT }, false, (err, invoiceList) => {
expect(err).to.be.null;
invoices = invoiceList;
done();
});
});
it('clears relevant payments', (done) => {
async.eachSeries(
invoices,
(invoice, asyncCallback) => db.remove('payment', { invoice: '' + invoice._id }, (err) => {
expect(err).to.be.null;
asyncCallback();
}),
done
);
});
it('clears relevant history', (done) => {
async.eachSeries(
invoices,
(invoice, asyncCallback) => db.remove('history', { invoice: '' + invoice._id }, (err) => {
expect(err).to.be.null;
asyncCallback();
}),
done
);
});
it('clears relevant invoices', (done) => {
db.remove('invoice', { content: INVOICE_CONTENT }, (err) => {
expect(err).to.be.null;
done();
});
});
});
describe('bctest end', () => {
it('shuts down', function(done) {
this.timeout(6000);
net.shutdown(() => {
for (const n of net.nodes) {
expect(n.running).to.be.false;
}
done();
});
});
});