@@ -181,21 +181,39 @@ def initialize_datadir(dirname, n):
181181 datadir = os .path .join (dirname , "node" + str (n ))
182182 if not os .path .isdir (datadir ):
183183 os .makedirs (datadir )
184- rpc_u , rpc_p = rpc_auth_pair (n )
185184 with open (os .path .join (datadir , "bitcoin.conf" ), 'w' , encoding = 'utf8' ) as f :
186185 f .write ("regtest=1\n " )
187- f .write ("rpcuser=" + rpc_u + "\n " )
188- f .write ("rpcpassword=" + rpc_p + "\n " )
189186 f .write ("port=" + str (p2p_port (n ))+ "\n " )
190187 f .write ("rpcport=" + str (rpc_port (n ))+ "\n " )
191188 f .write ("listenonion=0\n " )
192189 return datadir
193-
194- def rpc_auth_pair (n ):
195- return 'rpcuser💻' + str (n ), 'rpcpass🔑' + str (n )
196-
197- def rpc_url (i , rpchost = None ):
198- rpc_u , rpc_p = rpc_auth_pair (i )
190+
191+ def get_datadir_path (dirname , n ):
192+ return os .path .join (dirname , "node" + str (n ))
193+
194+ def get_auth_cookie (datadir , n ):
195+ if os .path .isfile (os .path .join (datadir , "regtest" , ".cookie" )):
196+ with open (os .path .join (datadir , "regtest" , ".cookie" ), 'r' ) as f :
197+ userpass = f .read ()
198+ split_userpass = userpass .split (':' )
199+ return split_userpass [0 ], split_userpass [1 ]
200+ else :
201+ with open (os .path .join (datadir , "bitcoin.conf" ), 'r' ) as f :
202+ user = None
203+ password = None
204+ for line in f :
205+ if line .startswith ("rpcuser=" ):
206+ assert user is None # Ensure that there is only one rpcuser line
207+ user = line .split ("=" )[1 ].strip ("\n " )
208+ if line .startswith ("rpcpassword=" ):
209+ assert password is None # Ensure that there is only one rpcpassword line
210+ password = line .split ("=" )[1 ].strip ("\n " )
211+ if user is None and password is None :
212+ raise ValueError ("No RPC credentials" )
213+ return user , password
214+
215+ def rpc_url (datadir , i , rpchost = None ):
216+ rpc_u , rpc_p = get_auth_cookie (datadir , i )
199217 host = '127.0.0.1'
200218 port = rpc_port (i )
201219 if rpchost :
@@ -206,7 +224,7 @@ def rpc_url(i, rpchost=None):
206224 host = rpchost
207225 return "http://%s:%s@%s:%d" % (rpc_u , rpc_p , host , int (port ))
208226
209- def wait_for_bitcoind_start (process , url , i ):
227+ def wait_for_bitcoind_start (process , datadir , i ):
210228 '''
211229 Wait for bitcoind to start. This means that RPC is accessible and fully initialized.
212230 Raise an exception if bitcoind exits during initialization.
@@ -215,7 +233,8 @@ def wait_for_bitcoind_start(process, url, i):
215233 if process .poll () is not None :
216234 raise Exception ('bitcoind exited with status %i during initialization' % process .returncode )
217235 try :
218- rpc = get_rpc_proxy (url , i )
236+ # Check if .cookie file to be created
237+ rpc = get_rpc_proxy (rpc_url (datadir , i ), i )
219238 blocks = rpc .getblockcount ()
220239 break # break out of loop on success
221240 except IOError as e :
@@ -224,6 +243,9 @@ def wait_for_bitcoind_start(process, url, i):
224243 except JSONRPCException as e : # Initialization phase
225244 if e .error ['code' ] != - 28 : # RPC in warmup?
226245 raise # unknown JSON RPC exception
246+ except ValueError as e : # cookie file not found and no rpcuser or rpcassword. bitcoind still starting
247+ if "No RPC credentials" not in str (e ):
248+ raise
227249 time .sleep (0.25 )
228250
229251
@@ -239,10 +261,9 @@ def _start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary
239261 if extra_args is not None : args .extend (extra_args )
240262 bitcoind_processes [i ] = subprocess .Popen (args , stderr = stderr )
241263 logger .debug ("initialize_chain: bitcoind started, waiting for RPC to come up" )
242- url = rpc_url (i , rpchost )
243- wait_for_bitcoind_start (bitcoind_processes [i ], url , i )
264+ wait_for_bitcoind_start (bitcoind_processes [i ], datadir , i )
244265 logger .debug ("initialize_chain: RPC successfully started" )
245- proxy = get_rpc_proxy (url , i , timeout = timewait )
266+ proxy = get_rpc_proxy (rpc_url ( datadir , i , rpchost ) , i , timeout = timewait )
246267
247268 if COVERAGE_DIR :
248269 coverage .write_all_rpc_commands (COVERAGE_DIR , proxy )
0 commit comments