@@ -181,21 +181,39 @@ def initialize_datadir(dirname, n):
181
181
datadir = os .path .join (dirname , "node" + str (n ))
182
182
if not os .path .isdir (datadir ):
183
183
os .makedirs (datadir )
184
- rpc_u , rpc_p = rpc_auth_pair (n )
185
184
with open (os .path .join (datadir , "bitcoin.conf" ), 'w' , encoding = 'utf8' ) as f :
186
185
f .write ("regtest=1\n " )
187
- f .write ("rpcuser=" + rpc_u + "\n " )
188
- f .write ("rpcpassword=" + rpc_p + "\n " )
189
186
f .write ("port=" + str (p2p_port (n ))+ "\n " )
190
187
f .write ("rpcport=" + str (rpc_port (n ))+ "\n " )
191
188
f .write ("listenonion=0\n " )
192
189
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 )
199
217
host = '127.0.0.1'
200
218
port = rpc_port (i )
201
219
if rpchost :
@@ -206,7 +224,7 @@ def rpc_url(i, rpchost=None):
206
224
host = rpchost
207
225
return "http://%s:%s@%s:%d" % (rpc_u , rpc_p , host , int (port ))
208
226
209
- def wait_for_bitcoind_start (process , url , i ):
227
+ def wait_for_bitcoind_start (process , datadir , i ):
210
228
'''
211
229
Wait for bitcoind to start. This means that RPC is accessible and fully initialized.
212
230
Raise an exception if bitcoind exits during initialization.
@@ -215,7 +233,8 @@ def wait_for_bitcoind_start(process, url, i):
215
233
if process .poll () is not None :
216
234
raise Exception ('bitcoind exited with status %i during initialization' % process .returncode )
217
235
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 )
219
238
blocks = rpc .getblockcount ()
220
239
break # break out of loop on success
221
240
except IOError as e :
@@ -224,6 +243,9 @@ def wait_for_bitcoind_start(process, url, i):
224
243
except JSONRPCException as e : # Initialization phase
225
244
if e .error ['code' ] != - 28 : # RPC in warmup?
226
245
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
227
249
time .sleep (0.25 )
228
250
229
251
@@ -239,10 +261,9 @@ def _start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary
239
261
if extra_args is not None : args .extend (extra_args )
240
262
bitcoind_processes [i ] = subprocess .Popen (args , stderr = stderr )
241
263
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 )
244
265
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 )
246
267
247
268
if COVERAGE_DIR :
248
269
coverage .write_all_rpc_commands (COVERAGE_DIR , proxy )
0 commit comments