Skip to content

Commit 845b404

Browse files
authored
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 5bce509 commit 845b404

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
@@ -375,6 +375,10 @@ else
375375
PYTEST_OPTS += -x
376376
endif
377377

378+
ifneq ($(PYTEST_MOREOPTS),)
379+
PYTEST_OPTS += $(PYTEST_MOREOPTS)
380+
endif
381+
378382
check-units:
379383

380384
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
@@ -512,6 +512,33 @@ def getnewaddress(self):
512512
info = self.rpc.getaddressinfo(addr)
513513
return info['unconfidential']
514514

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

516543
class LightningD(TailableProc):
517544
def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, node_id=0):
@@ -521,7 +548,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
521548
self.port = port
522549
self.cmd_prefix = []
523550
self.disconnect_file = None
524-
551+
self.lightning_dir = lightning_dir
552+
self.rsignerd_dir = os.path.join(lightning_dir, "remotesigner")
553+
525554
self.rpcproxy = bitcoindproxy
526555

527556
self.opts = LIGHTNINGD_CONFIG.copy()
@@ -548,6 +577,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
548577
if not os.path.exists(os.path.join(lightning_dir, TEST_NETWORK)):
549578
os.makedirs(os.path.join(lightning_dir, TEST_NETWORK))
550579

580+
if not os.path.exists(self.rsignerd_dir):
581+
os.makedirs(self.rsignerd_dir)
582+
551583
# Last 32-bytes of final part of dir -> seed.
552584
seed = (bytes(re.search('([^/]+)/*$', lightning_dir).group(1), encoding='utf-8') + bytes(32))[:32]
553585
if not random_hsm:
@@ -559,6 +591,8 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
559591
self.prefix = 'lightningd-%d' % (node_id)
560592

561593
def cleanup(self):
594+
# Make sure the remotesigner is shutdown
595+
self.rsignerd.stop()
562596
# To force blackhole to exit, disconnect file must be truncated!
563597
if self.disconnect_file:
564598
with open(self.disconnect_file, "w") as f:
@@ -581,11 +615,34 @@ def cmd_line(self):
581615

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

590647
def wait(self, timeout=10):
591648
"""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)