Skip to content

Commit 3634aaf

Browse files
committed
Merge #422: feature_fedpeg.py bitcoind parent2 uses rpc cookie auth
f6528e9 feature_fedpeg.py parent2 uses rpc cookie auth (Gregory Sanders) f349e17 allow start_node to create/take cookie files (Gregory Sanders)
2 parents f9231a2 + f6528e9 commit 3634aaf

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

qa/rpc-tests/feature_fedpeg.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,41 @@ def add_options(self, parser):
3939
def setup_network(self, split=False):
4040
if self.options.parent_bitcoin and self.options.parent_binpath == "":
4141
raise Exception("Can't run with --parent_bitcoin without specifying --parent_binpath")
42-
4342
self.nodes = []
4443
self.extra_args = []
4544
# Parent chain args
4645
for n in range(2):
46+
# We want to test the rpc cookie method so we force the use of a
47+
# dummy conf file to avoid loading rpcuser/rpcpassword lines
48+
use_cookie_auth = n==1
49+
rpc_u, rpc_p = rpc_auth_pair(n)
4750
if self.options.parent_bitcoin:
4851
self.parent_chain = 'regtest'
49-
rpc_u, rpc_p = rpc_auth_pair(n)
5052
self.extra_args.append([
53+
"-conf=dummy",
5154
"-printtoconsole=0",
52-
"-port="+str(p2p_port(n)),
53-
"-rpcuser="+rpc_u,
54-
"-rpcpassword="+rpc_p,
55-
"-rpcport="+str(rpc_port(n)),
5655
"-addresstype=legacy", # To make sure bitcoind gives back p2pkh no matter version
5756
"-deprecatedrpc=validateaddress",
57+
"-port="+str(p2p_port(n)),
58+
"-rpcport="+str(rpc_port(n))
5859
])
5960
else:
6061
self.parent_chain = 'parent'
6162
self.extra_args.append([
63+
"-conf=dummy",
6264
"-printtoconsole=0",
6365
'-validatepegin=0',
6466
'-anyonecanspendaremine',
6567
'-initialfreecoins=2100000000000000',
68+
"-port="+str(p2p_port(n)),
69+
"-rpcport="+str(rpc_port(n))
6670
])
71+
# Only first parent uses name/password, the 2nd uses cookie auth
72+
if not use_cookie_auth:
73+
self.extra_args[n].extend(["-rpcuser="+rpc_u, "-rpcpassword="+rpc_p])
74+
6775
self.binary = self.options.parent_binpath if self.options.parent_binpath != "" else None
68-
self.nodes.append(start_node(n, self.options.tmpdir, self.extra_args[n], binary=self.binary, chain=self.parent_chain))
76+
self.nodes.append(start_node(n, self.options.tmpdir, self.extra_args[n], binary=self.binary, chain=self.parent_chain, cookie_auth=use_cookie_auth))
6977

7078
connect_nodes_bi(self.nodes, 0, 1)
7179
self.parentgenesisblockhash = self.nodes[0].getblockhash(0)
@@ -77,6 +85,7 @@ def setup_network(self, split=False):
7785
self.fedpeg_script = "512103dff4923d778550cc13ce0d887d737553b4b58f4e8e886507fc39f5e447b2186451ae"
7886
parent_chain_signblockscript = '51'
7987
for n in range(2):
88+
used_cookie_auth = n==1
8089
rpc_u, rpc_p = rpc_auth_pair(n)
8190
args = [
8291
"-printtoconsole=0",
@@ -88,8 +97,6 @@ def setup_network(self, split=False):
8897
'-peginconfirmationdepth=10',
8998
'-mainchainrpchost=127.0.0.1',
9099
'-mainchainrpcport=%s' % rpc_port(n),
91-
'-mainchainrpcuser=%s' % rpc_u,
92-
'-mainchainrpcpassword=%s' % rpc_p,
93100
]
94101
if not self.options.parent_bitcoin:
95102
args.extend([
@@ -98,9 +105,21 @@ def setup_network(self, split=False):
98105
'-con_parent_chain_signblockscript=%s' % parent_chain_signblockscript,
99106
'-con_parent_pegged_asset=%s' % parent_pegged_asset,
100107
])
108+
109+
if used_cookie_auth:
110+
# Need to specify where to find parent cookie file
111+
datadir = os.path.join(self.options.tmpdir, "node"+str(n))
112+
args.append('-mainchainrpccookiefile='+datadir+"/" + self.parent_chain + "/.cookie")
113+
else:
114+
args.extend([
115+
'-mainchainrpcuser=%s' % rpc_u,
116+
'-mainchainrpcpassword=%s' % rpc_p,
117+
])
118+
101119
self.extra_args.append(args)
102120
self.nodes.append(start_node(n + 2, self.options.tmpdir, self.extra_args[n + 2], chain='sidechain'))
103121

122+
# We only connect the same-chain nodes, so sync_all works correctly
104123
connect_nodes_bi(self.nodes, 2, 3)
105124
self.is_network_split = True
106125
self.sync_all()
@@ -297,7 +316,7 @@ def run_test(self):
297316
assert(sidechain.getblockcount() != sidechain2.getblockcount())
298317

299318
print("Restarting parent2")
300-
self.nodes[1] = start_node(1, self.options.tmpdir, self.extra_args[1], binary=self.binary, chain=self.parent_chain)
319+
self.nodes[1] = start_node(1, self.options.tmpdir, self.extra_args[1], binary=self.binary, chain=self.parent_chain, cookie_auth=True)
301320
parent2 = self.nodes[1]
302321
connect_nodes_bi(self.nodes, 0, 1)
303322
time.sleep(5)

qa/rpc-tests/test_framework/util.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import time
2323
import re
2424
import errno
25+
from pathlib import Path
2526

2627
from . import coverage
2728
from .authproxy import AuthServiceProxy, JSONRPCException
@@ -198,8 +199,13 @@ def initialize_datadir(dirname, n):
198199
def 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

213219
def 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

Comments
 (0)