-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathchain-full-test.js
1062 lines (817 loc) · 28.5 KB
/
chain-full-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 consensus = require('../lib/protocol/consensus');
const Address = require('../lib/primitives/address');
const Coin = require('../lib/primitives/coin');
const Script = require('../lib/script/script');
const BlockStore = require('../lib/blockstore/level');
const Chain = require('../lib/blockchain/chain');
const WorkerPool = require('../lib/workers/workerpool');
const Miner = require('../lib/mining/miner');
const {BlockEntry} = require('../lib/mining/template');
const MTX = require('../lib/primitives/mtx');
const MemWallet = require('./util/memwallet');
const Network = require('../lib/protocol/network');
const Output = require('../lib/primitives/output');
const MerkleBlock = require('../lib/primitives/merkleblock');
const common = require('../lib/blockchain/common');
const opcodes = Script.opcodes;
const network = Network.get('regtest');
const workers = new WorkerPool({
enabled: true,
size: 2
});
const blocks = new BlockStore({
memory: true,
network
});
const chain = new Chain({
memory: true,
blocks,
network,
workers
});
const miner = new Miner({
chain,
workers
});
const cpu = miner.cpu;
let wallet = new MemWallet({
network
});
let tip1 = null;
let tip2 = null;
async function addBlock(block, flags) {
let entry;
try {
entry = await chain.add(block, flags);
} catch (e) {
assert.strictEqual(e.type, 'VerifyError');
return e.reason;
}
if (!entry)
return 'bad-prevblk';
return 'OK';
}
async function mineBlock(job, flags) {
const block = await job.mineAsync();
return addBlock(block, flags);
}
chain.on('connect', (entry, block) => {
wallet.addBlock(entry, block.txs);
});
chain.on('disconnect', (entry, block) => {
wallet.removeBlock(entry, block.txs);
});
describe('Chain', function() {
this.timeout(45000);
before(async () => {
await blocks.open();
await chain.open();
await miner.open();
});
after(async () => {
await miner.close();
await chain.close();
await blocks.close();
});
it('should add addrs to miner', async () => {
miner.addresses.length = 0;
miner.addAddress(wallet.getReceive());
});
it('should mine 200 blocks', async () => {
for (let i = 0; i < 200; i++) {
const block = await cpu.mineBlock();
assert(block);
assert(await chain.add(block));
}
assert.strictEqual(chain.height, 200);
});
it('should mine competing chains', async () => {
for (let i = 0; i < 10; i++) {
const job1 = await cpu.createJob(tip1);
const job2 = await cpu.createJob(tip2);
const mtx = await wallet.create({
outputs: [{
address: wallet.getAddress(),
value: 10 * 1e8
}]
});
assert(job1.addTX(mtx.toTX(), mtx.view));
assert(job2.addTX(mtx.toTX(), mtx.view));
job1.refresh();
job2.refresh();
const blk1 = await job1.mineAsync();
const blk2 = await job2.mineAsync();
const hash1 = blk1.hash();
const hash2 = blk2.hash();
assert(await chain.add(blk1));
assert(await chain.add(blk2));
assert.bufferEqual(chain.tip.hash, hash1);
tip1 = await chain.getEntry(hash1);
tip2 = await chain.getEntry(hash2);
assert(tip1);
assert(tip2);
assert(!await chain.isMainChain(tip2));
}
});
it('should have correct chain value', () => {
assert.strictEqual(chain.db.state.value, 422002210000);
assert.strictEqual(chain.db.state.coin, 221);
assert.strictEqual(chain.db.state.tx, 221);
});
it('should have correct wallet balance', async () => {
assert.strictEqual(wallet.balance, 420000000000);
});
it('should handle a reorg', async () => {
assert.strictEqual(chain.height, 210);
const entry = await chain.getEntry(tip2.hash);
assert(entry);
assert.strictEqual(chain.height, entry.height);
const block = await cpu.mineBlock(entry);
assert(block);
let forked = false;
chain.once('reorganize', () => {
forked = true;
});
assert(await chain.add(block));
assert(forked);
assert.bufferEqual(chain.tip.hash, block.hash());
assert(chain.tip.chainwork.gt(tip1.chainwork));
});
it('should have correct chain value', () => {
assert.strictEqual(chain.db.state.value, 424002210000);
assert.strictEqual(chain.db.state.coin, 222);
assert.strictEqual(chain.db.state.tx, 222);
});
it('should have correct wallet balance', async () => {
assert.strictEqual(wallet.balance, 422000000000);
});
it('should check main chain', async () => {
const result = await chain.isMainChain(tip1);
assert(!result);
});
it('should mine a block after a reorg', async () => {
const block = await cpu.mineBlock();
assert(await chain.add(block));
const hash = block.hash();
const entry = await chain.getEntry(hash);
assert(entry);
assert.bufferEqual(chain.tip.hash, entry.hash);
const result = await chain.isMainChain(entry);
assert(result);
});
it('should prevent double spend on new chain', async () => {
const mtx = await wallet.create({
outputs: [{
address: wallet.getAddress(),
value: 10 * consensus.COIN
}]
});
{
const job = await cpu.createJob();
assert(job.addTX(mtx.toTX(), mtx.view));
job.refresh();
const block = await job.mineAsync();
assert(await chain.add(block));
}
{
const job = await cpu.createJob();
assert(mtx.outputs.length > 1);
mtx.outputs.pop();
assert(job.addTX(mtx.toTX(), mtx.view));
job.refresh();
assert.strictEqual(await mineBlock(job),
'bad-txns-inputs-missingorspent');
}
});
it('should fail to connect coins on an alternate chain', async () => {
const block = await chain.getBlock(tip1.hash);
const cb = block.txs[0];
const mtx = new MTX();
mtx.addTX(cb, 0);
mtx.addOutput(wallet.getAddress(), 10 * consensus.COIN);
wallet.sign(mtx);
const job = await cpu.createJob();
assert(job.addTX(mtx.toTX(), mtx.view));
job.refresh();
assert.strictEqual(await mineBlock(job), 'bad-txns-inputs-missingorspent');
});
it('should have correct chain value', () => {
assert.strictEqual(chain.db.state.value, 428002210000);
assert.strictEqual(chain.db.state.coin, 225);
assert.strictEqual(chain.db.state.tx, 225);
});
it('should get coin', async () => {
const mtx = await wallet.send({
outputs: [
{
address: wallet.getAddress(),
value: 1e8
},
{
address: wallet.getAddress(),
value: 1e8
},
{
address: wallet.getAddress(),
value: 1e8
}
]
});
const job = await cpu.createJob();
assert(job.addTX(mtx.toTX(), mtx.view));
job.refresh();
const block = await job.mineAsync();
assert(await chain.add(block));
const tx = block.txs[1];
const output = Coin.fromTX(tx, 2, chain.height);
const coin = await chain.getCoin(tx.hash(), 2);
assert.bufferEqual(coin.encode(), output.encode());
});
it('should have correct wallet balance', async () => {
assert.strictEqual(wallet.balance, 428000000000);
assert.strictEqual(wallet.receiveDepth, 15);
assert.strictEqual(wallet.changeDepth, 14);
assert.strictEqual(wallet.txs, 226);
});
it('should get tips and remove chains', async () => {
{
const tips = await chain.db.getTips();
// XXX
// assert.notStrictEqual(tips.indexOf(chain.tip.hash), -1);
assert.strictEqual(tips.length, 2);
}
await chain.db.removeChains();
{
const tips = await chain.db.getTips();
// XXX
// assert.notStrictEqual(tips.indexOf(chain.tip.hash), -1);
assert.strictEqual(tips.length, 1);
}
});
it('should rescan for transactions', async () => {
let total = 0;
await chain.scan(0, wallet.filter, async (block, txs) => {
total += txs.length;
});
assert.strictEqual(total, 226);
});
it('should have correct wallet balance', async () => {
assert.strictEqual(wallet.balance, 428000000000);
});
it('should fail to connect bad bits', async () => {
const job = await cpu.createJob();
job.attempt.bits = 553713663;
assert.strictEqual(await mineBlock(job), 'bad-diffbits');
});
it('should fail to connect bad MTP', async () => {
const mtp = await chain.getMedianTime(chain.tip);
const job = await cpu.createJob();
job.attempt.time = mtp - 1;
assert.strictEqual(await mineBlock(job), 'time-too-old');
});
it('should fail to connect bad time', async () => {
const job = await cpu.createJob();
const now = network.now() + 3 * 60 * 60;
job.attempt.time = now;
assert.strictEqual(await mineBlock(job), 'time-too-new');
});
it('should fail to connect bad locktime', async () => {
const job = await cpu.createJob();
const tx = await wallet.send({ locktime: 100000 });
job.pushTX(tx.toTX());
job.refresh();
assert.strictEqual(await mineBlock(job), 'bad-txns-nonfinal');
});
it('should fail to connect bad cb height', async () => {
const job = await cpu.createJob();
job.attempt.height = 10;
job.attempt.refresh();
assert.strictEqual(await mineBlock(job), 'bad-cb-height');
});
it('should fail to connect bad witness root', async () => {
const block = await cpu.mineBlock();
const tx = block.txs[0];
const input = tx.inputs[0];
input.witness.set(0, Buffer.alloc(1001));
block.refresh(true);
assert.strictEqual(await addBlock(block), 'bad-witnessroot');
});
it('should fail to connect bad tx merkle root', async () => {
const flags = common.flags.DEFAULT_FLAGS & ~common.flags.VERIFY_POW;
const block = await cpu.mineBlock();
block.merkleRoot = Buffer.from(block.merkleRoot);
block.merkleRoot[0] ^= 1;
assert.strictEqual(await addBlock(block, flags), 'bad-txnmrklroot');
});
it('should mine 2000 blocks', async () => {
for (let i = 0; i < 2001; i++) {
const block = await cpu.mineBlock();
assert(block);
assert(await chain.add(block));
}
assert.strictEqual(chain.height, 2215);
});
it('should mine a witness tx', async () => {
const prev = await chain.getBlock(chain.height - 2000);
const cb = prev.txs[0];
const mtx = new MTX();
mtx.addTX(cb, 0);
mtx.addOutput(wallet.getAddress(), 1000);
wallet.sign(mtx);
const job = await cpu.createJob();
job.addTX(mtx.toTX(), mtx.view);
job.refresh();
const block = await job.mineAsync();
assert(await chain.add(block));
});
it('should mine fail to connect too much weight', async () => {
const start = chain.height - 2000;
const end = chain.height - 200;
const job = await cpu.createJob();
for (let i = start; i <= end; i++) {
const block = await chain.getBlock(i);
const cb = block.txs[0];
const mtx = new MTX();
mtx.addTX(cb, 0);
for (let j = 0; j < 15; j++)
mtx.addOutput(wallet.getAddress(), 1);
wallet.sign(mtx);
job.pushTX(mtx.toTX());
}
job.refresh();
assert.strictEqual(await mineBlock(job), 'bad-blk-weight');
});
it('should mine fail to connect too much size', async () => {
const start = chain.height - 2000;
const end = chain.height - 200;
const job = await cpu.createJob();
for (let i = start; i <= end; i++) {
const block = await chain.getBlock(i);
const cb = block.txs[0];
const mtx = new MTX();
mtx.addTX(cb, 0);
for (let j = 0; j < 20; j++)
mtx.addOutput(wallet.getAddress(), 1);
wallet.sign(mtx);
job.pushTX(mtx.toTX());
}
job.refresh();
assert.strictEqual(await mineBlock(job), 'bad-blk-length');
});
it('should mine a big block', async () => {
const start = chain.height - 2000;
const end = chain.height - 200;
const job = await cpu.createJob();
const prove = [];
for (let i = start; i <= end - 1; i++) {
const block = await chain.getBlock(i);
const cb = block.txs[0];
const mtx = new MTX();
mtx.addTX(cb, 0);
for (let j = 0; j < 14; j++)
mtx.addOutput(wallet.getAddress(), 1);
wallet.sign(mtx);
if (i & 1)
prove.push(mtx.hash());
job.pushTX(mtx.toTX());
}
job.refresh();
const block = await job.mineAsync();
const entry = await chain.add(block);
assert(entry);
assert(block.txs.length & 1);
const merkle = MerkleBlock.fromHashes(block, prove);
assert(merkle.verify());
const tree = merkle.getTree();
assert.strictEqual(tree.matches.length, prove.length);
assert.strictEqual(tree.map.size, prove.length);
for (const hash of prove)
assert(tree.map.has(hash));
});
it('should fail to connect bad amount', async () => {
const job = await cpu.createJob();
job.attempt.fees += 1;
job.refresh();
assert.strictEqual(await mineBlock(job), 'bad-cb-amount');
});
it('should fail to connect premature cb spend', async () => {
const job = await cpu.createJob();
const block = await chain.getBlock(chain.height);
const cb = block.txs[0];
const mtx = new MTX();
mtx.addTX(cb, 0);
mtx.addOutput(wallet.getAddress(), 1);
wallet.sign(mtx);
job.addTX(mtx.toTX(), mtx.view);
job.refresh();
assert.strictEqual(await mineBlock(job),
'bad-txns-premature-spend-of-coinbase');
});
it('should fail to connect vout belowout', async () => {
const job = await cpu.createJob();
const block = await chain.getBlock(chain.height - 99);
const cb = block.txs[0];
const mtx = new MTX();
mtx.addTX(cb, 0);
mtx.addOutput(wallet.getAddress(), cb.outputs[0].value + 1);
wallet.sign(mtx);
job.pushTX(mtx.toTX());
job.refresh();
assert.strictEqual(await mineBlock(job),
'bad-txns-in-belowout');
});
it('should fail to connect outtotal toolarge', async () => {
const job = await cpu.createJob();
const block = await chain.getBlock(chain.height - 99);
const cb = block.txs[0];
const mtx = new MTX();
mtx.addTX(cb, 0);
const value = Math.floor(consensus.MAX_MONEY / 2);
mtx.addOutput(wallet.getAddress(), value);
mtx.addOutput(wallet.getAddress(), value);
mtx.addOutput(wallet.getAddress(), value);
wallet.sign(mtx);
job.pushTX(mtx.toTX());
job.refresh();
assert.strictEqual(await mineBlock(job),
'bad-txns-txouttotal-toolarge');
});
describe('Sigops', function() {
// Create a script with 100 sigops.
// The boolean logic at the top makes it ANYONECANSPEND.
// The following sigops are still counted even though they are not executed.
// This script is based on the Bitcoin Core test p2p_segwit.py
const script = new Script();
script.pushOp(opcodes.OP_1);
script.pushOp(opcodes.OP_IF);
script.pushOp(opcodes.OP_1);
script.pushOp(opcodes.OP_ELSE);
for (let i = 0; i < 100; i++)
script.pushOp(opcodes.OP_CHECKSIG);
script.pushOp(opcodes.OP_ENDIF);
script.compile();
// Address from script
const addr = Address.fromScripthash(script.sha3());
// Value of each UTXO
const value = 1;
// First block to spend from
let start;
it('should mine 80 blocks to 100-sigops script hash address', async () => {
// We are going to modify blocks after mining,
// just to get UTXO for the next test.
// These blocks don't have to be strictly 100% valid.
const flags = common.flags.DEFAULT_FLAGS & ~common.flags.VERIFY_POW;
// Generate 80 blocks where each coinbase transaction
// has 10 outputs to the 100-sigop address.
start = chain.height + 1;
for (let i = 0; i < 80; i++) {
const block = await cpu.mineBlock();
const cb = block.txs[0];
// Clear coinbase outputs
cb.outputs.length = 0;
// Add 10 outputs to our sigops address instead
for (let j = 0; j < 10; j++) {
const output = new Output();
output.address = addr;
output.value = value;
cb.outputs.push(output);
}
block.refresh(true);
block.merkleRoot = block.createMerkleRoot();
block.witnessRoot = block.createWitnessRoot();
assert(await chain.add(block, flags));
}
});
it('should mine 10 blocks to wallet', async () => {
for (let i = 0; i < 10; i++) {
const block = await cpu.mineBlock(null, wallet.getReceive());
assert(block);
assert(await chain.add(block));
}
});
it('should connect a block with maximum sigops', async () => {
// Mine a block with exactly 80,000 sigops.
// We do this by spending each of the 10 coinbase outputs from each
// of those previously generated 80 blocks all in the same new block.
// Each redeem script for each of those outputs has 100 sigops:
// 100 sigops * 10 outputs * 80 blocks = 80,000 sigops total
// The consensus limit MAX_BLOCK_SIGOPS is 80,000
const job = await cpu.createJob();
// Reset the sigops counter
// (Initial value is non-zero to reserve space for coinbase)
job.attempt.sigops = 0;
const end = start + 80;
for (let b = start; b < end; b++) {
const mtx = new MTX();
mtx.addOutput(wallet.getReceive(), 10);
const block = await chain.getBlock(b);
const cb = block.txs[0];
assert.strictEqual(cb.outputs.length, 10);
for (let i = 0; i < 10; i++) {
mtx.addTX(cb, i);
// Push redeem script
mtx.inputs[i].witness.push(script.encode());
}
job.pushTX(mtx.toTX(), mtx.view);
}
assert.strictEqual(job.attempt.sigops, 80000);
job.refresh();
assert.strictEqual(await mineBlock(job), 'OK');
});
it('should fail to connect a block with too many sigops', async () => {
// Remove last block so we can re-use the 100-sigop outputs.
await chain.disconnect(chain.tip);
// Mine a block with exactly 80,001 sigops.
const job = await cpu.createJob();
job.attempt.sigops = 0;
const end = start + 80;
for (let b = start; b < end; b++) {
const mtx = new MTX();
mtx.addOutput(wallet.getReceive(), 10);
const block = await chain.getBlock(b);
const cb = block.txs[0];
assert.strictEqual(cb.outputs.length, 10);
for (let i = 0; i < 10; i++) {
mtx.addTX(cb, i);
// Push redeem script
mtx.inputs[i].witness.push(script.encode());
}
job.pushTX(mtx.toTX(), mtx.view);
}
assert.strictEqual(job.attempt.sigops, 80000);
// Add one more sigop
const lastMTX = await wallet.create({
value: 1,
address: new Address()
});
job.pushTX(lastMTX.toTX(), lastMTX.view);
assert.strictEqual(job.attempt.sigops, 80001);
job.refresh();
assert.strictEqual(await mineBlock(job), 'bad-blk-sigops');
});
});
describe('Covenant limits', function() {
before(async () => {
await chain.reset(0);
wallet = new MemWallet({ network });
wallet.getNameStatus = async (nameHash) => {
assert(Buffer.isBuffer(nameHash));
const height = chain.height + 1;
return chain.db.getNameStatus(nameHash, height);
};
});
it('should mine 2000 blocks', async () => {
miner.addresses.length = 0;
miner.addAddress(wallet.getReceive());
for (let i = 0; i < 2000; i++) {
const block = await cpu.mineBlock();
assert(block);
assert(await chain.add(block));
}
assert.strictEqual(chain.height, 2000);
});
it('should fail to connect too many OPENs', async () => {
const job1 = await cpu.createJob();
for (let i = 0; i < consensus.MAX_BLOCK_OPENS + 1; i++) {
const name = `test_${i}`;
const open = await wallet.sendOpen(name);
const item = BlockEntry.fromTX(open.toTX(), open.view, job1.attempt);
job1.attempt.items.push(item);
}
job1.attempt.refresh();
assert.strictEqual(await mineBlock(job1), 'bad-blk-opens');
// "zap" memwallet
for (const {tx} of job1.attempt.items) {
wallet.removeTX(tx);
}
});
it('should connect max number of OPENs', async () => {
// We do this 3 times to open enough names for the following tests
for (let blocks = 0; blocks < 3; blocks++) {
const job1 = await cpu.createJob();
for (let txs = 0; txs < consensus.MAX_BLOCK_OPENS; txs++) {
const name = `test_${blocks}_${txs}`;
const open = await wallet.sendOpen(name);
const item = BlockEntry.fromTX(open.toTX(), open.view, job1.attempt);
job1.attempt.items.push(item);
}
job1.attempt.refresh();
assert.strictEqual(await mineBlock(job1), 'OK');
}
});
it('should win 900 name auctions', async () => {
for (let i = 0; i < network.names.treeInterval; i++) {
const block = await cpu.mineBlock();
assert(await chain.add(block));
}
for (let blocks = 0; blocks < 3; blocks++) {
const job1 = await cpu.createJob();
for (let txs = 0; txs < consensus.MAX_BLOCK_OPENS; txs++) {
const name = `test_${blocks}_${txs}`;
const bid = await wallet.sendBid(name, 1, 1);
const item = BlockEntry.fromTX(bid.toTX(), bid.view, job1.attempt);
job1.attempt.items.push(item);
}
job1.attempt.refresh();
assert.strictEqual(await mineBlock(job1), 'OK');
}
for (let i = 0; i < network.names.biddingPeriod; i++) {
const block = await cpu.mineBlock();
assert(await chain.add(block));
}
for (let blocks = 0; blocks < 3; blocks++) {
const job2 = await cpu.createJob();
for (let txs = 0; txs < consensus.MAX_BLOCK_OPENS; txs++) {
const name = `test_${blocks}_${txs}`;
const reveal = await wallet.sendReveal(name);
const item = BlockEntry.fromTX(reveal.toTX(), reveal.view, job2.attempt);
job2.attempt.items.push(item);
}
job2.attempt.refresh();
assert.strictEqual(await mineBlock(job2), 'OK');
}
for (let i = 0; i < network.names.revealPeriod; i++) {
const block = await cpu.mineBlock();
assert(await chain.add(block));
}
});
it('should fail to connect too many RENEWALs', async () => {
// As far as block limits are concerned
// REGISTER counts as a renewal, not an update.
const job1 = await cpu.createJob();
let x = 0;
let i = 0;
let count = 0;
while (count < consensus.MAX_BLOCK_RENEWALS + 1) {
const name = `test_${x}_${i}`;
// This is definitely a REGISTER (renewal) not an UPDATE
const ns = await chain.db.getNameStateByName(name);
assert(!ns.registered);
const register = await wallet.sendUpdate(name, null);
const item = BlockEntry.fromTX(register.toTX(), register.view, job1.attempt);
job1.attempt.items.push(item);
count++;
i++;
if (i >= consensus.MAX_BLOCK_OPENS) {
i = 0;
x++;
}
}
job1.attempt.refresh();
assert.strictEqual(await mineBlock(job1), 'bad-blk-renewals');
// "zap" memwallet
for (const {tx} of job1.attempt.items)
wallet.removeTX(tx);
});
it('should connect max number of RENEWALs', async () => {
const job1 = await cpu.createJob();
let x = 0;
let i = 0;
let count = 0;
while (count < consensus.MAX_BLOCK_RENEWALS) {
const name = `test_${x}_${i}`;
// This is definitely a REGISTER (renewal) not an UPDATE
const ns = await chain.db.getNameStateByName(name);
assert(!ns.registered);
const register = await wallet.sendUpdate(name, null);
const item = BlockEntry.fromTX(register.toTX(), register.view, job1.attempt);
job1.attempt.items.push(item);
count++;
i++;
if (i >= consensus.MAX_BLOCK_OPENS) {
i = 0;
x++;
}
}
job1.attempt.refresh();
assert.strictEqual(await mineBlock(job1), 'OK');
// Register 100 more so we can blow the limit on UPDATES in the next test
const job2 = await cpu.createJob();
while (count < consensus.MAX_BLOCK_RENEWALS + 100) {
const name = `test_${x}_${i}`;
// This is definitely a REGISTER (renewal) not an UPDATE
const ns = await chain.db.getNameStateByName(name);
assert(!ns.registered);
const register = await wallet.sendUpdate(name, null);
const item = BlockEntry.fromTX(register.toTX(), register.view, job2.attempt);
job2.attempt.items.push(item);
count++;
i++;
if (i >= consensus.MAX_BLOCK_OPENS) {
i = 0;
x++;
}
}
job2.attempt.refresh();
assert.strictEqual(await mineBlock(job2), 'OK');
});
it('should fail to connect too many UPDATEs', async () => {
const job1 = await cpu.createJob();
let x = 0;
let i = 0;
let count = 0;
while (count < consensus.MAX_BLOCK_UPDATES + 1) {
const name = `test_${x}_${i}`;
// This is definitely an UPDATE, not a REGISTER
const ns = await chain.db.getNameStateByName(name);
assert(ns.registered);
const update = await wallet.sendUpdate(name, null);
const item = BlockEntry.fromTX(update.toTX(), update.view, job1.attempt);
job1.attempt.items.push(item);
count++;
i++;
if (i >= consensus.MAX_BLOCK_OPENS) {
i = 0;
x++;
}
}
job1.attempt.refresh();
assert.strictEqual(await mineBlock(job1), 'bad-blk-updates');
// "zap" memwallet
for (const {tx} of job1.attempt.items)
wallet.removeTX(tx);
});
it('should connect max number of UPDATEs', async () => {
const job1 = await cpu.createJob();
let x = 0;
let i = 0;
let count = 0;
while (count < consensus.MAX_BLOCK_UPDATES) {
const name = `test_${x}_${i}`;
// This is definitely an UPDATE, not a REGISTER
const ns = await chain.db.getNameStateByName(name);
assert(ns.registered);
const update = await wallet.sendUpdate(name, null);
const item = BlockEntry.fromTX(update.toTX(), update.view, job1.attempt);
job1.attempt.items.push(item);
count++;
i++;
if (i >= consensus.MAX_BLOCK_OPENS) {
i = 0;
x++;
}
}
job1.attempt.refresh();
assert.strictEqual(await mineBlock(job1), 'OK');
});
it('should fail to connect a block with duplicate names', async () => {
const job1 = await cpu.createJob();
for (let i = 0; i < 2; i++) {
const name = 'test_duplicate';
const open = await wallet.sendOpen(name);
const item = BlockEntry.fromTX(open.toTX(), open.view, job1.attempt);
job1.attempt.items.push(item);
}
job1.attempt.refresh();