2222import time
2323import re
2424import errno
25+ from pathlib import Path
2526
2627from . import coverage
2728from .authproxy import AuthServiceProxy , JSONRPCException
@@ -198,8 +199,13 @@ def initialize_datadir(dirname, n):
198199def rpc_auth_pair (n ):
199200 return 'rpcuser💻' + str (n ), 'rpcpass🔑' + str (n )
200201
201- def rpc_url (i , rpchost = None ):
202- rpc_u , rpc_p = rpc_auth_pair (i )
202+ def rpc_url (i , rpchost = None , cookie_file = None ):
203+ if cookie_file :
204+ with open (cookie_file , 'r' ) as f :
205+ rpc_auth = f .readline ()
206+ else :
207+ rpc_u , rpc_p = rpc_auth_pair (i )
208+ rpc_auth = rpc_u + ":" + rpc_p
203209 host = '127.0.0.1'
204210 port = rpc_port (i )
205211 if rpchost :
@@ -208,7 +214,7 @@ def rpc_url(i, rpchost=None):
208214 host , port = parts
209215 else :
210216 host = rpchost
211- return "http://%s:%s @%s:%d" % (rpc_u , rpc_p , host , int (port ))
217+ return "http://%s@%s:%d" % (rpc_auth , host , int (port ))
212218
213219def wait_for_bitcoind_start (process , url , i ):
214220 '''
@@ -333,11 +339,12 @@ def _rpchost_to_args(rpchost):
333339 rv += ['-rpcport=' + rpcport ]
334340 return rv
335341
336- def start_node (i , dirname , extra_args = None , rpchost = None , timewait = None , binary = None , chain = 'elementsregtest' ):
342+ def start_node (i , dirname , extra_args = None , rpchost = None , timewait = None , binary = None , chain = 'elementsregtest' , cookie_auth = False ):
337343 """
338344 Start a bitcoind and return RPC connection to it
339345 """
340346 datadir = os .path .join (dirname , "node" + str (i ))
347+ cookie_file = datadir + "/" + chain + "/.cookie"
341348 if binary is None :
342349 binary = os .getenv ("ELEMENTSD" , "elementsd" )
343350 args = [ binary , "-datadir=" + datadir , "-server" , "-keypool=1" , "-discover=0" , "-rest" , "-mocktime=" + str (get_mocktime ()) ]
@@ -346,7 +353,15 @@ def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=
346353 bitcoind_processes [i ] = subprocess .Popen (args )
347354 if os .getenv ("PYTHON_DEBUG" , "" ):
348355 print ("start_node: bitcoind started, waiting for RPC to come up" )
349- url = rpc_url (i , rpchost )
356+
357+ # We need to make sure the cookie auth file is created before reading it
358+ wait_for_cookie_time = 10
359+ cookie_file_handle = Path (cookie_file )
360+ while cookie_auth and wait_for_cookie_time > 0 and not cookie_file_handle .is_file ():
361+ wait_for_cookie_time -= 1
362+ time .sleep (1 )
363+
364+ url = rpc_url (i , rpchost , cookie_file if cookie_auth else None )
350365 wait_for_bitcoind_start (bitcoind_processes [i ], url , i )
351366 if os .getenv ("PYTHON_DEBUG" , "" ):
352367 print ("start_node: RPC successfully started" )
0 commit comments