Skip to content

Commit ed437c6

Browse files
committed
Spawn remotesigner instances for integration tests (#24)
* Made the integration tests spawn remotesigner instances for each lightningd. * fixes to make infrastructure more reliable
1 parent d725f70 commit ed437c6

File tree

5 files changed

+79
-11
lines changed

5 files changed

+79
-11
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ else
393393
PYTEST_OPTS += -x
394394
endif
395395

396+
ifneq ($(PYTEST_MOREOPTS),)
397+
PYTEST_OPTS += $(PYTEST_MOREOPTS)
398+
endif
399+
396400
check-units:
397401

398402
check: check-units installcheck check-protos pytest

contrib/pyln-testing/pyln/testing/utils.py

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,33 @@ def getnewaddress(self):
514514
info = self.rpc.getaddressinfo(addr)
515515
return info['unconfidential']
516516

517+
class RemoteSignerD(TailableProc):
518+
def __init__(self, rsignerd_dir, rsignerd_port):
519+
TailableProc.__init__(self, rsignerd_dir)
520+
self.executable = env("REMOTE_SIGNER_CMD", 'rsignerd')
521+
self.opts = [
522+
'--datadir={}'.format(rsignerd_dir),
523+
'--port={}'.format(rsignerd_port),
524+
]
525+
self.prefix = 'rsignerd'
526+
self.rsignerd_port = rsignerd_port
527+
528+
@property
529+
def cmd_line(self):
530+
return [self.executable] + self.opts
531+
532+
def start(self, stdin=None, stdout=None, stderr=None,
533+
wait_for_initialized=True):
534+
TailableProc.start(self, stdin, stdout, stderr)
535+
# We need to always wait for initialization
536+
self.wait_for_log("rsignerd [0-9]* ready on .*:{}".format(self.rsignerd_port))
537+
logging.info("rsignerd started")
538+
539+
def stop(self, timeout=10):
540+
logging.info("stopping rsignerd")
541+
rc = TailableProc.stop(self, timeout)
542+
logging.info("rsignerd stopped")
543+
return rc
517544

518545
class LightningD(TailableProc):
519546
def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, node_id=0):
@@ -523,7 +550,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
523550
self.port = port
524551
self.cmd_prefix = []
525552
self.disconnect_file = None
526-
553+
self.lightning_dir = lightning_dir
554+
self.rsignerd_dir = os.path.join(lightning_dir, "remotesigner")
555+
527556
self.rpcproxy = bitcoindproxy
528557

529558
self.opts = LIGHTNINGD_CONFIG.copy()
@@ -550,6 +579,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
550579
if not os.path.exists(os.path.join(lightning_dir, TEST_NETWORK)):
551580
os.makedirs(os.path.join(lightning_dir, TEST_NETWORK))
552581

582+
if not os.path.exists(self.rsignerd_dir):
583+
os.makedirs(self.rsignerd_dir)
584+
553585
# Last 32-bytes of final part of dir -> seed.
554586
seed = (bytes(re.search('([^/]+)/*$', lightning_dir).group(1), encoding='utf-8') + bytes(32))[:32]
555587
if not random_hsm:
@@ -561,6 +593,8 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
561593
self.prefix = 'lightningd-%d' % (node_id)
562594

563595
def cleanup(self):
596+
# Make sure the remotesigner is shutdown
597+
self.rsignerd.stop()
564598
# To force blackhole to exit, disconnect file must be truncated!
565599
if self.disconnect_file:
566600
with open(self.disconnect_file, "w") as f:
@@ -583,11 +617,34 @@ def cmd_line(self):
583617

584618
def start(self, stdin=None, stdout=None, stderr=None,
585619
wait_for_initialized=True):
586-
self.opts['bitcoin-rpcport'] = self.rpcproxy.rpcport
587-
TailableProc.start(self, stdin, stdout, stderr)
588-
if wait_for_initialized:
589-
self.wait_for_log("Server started with public key")
590-
logging.info("LightningD started")
620+
try:
621+
# Start the remote signer first
622+
rsignerd_port = reserve()
623+
self.rsignerd = RemoteSignerD(self.rsignerd_dir, rsignerd_port)
624+
self.rsignerd.start(stdin, stdout, stderr, wait_for_initialized)
625+
626+
# We can't do this in the constructor because we need a new port on each restart.
627+
self.env['REMOTE_HSMD_ENDPOINT'] = 'localhost:{}'.format(rsignerd_port)
628+
629+
self.opts['bitcoin-rpcport'] = self.rpcproxy.rpcport
630+
TailableProc.start(self, stdin, stdout, stderr)
631+
if wait_for_initialized:
632+
self.wait_for_log("Server started with public key")
633+
logging.info("LightningD started")
634+
except:
635+
# LightningD didn't start, stop the remotesigner
636+
self.rsignerd.stop()
637+
raise
638+
639+
def stop(self, timeout=10):
640+
# Stop the remote signer first
641+
self.rsignerd.stop(timeout)
642+
return TailableProc.stop(self, timeout)
643+
644+
def kill(self):
645+
# Kill the remote signer first
646+
self.rsignerd.kill()
647+
TailableProc.kill(self)
591648

592649
def wait(self, timeout=10):
593650
"""Wait for the daemon to stop for up to timeout seconds

contrib/remote_hsmd/proxy.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ proxy_stat map_status(Status const & status)
5959
// FIXME - this is bogus, but the pytest framework loses our
6060
// status_unusual messages.
6161
if (code != StatusCode::OK) {
62-
cerr << "PROXY-HSMD grpc::StatusCode " << int(code)
63-
<< ": " << status.error_message()
62+
cerr << "PID: " << getpid() << ' '
63+
<< "PROXY-HSMD grpc::StatusCode " << int(code)
64+
<< ": " << status.error_message()
6465
<< endl;
6566
}
6667
switch (code) {
@@ -317,7 +318,8 @@ void proxy_setup()
317318
{
318319
const char *endpointvar = getenv("REMOTE_HSMD_ENDPOINT");
319320
const char *endpoint = endpointvar != NULL ? endpointvar : "localhost:50051";
320-
STATUS_DEBUG("%s:%d %s endpoint:%s", __FILE__, __LINE__, __FUNCTION__, endpoint);
321+
STATUS_DEBUG("%s:%d %s pid:%d, endpoint:%s", __FILE__, __LINE__, __FUNCTION__,
322+
getpid(), endpoint);
321323
auto channel = grpc::CreateChannel(endpoint,
322324
grpc::InsecureChannelCredentials());
323325
stub = Signer::NewStub(channel);
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/usr/bin/sh
22

3+
4+
35
SUBDAEMON='hsmd:remote_hsmd' \
6+
REMOTE_SIGNER_CMD=$(pwd)/../rust-lightning-signer/target/debug/server \
47
make \
5-
PYTEST_PAR=1 \
8+
-j32 PYTEST_PAR=64 \
69
DEVELOPER=1 \
710
VALGRIND=0 \
811
SLOW_MACHINE=1 \
12+
PYTEST_MOREOPTS='--timeout=300 --timeout_method=thread' \
913
pytest
1014

contrib/remote_hsmd/scripts/run-one-test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ DEVELOPER=1 \
1111
VALGRIND=0 \
1212
SLOW_MACHINE=1 \
1313
SUBDAEMON='hsmd:remote_hsmd' \
14+
REMOTE_SIGNER_CMD=$(pwd)/../rust-lightning-signer/target/debug/server \
1415
pytest \
1516
$THETEST \
16-
-v --timeout=550 --timeout_method=thread -x
17+
-v --timeout=300 --timeout_method=thread -x

0 commit comments

Comments
 (0)