18
18
import sys
19
19
import tempfile
20
20
import time
21
+ import types
21
22
22
23
from .address import create_deterministic_address_bcrt1_p2tr_op_true
23
24
from .authproxy import JSONRPCException
@@ -56,6 +57,48 @@ def __init__(self, message):
56
57
self .message = message
57
58
58
59
60
+ class Binaries :
61
+ """Helper class to provide information about bitcoin binaries
62
+
63
+ Attributes:
64
+ paths: Object returned from get_binary_paths() containing information
65
+ which binaries and command lines to use from environment variables and
66
+ the config file.
67
+ bin_dir: An optional string containing a directory path to look for
68
+ binaries, which takes precedence over the paths above, if specified.
69
+ This is used by tests calling binaries from previous releases.
70
+ """
71
+ def __init__ (self , paths , bin_dir ):
72
+ self .paths = paths
73
+ self .bin_dir = bin_dir
74
+
75
+ def daemon_argv (self ):
76
+ "Return argv array that should be used to invoke bitcoind"
77
+ return self ._argv (self .paths .bitcoind )
78
+
79
+ def rpc_argv (self ):
80
+ "Return argv array that should be used to invoke bitcoin-cli"
81
+ return self ._argv (self .paths .bitcoincli )
82
+
83
+ def util_argv (self ):
84
+ "Return argv array that should be used to invoke bitcoin-util"
85
+ return self ._argv (self .paths .bitcoinutil )
86
+
87
+ def wallet_argv (self ):
88
+ "Return argv array that should be used to invoke bitcoin-wallet"
89
+ return self ._argv (self .paths .bitcoinwallet )
90
+
91
+ def _argv (self , bin_path ):
92
+ """Return argv array that should be used to invoke the command.
93
+ Normally this will return binary paths directly from the paths object,
94
+ but when bin_dir is set (by tests calling binaries from previous
95
+ releases) it will return paths relative to bin_dir instead."""
96
+ if self .bin_dir is not None :
97
+ return [os .path .join (self .bin_dir , os .path .basename (bin_path ))]
98
+ else :
99
+ return [bin_path ]
100
+
101
+
59
102
class BitcoinTestMetaClass (type ):
60
103
"""Metaclass for BitcoinTestFramework.
61
104
@@ -220,6 +263,7 @@ def parse_args(self, test_file):
220
263
config = configparser .ConfigParser ()
221
264
config .read_file (open (self .options .configfile ))
222
265
self .config = config
266
+ self .binary_paths = self .get_binary_paths ()
223
267
if self .options .v1transport :
224
268
self .options .v2transport = False
225
269
@@ -239,9 +283,10 @@ def parse_args(self, test_file):
239
283
240
284
PortSeed .n = self .options .port_seed
241
285
242
- def set_binary_paths (self ):
243
- """Update self.options with the paths of all binaries from environment variables or their default values"""
286
+ def get_binary_paths (self ):
287
+ """Get paths of all binaries from environment variables or their default values"""
244
288
289
+ paths = types .SimpleNamespace ()
245
290
binaries = {
246
291
"bitcoind" : ("bitcoind" , "BITCOIND" ),
247
292
"bitcoin-cli" : ("bitcoincli" , "BITCOINCLI" ),
@@ -254,7 +299,11 @@ def set_binary_paths(self):
254
299
"bin" ,
255
300
binary + self .config ["environment" ]["EXEEXT" ],
256
301
)
257
- setattr (self .options , attribute_name , os .getenv (env_variable_name , default = default_filename ))
302
+ setattr (paths , attribute_name , os .getenv (env_variable_name , default = default_filename ))
303
+ return paths
304
+
305
+ def get_binaries (self , bin_dir = None ):
306
+ return Binaries (self .binary_paths , bin_dir )
258
307
259
308
def setup (self ):
260
309
"""Call this method to start up the test framework object with options set."""
@@ -265,8 +314,6 @@ def setup(self):
265
314
266
315
config = self .config
267
316
268
- self .set_binary_paths ()
269
-
270
317
os .environ ['PATH' ] = os .pathsep .join ([
271
318
os .path .join (config ['environment' ]['BUILDDIR' ], 'bin' ),
272
319
os .environ ['PATH' ]
@@ -473,14 +520,14 @@ def add_wallet_options(self, parser, *, descriptors=True, legacy=True):
473
520
group .add_argument ("--legacy-wallet" , action = 'store_const' , const = False , ** kwargs ,
474
521
help = "Run test using legacy wallets" , dest = 'descriptors' )
475
522
476
- def add_nodes (self , num_nodes : int , extra_args = None , * , rpchost = None , binary = None , binary_cli = None , versions = None ):
523
+ def add_nodes (self , num_nodes : int , extra_args = None , * , rpchost = None , versions = None ):
477
524
"""Instantiate TestNode objects.
478
525
479
526
Should only be called once after the nodes have been specified in
480
527
set_test_params()."""
481
- def get_bin_from_version (version , bin_name , bin_default ):
528
+ def bin_dir_from_version (version ):
482
529
if not version :
483
- return bin_default
530
+ return None
484
531
if version > 219999 :
485
532
# Starting at client version 220000 the first two digits represent
486
533
# the major version, e.g. v22.0 instead of v0.22.0.
@@ -498,7 +545,6 @@ def get_bin_from_version(version, bin_name, bin_default):
498
545
),
499
546
),
500
547
'bin' ,
501
- bin_name ,
502
548
)
503
549
504
550
if self .bind_to_localhost_only :
@@ -513,13 +559,12 @@ def get_bin_from_version(version, bin_name, bin_default):
513
559
extra_args [
i ]
= extra_args [
i ]
+ [
"-whitelist=noban,in,[email protected] " ]
514
560
if versions is None :
515
561
versions = [None ] * num_nodes
516
- if binary is None :
517
- binary = [get_bin_from_version (v , 'bitcoind' , self .options .bitcoind ) for v in versions ]
518
- if binary_cli is None :
519
- binary_cli = [get_bin_from_version (v , 'bitcoin-cli' , self .options .bitcoincli ) for v in versions ]
562
+ bin_dirs = [bin_dir_from_version (v ) for v in versions ]
520
563
# Fail test if any of the needed release binaries is missing
521
564
bins_missing = False
522
- for bin_path in binary + binary_cli :
565
+ for bin_path in (argv [0 ] for bin_dir in bin_dirs
566
+ for binaries in (self .get_binaries (bin_dir ),)
567
+ for argv in (binaries .daemon_argv (), binaries .rpc_argv ())):
523
568
if shutil .which (bin_path ) is None :
524
569
self .log .error (f"Binary not found: { bin_path } " )
525
570
bins_missing = True
@@ -529,8 +574,7 @@ def get_bin_from_version(version, bin_name, bin_default):
529
574
assert_equal (len (extra_confs ), num_nodes )
530
575
assert_equal (len (extra_args ), num_nodes )
531
576
assert_equal (len (versions ), num_nodes )
532
- assert_equal (len (binary ), num_nodes )
533
- assert_equal (len (binary_cli ), num_nodes )
577
+ assert_equal (len (bin_dirs ), num_nodes )
534
578
for i in range (num_nodes ):
535
579
args = list (extra_args [i ])
536
580
test_node_i = TestNode (
@@ -540,8 +584,7 @@ def get_bin_from_version(version, bin_name, bin_default):
540
584
rpchost = rpchost ,
541
585
timewait = self .rpc_timeout ,
542
586
timeout_factor = self .options .timeout_factor ,
543
- bitcoind = binary [i ],
544
- bitcoin_cli = binary_cli [i ],
587
+ binaries = self .get_binaries (bin_dirs [i ]),
545
588
version = versions [i ],
546
589
coverage_dir = self .options .coveragedir ,
547
590
cwd = self .options .tmpdir ,
@@ -852,8 +895,7 @@ def _initialize_chain(self):
852
895
rpchost = None ,
853
896
timewait = self .rpc_timeout ,
854
897
timeout_factor = self .options .timeout_factor ,
855
- bitcoind = self .options .bitcoind ,
856
- bitcoin_cli = self .options .bitcoincli ,
898
+ binaries = self .get_binaries (),
857
899
coverage_dir = None ,
858
900
cwd = self .options .tmpdir ,
859
901
descriptors = self .options .descriptors ,
0 commit comments