-
Notifications
You must be signed in to change notification settings - Fork 42
/
FullBlockTestGenerator.java
1818 lines (1598 loc) · 93 KB
/
FullBlockTestGenerator.java
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
package org.bitcoinj.core;
import org.bitcoinj.core.Transaction.SigHash;
import org.bitcoinj.crypto.TransactionSignature;
import org.bitcoinj.script.Script;
import org.bitcoinj.script.ScriptBuilder;
import com.google.common.base.Preconditions;
import javax.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;
import static org.bitcoinj.core.Coin.*;
import static org.bitcoinj.script.ScriptOpCodes.*;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
/**
* YOU ARE READING THIS CODE BECAUSE EITHER...
*
* a) You are testing an alternative implementation with full validation rules. If you are doing this, you should go
* rethink your life. Seriously, why are you reimplementing Bitcoin consensus rules? Instead, go work on making
* Bitcoin Core consensus rules a shared library and use that. Seriously, you wont get it right, and starting with
* this tester as a way to try to do so will simply end in pain and lost coins. SERIOUSLY, JUST STOP!
*
* b) Bitcoin Core is failing some test in here and you're wondering what test is causing failure. Just stop. There is no
* hope trying to read this file and decipher it. Give up and ping BlueMatt. Seriously, this stuff is a huge mess.
*
* c) You are trying to add a new test. STOP! WHY THE HELL WOULD YOU EVEN DO THAT? GO REWRITE THIS TESTER!
*
* d) You are BlueMatt and you're trying to hack more crap onto this multi-headed lopsided Proof Of Stake. Why are you
* doing this? Seriously, why have you not rewritten this thing yet? WTF man...
*
* IN ANY CASE, STOP READING NOW. IT WILL SAVE YOU MUCH PAIN AND MISERY LATER
*/
class NewBlock {
public Block block;
private TransactionOutPointWithValue spendableOutput;
public NewBlock(Block block, TransactionOutPointWithValue spendableOutput) {
this.block = block; this.spendableOutput = spendableOutput;
}
// Wrappers to make it more block-like
public Sha256Hash getHash() { return block.getHash(); }
public void solve() { block.solve(); }
public void addTransaction(Transaction tx) { block.addTransaction(tx); }
public TransactionOutPointWithValue getCoinbaseOutput() {
return new TransactionOutPointWithValue(block.getTransactions().get(0), 0);
}
public TransactionOutPointWithValue getSpendableOutput() {
return spendableOutput;
}
}
class TransactionOutPointWithValue {
public TransactionOutPoint outpoint;
public Coin value;
public Script scriptPubKey;
public TransactionOutPointWithValue(TransactionOutPoint outpoint, Coin value, Script scriptPubKey) {
this.outpoint = outpoint;
this.value = value;
this.scriptPubKey = scriptPubKey;
}
public TransactionOutPointWithValue(Transaction tx, int output) {
this(new TransactionOutPoint(tx.getParams(), output, tx.getHash()),
tx.getOutput(output).getValue(), tx.getOutput(output).getScriptPubKey());
}
}
/** An arbitrary rule which the testing client must match */
class Rule {
String ruleName;
Rule(String ruleName) {
this.ruleName = ruleName;
}
}
/**
* A test which checks the mempool state (ie defined which transactions should be in memory pool
*/
class MemoryPoolState extends Rule {
Set<InventoryItem> mempool;
public MemoryPoolState(Set<InventoryItem> mempool, String ruleName) {
super(ruleName);
this.mempool = mempool;
}
}
class RuleList {
public List<Rule> list;
public int maximumReorgBlockCount;
Map<Sha256Hash, Block> hashHeaderMap;
public RuleList(List<Rule> list, Map<Sha256Hash, Block> hashHeaderMap, int maximumReorgBlockCount) {
this.list = list;
this.hashHeaderMap = hashHeaderMap;
this.maximumReorgBlockCount = maximumReorgBlockCount;
}
}
public class FullBlockTestGenerator {
// Used by BitcoindComparisonTool and AbstractFullPrunedBlockChainTest to create test cases
private NetworkParameters params;
private ECKey coinbaseOutKey;
private byte[] coinbaseOutKeyPubKey;
// Used to double-check that we are always using the right next-height
private Map<Sha256Hash, Integer> blockToHeightMap = new HashMap<Sha256Hash, Integer>();
private Map<Sha256Hash, Block> hashHeaderMap = new HashMap<Sha256Hash, Block>();
private Map<Sha256Hash, Sha256Hash> coinbaseBlockMap = new HashMap<Sha256Hash, Sha256Hash>();
public FullBlockTestGenerator(NetworkParameters params) {
this.params = params;
coinbaseOutKey = new ECKey();
coinbaseOutKeyPubKey = coinbaseOutKey.getPubKey();
Utils.setMockClock();
}
public RuleList getBlocksToTest(boolean runBarelyExpensiveTests, boolean runExpensiveTests, File blockStorageFile) throws ScriptException, ProtocolException, IOException {
final FileOutputStream outStream = blockStorageFile != null ? new FileOutputStream(blockStorageFile) : null;
final Script OP_TRUE_SCRIPT = new ScriptBuilder().op(OP_TRUE).build();
final Script OP_NOP_SCRIPT = new ScriptBuilder().op(OP_NOP).build();
// TODO: Rename this variable.
List<Rule> blocks = new LinkedList<Rule>() {
@Override
public boolean add(Rule element) {
if (outStream != null && element instanceof BlockAndValidity) {
try {
outStream.write((int) (params.getPacketMagic() >>> 24));
outStream.write((int) (params.getPacketMagic() >>> 16));
outStream.write((int) (params.getPacketMagic() >>> 8));
outStream.write((int) (params.getPacketMagic() >>> 0));
byte[] block = ((BlockAndValidity)element).block.bitcoinSerialize();
byte[] length = new byte[4];
Utils.uint32ToByteArrayBE(block.length, length, 0);
outStream.write(Utils.reverseBytes(length));
outStream.write(block);
((BlockAndValidity)element).block = null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return super.add(element);
}
};
RuleList ret = new RuleList(blocks, hashHeaderMap, 10);
Queue<TransactionOutPointWithValue> spendableOutputs = new LinkedList<TransactionOutPointWithValue>();
int chainHeadHeight = 1;
Block chainHead = params.getGenesisBlock().createNextBlockWithCoinbase(coinbaseOutKeyPubKey);
blocks.add(new BlockAndValidity(chainHead, true, false, chainHead.getHash(), 1, "Initial Block"));
spendableOutputs.offer(new TransactionOutPointWithValue(
new TransactionOutPoint(params, 0, chainHead.getTransactions().get(0).getHash()),
FIFTY_COINS, chainHead.getTransactions().get(0).getOutputs().get(0).getScriptPubKey()));
for (int i = 1; i < params.getSpendableCoinbaseDepth(); i++) {
chainHead = chainHead.createNextBlockWithCoinbase(coinbaseOutKeyPubKey);
chainHeadHeight++;
blocks.add(new BlockAndValidity(chainHead, true, false, chainHead.getHash(), i+1, "Initial Block chain output generation"));
spendableOutputs.offer(new TransactionOutPointWithValue(
new TransactionOutPoint(params, 0, chainHead.getTransactions().get(0).getHash()),
FIFTY_COINS, chainHead.getTransactions().get(0).getOutputs().get(0).getScriptPubKey()));
}
// Start by building a couple of blocks on top of the genesis block.
NewBlock b1 = createNextBlock(chainHead, chainHeadHeight + 1, spendableOutputs.poll(), null);
blocks.add(new BlockAndValidity(b1, true, false, b1.getHash(), chainHeadHeight + 1, "b1"));
spendableOutputs.offer(b1.getCoinbaseOutput());
TransactionOutPointWithValue out1 = spendableOutputs.poll(); checkState(out1 != null);
NewBlock b2 = createNextBlock(b1, chainHeadHeight + 2, out1, null);
blocks.add(new BlockAndValidity(b2, true, false, b2.getHash(), chainHeadHeight + 2, "b2"));
// Make sure nothing funky happens if we try to re-add b2
blocks.add(new BlockAndValidity(b2, true, false, b2.getHash(), chainHeadHeight + 2, "b2"));
spendableOutputs.offer(b2.getCoinbaseOutput());
// We now have the following chain (which output is spent is in parentheses):
// genesis -> b1 (0) -> b2 (1)
//
// so fork like this:
//
// genesis -> b1 (0) -> b2 (1)
// \-> b3 (1)
//
// Nothing should happen at this point. We saw b2 first so it takes priority.
NewBlock b3 = createNextBlock(b1, chainHeadHeight + 2, out1, null);
blocks.add(new BlockAndValidity(b3, true, false, b2.getHash(), chainHeadHeight + 2, "b3"));
// Make sure nothing breaks if we add b3 twice
blocks.add(new BlockAndValidity(b3, true, false, b2.getHash(), chainHeadHeight + 2, "b3"));
// Now we add another block to make the alternative chain longer.
//
// genesis -> b1 (0) -> b2 (1)
// \-> b3 (1) -> b4 (2)
//
TransactionOutPointWithValue out2 = checkNotNull(spendableOutputs.poll());
NewBlock b4 = createNextBlock(b3, chainHeadHeight + 3, out2, null);
blocks.add(new BlockAndValidity(b4, true, false, b4.getHash(), chainHeadHeight + 3, "b4"));
// ... and back to the first chain.
NewBlock b5 = createNextBlock(b2, chainHeadHeight + 3, out2, null);
blocks.add(new BlockAndValidity(b5, true, false, b4.getHash(), chainHeadHeight + 3, "b5"));
spendableOutputs.offer(b5.getCoinbaseOutput());
TransactionOutPointWithValue out3 = spendableOutputs.poll();
NewBlock b6 = createNextBlock(b5, chainHeadHeight + 4, out3, null);
blocks.add(new BlockAndValidity(b6, true, false, b6.getHash(), chainHeadHeight + 4, "b6"));
//
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b3 (1) -> b4 (2)
//
// Try to create a fork that double-spends
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b7 (2) -> b8 (4)
// \-> b3 (1) -> b4 (2)
//
NewBlock b7 = createNextBlock(b5, chainHeadHeight + 5, out2, null);
blocks.add(new BlockAndValidity(b7, true, false, b6.getHash(), chainHeadHeight + 4, "b7"));
TransactionOutPointWithValue out4 = spendableOutputs.poll();
NewBlock b8 = createNextBlock(b7, chainHeadHeight + 6, out4, null);
blocks.add(new BlockAndValidity(b8, false, true, b6.getHash(), chainHeadHeight + 4, "b8"));
// Try to create a block that has too much fee
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b9 (4)
// \-> b3 (1) -> b4 (2)
//
NewBlock b9 = createNextBlock(b6, chainHeadHeight + 5, out4, SATOSHI);
blocks.add(new BlockAndValidity(b9, false, true, b6.getHash(), chainHeadHeight + 4, "b9"));
// Create a fork that ends in a block with too much fee (the one that causes the reorg)
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b10 (3) -> b11 (4)
// \-> b3 (1) -> b4 (2)
//
NewBlock b10 = createNextBlock(b5, chainHeadHeight + 4, out3, null);
blocks.add(new BlockAndValidity(b10, true, false, b6.getHash(), chainHeadHeight + 4, "b10"));
NewBlock b11 = createNextBlock(b10, chainHeadHeight + 5, out4, SATOSHI);
blocks.add(new BlockAndValidity(b11, false, true, b6.getHash(), chainHeadHeight + 4, "b11"));
// Try again, but with a valid fork first
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b12 (3) -> b13 (4) -> b14 (5)
// (b12 added last)
// \-> b3 (1) -> b4 (2)
//
NewBlock b12 = createNextBlock(b5, chainHeadHeight + 4, out3, null);
spendableOutputs.offer(b12.getCoinbaseOutput());
NewBlock b13 = createNextBlock(b12, chainHeadHeight + 5, out4, null);
blocks.add(new BlockAndValidity(b13, false, false, b6.getHash(), chainHeadHeight + 4, "b13"));
// Make sure we dont die if an orphan gets added twice
blocks.add(new BlockAndValidity(b13, false, false, b6.getHash(), chainHeadHeight + 4, "b13"));
spendableOutputs.offer(b13.getCoinbaseOutput());
TransactionOutPointWithValue out5 = spendableOutputs.poll();
NewBlock b14 = createNextBlock(b13, chainHeadHeight + 6, out5, SATOSHI);
// This will be "validly" added, though its actually invalid, it will just be marked orphan
// and will be discarded when an attempt is made to reorg to it.
// TODO: Use a WeakReference to check that it is freed properly after the reorg
blocks.add(new BlockAndValidity(b14, false, false, b6.getHash(), chainHeadHeight + 4, "b14"));
// Make sure we dont die if an orphan gets added twice
blocks.add(new BlockAndValidity(b14, false, false, b6.getHash(), chainHeadHeight + 4, "b14"));
blocks.add(new BlockAndValidity(b12, false, true, b13.getHash(), chainHeadHeight + 5, "b12"));
// Add a block with MAX_BLOCK_SIGOPS and one with one more sigop
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b12 (3) -> b13 (4) -> b15 (5) -> b16 (6)
// \-> b3 (1) -> b4 (2)
//
NewBlock b15 = createNextBlock(b13, chainHeadHeight + 6, out5, null);
{
int sigOps = 0;
for (Transaction tx : b15.block.getTransactions())
sigOps += tx.getSigOpCount();
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[Block.MAX_BLOCK_SIGOPS - sigOps];
Arrays.fill(outputScript, (byte) OP_CHECKSIG);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b15);
b15.addTransaction(tx);
sigOps = 0;
for (Transaction tx2 : b15.block.getTransactions())
sigOps += tx2.getSigOpCount();
checkState(sigOps == Block.MAX_BLOCK_SIGOPS);
}
b15.solve();
blocks.add(new BlockAndValidity(b15, true, false, b15.getHash(), chainHeadHeight + 6, "b15"));
spendableOutputs.offer(b15.getCoinbaseOutput());
TransactionOutPointWithValue out6 = spendableOutputs.poll();
NewBlock b16 = createNextBlock(b15, chainHeadHeight + 7, out6, null);
{
int sigOps = 0;
for (Transaction tx : b16.block.getTransactions()) {
sigOps += tx.getSigOpCount();
}
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[Block.MAX_BLOCK_SIGOPS - sigOps + 1];
Arrays.fill(outputScript, (byte) OP_CHECKSIG);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b16);
b16.addTransaction(tx);
sigOps = 0;
for (Transaction tx2 : b16.block.getTransactions())
sigOps += tx2.getSigOpCount();
checkState(sigOps == Block.MAX_BLOCK_SIGOPS + 1);
}
b16.solve();
blocks.add(new BlockAndValidity(b16, false, true, b15.getHash(), chainHeadHeight + 6, "b16"));
// Attempt to spend a transaction created on a different fork
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b12 (3) -> b13 (4) -> b15 (5) -> b17 (6)
// \-> b3 (1) -> b4 (2)
//
NewBlock b17 = createNextBlock(b15, chainHeadHeight + 7, out6, null);
{
Transaction tx = new Transaction(params);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, new byte[] {}));
addOnlyInputToTransaction(tx, b3);
b17.addTransaction(tx);
}
b17.solve();
blocks.add(new BlockAndValidity(b17, false, true, b15.getHash(), chainHeadHeight + 6, "b17"));
// Attempt to spend a transaction created on a different fork (on a fork this time)
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b12 (3) -> b13 (4) -> b15 (5)
// \-> b18 (5) -> b19 (6)
// \-> b3 (1) -> b4 (2)
//
NewBlock b18 = createNextBlock(b13, chainHeadHeight + 6, out5, null);
{
Transaction tx = new Transaction(params);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, new byte[] {}));
addOnlyInputToTransaction(tx, b3);
b18.addTransaction(tx);
}
b18.solve();
blocks.add(new BlockAndValidity(b18, true, false, b15.getHash(), chainHeadHeight + 6, "b17"));
NewBlock b19 = createNextBlock(b18, chainHeadHeight + 7, out6, null);
blocks.add(new BlockAndValidity(b19, false, true, b15.getHash(), chainHeadHeight + 6, "b19"));
// Attempt to spend a coinbase at depth too low
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b12 (3) -> b13 (4) -> b15 (5) -> b20 (7)
// \-> b3 (1) -> b4 (2)
//
TransactionOutPointWithValue out7 = spendableOutputs.poll();
NewBlock b20 = createNextBlock(b15.block, chainHeadHeight + 7, out7, null);
blocks.add(new BlockAndValidity(b20, false, true, b15.getHash(), chainHeadHeight + 6, "b20"));
// Attempt to spend a coinbase at depth too low (on a fork this time)
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b12 (3) -> b13 (4) -> b15 (5)
// \-> b21 (6) -> b22 (5)
// \-> b3 (1) -> b4 (2)
//
NewBlock b21 = createNextBlock(b13, chainHeadHeight + 6, out6, null);
blocks.add(new BlockAndValidity(b21.block, true, false, b15.getHash(), chainHeadHeight + 6, "b21"));
NewBlock b22 = createNextBlock(b21, chainHeadHeight + 7, out5, null);
blocks.add(new BlockAndValidity(b22.block, false, true, b15.getHash(), chainHeadHeight + 6, "b22"));
// Create a block on either side of MAX_BLOCK_SIZE and make sure its accepted/rejected
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b12 (3) -> b13 (4) -> b15 (5) -> b23 (6)
// \-> b24 (6) -> b25 (7)
// \-> b3 (1) -> b4 (2)
//
NewBlock b23 = createNextBlock(b15, chainHeadHeight + 7, out6, null);
{
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[Block.MAX_BLOCK_SIZE - b23.block.getMessageSize() - 65];
Arrays.fill(outputScript, (byte) OP_FALSE);
tx.addOutput(new TransactionOutput(params, tx, ZERO, outputScript));
addOnlyInputToTransaction(tx, b23);
b23.addTransaction(tx);
}
b23.solve();
checkState(b23.block.getMessageSize() == Block.MAX_BLOCK_SIZE);
blocks.add(new BlockAndValidity(b23, true, false, b23.getHash(), chainHeadHeight + 7, "b23"));
spendableOutputs.offer(b23.getCoinbaseOutput());
NewBlock b24 = createNextBlock(b15, chainHeadHeight + 7, out6, null);
{
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[Block.MAX_BLOCK_SIZE - b24.block.getMessageSize() - 64];
Arrays.fill(outputScript, (byte) OP_FALSE);
tx.addOutput(new TransactionOutput(params, tx, ZERO, outputScript));
addOnlyInputToTransaction(tx, b24);
b24.addTransaction(tx);
}
b24.solve();
checkState(b24.block.getMessageSize() == Block.MAX_BLOCK_SIZE + 1);
blocks.add(new BlockAndValidity(b24, false, true, b23.getHash(), chainHeadHeight + 7, "b24"));
// Extend the b24 chain to make sure bitcoind isn't accepting b24
NewBlock b25 = createNextBlock(b24, chainHeadHeight + 8, out7, null);
blocks.add(new BlockAndValidity(b25, false, false, b23.getHash(), chainHeadHeight + 7, "b25"));
// Create blocks with a coinbase input script size out of range
// genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
// \-> b12 (3) -> b13 (4) -> b15 (5) -> b23 (6) -> b30 (7)
// \-> ... (6) -> ... (7)
// \-> b3 (1) -> b4 (2)
//
NewBlock b26 = createNextBlock(b15, chainHeadHeight + 7, out6, null);
// 1 is too small, but we already generate every other block with 2, so that is tested
b26.block.getTransactions().get(0).getInputs().get(0).setScriptBytes(new byte[] {0});
b26.block.setMerkleRoot(null);
b26.solve();
blocks.add(new BlockAndValidity(b26, false, true, b23.getHash(), chainHeadHeight + 7, "b26"));
// Extend the b26 chain to make sure bitcoind isn't accepting b26
NewBlock b27 = createNextBlock(b26, chainHeadHeight + 8, out7, null);
blocks.add(new BlockAndValidity(b27, false, false, b23.getHash(), chainHeadHeight + 7, "b27"));
NewBlock b28 = createNextBlock(b15, chainHeadHeight + 7, out6, null);
{
byte[] coinbase = new byte[101];
Arrays.fill(coinbase, (byte)0);
b28.block.getTransactions().get(0).getInputs().get(0).setScriptBytes(coinbase);
}
b28.block.setMerkleRoot(null);
b28.solve();
blocks.add(new BlockAndValidity(b28, false, true, b23.getHash(), chainHeadHeight + 7, "b28"));
// Extend the b28 chain to make sure bitcoind isn't accepting b28
NewBlock b29 = createNextBlock(b28, chainHeadHeight + 8, out7, null);
blocks.add(new BlockAndValidity(b29, false, false, b23.getHash(), chainHeadHeight + 7, "b29"));
NewBlock b30 = createNextBlock(b23, chainHeadHeight + 8, out7, null);
{
byte[] coinbase = new byte[100];
Arrays.fill(coinbase, (byte)0);
b30.block.getTransactions().get(0).getInputs().get(0).setScriptBytes(coinbase);
}
b30.block.setMerkleRoot(null);
b30.solve();
blocks.add(new BlockAndValidity(b30, true, false, b30.getHash(), chainHeadHeight + 8, "b30"));
spendableOutputs.offer(b30.getCoinbaseOutput());
// Check sigops of OP_CHECKMULTISIG/OP_CHECKMULTISIGVERIFY/OP_CHECKSIGVERIFY
// 6 (3)
// 12 (3) -> b13 (4) -> b15 (5) -> b23 (6) -> b30 (7) -> b31 (8) -> b33 (9) -> b35 (10)
// \-> b36 (11)
// \-> b34 (10)
// \-> b32 (9)
//
TransactionOutPointWithValue out8 = spendableOutputs.poll();
NewBlock b31 = createNextBlock(b30, chainHeadHeight + 9, out8, null);
{
int sigOps = 0;
for (Transaction tx : b31.block.transactions) {
sigOps += tx.getSigOpCount();
}
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[(Block.MAX_BLOCK_SIGOPS - sigOps)/20];
Arrays.fill(outputScript, (byte) OP_CHECKMULTISIG);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b31);
b31.addTransaction(tx);
}
b31.solve();
blocks.add(new BlockAndValidity(b31, true, false, b31.getHash(), chainHeadHeight + 9, "b31"));
spendableOutputs.offer(b31.getCoinbaseOutput());
TransactionOutPointWithValue out9 = spendableOutputs.poll();
NewBlock b32 = createNextBlock(b31, chainHeadHeight + 10, out9, null);
{
int sigOps = 0;
for (Transaction tx : b32.block.transactions) {
sigOps += tx.getSigOpCount();
}
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[(Block.MAX_BLOCK_SIGOPS - sigOps)/20 + (Block.MAX_BLOCK_SIGOPS - sigOps)%20 + 1];
Arrays.fill(outputScript, (byte) OP_CHECKMULTISIG);
for (int i = 0; i < (Block.MAX_BLOCK_SIGOPS - sigOps)%20; i++)
outputScript[i] = (byte) OP_CHECKSIG;
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b32);
b32.addTransaction(tx);
}
b32.solve();
blocks.add(new BlockAndValidity(b32, false, true, b31.getHash(), chainHeadHeight + 9, "b32"));
NewBlock b33 = createNextBlock(b31, chainHeadHeight + 10, out9, null);
{
int sigOps = 0;
for (Transaction tx : b33.block.transactions) {
sigOps += tx.getSigOpCount();
}
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[(Block.MAX_BLOCK_SIGOPS - sigOps)/20];
Arrays.fill(outputScript, (byte) OP_CHECKMULTISIGVERIFY);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b33);
b33.addTransaction(tx);
}
b33.solve();
blocks.add(new BlockAndValidity(b33, true, false, b33.getHash(), chainHeadHeight + 10, "b33"));
spendableOutputs.offer(b33.getCoinbaseOutput());
TransactionOutPointWithValue out10 = spendableOutputs.poll();
NewBlock b34 = createNextBlock(b33, chainHeadHeight + 11, out10, null);
{
int sigOps = 0;
for (Transaction tx : b34.block.getTransactions()) {
sigOps += tx.getSigOpCount();
}
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[(Block.MAX_BLOCK_SIGOPS - sigOps)/20 + (Block.MAX_BLOCK_SIGOPS - sigOps)%20 + 1];
Arrays.fill(outputScript, (byte) OP_CHECKMULTISIGVERIFY);
for (int i = 0; i < (Block.MAX_BLOCK_SIGOPS - sigOps)%20; i++)
outputScript[i] = (byte) OP_CHECKSIG;
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b34);
b34.addTransaction(tx);
}
b34.solve();
blocks.add(new BlockAndValidity(b34, false, true, b33.getHash(), chainHeadHeight + 10, "b34"));
NewBlock b35 = createNextBlock(b33, chainHeadHeight + 11, out10, null);
{
int sigOps = 0;
for (Transaction tx : b35.block.getTransactions()) {
sigOps += tx.getSigOpCount();
}
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[Block.MAX_BLOCK_SIGOPS - sigOps];
Arrays.fill(outputScript, (byte) OP_CHECKSIGVERIFY);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b35);
b35.addTransaction(tx);
}
b35.solve();
blocks.add(new BlockAndValidity(b35, true, false, b35.getHash(), chainHeadHeight + 11, "b35"));
spendableOutputs.offer(b35.getCoinbaseOutput());
TransactionOutPointWithValue out11 = spendableOutputs.poll();
NewBlock b36 = createNextBlock(b35, chainHeadHeight + 12, out11, null);
{
int sigOps = 0;
for (Transaction tx : b36.block.getTransactions()) {
sigOps += tx.getSigOpCount();
}
Transaction tx = new Transaction(params);
byte[] outputScript = new byte[Block.MAX_BLOCK_SIGOPS - sigOps + 1];
Arrays.fill(outputScript, (byte) OP_CHECKSIGVERIFY);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
addOnlyInputToTransaction(tx, b36);
b36.addTransaction(tx);
}
b36.solve();
blocks.add(new BlockAndValidity(b36, false, true, b35.getHash(), chainHeadHeight + 11, "b36"));
// Check spending of a transaction in a block which failed to connect
// (test block store transaction abort handling, not that it should get this far if that's broken...)
// 6 (3)
// 12 (3) -> b13 (4) -> b15 (5) -> b23 (6) -> b30 (7) -> b31 (8) -> b33 (9) -> b35 (10)
// \-> b37 (11)
// \-> b38 (11)
//
NewBlock b37 = createNextBlock(b35, chainHeadHeight + 12, out11, null);
{
Transaction tx = new Transaction(params);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, new byte[] {}));
addOnlyInputToTransaction(tx, out11); // double spend out11
b37.addTransaction(tx);
}
b37.solve();
blocks.add(new BlockAndValidity(b37, false, true, b35.getHash(), chainHeadHeight + 11, "b37"));
NewBlock b38 = createNextBlock(b35, chainHeadHeight + 12, out11, null);
{
Transaction tx = new Transaction(params);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, new byte[] {}));
// Attempt to spend b37's first non-coinbase tx, at which point b37 was still considered valid
addOnlyInputToTransaction(tx, b37);
b38.addTransaction(tx);
}
b38.solve();
blocks.add(new BlockAndValidity(b38, false, true, b35.getHash(), chainHeadHeight + 11, "b38"));
// Check P2SH SigOp counting
// 13 (4) -> b15 (5) -> b23 (6) -> b30 (7) -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b41 (12)
// \-> b40 (12)
//
// Create some P2SH outputs that will require 6 sigops to spend
byte[] b39p2shScriptPubKey;
int b39numP2SHOutputs = 0, b39sigOpsPerOutput = 6;
NewBlock b39 = createNextBlock(b35, chainHeadHeight + 12, null, null);
{
ByteArrayOutputStream p2shScriptPubKey = new UnsafeByteArrayOutputStream();
try {
Script.writeBytes(p2shScriptPubKey, coinbaseOutKeyPubKey);
p2shScriptPubKey.write(OP_2DUP);
p2shScriptPubKey.write(OP_CHECKSIGVERIFY);
p2shScriptPubKey.write(OP_2DUP);
p2shScriptPubKey.write(OP_CHECKSIGVERIFY);
p2shScriptPubKey.write(OP_2DUP);
p2shScriptPubKey.write(OP_CHECKSIGVERIFY);
p2shScriptPubKey.write(OP_2DUP);
p2shScriptPubKey.write(OP_CHECKSIGVERIFY);
p2shScriptPubKey.write(OP_2DUP);
p2shScriptPubKey.write(OP_CHECKSIGVERIFY);
p2shScriptPubKey.write(OP_CHECKSIG);
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen.
}
b39p2shScriptPubKey = p2shScriptPubKey.toByteArray();
byte[] scriptHash = Utils.sha256hash160(b39p2shScriptPubKey);
UnsafeByteArrayOutputStream scriptPubKey = new UnsafeByteArrayOutputStream(scriptHash.length + 3);
scriptPubKey.write(OP_HASH160);
try {
Script.writeBytes(scriptPubKey, scriptHash);
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen.
}
scriptPubKey.write(OP_EQUAL);
Coin lastOutputValue = out11.value.subtract(SATOSHI);
TransactionOutPoint lastOutPoint;
{
Transaction tx = new Transaction(params);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, scriptPubKey.toByteArray()));
tx.addOutput(new TransactionOutput(params, tx, lastOutputValue, new byte[]{OP_1}));
addOnlyInputToTransaction(tx, out11);
lastOutPoint = new TransactionOutPoint(params, 1, tx.getHash());
b39.addTransaction(tx);
}
b39numP2SHOutputs++;
while (b39.block.getMessageSize() < Block.MAX_BLOCK_SIZE)
{
Transaction tx = new Transaction(params);
lastOutputValue = lastOutputValue.subtract(SATOSHI);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, scriptPubKey.toByteArray()));
tx.addOutput(new TransactionOutput(params, tx, lastOutputValue, new byte[]{OP_1}));
tx.addInput(new TransactionInput(params, tx, new byte[]{OP_1}, lastOutPoint));
lastOutPoint = new TransactionOutPoint(params, 1, tx.getHash());
if (b39.block.getMessageSize() + tx.getMessageSize() < Block.MAX_BLOCK_SIZE) {
b39.addTransaction(tx);
b39numP2SHOutputs++;
} else
break;
}
}
b39.solve();
blocks.add(new BlockAndValidity(b39, true, false, b39.getHash(), chainHeadHeight + 12, "b39"));
spendableOutputs.offer(b39.getCoinbaseOutput());
TransactionOutPointWithValue out12 = spendableOutputs.poll();
NewBlock b40 = createNextBlock(b39, chainHeadHeight + 13, out12, null);
{
int sigOps = 0;
for (Transaction tx : b40.block.getTransactions()) {
sigOps += tx.getSigOpCount();
}
int numTxes = (Block.MAX_BLOCK_SIGOPS - sigOps) / b39sigOpsPerOutput;
checkState(numTxes <= b39numP2SHOutputs);
TransactionOutPoint lastOutPoint = new TransactionOutPoint(params, 1, b40.block.getTransactions().get(1).getHash());
byte[] scriptSig = null;
for (int i = 1; i <= numTxes; i++) {
Transaction tx = new Transaction(params);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, new byte[] {OP_1}));
tx.addInput(new TransactionInput(params, tx, new byte[]{OP_1}, lastOutPoint));
TransactionInput input = new TransactionInput(params, tx, new byte[]{},
new TransactionOutPoint(params, 0, b39.block.getTransactions().get(i).getHash()));
tx.addInput(input);
if (scriptSig == null) {
// Exploit the SigHash.SINGLE bug to avoid having to make more than one signature
Sha256Hash hash = tx.hashForSignature(1, b39p2shScriptPubKey, SigHash.SINGLE, false);
// Sign input
try {
ByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(73);
bos.write(coinbaseOutKey.sign(hash).encodeToDER());
bos.write(SigHash.SINGLE.ordinal() + 1);
byte[] signature = bos.toByteArray();
ByteArrayOutputStream scriptSigBos = new UnsafeByteArrayOutputStream(signature.length + b39p2shScriptPubKey.length + 3);
Script.writeBytes(scriptSigBos, new byte[] {(byte) OP_CHECKSIG});
scriptSigBos.write(Script.createInputScript(signature));
Script.writeBytes(scriptSigBos, b39p2shScriptPubKey);
scriptSig = scriptSigBos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
input.setScriptBytes(scriptSig);
lastOutPoint = new TransactionOutPoint(params, 0, tx.getHash());
b40.addTransaction(tx);
}
sigOps += numTxes * b39sigOpsPerOutput;
Transaction tx = new Transaction(params);
tx.addInput(new TransactionInput(params, tx, new byte[]{OP_1}, lastOutPoint));
byte[] scriptPubKey = new byte[Block.MAX_BLOCK_SIGOPS - sigOps + 1];
Arrays.fill(scriptPubKey, (byte) OP_CHECKSIG);
tx.addOutput(new TransactionOutput(params, tx, ZERO, scriptPubKey));
b40.addTransaction(tx);
}
b40.solve();
blocks.add(new BlockAndValidity(b40, false, true, b39.getHash(), chainHeadHeight + 12, "b40"));
NewBlock b41 = null;
if (runBarelyExpensiveTests) {
b41 = createNextBlock(b39, chainHeadHeight + 13, out12, null);
{
int sigOps = 0;
for (Transaction tx : b41.block.getTransactions()) {
sigOps += tx.getSigOpCount();
}
int numTxes = (Block.MAX_BLOCK_SIGOPS - sigOps)
/ b39sigOpsPerOutput;
checkState(numTxes <= b39numP2SHOutputs);
TransactionOutPoint lastOutPoint = new TransactionOutPoint(
params, 1, b41.block.getTransactions().get(1).getHash());
byte[] scriptSig = null;
for (int i = 1; i <= numTxes; i++) {
Transaction tx = new Transaction(params);
tx.addOutput(new TransactionOutput(params, tx, Coin
.SATOSHI, new byte[] {OP_1}));
tx.addInput(new TransactionInput(params, tx,
new byte[] {OP_1}, lastOutPoint));
TransactionInput input = new TransactionInput(params, tx,
new byte[] {}, new TransactionOutPoint(params, 0,
b39.block.getTransactions().get(i).getHash()));
tx.addInput(input);
if (scriptSig == null) {
// Exploit the SigHash.SINGLE bug to avoid having to make more than one signature
Sha256Hash hash = tx.hashForSignature(1,
b39p2shScriptPubKey, SigHash.SINGLE, false);
// Sign input
try {
ByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(
73);
bos.write(coinbaseOutKey.sign(hash).encodeToDER());
bos.write(SigHash.SINGLE.ordinal() + 1);
byte[] signature = bos.toByteArray();
ByteArrayOutputStream scriptSigBos = new UnsafeByteArrayOutputStream(
signature.length
+ b39p2shScriptPubKey.length + 3);
Script.writeBytes(scriptSigBos,
new byte[] { (byte) OP_CHECKSIG});
scriptSigBos.write(Script
.createInputScript(signature));
Script.writeBytes(scriptSigBos, b39p2shScriptPubKey);
scriptSig = scriptSigBos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
input.setScriptBytes(scriptSig);
lastOutPoint = new TransactionOutPoint(params, 0,
tx.getHash());
b41.addTransaction(tx);
}
sigOps += numTxes * b39sigOpsPerOutput;
Transaction tx = new Transaction(params);
tx.addInput(new TransactionInput(params, tx,
new byte[] {OP_1}, lastOutPoint));
byte[] scriptPubKey = new byte[Block.MAX_BLOCK_SIGOPS - sigOps];
Arrays.fill(scriptPubKey, (byte) OP_CHECKSIG);
tx.addOutput(new TransactionOutput(params, tx, ZERO, scriptPubKey));
b41.addTransaction(tx);
}
b41.solve();
blocks.add(new BlockAndValidity(b41, true, false, b41.getHash(), chainHeadHeight + 13, "b41"));
}
// Fork off of b39 to create a constant base again
// b23 (6) -> b30 (7) -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13)
// \-> b41 (12)
//
NewBlock b42 = createNextBlock(b39, chainHeadHeight + 13, out12, null);
blocks.add(new BlockAndValidity(b42, true, false, b41 == null ? b42.getHash() : b41.getHash(), chainHeadHeight + 13, "b42"));
spendableOutputs.offer(b42.getCoinbaseOutput());
TransactionOutPointWithValue out13 = spendableOutputs.poll();
NewBlock b43 = createNextBlock(b42, chainHeadHeight + 14, out13, null);
blocks.add(new BlockAndValidity(b43, true, false, b43.getHash(), chainHeadHeight + 14, "b43"));
spendableOutputs.offer(b43.getCoinbaseOutput());
// Test a number of really invalid scenarios
// -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13) -> b44 (14)
// \-> ??? (15)
//
TransactionOutPointWithValue out14 = spendableOutputs.poll();
// A valid block created exactly like b44 to make sure the creation itself works
Block b44 = new Block(params);
byte[] outScriptBytes = ScriptBuilder.createOutputScript(ECKey.fromPublicOnly(coinbaseOutKeyPubKey)).getProgram();
{
b44.setDifficultyTarget(b43.block.getDifficultyTarget());
b44.addCoinbaseTransaction(coinbaseOutKeyPubKey, ZERO);
Transaction t = new Transaction(params);
// Entirely invalid scriptPubKey to ensure we aren't pre-verifying too much
t.addOutput(new TransactionOutput(params, t, ZERO, new byte[] {OP_PUSHDATA1 - 1 }));
t.addOutput(new TransactionOutput(params, t, SATOSHI, outScriptBytes));
// Spendable output
t.addOutput(new TransactionOutput(params, t, ZERO, new byte[] {OP_1}));
addOnlyInputToTransaction(t, out14);
b44.addTransaction(t);
b44.setPrevBlockHash(b43.getHash());
b44.setTime(b43.block.getTimeSeconds() + 1);
}
b44.solve();
blocks.add(new BlockAndValidity(b44, true, false, b44.getHash(), chainHeadHeight + 15, "b44"));
TransactionOutPointWithValue out15 = spendableOutputs.poll();
// A block with a non-coinbase as the first tx
Block b45 = new Block(params);
{
b45.setDifficultyTarget(b44.getDifficultyTarget());
//b45.addCoinbaseTransaction(pubKey, coinbaseValue);
Transaction t = new Transaction(params);
// Entirely invalid scriptPubKey to ensure we aren't pre-verifying too much
t.addOutput(new TransactionOutput(params, t, ZERO, new byte[] {OP_PUSHDATA1 - 1 }));
t.addOutput(new TransactionOutput(params, t, SATOSHI, outScriptBytes));
// Spendable output
t.addOutput(new TransactionOutput(params, t, ZERO, new byte[] {OP_1}));
addOnlyInputToTransaction(t, out15);
try {
b45.addTransaction(t);
} catch (RuntimeException e) { } // Should happen
if (b45.getTransactions().size() > 0)
throw new RuntimeException("addTransaction doesn't properly check for adding a non-coinbase as first tx");
b45.addTransaction(t, false);
b45.setPrevBlockHash(b44.getHash());
b45.setTime(b44.getTimeSeconds() + 1);
}
b45.solve();
blocks.add(new BlockAndValidity(b45, false, true, b44.getHash(), chainHeadHeight + 15, "b45"));
// A block with no txn
Block b46 = new Block(params);
{
b46.transactions = new ArrayList<Transaction>();
b46.setDifficultyTarget(b44.getDifficultyTarget());
b46.setMerkleRoot(Sha256Hash.ZERO_HASH);
b46.setPrevBlockHash(b44.getHash());
b46.setTime(b44.getTimeSeconds() + 1);
}
b46.solve();
blocks.add(new BlockAndValidity(b46, false, true, b44.getHash(), chainHeadHeight + 15, "b46"));
// A block with invalid work
NewBlock b47 = createNextBlock(b44, chainHeadHeight + 16, out15, null);
{
try {
// Inverse solve
BigInteger target = b47.block.getDifficultyTargetAsInteger();
while (true) {
BigInteger h = b47.getHash().toBigInteger();
if (h.compareTo(target) > 0) // if invalid
break;
// increment the nonce and try again.
b47.block.setNonce(b47.block.getNonce() + 1);
}
} catch (VerificationException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
blocks.add(new BlockAndValidity(b47, false, true, b44.getHash(), chainHeadHeight + 15, "b47"));
// Block with timestamp > 2h in the future
NewBlock b48 = createNextBlock(b44, chainHeadHeight + 16, out15, null);
b48.block.setTime(Utils.currentTimeSeconds() + 60 * 60 * 3);
b48.solve();
blocks.add(new BlockAndValidity(b48, false, true, b44.getHash(), chainHeadHeight + 15, "b48"));
// Block with invalid merkle hash
NewBlock b49 = createNextBlock(b44, chainHeadHeight + 16, out15, null);
byte[] b49MerkleHash = Sha256Hash.ZERO_HASH.getBytes().clone();
b49MerkleHash[1] = (byte) 0xDE;
b49.block.setMerkleRoot(Sha256Hash.create(b49MerkleHash));
b49.solve();
blocks.add(new BlockAndValidity(b49, false, true, b44.getHash(), chainHeadHeight + 15, "b49"));
// Block with incorrect POW limit
NewBlock b50 = createNextBlock(b44, chainHeadHeight + 16, out15, null);
{
long diffTarget = b44.getDifficultyTarget();
diffTarget &= 0xFFBFFFFF; // Make difficulty one bit harder
b50.block.setDifficultyTarget(diffTarget);
}
b50.solve();
blocks.add(new BlockAndValidity(b50, false, true, b44.getHash(), chainHeadHeight + 15, "b50"));
// A block with two coinbase txn
NewBlock b51 = createNextBlock(b44, chainHeadHeight + 16, out15, null);
{
Transaction coinbase = new Transaction(params);
coinbase.addInput(new TransactionInput(params, coinbase, new byte[]{(byte) 0xff, 110, 1}));
coinbase.addOutput(new TransactionOutput(params, coinbase, SATOSHI, outScriptBytes));
b51.block.addTransaction(coinbase, false);
}
b51.solve();
blocks.add(new BlockAndValidity(b51, false, true, b44.getHash(), chainHeadHeight + 15, "b51"));
// A block with duplicate txn
NewBlock b52 = createNextBlock(b44, chainHeadHeight + 16, out15, null);
{
Transaction tx = new Transaction(params);
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, new byte[] {}));
addOnlyInputToTransaction(tx, b52);
b52.addTransaction(tx);
b52.addTransaction(tx);
}
b52.solve();
blocks.add(new BlockAndValidity(b52, false, true, b44.getHash(), chainHeadHeight + 15, "b52"));
// Test block timestamp
// -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15)
// \-> b54 (15)
// \-> b44 (14)
//
NewBlock b53 = createNextBlock(b43, chainHeadHeight + 15, out14, null);
blocks.add(new BlockAndValidity(b53, true, false, b44.getHash(), chainHeadHeight + 15, "b53"));
spendableOutputs.offer(b53.getCoinbaseOutput());
// Block with invalid timestamp
NewBlock b54 = createNextBlock(b53, chainHeadHeight + 16, out15, null);
b54.block.setTime(b35.block.getTimeSeconds() - 1);
b54.solve();
blocks.add(new BlockAndValidity(b54, false, true, b44.getHash(), chainHeadHeight + 15, "b54"));
// Block with valid timestamp
NewBlock b55 = createNextBlock(b53, chainHeadHeight + 16, out15, null);
b55.block.setTime(b35.block.getTimeSeconds());
b55.solve();
blocks.add(new BlockAndValidity(b55, true, false, b55.getHash(), chainHeadHeight + 16, "b55"));
spendableOutputs.offer(b55.getCoinbaseOutput());