-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathmempool-test.js
1765 lines (1434 loc) · 51.6 KB
/
mempool-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
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const assert = require('bsert');
const random = require('bcrypto/lib/random');
const Network = require('../lib/protocol/network');
const MempoolEntry = require('../lib/mempool/mempoolentry');
const Mempool = require('../lib/mempool/mempool');
const WorkerPool = require('../lib/workers/workerpool');
const Chain = require('../lib/blockchain/chain');
const BlockStore = require('../lib/blockstore/level');
const ChainEntry = require('../lib/blockchain/chainentry');
const MTX = require('../lib/primitives/mtx');
const Claim = require('../lib/primitives/claim');
const Coin = require('../lib/primitives/coin');
const KeyRing = require('../lib/primitives/keyring');
const Address = require('../lib/primitives/address');
const Outpoint = require('../lib/primitives/outpoint');
const Covenant = require('../lib/primitives/covenant');
const Input = require('../lib/primitives/input');
const Output = require('../lib/primitives/output');
const Block = require('../lib/primitives/block');
const Script = require('../lib/script/script');
const Witness = require('../lib/script/witness');
const CoinView = require('../lib/coins/coinview');
const util = require('../lib/utils/util');
const consensus = require('../lib/protocol/consensus');
const policy = require('../lib/protocol/policy');
const MemWallet = require('./util/memwallet');
const ALL = Script.hashType.ALL;
const common = require('../lib/blockchain/common');
const VERIFY_BODY = common.flags.VERIFY_BODY;
const rules = require('../lib/covenants/rules');
const NameState = require('../lib/covenants/namestate');
const {states} = NameState;
const {ownership} = require('../lib/covenants/ownership');
const {CachedStubResolver, STUB_SERVERS} = require('./util/stub');
const ONE_HASH = Buffer.alloc(32, 0x00);
ONE_HASH[0] = 0x01;
const network = Network.get('regtest');
async function getMockBlock(chain, txs = [], cb = true) {
if (cb) {
const raddr = KeyRing.generate().getAddress();
const mtx = new MTX();
mtx.addInput(new Input());
mtx.addOutput(raddr, 0);
mtx.locktime = chain.height + 1;
txs = [mtx.toTX(), ...txs];
}
const view = new CoinView();
for (const tx of txs) {
view.addTX(tx, -1);
}
const now = util.now();
const time = chain.tip.time <= now ? chain.tip.time + 1 : now;
const block = new Block();
block.txs = txs;
block.prevBlock = chain.tip.hash;
block.time = time;
block.bits = await chain.getTarget(block.time, chain.tip);
// Ensure mockblocks are unique (required for reorg testing)
block.merkleRoot = block.createMerkleRoot();
block.witnessRoot = block.createWitnessRoot();
block.treeRoot = chain.db.treeRoot();
return [block, view];
}
describe('Mempool', function() {
this.timeout(5000);
const originalResolver = ownership.Resolver;
const originalServers = ownership.servers;
before(() => {
ownership.Resolver = CachedStubResolver;
ownership.servers = STUB_SERVERS;
});
after(() => {
ownership.Resolver = originalResolver;
ownership.servers = originalServers;
});
describe('Mempool TXs', function() {
let workers, blocks, chain, mempool, wallet;
let cachedTX;
const dummyInput = (addr, hash, value = 70000) => {
const coin = new Coin();
coin.height = 0;
coin.value = 0;
coin.address = addr;
coin.hash = hash;
coin.index = 0;
const fund = new MTX();
fund.addCoin(coin);
fund.addOutput(addr, value);
const [tx, view] = fund.commit();
const entry = MempoolEntry.fromTX(tx, view, 0);
mempool.trackEntry(entry, view);
return Coin.fromTX(fund, 0, -1);
};
before(async () => {
workers = new WorkerPool({
enabled: true,
size: 2
});
blocks = new BlockStore({
memory: true,
network
});
chain = new Chain({
memory: true,
network,
blocks,
workers
});
mempool = new Mempool({
chain,
memory: true,
workers
});
wallet = new MemWallet({ network });
await workers.open();
await blocks.open();
await chain.open();
await mempool.open();
});
after(async () => {
await mempool.close();
await chain.close();
await blocks.close();
await workers.close();
});
it('should handle incoming orphans and TXs', async () => {
const key = KeyRing.generate();
const addr = key.getAddress();
const t1 = new MTX();
t1.addOutput(wallet.getAddress(), 50000);
t1.addOutput(wallet.getAddress(), 10000);
const script = Script.fromPubkeyhash(key.getHash());
t1.addCoin(dummyInput(addr, ONE_HASH));
const sig = t1.signature(0, script, 70000, key.privateKey, ALL);
t1.inputs[0].witness = Witness.fromItems([sig, key.publicKey]);
// balance: 51000
wallet.sign(t1);
const t2 = new MTX();
t2.addTX(t1, 0); // 50000
t2.addOutput(wallet.getAddress(), 20000);
t2.addOutput(wallet.getAddress(), 20000);
// balance: 49000
wallet.sign(t2);
const t3 = new MTX();
t3.addTX(t1, 1); // 10000
t3.addTX(t2, 0); // 20000
t3.addOutput(wallet.getAddress(), 23000);
// balance: 47000
wallet.sign(t3);
const t4 = new MTX();
t4.addTX(t2, 1); // 24000
t4.addTX(t3, 0); // 23000
t4.addOutput(wallet.getAddress(), 11000);
t4.addOutput(wallet.getAddress(), 11000);
// balance: 22000
wallet.sign(t4);
const f1 = new MTX();
f1.addTX(t4, 1); // 11000
f1.addOutput(new Address(), 9000);
// balance: 11000
wallet.sign(f1);
const fake = new MTX();
fake.addTX(t1, 1); // 1000 (already redeemed)
fake.addOutput(wallet.getAddress(), 6000); // 6000 instead of 500
// Script inputs but do not sign
wallet.template(fake);
// Fake signature
const input = fake.inputs[0];
input.witness.setData(0, Buffer.alloc(65, 0x00));
input.witness.compile();
// balance: 11000
{
await mempool.addTX(fake.toTX());
await mempool.addTX(t4.toTX());
const balance = mempool.getBalance();
assert.strictEqual(balance, 70000);
}
{
await mempool.addTX(t1.toTX());
const balance = mempool.getBalance();
assert.strictEqual(balance, 60000);
}
{
await mempool.addTX(t2.toTX());
const balance = mempool.getBalance();
assert.strictEqual(balance, 50000);
}
{
await mempool.addTX(t3.toTX());
const balance = mempool.getBalance();
assert.strictEqual(balance, 22000);
}
{
await mempool.addTX(f1.toTX());
const balance = mempool.getBalance();
assert.strictEqual(balance, 20000);
}
const txs = mempool.getHistory();
assert(txs.some((tx) => {
return tx.hash().equals(f1.hash());
}));
});
it('should get spent coins and reflect in coinview', async () => {
const wallet = new MemWallet({ network });
const addr = wallet.getAddress();
const dummyCoin = dummyInput(addr, random.randomBytes(32));
const mtx1 = new MTX();
mtx1.addOutput(wallet.getAddress(), 50000);
mtx1.addCoin(dummyCoin);
wallet.sign(mtx1);
const tx1 = mtx1.toTX();
const coin1 = Coin.fromTX(tx1, 0, -1);
const mtx2 = new MTX();
mtx2.addOutput(wallet.getAddress(), 10000);
mtx2.addOutput(wallet.getAddress(), 30000); // 10k fee
mtx2.addCoin(coin1);
wallet.sign(mtx2);
const tx2 = mtx2.toTX();
await mempool.addTX(tx1);
{
const view = await mempool.getCoinView(tx2);
const sview = await mempool.getSpentView(tx2);
assert(view.hasEntry(coin1));
assert(sview.hasEntry(coin1));
assert.strictEqual(mempool.hasCoin(coin1.hash, coin1.index), true);
assert.strictEqual(mempool.isSpent(coin1.hash, coin1.index), false);
}
await mempool.addTX(tx2);
{
const view = await mempool.getCoinView(tx1);
const sview = await mempool.getSpentView(tx1);
assert(!view.hasEntry(dummyCoin));
assert(sview.hasEntry(dummyCoin));
assert.strictEqual(mempool.hasCoin(coin1.hash, coin1.index), false);
assert.strictEqual(mempool.isSpent(coin1.hash, coin1.index), true);
}
{
const view = await mempool.getCoinView(tx2);
const sview = await mempool.getSpentView(tx2);
assert(!view.hasEntry(coin1));
assert(sview.hasEntry(coin1));
assert.strictEqual(mempool.hasCoin(coin1.hash, coin1.index), false);
assert.strictEqual(mempool.isSpent(coin1.hash, coin1.index), true);
}
});
it('should handle locktime', async () => {
const key = KeyRing.generate();
const addr = key.getAddress();
const tx = new MTX();
tx.addOutput(wallet.getAddress(), 50000);
tx.addOutput(wallet.getAddress(), 10000);
const prev = Script.fromPubkeyhash(key.getHash());
const prevHash = random.randomBytes(32);
tx.addCoin(dummyInput(addr, prevHash));
tx.setLocktime(200);
chain.tip.height = 200;
const sig = tx.signature(0, prev, 70000, key.privateKey, ALL);
tx.inputs[0].witness = Witness.fromItems([sig, key.publicKey]);
await mempool.addTX(tx.toTX());
chain.tip.height = 0;
});
it('should handle invalid locktime', async () => {
const key = KeyRing.generate();
const addr = key.getAddress();
const tx = new MTX();
tx.addOutput(wallet.getAddress(), 50000);
tx.addOutput(wallet.getAddress(), 10000);
const prev = Script.fromPubkeyhash(key.getHash());
const prevHash = random.randomBytes(32);
tx.addCoin(dummyInput(addr, prevHash));
tx.setLocktime(200);
chain.tip.height = 200 - 1;
const sig = tx.signature(0, prev, 70000, key.privateKey, ALL);
tx.inputs[0].witness = Witness.fromItems([sig, key.publicKey]);
let err;
try {
await mempool.addTX(tx.toTX());
} catch (e) {
err = e;
}
assert(err);
chain.tip.height = 0;
});
it('should not cache a malleated wtx with mutated sig', async () => {
const key = KeyRing.generate();
const addr = key.getAddress();
const tx = new MTX();
tx.addOutput(wallet.getAddress(), 50000);
tx.addOutput(wallet.getAddress(), 10000);
const prevHash = random.randomBytes(32);
tx.addCoin(dummyInput(addr, prevHash));
const prevs = Script.fromPubkeyhash(key.getKeyHash());
const sig = tx.signature(0, prevs, 70000, key.privateKey, ALL);
sig[sig.length - 1] = 0;
tx.inputs[0].witness = new Witness([sig, key.publicKey]);
let err;
try {
await mempool.addTX(tx.toTX());
} catch (e) {
err = e;
}
assert(err);
assert(!mempool.hasReject(tx.hash()));
});
it('should not cache non-malleated tx without sig', async () => {
const key = KeyRing.generate();
const addr = key.getAddress();
const tx = new MTX();
tx.addOutput(wallet.getAddress(), 50000);
tx.addOutput(wallet.getAddress(), 10000);
const prevHash = random.randomBytes(32);
tx.addCoin(dummyInput(addr, prevHash));
let err;
try {
await mempool.addTX(tx.toTX());
} catch (e) {
err = e;
}
assert(err);
assert(!mempool.hasReject(tx.hash()));
cachedTX = tx;
});
it('should clear reject cache', async () => {
const tx = new MTX();
tx.addOutpoint(new Outpoint());
tx.addOutput(wallet.getAddress(), 50000);
assert(!mempool.hasReject(cachedTX.hash()));
await mempool.addBlock({ height: 1 }, [tx.toTX()], new CoinView());
assert(!mempool.hasReject(cachedTX.hash()));
});
it('should remove tx after being included in block', async () => {
const key = KeyRing.generate();
const addr = key.getAddress();
const t1 = new MTX();
{
t1.addOutput(wallet.getAddress(), 50000);
t1.addOutput(wallet.getAddress(), 10000);
const script = Script.fromPubkeyhash(key.getHash());
t1.addCoin(dummyInput(addr, ONE_HASH));
const sig = t1.signature(0, script, 70000, key.privateKey, ALL);
t1.inputs[0].witness = Witness.fromItems([sig, key.publicKey]);
await mempool.addTX(t1.toTX());
}
const t2 = new MTX();
{
const key = KeyRing.generate();
const addr = key.getAddress();
t2.addOutput(wallet.getAddress(), 50000);
t2.addOutput(wallet.getAddress(), 10000);
const script = Script.fromPubkeyhash(key.getHash());
t2.addCoin(dummyInput(addr, ONE_HASH));
const sig = t2.signature(0, script, 70000, key.privateKey, ALL);
t2.inputs[0].witness = Witness.fromItems([sig, key.publicKey]);
await mempool.addTX(t2.toTX());
}
const [block, view] = await getMockBlock(chain, [t1], true);
{
const entry = await mempool.getEntry(t1.hash());
assert.equal(entry.txid(), t1.txid());
}
await mempool.addBlock(block, block.txs, view);
{
const entry = await mempool.getEntry(t1.hash());
assert.equal(entry, undefined);
}
{
const tx = t2.toTX();
const entry = await mempool.getEntry(tx.hash());
assert.equal(entry.txid(), tx.txid());
}
});
it('should reject absurd fee', async () => {
const wallet = new MemWallet({ network });
const addr = wallet.getAddress();
const funds = 10000e6;
const mtx = new MTX();
mtx.addCoin(
dummyInput(
addr,
random.randomBytes(32),
funds
)
);
mtx.addOutput(wallet.getAddress(), 0); // temp
wallet.sign(mtx);
const vsize = mtx.getVirtualSize();
const minFee = (vsize / 1000) * network.minRelay;
const absurdFee = minFee * policy.ABSURD_FEE_FACTOR;
// Revise with exactly absurd fee
mtx.outputs[0].value = funds - absurdFee - 1;
mtx.inputs[0].witness.items.length = 0;
wallet.sign(mtx);
const tx1 = mtx.toTX();
await assert.rejects(
mempool.addTX(tx1),
{message: /absurdly-high-fee/}
);
// Revise again with just under absurd fee
mtx.outputs[0].value = funds - absurdFee;
mtx.inputs[0].witness.items.length = 0;
wallet.sign(mtx);
const tx2 = mtx.toTX();
await mempool.addTX(tx2);
});
it('should reject too-low fee', async () => {
const wallet = new MemWallet({ network });
const addr = wallet.getAddress();
const funds = 10000e6;
const mtx = new MTX();
mtx.addCoin(
dummyInput(
addr,
random.randomBytes(32),
funds
)
);
mtx.addOutput(wallet.getAddress(), 0); // temp
wallet.sign(mtx);
const vsize = mtx.getVirtualSize();
const minFee = (vsize / 1000) * network.minRelay;
// Revise with just under minFee
mtx.outputs[0].value = funds - minFee + 1;
mtx.inputs[0].witness.items.length = 0;
wallet.sign(mtx);
const tx1 = mtx.toTX();
await assert.rejects(
mempool.addTX(tx1),
{message: /insufficient priority/}
);
// Revise again with exactly minFee
mtx.outputs[0].value = funds - minFee;
mtx.inputs[0].witness.items.length = 0;
wallet.sign(mtx);
const tx2 = mtx.toTX();
await mempool.addTX(tx2);
});
});
describe('Mempool disconnect and reorg handling', function () {
const workers = new WorkerPool({
// Must be disabled for `ownership.ignore`.
enabled: false,
size: 2
});
const blocks = new BlockStore({
memory: true,
network
});
const chain = new Chain({
memory: true,
blocks,
workers,
network
});
const mempool = new Mempool({
chain,
workers,
memory: true
});
const wallet = new MemWallet({ network });
const COINBASE_MATURITY = mempool.network.coinbaseMaturity;
const TREE_INTERVAL = mempool.network.names.treeInterval;
before(async () => {
await mempool.open();
await blocks.open();
await chain.open();
await workers.open();
});
after(async () => {
await workers.close();
await chain.close();
await blocks.close();
await mempool.close();
});
// Number of coins available in
// chaincoins (100k satoshi per coin).
const N = 100;
const chaincoins = new MemWallet({ network });
chain.on('block', (block, entry) => {
chaincoins.addBlock(entry, block.txs);
});
chain.on('disconnect', (entry, block) => {
chaincoins.removeBlock(entry, block.txs);
});
chaincoins.getNameStatus = async (nameHash) => {
assert(Buffer.isBuffer(nameHash));
const height = chain.height + 1;
return chain.db.getNameStatus(nameHash, height);
};
it('should create coins in chain', async () => {
const mtx = new MTX();
mtx.locktime = chain.height + 1;
mtx.addInput(new Input());
for (let i = 0; i < N; i++) {
const addr = chaincoins.createReceive().getAddress();
mtx.addOutput(addr, 100000);
}
const cb = mtx.toTX();
const [block, view] = await getMockBlock(chain, [cb], false);
const entry = await chain.add(block, VERIFY_BODY);
await mempool.addBlock(entry, block.txs, view);
// Add 100 blocks so we don't get
// premature spend of coinbase.
for (let i = 0; i < 100; i++) {
const [block, view] = await getMockBlock(chain);
const entry = await chain.add(block, VERIFY_BODY);
await mempool.addBlock(entry, block.txs, view);
}
chaincoins.addTX(cb);
});
it('should insert unconfirmed txs from removed block', async () => {
await mempool.reset();
// Mempool is empty
assert.strictEqual(mempool.map.size, 0);
// Create 1 TX
const coin1 = chaincoins.getCoins()[0];
const addr = wallet.createReceive().getAddress();
const mtx1 = new MTX();
mtx1.addCoin(coin1);
mtx1.addOutput(addr, 90000);
chaincoins.sign(mtx1);
const tx1 = mtx1.toTX();
chaincoins.addTX(tx1);
wallet.addTX(tx1);
// Create 1 block (no need to actually add it to chain)
const [block1] = await getMockBlock(chain, [tx1]);
const entry1 = await ChainEntry.fromBlock(block1, chain.tip);
// Unconfirm block into mempool
await mempool._removeBlock(entry1, block1.txs);
// Mempool should contain newly unconfirmed TX
assert(mempool.hasEntry(tx1.hash()));
// Mempool is NOT empty
assert.strictEqual(mempool.map.size, 1);
// Create second TX
const coin2 = chaincoins.getCoins()[0];
const mtx2 = new MTX();
mtx2.addCoin(coin2);
mtx2.addOutput(addr, 90000);
chaincoins.sign(mtx2);
const tx2 = mtx2.toTX();
chaincoins.addTX(tx2);
wallet.addTX(tx2);
// Create 1 block (no need to actually add it to chain)
const [block2] = await getMockBlock(chain, [tx2]);
const entry2 = await ChainEntry.fromBlock(block2, chain.tip);
// Unconfirm block into mempool
await mempool._removeBlock(entry2, block2.txs);
await mempool._handleReorg();
// Mempool should contain both TXs
assert(mempool.hasEntry(tx2.hash()));
assert(mempool.hasEntry(tx1.hash()));
assert.strictEqual(mempool.map.size, 2);
// Ensure mempool contents are valid in next block
const [newBlock, newView] = await getMockBlock(chain, [tx1, tx2]);
const newEntry = await chain.add(newBlock, VERIFY_BODY);
await mempool.addBlock(newEntry, newBlock.txs, newView);
assert.strictEqual(mempool.map.size, 0);
});
it('should insert resolved orphan tx after parent confirmed', async () => {
await mempool.reset();
// Mempool is empty
assert.strictEqual(mempool.map.size, 0);
// No orphans either
assert.strictEqual(mempool.waiting.size, 0);
assert.strictEqual(mempool.orphans.size, 0);
// Create first TX
const coin1 = chaincoins.getCoins()[0];
const addr = wallet.createReceive().getAddress();
const mtx1 = new MTX();
mtx1.addCoin(coin1);
mtx1.addOutput(addr, 90000);
chaincoins.sign(mtx1);
const tx1 = mtx1.toTX();
chaincoins.addTX(tx1);
wallet.addTX(tx1);
// Create second TX, spending output of first
const mtx2 = new MTX();
mtx2.addTX(tx1, 0);
mtx2.addOutput(addr, 80000);
wallet.sign(mtx2);
const tx2 = mtx2.toTX();
chaincoins.addTX(tx2);
wallet.addTX(tx2);
// Attempt to add second TX to mempool
await mempool.addTX(tx2);
// tx2 is orphan waiting on tx1
assert.strictEqual(mempool.map.size, 0);
assert.strictEqual(mempool.waiting.size, 1);
assert.strictEqual(mempool.orphans.size, 1);
assert(mempool.waiting.has(tx1.hash()));
assert(mempool.orphans.has(tx2.hash()));
// Confirm tx1 in a block
const [block, view] = await getMockBlock(chain, [tx1], true);
const entry = await chain.add(block, VERIFY_BODY);
await mempool.addBlock(entry, block.txs, view);
// tx2 has been resolved back in to mempool
assert.strictEqual(mempool.map.size, 1);
assert.strictEqual(mempool.waiting.size, 0);
assert.strictEqual(mempool.orphans.size, 0);
assert(mempool.map.has(tx2.hash()));
// Ensure mempool contents are valid in next block
const [newBlock, newView] = await getMockBlock(chain, [tx2]);
const newEntry = await chain.add(newBlock, VERIFY_BODY);
await mempool._addBlock(newEntry, newBlock.txs, newView);
assert.strictEqual(mempool.map.size, 0);
assert.strictEqual(mempool.waiting.size, 0);
assert.strictEqual(mempool.orphans.size, 0);
});
it('should handle reorg: coinbase spends', async () => {
// Mempool is empty
await mempool.reset();
assert.strictEqual(mempool.map.size, 0);
// Create a fresh coinbase tx
let cb = new MTX();
cb.addInput(new Input());
const addr = chaincoins.createReceive().getAddress();
cb.addOutput(addr, 100000);
cb.locktime = chain.height + 1;
cb = cb.toTX();
// Add it to block and mempool
const [block1, view1] = await getMockBlock(chain, [cb], false);
const entry1 = await chain.add(block1, VERIFY_BODY);
await mempool.addBlock(entry1, block1.txs, view1);
// The coinbase output is a valid UTXO in the chain
assert(await chain.getCoin(cb.hash(), 0));
// Mempool is empty
assert.strictEqual(mempool.map.size, 0);
// Attempt to spend the coinbase early
let spend = new MTX();
spend.addTX(cb, 0);
spend.addOutput(addr, 90000);
chaincoins.sign(spend);
spend = spend.toTX();
// It's too early
await assert.rejects(async () => {
await mempool.addTX(spend, -1);
}, {
name: 'Error',
reason: 'bad-txns-premature-spend-of-coinbase'
});
// Add more blocks
let block2;
let view2;
let entry2;
for (let i = 0; i < COINBASE_MATURITY - 1; i++) {
[block2, view2] = await getMockBlock(chain);
entry2 = await chain.add(block2, VERIFY_BODY);
await mempool.addBlock(entry2, block2.txs, view2);
}
// Try again
await mempool.addTX(spend, -1);
// Coinbase spend is in the mempool
assert.strictEqual(mempool.map.size, 1);
assert(mempool.getTX(spend.hash()));
// Confirm coinbase spend in a block
const [block3, view3] = await getMockBlock(chain, [spend]);
const entry3 = await chain.add(block3, VERIFY_BODY);
await mempool.addBlock(entry3, block3.txs, view3);
// Coinbase spend has been removed from the mempool
assert.strictEqual(mempool.map.size, 0);
assert(!mempool.getTX(spend.hash()));
// Now the block gets disconnected
await chain.disconnect(entry3);
await mempool._removeBlock(entry3, block3.txs);
await mempool._handleReorg();
// Coinbase spend is back in the mempool
assert.strictEqual(mempool.map.size, 1);
assert(mempool.getTX(spend.hash()));
// Now remove one more block from the chain, thus
// making the spend TX premature
await chain.disconnect(entry2);
await mempool._removeBlock(entry2, block2.txs);
// Coinbase spend is still in the mempool
assert.strictEqual(mempool.map.size, 1);
assert(mempool.getTX(spend.hash()));
// This is normally triggered by 'reorganize' event
await mempool._handleReorg();
// Premature coinbase spend has been evicted
assert.strictEqual(mempool.map.size, 0);
assert(!mempool.getTX(spend.hash()));
});
it('should handle reorg: BIP68 sequence locks', async () => {
// Mempool is empty
await mempool.reset();
assert.strictEqual(mempool.map.size, 0);
// Create a fresh UTXO
const fundCoin = chaincoins.getCoins()[0];
let fund = new MTX();
fund.addCoin(fundCoin);
const addr = chaincoins.createReceive().getAddress();
fund.addOutput(addr, 90000);
chaincoins.sign(fund);
fund = fund.toTX();
chaincoins.addTX(fund);
// Add it to block and mempool
const [block1, view1] = await getMockBlock(chain, [fund]);
const entry1 = await chain.add(block1, VERIFY_BODY);
await mempool.addBlock(entry1, block1.txs, view1);
// The fund TX output is a valid UTXO in the chain
const spendCoin = await chain.getCoin(fund.hash(), 0);
assert(spendCoin);
// Mempool is empty
assert.strictEqual(mempool.map.size, 0);
// Spend the coin with a sequence lock of 0x00000001.
// This should require the input coin to be 1 block old.
let spend = new MTX();
spend.addCoin(spendCoin);
spend.addOutput(addr, 70000);
spend.inputs[0].sequence = 1;
spend.version = 0;
chaincoins.sign(spend);
spend = spend.toTX();
// Valid spend into mempool
await mempool.addTX(spend);
assert.strictEqual(mempool.map.size, 1);
assert(mempool.getTX(spend.hash()));
// Confirm spend into block
const [block2, view2] = await getMockBlock(chain, [spend]);
const entry2 = await chain.add(block2, VERIFY_BODY);
await mempool.addBlock(entry2, block2.txs, view2);
// Spend has been removed from the mempool
assert.strictEqual(mempool.map.size, 0);
assert(!mempool.getTX(spend.hash()));
// Now the block gets disconnected
await chain.disconnect(entry2);
await mempool._removeBlock(entry2, block2.txs);
await mempool._handleReorg();
// Spend is back in the mempool
assert.strictEqual(mempool.map.size, 1);
assert(mempool.getTX(spend.hash()));
// Now remove one more block from the chain,
// re-inserting the funding TX back into the mempool.
// This should make the sequence-locked spend invalid
// because its input coin is no lnger 1 block old.
await chain.disconnect(entry1);
await mempool._removeBlock(entry1, block1.txs);
// Fund TX & spend TX are both still in the mempool
assert.strictEqual(mempool.map.size, 2);
assert(mempool.getTX(spend.hash()));
assert(mempool.getTX(fund.hash()));
// This is normally triggered by 'reorganize' event
await mempool._handleReorg();
// Premature sequence lock spend has been evicted, fund TX remains
assert.strictEqual(mempool.map.size, 1);
assert(mempool.getTX(fund.hash()));
assert(!mempool.getTX(spend.hash()));
// Ensure mempool contents are valid in next block
const [newBlock, newView] = await getMockBlock(chain, [fund]);
const newEntry = await chain.add(newBlock, VERIFY_BODY);
await mempool.addBlock(newEntry, newBlock.txs, newView);
assert.strictEqual(mempool.map.size, 0);
});
it('should handle reorg: covenants', async () => {
// Mempool is empty
await mempool.reset();
assert.strictEqual(mempool.map.size, 0);
// Create a fresh UTXO with an OPEN
const openCoin = chaincoins.getCoins()[0];
let open = new MTX();
open.addCoin(openCoin);
const addr = chaincoins.createReceive().getAddress();
open.addOutput(addr, 90000);
const name = rules.grindName(10, 0, mempool.network);
const rawName = Buffer.from(name, 'ascii');
const nameHash = rules.hashName(rawName);
open.outputs[0].covenant.setOpen(nameHash, rawName);
chaincoins.sign(open);
open = open.toTX();
// Add it to block and mempool
const [block1, view1] = await getMockBlock(chain, [open]);
const entry1 = await chain.add(block1, VERIFY_BODY);
await mempool.addBlock(entry1, block1.txs, view1);
// The open TX output is a valid UTXO in the chain
assert(await chain.getCoin(open.hash(), 0));
// Name is OPEN
let ns = await chain.db.getNameStateByName(name);
assert.strictEqual(
ns.state(chain.height, mempool.network),
states.OPENING
);
// Mempool is empty
assert.strictEqual(mempool.map.size, 0);
// Create a BID on the name.
// We don't need a real blind.