4848import time
4949
5050from 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
5353from test_framework .script import (
5454 CScript ,
5555 OP_CHECKSEQUENCEVERIFY ,
5656 OP_DROP ,
5757)
58- from test_framework .test_framework import ComparisonTestFramework
58+ from test_framework .test_framework import BitcoinTestFramework
5959from test_framework .util import (
6060 assert_equal ,
6161 get_bip9_status ,
@@ -149,22 +149,18 @@ def create_bip112txs(node, bip112inputs, varyOP_CSV, txversion, address, locktim
149149 txs .append ({'tx' : signtx , 'sdf' : sdf , 'stf' : stf })
150150 return txs
151151
152- class BIP68_112_113Test (ComparisonTestFramework ):
152+ class BIP68_112_113Test (BitcoinTestFramework ):
153153 def set_test_params (self ):
154154 self .num_nodes = 1
155155 self .setup_clean_chain = True
156156 self .extra_args = [['-whitelist=127.0.0.1' , '-blockversion=4' , '-addresstype=legacy' ]]
157157
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 = []
165161 for i in range (number ):
166162 block = self .create_test_block ([], version )
167- test_blocks .append ([ block , True ] )
163+ test_blocks .append (block )
168164 self .last_block_time += 600
169165 self .tip = block .sha256
170166 self .tipheight += 1
@@ -179,7 +175,17 @@ def create_test_block(self, txs, version=536870912):
179175 block .solve ()
180176 return block
181177
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+
183189 self .log .info ("Generate blocks in the past for coinbase outputs." )
184190 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
185191 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):
193199 self .log .info ("Test that the csv softfork is DEFINED" )
194200 assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'defined' )
195201 test_blocks = self .generate_blocks (61 , 4 )
196- yield TestInstance (test_blocks , sync_every_block = False )
202+ self . sync_blocks (test_blocks )
197203
198204 self .log .info ("Advance from DEFINED to STARTED, height = 143" )
199205 assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'started' )
@@ -205,7 +211,7 @@ def get_tests(self):
205211 test_blocks = self .generate_blocks (20 , 4 , test_blocks ) # 0x00000004 (signalling not)
206212 test_blocks = self .generate_blocks (50 , 536871169 , test_blocks ) # 0x20000101 (signalling ready)
207213 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 )
209215
210216 self .log .info ("Failed to advance past STARTED, height = 287" )
211217 assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'started' )
@@ -217,14 +223,14 @@ def get_tests(self):
217223 test_blocks = self .generate_blocks (26 , 4 , test_blocks ) # 0x00000004 (signalling not)
218224 test_blocks = self .generate_blocks (50 , 536871169 , test_blocks ) # 0x20000101 (signalling ready)
219225 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 )
221227
222228 self .log .info ("Advanced from STARTED to LOCKED_IN, height = 431" )
223229 assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'locked_in' )
224230
225231 # Generate 140 more version 4 blocks
226232 test_blocks = self .generate_blocks (140 , 4 )
227- yield TestInstance (test_blocks , sync_every_block = False )
233+ self . sync_blocks (test_blocks )
228234
229235 # Inputs at height = 572
230236 #
@@ -267,7 +273,7 @@ def get_tests(self):
267273
268274 # 2 more version 4 blocks
269275 test_blocks = self .generate_blocks (2 , 4 )
270- yield TestInstance (test_blocks , sync_every_block = False )
276+ self . sync_blocks (test_blocks )
271277
272278 self .log .info ("Not yet advanced to ACTIVE, height = 574 (will activate for block 576, not 575)" )
273279 assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'locked_in' )
@@ -321,7 +327,7 @@ def get_tests(self):
321327 # try BIP 112 with seq=9 txs
322328 success_txs .extend (all_rlt_txs (bip112txs_vary_nSequence_9_v1 ))
323329 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 )])
325331 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
326332
327333 self .log .info ("Test version 2 txs" )
@@ -340,12 +346,12 @@ def get_tests(self):
340346 # try BIP 112 with seq=9 txs
341347 success_txs .extend (all_rlt_txs (bip112txs_vary_nSequence_9_v2 ))
342348 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 )])
344350 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
345351
346352 # 1 more version 4 block to get us to height 575 so the fork should now be active for the next block
347353 test_blocks = self .generate_blocks (1 , 4 )
348- yield TestInstance (test_blocks , sync_every_block = False )
354+ self . sync_blocks (test_blocks )
349355 assert_equal (get_bip9_status (self .nodes [0 ], 'csv' )['status' ], 'active' )
350356
351357 self .log .info ("Post-Soft Fork Tests." )
@@ -357,74 +363,74 @@ def get_tests(self):
357363 bip113tx_v2 .nLockTime = self .last_block_time - 600 * 5 # = MTP of prior block (not <) but < time put on current block
358364 bip113signed2 = sign_transaction (self .nodes [0 ], bip113tx_v2 )
359365 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 )
361367 # BIP 113 tests should now pass if the locktime is < MTP
362368 bip113tx_v1 .nLockTime = self .last_block_time - 600 * 5 - 1 # < MTP of prior block
363369 bip113signed1 = sign_transaction (self .nodes [0 ], bip113tx_v1 )
364370 bip113tx_v2 .nLockTime = self .last_block_time - 600 * 5 - 1 # < MTP of prior block
365371 bip113signed2 = sign_transaction (self .nodes [0 ], bip113tx_v2 )
366372 for bip113tx in [bip113signed1 , bip113signed2 ]:
367- yield TestInstance ([[ self .create_test_block ([bip113tx ]), True ] ])
373+ self . sync_blocks ([ self .create_test_block ([bip113tx ])])
368374 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
369375
370376 # Next block height = 580 after 4 blocks of random version
371377 test_blocks = self .generate_blocks (4 , 1234 )
372- yield TestInstance (test_blocks , sync_every_block = False )
378+ self . sync_blocks (test_blocks )
373379
374380 self .log .info ("BIP 68 tests" )
375381 self .log .info ("Test version 1 txs - all should still pass" )
376382
377383 success_txs = []
378384 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 )])
380386 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
381387
382388 self .log .info ("Test version 2 txs" )
383389
384390 # All txs with SEQUENCE_LOCKTIME_DISABLE_FLAG set pass
385391 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 )])
387393 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
388394
389395 # All txs without flag fail as we are at delta height = 8 < 10 and delta time = 8 * 600 < 10 * 512
390396 bip68timetxs = [tx ['tx' ] for tx in bip68txs_v2 if not tx ['sdf' ] and tx ['stf' ]]
391397 for tx in bip68timetxs :
392- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
398+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
393399
394400 bip68heighttxs = [tx ['tx' ] for tx in bip68txs_v2 if not tx ['sdf' ] and not tx ['stf' ]]
395401 for tx in bip68heighttxs :
396- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
402+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
397403
398404 # Advance one block to 581
399405 test_blocks = self .generate_blocks (1 , 1234 )
400- yield TestInstance (test_blocks , sync_every_block = False )
406+ self . sync_blocks (test_blocks )
401407
402408 # Height txs should fail and time txs should now pass 9 * 600 > 10 * 512
403409 bip68success_txs .extend (bip68timetxs )
404- yield TestInstance ([[ self .create_test_block (bip68success_txs ), True ] ])
410+ self . sync_blocks ([ self .create_test_block (bip68success_txs )])
405411 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
406412 for tx in bip68heighttxs :
407- yield TestInstance ([[ self .create_test_block ([tx ]), False ]] )
413+ self . sync_blocks ([ self .create_test_block ([tx ])], success = False )
408414
409415 # Advance one block to 582
410416 test_blocks = self .generate_blocks (1 , 1234 )
411- yield TestInstance (test_blocks , sync_every_block = False )
417+ self . sync_blocks (test_blocks )
412418
413419 # All BIP 68 txs should pass
414420 bip68success_txs .extend (bip68heighttxs )
415- yield TestInstance ([[ self .create_test_block (bip68success_txs ), True ] ])
421+ self . sync_blocks ([ self .create_test_block (bip68success_txs )])
416422 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
417423
418424 self .log .info ("BIP 112 tests" )
419425 self .log .info ("Test version 1 txs" )
420426
421427 # -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 )
423429 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 1 txs should still pass
424430
425431 success_txs = [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_v1 if tx ['sdf' ]]
426432 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 )])
428434 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
429435
430436 # 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):
433439 fail_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_9_v1 if not tx ['sdf' ]]
434440 fail_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_9_v1 if not tx ['sdf' ]]
435441 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 )
437443
438- self .log .info ("Version 2 txs" )
444+ self .log .info ("Test version 2 txs" )
439445
440446 # -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 )
442448
443449 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in argument to OP_CSV, version 2 txs should pass (all sequence locks are met)
444450 success_txs = [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_v2 if tx ['sdf' ]]
445451 success_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_9_v2 if tx ['sdf' ]]
446452
447- yield TestInstance ([[ self .create_test_block (success_txs ), True ] ])
453+ self . sync_blocks ([ self .create_test_block (success_txs )])
448454 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
449455
450456 # SEQUENCE_LOCKTIME_DISABLE_FLAG is unset in argument to OP_CSV for all remaining txs ##
@@ -453,23 +459,23 @@ def get_tests(self):
453459 fail_txs = all_rlt_txs (bip112txs_vary_nSequence_9_v2 )
454460 fail_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_9_v2 if not tx ['sdf' ]]
455461 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 )
457463
458464 # If SEQUENCE_LOCKTIME_DISABLE_FLAG is set in nSequence, tx should fail
459465 fail_txs = [tx ['tx' ] for tx in bip112txs_vary_nSequence_v2 if tx ['sdf' ]]
460466 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 )
462468
463469 # If sequencelock types mismatch, tx should fail
464470 fail_txs = [tx ['tx' ] for tx in bip112txs_vary_nSequence_v2 if not tx ['sdf' ] and tx ['stf' ]]
465471 fail_txs += [tx ['tx' ] for tx in bip112txs_vary_OP_CSV_v2 if not tx ['sdf' ] and tx ['stf' ]]
466472 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 )
468474
469475 # Remaining txs should pass, just test masking works properly
470476 success_txs = [tx ['tx' ] for tx in bip112txs_vary_nSequence_v2 if not tx ['sdf' ] and not tx ['stf' ]]
471477 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 )])
473479 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
474480
475481 # Additional test, of checking that comparison of two time types works properly
@@ -479,7 +485,7 @@ def get_tests(self):
479485 signtx = sign_transaction (self .nodes [0 ], tx )
480486 time_txs .append (signtx )
481487
482- yield TestInstance ([[ self .create_test_block (time_txs ), True ] ])
488+ self . sync_blocks ([ self .create_test_block (time_txs )])
483489 self .nodes [0 ].invalidateblock (self .nodes [0 ].getbestblockhash ())
484490
485491 # TODO: Test empty stack fails
0 commit comments