29
29
disconnect_nodes ,
30
30
get_datadir_path ,
31
31
initialize_datadir ,
32
- p2p_port ,
33
32
sync_blocks ,
34
33
sync_mempools ,
35
34
)
@@ -468,48 +467,34 @@ def _start_logging(self):
468
467
def _initialize_chain (self ):
469
468
"""Initialize a pre-mined blockchain for use by the test.
470
469
471
- Create a cache of a 199-block-long chain (with wallet) for MAX_NODES
470
+ Create a cache of a 199-block-long chain
472
471
Afterward, create num_nodes copies from the cache."""
473
472
473
+ CACHE_NODE_ID = 0 # Use node 0 to create the cache for all other nodes
474
+ cache_node_dir = get_datadir_path (self .options .cachedir , CACHE_NODE_ID )
474
475
assert self .num_nodes <= MAX_NODES
475
- create_cache = False
476
- for i in range (MAX_NODES ):
477
- if not os .path .isdir (get_datadir_path (self .options .cachedir , i )):
478
- create_cache = True
479
- break
480
-
481
- if create_cache :
482
- self .log .debug ("Creating data directories from cached datadir" )
483
-
484
- # find and delete old cache directories if any exist
485
- for i in range (MAX_NODES ):
486
- if os .path .isdir (get_datadir_path (self .options .cachedir , i )):
487
- shutil .rmtree (get_datadir_path (self .options .cachedir , i ))
488
-
489
- # Create cache directories, run bitcoinds:
490
- for i in range (MAX_NODES ):
491
- datadir = initialize_datadir (self .options .cachedir , i )
492
- args = [self .options .bitcoind , "-datadir=" + datadir , '-disablewallet' ]
493
- if i > 0 :
494
- args .append ("-connect=127.0.0.1:" + str (p2p_port (0 )))
495
- self .nodes .append (TestNode (
496
- i ,
497
- get_datadir_path (self .options .cachedir , i ),
476
+
477
+ if not os .path .isdir (cache_node_dir ):
478
+ self .log .debug ("Creating cache directory {}" .format (cache_node_dir ))
479
+
480
+ initialize_datadir (self .options .cachedir , CACHE_NODE_ID )
481
+ self .nodes .append (
482
+ TestNode (
483
+ CACHE_NODE_ID ,
484
+ cache_node_dir ,
498
485
extra_conf = ["bind=127.0.0.1" ],
499
- extra_args = [],
486
+ extra_args = ['-disablewallet' ],
500
487
rpchost = None ,
501
488
timewait = self .rpc_timeout ,
502
489
bitcoind = self .options .bitcoind ,
503
490
bitcoin_cli = self .options .bitcoincli ,
504
491
coverage_dir = None ,
505
492
cwd = self .options .tmpdir ,
506
493
))
507
- self .nodes [i ].args = args
508
- self .start_node (i )
494
+ self .start_node (CACHE_NODE_ID )
509
495
510
496
# Wait for RPC connections to be ready
511
- for node in self .nodes :
512
- node .wait_for_rpc_connection ()
497
+ self .nodes [CACHE_NODE_ID ].wait_for_rpc_connection ()
513
498
514
499
# Create a 199-block-long chain; each of the 4 first nodes
515
500
# gets 25 mature blocks and 25 immature.
@@ -518,29 +503,29 @@ def _initialize_chain(self):
518
503
# This is needed so that we are out of IBD when the test starts,
519
504
# see the tip age check in IsInitialBlockDownload().
520
505
for i in range (8 ):
521
- self .nodes [0 ].generatetoaddress (25 if i != 7 else 24 , self .nodes [i % 4 ].get_deterministic_priv_key ().address )
522
- self .sync_blocks ()
506
+ self .nodes [CACHE_NODE_ID ].generatetoaddress (
507
+ nblocks = 25 if i != 7 else 24 ,
508
+ address = TestNode .PRIV_KEYS [i % 4 ].address ,
509
+ )
523
510
524
- for n in self .nodes :
525
- assert_equal (n .getblockchaininfo ()["blocks" ], 199 )
511
+ assert_equal (self .nodes [CACHE_NODE_ID ].getblockchaininfo ()["blocks" ], 199 )
526
512
527
- # Shut them down, and clean up cache directories:
513
+ # Shut it down, and clean up cache directories:
528
514
self .stop_nodes ()
529
515
self .nodes = []
530
516
531
- def cache_path (n , * paths ):
532
- return os .path .join (get_datadir_path ( self . options . cachedir , n ) , "regtest" , * paths )
517
+ def cache_path (* paths ):
518
+ return os .path .join (cache_node_dir , "regtest" , * paths )
533
519
534
- for i in range (MAX_NODES ):
535
- os .rmdir (cache_path (i , 'wallets' )) # Remove empty wallets dir
536
- for entry in os .listdir (cache_path (i )):
537
- if entry not in ['chainstate' , 'blocks' ]:
538
- os .remove (cache_path (i , entry ))
520
+ os .rmdir (cache_path ('wallets' )) # Remove empty wallets dir
521
+ for entry in os .listdir (cache_path ()):
522
+ if entry not in ['chainstate' , 'blocks' ]: # Only keep chainstate and blocks folder
523
+ os .remove (cache_path (entry ))
539
524
540
525
for i in range (self .num_nodes ):
541
- from_dir = get_datadir_path ( self .options . cachedir , i )
526
+ self .log . debug ( "Copy cache directory {} to node {}" . format ( cache_node_dir , i ) )
542
527
to_dir = get_datadir_path (self .options .tmpdir , i )
543
- shutil .copytree (from_dir , to_dir )
528
+ shutil .copytree (cache_node_dir , to_dir )
544
529
initialize_datadir (self .options .tmpdir , i ) # Overwrite port/rpcport in bitcoin.conf
545
530
546
531
def _initialize_chain_clean (self ):
0 commit comments