7
7
import struct
8
8
import time
9
9
10
- from test_framework .blocktools import create_block , create_coinbase , create_transaction , get_legacy_sigopcount_block
10
+ from test_framework .blocktools import create_block , create_coinbase , create_tx_with_script , get_legacy_sigopcount_block
11
11
from test_framework .key import CECKey
12
12
from test_framework .messages import (
13
13
CBlock ,
48
48
49
49
MAX_BLOCK_SIGOPS = 20000
50
50
51
- class PreviousSpendableOutput ():
52
- def __init__ (self , tx = CTransaction (), n = - 1 ):
53
- self .tx = tx
54
- self .n = n # the output we're spending
55
-
56
51
# Use this class for tests that require behavior other than normal "mininode" behavior.
57
52
# For now, it is used to serialize a bloated varint (b64).
58
53
class CBrokenBlock (CBlock ):
@@ -132,7 +127,7 @@ def run_test(self):
132
127
self .log .info ("Don't reorg to a chain of the same length" )
133
128
self .move_tip (1 )
134
129
b3 = self .next_block (3 , spend = out [1 ])
135
- txout_b3 = PreviousSpendableOutput ( b3 .vtx [1 ], 0 )
130
+ txout_b3 = b3 .vtx [1 ]
136
131
self .sync_blocks ([b3 ], False )
137
132
138
133
# Now we add another block to make the alternative chain longer.
@@ -397,8 +392,8 @@ def run_test(self):
397
392
self .log .info ("Reject a block spending transaction from a block which failed to connect" )
398
393
self .move_tip (35 )
399
394
b37 = self .next_block (37 , spend = out [11 ])
400
- txout_b37 = PreviousSpendableOutput ( b37 .vtx [1 ], 0 )
401
- tx = self .create_and_sign_transaction (out [11 ]. tx , out [ 11 ]. n , 0 )
395
+ txout_b37 = b37 .vtx [1 ]
396
+ tx = self .create_and_sign_transaction (out [11 ], 0 )
402
397
b37 = self .update_block (37 , [tx ])
403
398
self .sync_blocks ([b37 ], False , 16 , b'bad-txns-inputs-missingorspent' , reconnect = True )
404
399
@@ -432,9 +427,9 @@ def run_test(self):
432
427
# Create a transaction that spends one satoshi to the p2sh_script, the rest to OP_TRUE
433
428
# This must be signed because it is spending a coinbase
434
429
spend = out [11 ]
435
- tx = self .create_tx (spend . tx , spend . n , 1 , p2sh_script )
436
- tx .vout .append (CTxOut (spend .tx . vout [spend . n ].nValue - 1 , CScript ([OP_TRUE ])))
437
- self .sign_tx (tx , spend . tx , spend . n )
430
+ tx = self .create_tx (spend , 0 , 1 , p2sh_script )
431
+ tx .vout .append (CTxOut (spend .vout [0 ].nValue - 1 , CScript ([OP_TRUE ])))
432
+ self .sign_tx (tx , spend )
438
433
tx .rehash ()
439
434
b39 = self .update_block (39 , [tx ])
440
435
b39_outputs += 1
@@ -548,7 +543,7 @@ def run_test(self):
548
543
self .sync_blocks ([b44 ], True )
549
544
550
545
self .log .info ("Reject a block with a non-coinbase as the first tx" )
551
- non_coinbase = self .create_tx (out [15 ]. tx , out [ 15 ]. n , 1 )
546
+ non_coinbase = self .create_tx (out [15 ], 0 , 1 )
552
547
b45 = CBlock ()
553
548
b45 .nTime = self .tip .nTime + 1
554
549
b45 .hashPrevBlock = self .tip .sha256
@@ -675,7 +670,7 @@ def run_test(self):
675
670
# b57 - a good block with 2 txs, don't submit until end
676
671
self .move_tip (55 )
677
672
b57 = self .next_block (57 )
678
- tx = self .create_and_sign_transaction (out [16 ]. tx , out [ 16 ]. n , 1 )
673
+ tx = self .create_and_sign_transaction (out [16 ], 1 )
679
674
tx1 = self .create_tx (tx , 0 , 1 )
680
675
b57 = self .update_block (57 , [tx , tx1 ])
681
676
@@ -692,7 +687,7 @@ def run_test(self):
692
687
# b57p2 - a good block with 6 tx'es, don't submit until end
693
688
self .move_tip (55 )
694
689
b57p2 = self .next_block ("57p2" )
695
- tx = self .create_and_sign_transaction (out [16 ]. tx , out [ 16 ]. n , 1 )
690
+ tx = self .create_and_sign_transaction (out [16 ], 1 )
696
691
tx1 = self .create_tx (tx , 0 , 1 )
697
692
tx2 = self .create_tx (tx1 , 0 , 1 )
698
693
tx3 = self .create_tx (tx2 , 0 , 1 )
@@ -727,8 +722,8 @@ def run_test(self):
727
722
self .move_tip (57 )
728
723
b58 = self .next_block (58 , spend = out [17 ])
729
724
tx = CTransaction ()
730
- assert (len (out [17 ].tx . vout ) < 42 )
731
- tx .vin .append (CTxIn (COutPoint (out [17 ].tx . sha256 , 42 ), CScript ([OP_TRUE ]), 0xffffffff ))
725
+ assert (len (out [17 ].vout ) < 42 )
726
+ tx .vin .append (CTxIn (COutPoint (out [17 ].sha256 , 42 ), CScript ([OP_TRUE ]), 0xffffffff ))
732
727
tx .vout .append (CTxOut (0 , b"" ))
733
728
tx .calc_sha256 ()
734
729
b58 = self .update_block (58 , [tx ])
@@ -738,7 +733,7 @@ def run_test(self):
738
733
self .log .info ("Reject a block with a transaction with outputs > inputs" )
739
734
self .move_tip (57 )
740
735
b59 = self .next_block (59 )
741
- tx = self .create_and_sign_transaction (out [17 ]. tx , out [ 17 ]. n , 51 * COIN )
736
+ tx = self .create_and_sign_transaction (out [17 ], 51 * COIN )
742
737
b59 = self .update_block (59 , [tx ])
743
738
self .sync_blocks ([b59 ], False , 16 , b'bad-txns-in-belowout' , reconnect = True )
744
739
@@ -776,8 +771,7 @@ def run_test(self):
776
771
b62 = self .next_block (62 )
777
772
tx = CTransaction ()
778
773
tx .nLockTime = 0xffffffff # this locktime is non-final
779
- assert (out [18 ].n < len (out [18 ].tx .vout ))
780
- tx .vin .append (CTxIn (COutPoint (out [18 ].tx .sha256 , out [18 ].n ))) # don't set nSequence
774
+ tx .vin .append (CTxIn (COutPoint (out [18 ].sha256 , 0 ))) # don't set nSequence
781
775
tx .vout .append (CTxOut (0 , CScript ([OP_TRUE ])))
782
776
assert (tx .vin [0 ].nSequence < 0xffffffff )
783
777
tx .calc_sha256 ()
@@ -856,8 +850,8 @@ def run_test(self):
856
850
self .log .info ("Accept a block with a transaction spending an output created in the same block" )
857
851
self .move_tip (64 )
858
852
b65 = self .next_block (65 )
859
- tx1 = self .create_and_sign_transaction (out [19 ]. tx , out [19 ]. n , out [ 19 ]. tx .vout [0 ].nValue )
860
- tx2 = self .create_and_sign_transaction (tx1 , 0 , 0 )
853
+ tx1 = self .create_and_sign_transaction (out [19 ], out [19 ].vout [0 ].nValue )
854
+ tx2 = self .create_and_sign_transaction (tx1 , 0 )
861
855
b65 = self .update_block (65 , [tx1 , tx2 ])
862
856
self .sync_blocks ([b65 ], True )
863
857
self .save_spendable_output ()
@@ -869,8 +863,8 @@ def run_test(self):
869
863
self .log .info ("Reject a block with a transaction spending an output created later in the same block" )
870
864
self .move_tip (65 )
871
865
b66 = self .next_block (66 )
872
- tx1 = self .create_and_sign_transaction (out [20 ]. tx , out [20 ]. n , out [ 20 ]. tx .vout [0 ].nValue )
873
- tx2 = self .create_and_sign_transaction (tx1 , 0 , 1 )
866
+ tx1 = self .create_and_sign_transaction (out [20 ], out [20 ].vout [0 ].nValue )
867
+ tx2 = self .create_and_sign_transaction (tx1 , 1 )
874
868
b66 = self .update_block (66 , [tx2 , tx1 ])
875
869
self .sync_blocks ([b66 ], False , 16 , b'bad-txns-inputs-missingorspent' , reconnect = True )
876
870
@@ -883,9 +877,9 @@ def run_test(self):
883
877
self .log .info ("Reject a block with a transaction double spending a transaction creted in the same block" )
884
878
self .move_tip (65 )
885
879
b67 = self .next_block (67 )
886
- tx1 = self .create_and_sign_transaction (out [20 ]. tx , out [20 ]. n , out [ 20 ]. tx .vout [0 ].nValue )
887
- tx2 = self .create_and_sign_transaction (tx1 , 0 , 1 )
888
- tx3 = self .create_and_sign_transaction (tx1 , 0 , 2 )
880
+ tx1 = self .create_and_sign_transaction (out [20 ], out [20 ].vout [0 ].nValue )
881
+ tx2 = self .create_and_sign_transaction (tx1 , 1 )
882
+ tx3 = self .create_and_sign_transaction (tx1 , 2 )
889
883
b67 = self .update_block (67 , [tx1 , tx2 , tx3 ])
890
884
self .sync_blocks ([b67 ], False , 16 , b'bad-txns-inputs-missingorspent' , reconnect = True )
891
885
@@ -904,14 +898,14 @@ def run_test(self):
904
898
self .log .info ("Reject a block trying to claim too much subsidy in the coinbase transaction" )
905
899
self .move_tip (65 )
906
900
b68 = self .next_block (68 , additional_coinbase_value = 10 )
907
- tx = self .create_and_sign_transaction (out [20 ]. tx , out [20 ]. n , out [ 20 ]. tx .vout [0 ].nValue - 9 )
901
+ tx = self .create_and_sign_transaction (out [20 ], out [20 ].vout [0 ].nValue - 9 )
908
902
b68 = self .update_block (68 , [tx ])
909
903
self .sync_blocks ([b68 ], False , 16 , b'bad-cb-amount' , reconnect = True )
910
904
911
905
self .log .info ("Accept a block claiming the correct subsidy in the coinbase transaction" )
912
906
self .move_tip (65 )
913
907
b69 = self .next_block (69 , additional_coinbase_value = 10 )
914
- tx = self .create_and_sign_transaction (out [20 ]. tx , out [20 ]. n , out [ 20 ]. tx .vout [0 ].nValue - 10 )
908
+ tx = self .create_and_sign_transaction (out [20 ], out [20 ].vout [0 ].nValue - 10 )
915
909
self .update_block (69 , [tx ])
916
910
self .sync_blocks ([b69 ], True )
917
911
self .save_spendable_output ()
@@ -942,8 +936,8 @@ def run_test(self):
942
936
self .log .info ("Reject a block containing a duplicate transaction but with the same Merkle root (Merkle tree malleability" )
943
937
self .move_tip (69 )
944
938
b72 = self .next_block (72 )
945
- tx1 = self .create_and_sign_transaction (out [21 ]. tx , out [ 21 ]. n , 2 )
946
- tx2 = self .create_and_sign_transaction (tx1 , 0 , 1 )
939
+ tx1 = self .create_and_sign_transaction (out [21 ], 2 )
940
+ tx2 = self .create_and_sign_transaction (tx1 , 1 )
947
941
b72 = self .update_block (72 , [tx1 , tx2 ]) # now tip is 72
948
942
b71 = copy .deepcopy (b72 )
949
943
b71 .vtx .append (tx2 ) # add duplicate tx2
@@ -990,7 +984,7 @@ def run_test(self):
990
984
a [MAX_BLOCK_SIGOPS + 2 ] = 0
991
985
a [MAX_BLOCK_SIGOPS + 3 ] = 0
992
986
993
- tx = self .create_and_sign_transaction (out [22 ]. tx , 0 , 1 , CScript (a ))
987
+ tx = self .create_and_sign_transaction (out [22 ], 1 , CScript (a ))
994
988
b73 = self .update_block (73 , [tx ])
995
989
assert_equal (get_legacy_sigopcount_block (b73 ), MAX_BLOCK_SIGOPS + 1 )
996
990
self .sync_blocks ([b73 ], False , 16 , b'bad-blk-sigops' , reconnect = True )
@@ -1015,7 +1009,7 @@ def run_test(self):
1015
1009
a [MAX_BLOCK_SIGOPS + 2 ] = 0xff
1016
1010
a [MAX_BLOCK_SIGOPS + 3 ] = 0xff
1017
1011
a [MAX_BLOCK_SIGOPS + 4 ] = 0xff
1018
- tx = self .create_and_sign_transaction (out [22 ]. tx , 0 , 1 , CScript (a ))
1012
+ tx = self .create_and_sign_transaction (out [22 ], 1 , CScript (a ))
1019
1013
b74 = self .update_block (74 , [tx ])
1020
1014
self .sync_blocks ([b74 ], False , 16 , b'bad-blk-sigops' , reconnect = True )
1021
1015
@@ -1028,7 +1022,7 @@ def run_test(self):
1028
1022
a [MAX_BLOCK_SIGOPS + 1 ] = 0xff
1029
1023
a [MAX_BLOCK_SIGOPS + 2 ] = 0xff
1030
1024
a [MAX_BLOCK_SIGOPS + 3 ] = 0xff
1031
- tx = self .create_and_sign_transaction (out [22 ]. tx , 0 , 1 , CScript (a ))
1025
+ tx = self .create_and_sign_transaction (out [22 ], 1 , CScript (a ))
1032
1026
b75 = self .update_block (75 , [tx ])
1033
1027
self .sync_blocks ([b75 ], True )
1034
1028
self .save_spendable_output ()
@@ -1039,7 +1033,7 @@ def run_test(self):
1039
1033
size = MAX_BLOCK_SIGOPS - 1 + MAX_SCRIPT_ELEMENT_SIZE + 1 + 5
1040
1034
a = bytearray ([OP_CHECKSIG ] * size )
1041
1035
a [MAX_BLOCK_SIGOPS - 1 ] = 0x4e # PUSHDATA4, but leave the following bytes as just checksigs
1042
- tx = self .create_and_sign_transaction (out [23 ]. tx , 0 , 1 , CScript (a ))
1036
+ tx = self .create_and_sign_transaction (out [23 ], 1 , CScript (a ))
1043
1037
b76 = self .update_block (76 , [tx ])
1044
1038
self .sync_blocks ([b76 ], True )
1045
1039
self .save_spendable_output ()
@@ -1064,7 +1058,7 @@ def run_test(self):
1064
1058
self .log .info ("Test transaction resurrection during a re-org" )
1065
1059
self .move_tip (76 )
1066
1060
b77 = self .next_block (77 )
1067
- tx77 = self .create_and_sign_transaction (out [24 ]. tx , out [ 24 ]. n , 10 * COIN )
1061
+ tx77 = self .create_and_sign_transaction (out [24 ], 10 * COIN )
1068
1062
b77 = self .update_block (77 , [tx77 ])
1069
1063
self .sync_blocks ([b77 ], True )
1070
1064
self .save_spendable_output ()
@@ -1109,9 +1103,9 @@ def run_test(self):
1109
1103
b83 = self .next_block (83 )
1110
1104
op_codes = [OP_IF , OP_INVALIDOPCODE , OP_ELSE , OP_TRUE , OP_ENDIF ]
1111
1105
script = CScript (op_codes )
1112
- tx1 = self .create_and_sign_transaction (out [28 ]. tx , out [28 ]. n , out [ 28 ]. tx .vout [0 ].nValue , script )
1106
+ tx1 = self .create_and_sign_transaction (out [28 ], out [28 ].vout [0 ].nValue , script )
1113
1107
1114
- tx2 = self .create_and_sign_transaction (tx1 , 0 , 0 , CScript ([OP_TRUE ]))
1108
+ tx2 = self .create_and_sign_transaction (tx1 , 0 , CScript ([OP_TRUE ]))
1115
1109
tx2 .vin [0 ].scriptSig = CScript ([OP_FALSE ])
1116
1110
tx2 .rehash ()
1117
1111
@@ -1126,13 +1120,13 @@ def run_test(self):
1126
1120
#
1127
1121
self .log .info ("Test re-orging blocks with OP_RETURN in them" )
1128
1122
b84 = self .next_block (84 )
1129
- tx1 = self .create_tx (out [29 ]. tx , out [ 29 ]. n , 0 , CScript ([OP_RETURN ]))
1123
+ tx1 = self .create_tx (out [29 ], 0 , 0 , CScript ([OP_RETURN ]))
1130
1124
tx1 .vout .append (CTxOut (0 , CScript ([OP_TRUE ])))
1131
1125
tx1 .vout .append (CTxOut (0 , CScript ([OP_TRUE ])))
1132
1126
tx1 .vout .append (CTxOut (0 , CScript ([OP_TRUE ])))
1133
1127
tx1 .vout .append (CTxOut (0 , CScript ([OP_TRUE ])))
1134
1128
tx1 .calc_sha256 ()
1135
- self .sign_tx (tx1 , out [29 ]. tx , out [ 29 ]. n )
1129
+ self .sign_tx (tx1 , out [29 ])
1136
1130
tx1 .rehash ()
1137
1131
tx2 = self .create_tx (tx1 , 1 , 0 , CScript ([OP_RETURN ]))
1138
1132
tx2 .vout .append (CTxOut (0 , CScript ([OP_RETURN ])))
@@ -1217,21 +1211,21 @@ def add_transactions_to_block(self, block, tx_list):
1217
1211
1218
1212
# this is a little handier to use than the version in blocktools.py
1219
1213
def create_tx (self , spend_tx , n , value , script = CScript ([OP_TRUE , OP_DROP ] * 15 + [OP_TRUE ])):
1220
- return create_transaction (spend_tx , n , b"" , value , script )
1214
+ return create_tx_with_script (spend_tx , n , amount = value , script_pub_key = script )
1221
1215
1222
1216
# sign a transaction, using the key we know about
1223
1217
# this signs input 0 in tx, which is assumed to be spending output n in spend_tx
1224
- def sign_tx (self , tx , spend_tx , n ):
1225
- scriptPubKey = bytearray (spend_tx .vout [n ].scriptPubKey )
1218
+ def sign_tx (self , tx , spend_tx ):
1219
+ scriptPubKey = bytearray (spend_tx .vout [0 ].scriptPubKey )
1226
1220
if (scriptPubKey [0 ] == OP_TRUE ): # an anyone-can-spend
1227
1221
tx .vin [0 ].scriptSig = CScript ()
1228
1222
return
1229
- (sighash , err ) = SignatureHash (spend_tx .vout [n ].scriptPubKey , tx , 0 , SIGHASH_ALL )
1223
+ (sighash , err ) = SignatureHash (spend_tx .vout [0 ].scriptPubKey , tx , 0 , SIGHASH_ALL )
1230
1224
tx .vin [0 ].scriptSig = CScript ([self .coinbase_key .sign (sighash ) + bytes (bytearray ([SIGHASH_ALL ]))])
1231
1225
1232
- def create_and_sign_transaction (self , spend_tx , n , value , script = CScript ([OP_TRUE ])):
1233
- tx = self .create_tx (spend_tx , n , value , script )
1234
- self .sign_tx (tx , spend_tx , n )
1226
+ def create_and_sign_transaction (self , spend_tx , value , script = CScript ([OP_TRUE ])):
1227
+ tx = self .create_tx (spend_tx , 0 , value , script )
1228
+ self .sign_tx (tx , spend_tx )
1235
1229
tx .rehash ()
1236
1230
return tx
1237
1231
@@ -1250,11 +1244,11 @@ def next_block(self, number, spend=None, additional_coinbase_value=0, script=CSc
1250
1244
if spend is None :
1251
1245
block = create_block (base_block_hash , coinbase , block_time )
1252
1246
else :
1253
- coinbase .vout [0 ].nValue += spend .tx . vout [spend . n ].nValue - 1 # all but one satoshi to fees
1247
+ coinbase .vout [0 ].nValue += spend .vout [0 ].nValue - 1 # all but one satoshi to fees
1254
1248
coinbase .rehash ()
1255
1249
block = create_block (base_block_hash , coinbase , block_time )
1256
- tx = create_transaction (spend . tx , spend . n , b"" , 1 , script ) # spend 1 satoshi
1257
- self .sign_tx (tx , spend . tx , spend . n )
1250
+ tx = self . create_tx (spend , 0 , 1 , script ) # spend 1 satoshi
1251
+ self .sign_tx (tx , spend )
1258
1252
self .add_transactions_to_block (block , [tx ])
1259
1253
block .hashMerkleRoot = block .calc_merkle_root ()
1260
1254
if solve :
@@ -1273,7 +1267,7 @@ def save_spendable_output(self):
1273
1267
# get an output that we previously marked as spendable
1274
1268
def get_spendable_output (self ):
1275
1269
self .log .debug ("getting spendable output %s" % self .spendable_outputs [0 ].vtx [0 ])
1276
- return PreviousSpendableOutput ( self .spendable_outputs .pop (0 ).vtx [0 ], 0 )
1270
+ return self .spendable_outputs .pop (0 ).vtx [0 ]
1277
1271
1278
1272
# move the tip back to a previous block
1279
1273
def move_tip (self , number ):
0 commit comments