@@ -85,6 +85,70 @@ def relative_locktime(sdf, srhb, stf, srlb):
85
85
def all_rlt_txs (txs ):
86
86
return [tx ['tx' ] for tx in txs ]
87
87
88
+ def create_transaction (node , txid , to_address , amount ):
89
+ inputs = [{"txid" : txid , "vout" : 0 }]
90
+ outputs = {to_address : amount }
91
+ rawtx = node .createrawtransaction (inputs , outputs )
92
+ tx = CTransaction ()
93
+ f = BytesIO (hex_str_to_bytes (rawtx ))
94
+ tx .deserialize (f )
95
+ return tx
96
+
97
+ def sign_transaction (node , unsignedtx ):
98
+ rawtx = ToHex (unsignedtx )
99
+ signresult = node .signrawtransactionwithwallet (rawtx )
100
+ tx = CTransaction ()
101
+ f = BytesIO (hex_str_to_bytes (signresult ['hex' ]))
102
+ tx .deserialize (f )
103
+ return tx
104
+
105
+ def create_bip112special (node , input , txversion , address ):
106
+ tx = create_transaction (node , input , address , Decimal ("49.98" ))
107
+ tx .nVersion = txversion
108
+ signtx = sign_transaction (node , tx )
109
+ signtx .vin [0 ].scriptSig = CScript ([- 1 , OP_CHECKSEQUENCEVERIFY , OP_DROP ] + list (CScript (signtx .vin [0 ].scriptSig )))
110
+ return signtx
111
+
112
+ def send_generic_input_tx (node , coinbases , address ):
113
+ amount = Decimal ("49.99" )
114
+ return node .sendrawtransaction (ToHex (sign_transaction (node , create_transaction (node , node .getblock (coinbases .pop ())['tx' ][0 ], address , amount ))))
115
+
116
+ def create_bip68txs (node , bip68inputs , txversion , address , locktime_delta = 0 ):
117
+ """Returns a list of bip68 transactions with different bits set."""
118
+ txs = []
119
+ assert (len (bip68inputs ) >= 16 )
120
+ for i , (sdf , srhb , stf , srlb ) in enumerate (product (* [[True , False ]] * 4 )):
121
+ locktime = relative_locktime (sdf , srhb , stf , srlb )
122
+ tx = create_transaction (node , bip68inputs [i ], address , Decimal ("49.98" ))
123
+ tx .nVersion = txversion
124
+ tx .vin [0 ].nSequence = locktime + locktime_delta
125
+ tx = sign_transaction (node , tx )
126
+ tx .rehash ()
127
+ txs .append ({'tx' : tx , 'sdf' : sdf , 'stf' : stf })
128
+
129
+ return txs
130
+
131
+ def create_bip112txs (node , bip112inputs , varyOP_CSV , txversion , address , locktime_delta = 0 ):
132
+ """Returns a list of bip68 transactions with different bits set."""
133
+ txs = []
134
+ assert (len (bip112inputs ) >= 16 )
135
+ for i , (sdf , srhb , stf , srlb ) in enumerate (product (* [[True , False ]] * 4 )):
136
+ locktime = relative_locktime (sdf , srhb , stf , srlb )
137
+ tx = create_transaction (node , bip112inputs [i ], address , Decimal ("49.98" ))
138
+ if (varyOP_CSV ): # if varying OP_CSV, nSequence is fixed
139
+ tx .vin [0 ].nSequence = BASE_RELATIVE_LOCKTIME + locktime_delta
140
+ else : # vary nSequence instead, OP_CSV is fixed
141
+ tx .vin [0 ].nSequence = locktime + locktime_delta
142
+ tx .nVersion = txversion
143
+ signtx = sign_transaction (node , tx )
144
+ if (varyOP_CSV ):
145
+ signtx .vin [0 ].scriptSig = CScript ([locktime , OP_CHECKSEQUENCEVERIFY , OP_DROP ] + list (CScript (signtx .vin [0 ].scriptSig )))
146
+ else :
147
+ signtx .vin [0 ].scriptSig = CScript ([BASE_RELATIVE_LOCKTIME , OP_CHECKSEQUENCEVERIFY , OP_DROP ] + list (CScript (signtx .vin [0 ].scriptSig )))
148
+ tx .rehash ()
149
+ txs .append ({'tx' : signtx , 'sdf' : sdf , 'stf' : stf })
150
+ return txs
151
+
88
152
class BIP68_112_113Test (ComparisonTestFramework ):
89
153
def set_test_params (self ):
90
154
self .num_nodes = 1
@@ -97,27 +161,6 @@ def run_test(self):
97
161
network_thread_start ()
98
162
test .run ()
99
163
100
- def send_generic_input_tx (self , node , coinbases ):
101
- amount = Decimal ("49.99" )
102
- return node .sendrawtransaction (ToHex (self .sign_transaction (node , self .create_transaction (node , node .getblock (coinbases .pop ())['tx' ][0 ], self .nodeaddress , amount ))))
103
-
104
- def create_transaction (self , node , txid , to_address , amount ):
105
- inputs = [{"txid" : txid , "vout" : 0 }]
106
- outputs = {to_address : amount }
107
- rawtx = node .createrawtransaction (inputs , outputs )
108
- tx = CTransaction ()
109
- f = BytesIO (hex_str_to_bytes (rawtx ))
110
- tx .deserialize (f )
111
- return tx
112
-
113
- def sign_transaction (self , node , unsignedtx ):
114
- rawtx = ToHex (unsignedtx )
115
- signresult = node .signrawtransactionwithwallet (rawtx )
116
- tx = CTransaction ()
117
- f = BytesIO (hex_str_to_bytes (signresult ['hex' ]))
118
- tx .deserialize (f )
119
- return tx
120
-
121
164
def generate_blocks (self , number , version , test_blocks = []):
122
165
for i in range (number ):
123
166
block = self .create_test_block ([], version )
@@ -136,49 +179,6 @@ def create_test_block(self, txs, version=536870912):
136
179
block .solve ()
137
180
return block
138
181
139
- def create_bip68txs (self , bip68inputs , txversion , locktime_delta = 0 ):
140
- """Returns a list of bip68 transactions with different bits set."""
141
- txs = []
142
- assert (len (bip68inputs ) >= 16 )
143
- for i , (sdf , srhb , stf , srlb ) in enumerate (product (* [[True , False ]] * 4 )):
144
- locktime = relative_locktime (sdf , srhb , stf , srlb )
145
- tx = self .create_transaction (self .nodes [0 ], bip68inputs [i ], self .nodeaddress , Decimal ("49.98" ))
146
- tx .nVersion = txversion
147
- tx .vin [0 ].nSequence = locktime + locktime_delta
148
- tx = self .sign_transaction (self .nodes [0 ], tx )
149
- tx .rehash ()
150
- txs .append ({'tx' : tx , 'sdf' : sdf , 'stf' : stf })
151
-
152
- return txs
153
-
154
- def create_bip112special (self , input , txversion ):
155
- tx = self .create_transaction (self .nodes [0 ], input , self .nodeaddress , Decimal ("49.98" ))
156
- tx .nVersion = txversion
157
- signtx = self .sign_transaction (self .nodes [0 ], tx )
158
- signtx .vin [0 ].scriptSig = CScript ([- 1 , OP_CHECKSEQUENCEVERIFY , OP_DROP ] + list (CScript (signtx .vin [0 ].scriptSig )))
159
- return signtx
160
-
161
- def create_bip112txs (self , bip112inputs , varyOP_CSV , txversion , locktime_delta = 0 ):
162
- """Returns a list of bip68 transactions with different bits set."""
163
- txs = []
164
- assert (len (bip112inputs ) >= 16 )
165
- for i , (sdf , srhb , stf , srlb ) in enumerate (product (* [[True , False ]] * 4 )):
166
- locktime = relative_locktime (sdf , srhb , stf , srlb )
167
- tx = self .create_transaction (self .nodes [0 ], bip112inputs [i ], self .nodeaddress , Decimal ("49.98" ))
168
- if (varyOP_CSV ): # if varying OP_CSV, nSequence is fixed
169
- tx .vin [0 ].nSequence = BASE_RELATIVE_LOCKTIME + locktime_delta
170
- else : # vary nSequence instead, OP_CSV is fixed
171
- tx .vin [0 ].nSequence = locktime + locktime_delta
172
- tx .nVersion = txversion
173
- signtx = self .sign_transaction (self .nodes [0 ], tx )
174
- if (varyOP_CSV ):
175
- signtx .vin [0 ].scriptSig = CScript ([locktime , OP_CHECKSEQUENCEVERIFY , OP_DROP ] + list (CScript (signtx .vin [0 ].scriptSig )))
176
- else :
177
- signtx .vin [0 ].scriptSig = CScript ([BASE_RELATIVE_LOCKTIME , OP_CHECKSEQUENCEVERIFY , OP_DROP ] + list (CScript (signtx .vin [0 ].scriptSig )))
178
- tx .rehash ()
179
- txs .append ({'tx' : signtx , 'sdf' : sdf , 'stf' : stf })
180
- return txs
181
-
182
182
def get_tests (self ):
183
183
self .log .info ("Generate blocks in the past for coinbase outputs." )
184
184
long_past_time = int (time .time ()) - 600 * 1000 # enough to build up to 1000 blocks 10 minutes apart without worrying about getting into the future
@@ -233,29 +233,29 @@ def get_tests(self):
233
233
# 16 normal inputs
234
234
bip68inputs = []
235
235
for i in range (16 ):
236
- bip68inputs .append (self . send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks ))
236
+ bip68inputs .append (send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks , self . nodeaddress ))
237
237
238
238
# 2 sets of 16 inputs with 10 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
239
239
bip112basicinputs = []
240
240
for j in range (2 ):
241
241
inputs = []
242
242
for i in range (16 ):
243
- inputs .append (self . send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks ))
243
+ inputs .append (send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks , self . nodeaddress ))
244
244
bip112basicinputs .append (inputs )
245
245
246
246
# 2 sets of 16 varied inputs with (relative_lock_time) OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
247
247
bip112diverseinputs = []
248
248
for j in range (2 ):
249
249
inputs = []
250
250
for i in range (16 ):
251
- inputs .append (self . send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks ))
251
+ inputs .append (send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks , self . nodeaddress ))
252
252
bip112diverseinputs .append (inputs )
253
253
254
254
# 1 special input with -1 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
255
- bip112specialinput = self . send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks )
255
+ bip112specialinput = send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks , self . nodeaddress )
256
256
257
257
# 1 normal input
258
- bip113input = self . send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks )
258
+ bip113input = send_generic_input_tx (self .nodes [0 ], self .coinbase_blocks , self . nodeaddress )
259
259
260
260
self .nodes [0 ].setmocktime (self .last_block_time + 600 )
261
261
inputblockhash = self .nodes [0 ].generate (1 )[0 ] # 1 block generated for inputs to be in chain at height 572
@@ -274,33 +274,33 @@ def get_tests(self):
274
274
275
275
# Test both version 1 and version 2 transactions for all tests
276
276
# BIP113 test transaction will be modified before each use to put in appropriate block time
277
- bip113tx_v1 = self . create_transaction (self .nodes [0 ], bip113input , self .nodeaddress , Decimal ("49.98" ))
277
+ bip113tx_v1 = create_transaction (self .nodes [0 ], bip113input , self .nodeaddress , Decimal ("49.98" ))
278
278
bip113tx_v1 .vin [0 ].nSequence = 0xFFFFFFFE
279
279
bip113tx_v1 .nVersion = 1
280
- bip113tx_v2 = self . create_transaction (self .nodes [0 ], bip113input , self .nodeaddress , Decimal ("49.98" ))
280
+ bip113tx_v2 = create_transaction (self .nodes [0 ], bip113input , self .nodeaddress , Decimal ("49.98" ))
281
281
bip113tx_v2 .vin [0 ].nSequence = 0xFFFFFFFE
282
282
bip113tx_v2 .nVersion = 2
283
283
284
284
# For BIP68 test all 16 relative sequence locktimes
285
- bip68txs_v1 = self . create_bip68txs (bip68inputs , 1 )
286
- bip68txs_v2 = self . create_bip68txs (bip68inputs , 2 )
285
+ bip68txs_v1 = create_bip68txs (self . nodes [ 0 ], bip68inputs , 1 , self . nodeaddress )
286
+ bip68txs_v2 = create_bip68txs (self . nodes [ 0 ], bip68inputs , 2 , self . nodeaddress )
287
287
288
288
# For BIP112 test:
289
289
# 16 relative sequence locktimes of 10 against 10 OP_CSV OP_DROP inputs
290
- bip112txs_vary_nSequence_v1 = self . create_bip112txs (bip112basicinputs [0 ], False , 1 )
291
- bip112txs_vary_nSequence_v2 = self . create_bip112txs (bip112basicinputs [0 ], False , 2 )
290
+ bip112txs_vary_nSequence_v1 = create_bip112txs (self . nodes [ 0 ], bip112basicinputs [0 ], False , 1 , self . nodeaddress )
291
+ bip112txs_vary_nSequence_v2 = create_bip112txs (self . nodes [ 0 ], bip112basicinputs [0 ], False , 2 , self . nodeaddress )
292
292
# 16 relative sequence locktimes of 9 against 10 OP_CSV OP_DROP inputs
293
- bip112txs_vary_nSequence_9_v1 = self . create_bip112txs (bip112basicinputs [1 ], False , 1 , - 1 )
294
- bip112txs_vary_nSequence_9_v2 = self . create_bip112txs (bip112basicinputs [1 ], False , 2 , - 1 )
293
+ bip112txs_vary_nSequence_9_v1 = create_bip112txs (self . nodes [ 0 ], bip112basicinputs [1 ], False , 1 , self . nodeaddress , - 1 )
294
+ bip112txs_vary_nSequence_9_v2 = create_bip112txs (self . nodes [ 0 ], bip112basicinputs [1 ], False , 2 , self . nodeaddress , - 1 )
295
295
# sequence lock time of 10 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
296
- bip112txs_vary_OP_CSV_v1 = self . create_bip112txs (bip112diverseinputs [0 ], True , 1 )
297
- bip112txs_vary_OP_CSV_v2 = self . create_bip112txs (bip112diverseinputs [0 ], True , 2 )
296
+ bip112txs_vary_OP_CSV_v1 = create_bip112txs (self . nodes [ 0 ], bip112diverseinputs [0 ], True , 1 , self . nodeaddress )
297
+ bip112txs_vary_OP_CSV_v2 = create_bip112txs (self . nodes [ 0 ], bip112diverseinputs [0 ], True , 2 , self . nodeaddress )
298
298
# sequence lock time of 9 against 16 (relative_lock_time) OP_CSV OP_DROP inputs
299
- bip112txs_vary_OP_CSV_9_v1 = self . create_bip112txs (bip112diverseinputs [1 ], True , 1 , - 1 )
300
- bip112txs_vary_OP_CSV_9_v2 = self . create_bip112txs (bip112diverseinputs [1 ], True , 2 , - 1 )
299
+ bip112txs_vary_OP_CSV_9_v1 = create_bip112txs (self . nodes [ 0 ], bip112diverseinputs [1 ], True , 1 , self . nodeaddress , - 1 )
300
+ bip112txs_vary_OP_CSV_9_v2 = create_bip112txs (self . nodes [ 0 ], bip112diverseinputs [1 ], True , 2 , self . nodeaddress , - 1 )
301
301
# -1 OP_CSV OP_DROP input
302
- bip112tx_special_v1 = self . create_bip112special (bip112specialinput , 1 )
303
- bip112tx_special_v2 = self . create_bip112special (bip112specialinput , 2 )
302
+ bip112tx_special_v1 = create_bip112special (self . nodes [ 0 ], bip112specialinput , 1 , self . nodeaddress )
303
+ bip112tx_special_v2 = create_bip112special (self . nodes [ 0 ], bip112specialinput , 2 , self . nodeaddress )
304
304
305
305
self .log .info ("TESTING" )
306
306
@@ -310,7 +310,7 @@ def get_tests(self):
310
310
success_txs = []
311
311
# add BIP113 tx and -1 CSV tx
312
312
bip113tx_v1 .nLockTime = self .last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
313
- bip113signed1 = self . sign_transaction (self .nodes [0 ], bip113tx_v1 )
313
+ bip113signed1 = sign_transaction (self .nodes [0 ], bip113tx_v1 )
314
314
success_txs .append (bip113signed1 )
315
315
success_txs .append (bip112tx_special_v1 )
316
316
# add BIP 68 txs
@@ -329,7 +329,7 @@ def get_tests(self):
329
329
success_txs = []
330
330
# add BIP113 tx and -1 CSV tx
331
331
bip113tx_v2 .nLockTime = self .last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
332
- bip113signed2 = self . sign_transaction (self .nodes [0 ], bip113tx_v2 )
332
+ bip113signed2 = sign_transaction (self .nodes [0 ], bip113tx_v2 )
333
333
success_txs .append (bip113signed2 )
334
334
success_txs .append (bip112tx_special_v2 )
335
335
# add BIP 68 txs
@@ -353,16 +353,16 @@ def get_tests(self):
353
353
self .log .info ("BIP 113 tests" )
354
354
# BIP 113 tests should now fail regardless of version number if nLockTime isn't satisfied by new rules
355
355
bip113tx_v1 .nLockTime = self .last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
356
- bip113signed1 = self . sign_transaction (self .nodes [0 ], bip113tx_v1 )
356
+ bip113signed1 = sign_transaction (self .nodes [0 ], bip113tx_v1 )
357
357
bip113tx_v2 .nLockTime = self .last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
358
- bip113signed2 = self . sign_transaction (self .nodes [0 ], bip113tx_v2 )
358
+ bip113signed2 = sign_transaction (self .nodes [0 ], bip113tx_v2 )
359
359
for bip113tx in [bip113signed1 , bip113signed2 ]:
360
360
yield TestInstance ([[self .create_test_block ([bip113tx ]), False ]])
361
361
# BIP 113 tests should now pass if the locktime is < MTP
362
362
bip113tx_v1 .nLockTime = self .last_block_time - 600 * 5 - 1 # < MTP of prior block
363
- bip113signed1 = self . sign_transaction (self .nodes [0 ], bip113tx_v1 )
363
+ bip113signed1 = sign_transaction (self .nodes [0 ], bip113tx_v1 )
364
364
bip113tx_v2 .nLockTime = self .last_block_time - 600 * 5 - 1 # < MTP of prior block
365
- bip113signed2 = self . sign_transaction (self .nodes [0 ], bip113tx_v2 )
365
+ bip113signed2 = sign_transaction (self .nodes [0 ], bip113tx_v2 )
366
366
for bip113tx in [bip113signed1 , bip113signed2 ]:
367
367
yield TestInstance ([[self .create_test_block ([bip113tx ]), True ]])
368
368
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
@@ -476,7 +476,7 @@ def get_tests(self):
476
476
time_txs = []
477
477
for tx in [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_v2 if not tx ['sdf' ] and tx ['stf' ]]:
478
478
tx .vin [0 ].nSequence = BASE_RELATIVE_LOCKTIME | SEQ_TYPE_FLAG
479
- signtx = self . sign_transaction (self .nodes [0 ], tx )
479
+ signtx = sign_transaction (self .nodes [0 ], tx )
480
480
time_txs .append (signtx )
481
481
482
482
yield TestInstance ([[self .create_test_block (time_txs ), True ]])
0 commit comments