48
48
import time
49
49
50
50
from test_framework .blocktools import create_coinbase , create_block
51
- from test_framework .comptool import TestInstance , TestManager
52
- from test_framework .mininode import ToHex , CTransaction , network_thread_start
51
+ from test_framework .messages import ToHex , CTransaction
52
+ from test_framework .mininode import network_thread_start , P2PDataStore
53
53
from test_framework .script import (
54
54
CScript ,
55
55
OP_CHECKSEQUENCEVERIFY ,
56
56
OP_DROP ,
57
57
)
58
- from test_framework .test_framework import ComparisonTestFramework
58
+ from test_framework .test_framework import BitcoinTestFramework
59
59
from test_framework .util import (
60
60
assert_equal ,
61
61
get_bip9_status ,
@@ -149,22 +149,18 @@ def create_bip112txs(node, bip112inputs, varyOP_CSV, txversion, address, locktim
149
149
txs .append ({'tx' : signtx , 'sdf' : sdf , 'stf' : stf })
150
150
return txs
151
151
152
- class BIP68_112_113Test (ComparisonTestFramework ):
152
+ class BIP68_112_113Test (BitcoinTestFramework ):
153
153
def set_test_params (self ):
154
154
self .num_nodes = 1
155
155
self .setup_clean_chain = True
156
156
self .extra_args = [['-whitelist=127.0.0.1' , '-blockversion=4' , '-addresstype=legacy' ]]
157
157
158
- def run_test (self ):
159
- test = TestManager (self , self .options .tmpdir )
160
- test .add_all_connections (self .nodes )
161
- network_thread_start ()
162
- test .run ()
163
-
164
- def generate_blocks (self , number , version , test_blocks = []):
158
+ def generate_blocks (self , number , version , test_blocks = None ):
159
+ if test_blocks is None :
160
+ test_blocks = []
165
161
for i in range (number ):
166
162
block = self .create_test_block ([], version )
167
- test_blocks .append ([ block , True ] )
163
+ test_blocks .append (block )
168
164
self .last_block_time += 600
169
165
self .tip = block .sha256
170
166
self .tipheight += 1
@@ -179,7 +175,17 @@ def create_test_block(self, txs, version=536870912):
179
175
block .solve ()
180
176
return block
181
177
182
- def get_tests (self ):
178
+ def sync_blocks (self , blocks , success = True , reject_code = None , reject_reason = None , request_block = True ):
179
+ """Sends blocks to test node. Syncs and verifies that tip has advanced to most recent block.
180
+
181
+ Call with success = False if the tip shouldn't advance to the most recent block."""
182
+ self .nodes [0 ].p2p .send_blocks_and_test (blocks , self .nodes [0 ], success = success , reject_code = reject_code , reject_reason = reject_reason , request_block = request_block )
183
+
184
+ def run_test (self ):
185
+ self .nodes [0 ].add_p2p_connection (P2PDataStore ())
186
+ network_thread_start ()
187
+ self .nodes [0 ].p2p .wait_for_verack ()
188
+
183
189
self .log .info ("Generate blocks in the past for coinbase outputs." )
184
190
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
185
191
self .nodes [0 ].setmocktime (long_past_time - 100 ) # enough so that the generated blocks will still all be before long_past_time
@@ -193,7 +199,7 @@ def get_tests(self):
193
199
self .log .info ("Test that the csv softfork is DEFINED" )
194
200
assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'defined' )
195
201
test_blocks = self .generate_blocks (61 , 4 )
196
- yield TestInstance (test_blocks , sync_every_block = False )
202
+ self . sync_blocks (test_blocks )
197
203
198
204
self .log .info ("Advance from DEFINED to STARTED, height = 143" )
199
205
assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'started' )
@@ -205,7 +211,7 @@ def get_tests(self):
205
211
test_blocks = self .generate_blocks (20 , 4 , test_blocks ) # 0x00000004 (signalling not)
206
212
test_blocks = self .generate_blocks (50 , 536871169 , test_blocks ) # 0x20000101 (signalling ready)
207
213
test_blocks = self .generate_blocks (24 , 536936448 , test_blocks ) # 0x20010000 (signalling not)
208
- yield TestInstance (test_blocks , sync_every_block = False )
214
+ self . sync_blocks (test_blocks )
209
215
210
216
self .log .info ("Failed to advance past STARTED, height = 287" )
211
217
assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'started' )
@@ -217,14 +223,14 @@ def get_tests(self):
217
223
test_blocks = self .generate_blocks (26 , 4 , test_blocks ) # 0x00000004 (signalling not)
218
224
test_blocks = self .generate_blocks (50 , 536871169 , test_blocks ) # 0x20000101 (signalling ready)
219
225
test_blocks = self .generate_blocks (10 , 536936448 , test_blocks ) # 0x20010000 (signalling not)
220
- yield TestInstance (test_blocks , sync_every_block = False )
226
+ self . sync_blocks (test_blocks )
221
227
222
228
self .log .info ("Advanced from STARTED to LOCKED_IN, height = 431" )
223
229
assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'locked_in' )
224
230
225
231
# Generate 140 more version 4 blocks
226
232
test_blocks = self .generate_blocks (140 , 4 )
227
- yield TestInstance (test_blocks , sync_every_block = False )
233
+ self . sync_blocks (test_blocks )
228
234
229
235
# Inputs at height = 572
230
236
#
@@ -267,7 +273,7 @@ def get_tests(self):
267
273
268
274
# 2 more version 4 blocks
269
275
test_blocks = self .generate_blocks (2 , 4 )
270
- yield TestInstance (test_blocks , sync_every_block = False )
276
+ self . sync_blocks (test_blocks )
271
277
272
278
self .log .info ("Not yet advanced to ACTIVE, height = 574 (will activate for block 576, not 575)" )
273
279
assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'locked_in' )
@@ -321,7 +327,7 @@ def get_tests(self):
321
327
# try BIP 112 with seq=9 txs
322
328
success_txs .extend (all_rlt_txs (bip112txs_vary_nSequence_9_v1 ))
323
329
success_txs .extend (all_rlt_txs (bip112txs_vary_OP_CSV_9_v1 ))
324
- yield TestInstance ([[ self .create_test_block (success_txs ), True ] ])
330
+ self . sync_blocks ([ self .create_test_block (success_txs )])
325
331
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
326
332
327
333
self .log .info ("Test version 2 txs" )
@@ -340,12 +346,12 @@ def get_tests(self):
340
346
# try BIP 112 with seq=9 txs
341
347
success_txs .extend (all_rlt_txs (bip112txs_vary_nSequence_9_v2 ))
342
348
success_txs .extend (all_rlt_txs (bip112txs_vary_OP_CSV_9_v2 ))
343
- yield TestInstance ([[ self .create_test_block (success_txs ), True ] ])
349
+ self . sync_blocks ([ self .create_test_block (success_txs )])
344
350
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
345
351
346
352
# 1 more version 4 block to get us to height 575 so the fork should now be active for the next block
347
353
test_blocks = self .generate_blocks (1 , 4 )
348
- yield TestInstance (test_blocks , sync_every_block = False )
354
+ self . sync_blocks (test_blocks )
349
355
assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'active' )
350
356
351
357
self .log .info ("Post-Soft Fork Tests." )
@@ -357,74 +363,74 @@ def get_tests(self):
357
363
bip113tx_v2 .nLockTime = self .last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
358
364
bip113signed2 = sign_transaction (self .nodes [0 ], bip113tx_v2 )
359
365
for bip113tx in [bip113signed1 , bip113signed2 ]:
360
- yield TestInstance ([[ self .create_test_block ([bip113tx ]), False ]] )
366
+ self . sync_blocks ([ self .create_test_block ([bip113tx ])], success = False )
361
367
# BIP 113 tests should now pass if the locktime is < MTP
362
368
bip113tx_v1 .nLockTime = self .last_block_time - 600 * 5 - 1 # < MTP of prior block
363
369
bip113signed1 = sign_transaction (self .nodes [0 ], bip113tx_v1 )
364
370
bip113tx_v2 .nLockTime = self .last_block_time - 600 * 5 - 1 # < MTP of prior block
365
371
bip113signed2 = sign_transaction (self .nodes [0 ], bip113tx_v2 )
366
372
for bip113tx in [bip113signed1 , bip113signed2 ]:
367
- yield TestInstance ([[ self .create_test_block ([bip113tx ]), True ] ])
373
+ self . sync_blocks ([ self .create_test_block ([bip113tx ])])
368
374
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
369
375
370
376
# Next block height = 580 after 4 blocks of random version
371
377
test_blocks = self .generate_blocks (4 , 1234 )
372
- yield TestInstance (test_blocks , sync_every_block = False )
378
+ self . sync_blocks (test_blocks )
373
379
374
380
self .log .info ("BIP 68 tests" )
375
381
self .log .info ("Test version 1 txs - all should still pass" )
376
382
377
383
success_txs = []
378
384
success_txs .extend (all_rlt_txs (bip68txs_v1 ))
379
- yield TestInstance ([[ self .create_test_block (success_txs ), True ] ])
385
+ self . sync_blocks ([ self .create_test_block (success_txs )])
380
386
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
381
387
382
388
self .log .info ("Test version 2 txs" )
383
389
384
390
# All txs with SEQUENCE_LOCKTIME_DISABLE_FLAG set pass
385
391
bip68success_txs = [tx ['tx' ] for tx in bip68txs_v2 if tx ['sdf' ]]
386
- yield TestInstance ([[ self .create_test_block (bip68success_txs ), True ] ])
392
+ self . sync_blocks ([ self .create_test_block (bip68success_txs )])
387
393
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
388
394
389
395
# All txs without flag fail as we are at delta height = 8 < 10 and delta time = 8 * 600 < 10 * 512
390
396
bip68timetxs = [tx ['tx' ] for tx in bip68txs_v2 if not tx ['sdf' ] and tx ['stf' ]]
391
397
for tx in bip68timetxs :
392
- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
398
+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
393
399
394
400
bip68heighttxs = [tx ['tx' ] for tx in bip68txs_v2 if not tx ['sdf' ] and not tx ['stf' ]]
395
401
for tx in bip68heighttxs :
396
- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
402
+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
397
403
398
404
# Advance one block to 581
399
405
test_blocks = self .generate_blocks (1 , 1234 )
400
- yield TestInstance (test_blocks , sync_every_block = False )
406
+ self . sync_blocks (test_blocks )
401
407
402
408
# Height txs should fail and time txs should now pass 9 * 600 > 10 * 512
403
409
bip68success_txs .extend (bip68timetxs )
404
- yield TestInstance ([[ self .create_test_block (bip68success_txs ), True ] ])
410
+ self . sync_blocks ([ self .create_test_block (bip68success_txs )])
405
411
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
406
412
for tx in bip68heighttxs :
407
- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
413
+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
408
414
409
415
# Advance one block to 582
410
416
test_blocks = self .generate_blocks (1 , 1234 )
411
- yield TestInstance (test_blocks , sync_every_block = False )
417
+ self . sync_blocks (test_blocks )
412
418
413
419
# All BIP 68 txs should pass
414
420
bip68success_txs .extend (bip68heighttxs )
415
- yield TestInstance ([[ self .create_test_block (bip68success_txs ), True ] ])
421
+ self . sync_blocks ([ self .create_test_block (bip68success_txs )])
416
422
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
417
423
418
424
self .log .info ("BIP 112 tests" )
419
425
self .log .info ("Test version 1 txs" )
420
426
421
427
# -1 OP_CSV tx should fail
422
- yield TestInstance ([[ self .create_test_block ([bip112tx_special_v1 ]), False ]] )
428
+ self . sync_blocks ([ self .create_test_block ([bip112tx_special_v1 ])], success = False )
423
429
# If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 1 txs should still pass
424
430
425
431
success_txs = [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_v1 if tx ['sdf' ]]
426
432
success_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_9_v1 if tx ['sdf' ]]
427
- yield TestInstance ([[ self .create_test_block (success_txs ), True ] ])
433
+ self . sync_blocks ([ self .create_test_block (success_txs )])
428
434
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
429
435
430
436
# If SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV, version 1 txs should now fail
@@ -433,18 +439,18 @@ def get_tests(self):
433
439
fail_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_9_v1 if not tx ['sdf' ]]
434
440
fail_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_9_v1 if not tx ['sdf' ]]
435
441
for tx in fail_txs :
436
- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
442
+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
437
443
438
- self .log .info ("Version 2 txs" )
444
+ self .log .info ("Test version 2 txs" )
439
445
440
446
# -1 OP_CSV tx should fail
441
- yield TestInstance ([[ self .create_test_block ([bip112tx_special_v2 ]), False ]] )
447
+ self . sync_blocks ([ self .create_test_block ([bip112tx_special_v2 ])], success = False )
442
448
443
449
# If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 2 txs should pass (all sequence locks are met)
444
450
success_txs = [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_v2 if tx ['sdf' ]]
445
451
success_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_9_v2 if tx ['sdf' ]]
446
452
447
- yield TestInstance ([[ self .create_test_block (success_txs ), True ] ])
453
+ self . sync_blocks ([ self .create_test_block (success_txs )])
448
454
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
449
455
450
456
# SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV for all remaining txs ##
@@ -453,23 +459,23 @@ def get_tests(self):
453
459
fail_txs = all_rlt_txs (bip112txs_vary_nSequence_9_v2 )
454
460
fail_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_9_v2 if not tx ['sdf' ]]
455
461
for tx in fail_txs :
456
- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
462
+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
457
463
458
464
# If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in nSequence, tx should fail
459
465
fail_txs = [tx ['tx' ] for tx in bip112txs_vary_nSequence_v2 if tx ['sdf' ]]
460
466
for tx in fail_txs :
461
- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
467
+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
462
468
463
469
# If sequencelock types mismatch, tx should fail
464
470
fail_txs = [tx ['tx' ] for tx in bip112txs_vary_nSequence_v2 if not tx ['sdf' ] and tx ['stf' ]]
465
471
fail_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_v2 if not tx ['sdf' ] and tx ['stf' ]]
466
472
for tx in fail_txs :
467
- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
473
+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
468
474
469
475
# Remaining txs should pass, just test masking works properly
470
476
success_txs = [tx ['tx' ] for tx in bip112txs_vary_nSequence_v2 if not tx ['sdf' ] and not tx ['stf' ]]
471
477
success_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_v2 if not tx ['sdf' ] and not tx ['stf' ]]
472
- yield TestInstance ([[ self .create_test_block (success_txs ), True ]]) # 124
478
+ self . sync_blocks ([ self .create_test_block (success_txs )])
473
479
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
474
480
475
481
# Additional test, of checking that comparison of two time types works properly
@@ -479,7 +485,7 @@ def get_tests(self):
479
485
signtx = sign_transaction (self .nodes [0 ], tx )
480
486
time_txs .append (signtx )
481
487
482
- yield TestInstance ([[ self .create_test_block (time_txs ), True ] ])
488
+ self . sync_blocks ([ self .create_test_block (time_txs )])
483
489
self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
484
490
485
491
# TODO: Test empty stack fails
0 commit comments