allow start_node to create/take cookie files

This commit is contained in:
Gregory Sanders 2018-09-27 14:59:13 -04:00
parent 5fe9c2cbeb
commit f349e17298

View file

@ -22,6 +22,7 @@ import subprocess
import time
import re
import errno
from pathlib import Path
from . import coverage
from .authproxy import AuthServiceProxy, JSONRPCException
@ -198,8 +199,13 @@ def initialize_datadir(dirname, n):
def rpc_auth_pair(n):
return 'rpcuser💻' + str(n), 'rpcpass🔑' + str(n)
def rpc_url(i, rpchost=None):
rpc_u, rpc_p = rpc_auth_pair(i)
def rpc_url(i, rpchost=None, cookie_file=None):
if cookie_file:
with open(cookie_file, 'r') as f:
rpc_auth = f.readline()
else:
rpc_u, rpc_p = rpc_auth_pair(i)
rpc_auth = rpc_u+":"+rpc_p
host = '127.0.0.1'
port = rpc_port(i)
if rpchost:
@ -208,7 +214,7 @@ def rpc_url(i, rpchost=None):
host, port = parts
else:
host = rpchost
return "http://%s:%s@%s:%d" % (rpc_u, rpc_p, host, int(port))
return "http://%s@%s:%d" % (rpc_auth, host, int(port))
def wait_for_bitcoind_start(process, url, i):
'''
@ -333,11 +339,12 @@ def _rpchost_to_args(rpchost):
rv += ['-rpcport=' + rpcport]
return rv
def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None, chain='elementsregtest'):
def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None, chain='elementsregtest', cookie_auth=False):
"""
Start a bitcoind and return RPC connection to it
"""
datadir = os.path.join(dirname, "node"+str(i))
cookie_file = datadir+"/"+chain+"/.cookie"
if binary is None:
binary = os.getenv("ELEMENTSD", "elementsd")
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=
bitcoind_processes[i] = subprocess.Popen(args)
if os.getenv("PYTHON_DEBUG", ""):
print("start_node: bitcoind started, waiting for RPC to come up")
url = rpc_url(i, rpchost)
# We need to make sure the cookie auth file is created before reading it
wait_for_cookie_time = 10
cookie_file_handle = Path(cookie_file)
while cookie_auth and wait_for_cookie_time > 0 and not cookie_file_handle.is_file():
wait_for_cookie_time -= 1
time.sleep(1)
url = rpc_url(i, rpchost, cookie_file if cookie_auth else None)
wait_for_bitcoind_start(bitcoind_processes[i], url, i)
if os.getenv("PYTHON_DEBUG", ""):
print("start_node: RPC successfully started")