Skip to content

Commit

Permalink
itest: use custom lightningd factory
Browse files Browse the repository at this point in the history
the lightningd factory from pyln-testing has too many teardown checks.
They are not useful for swapd cases, so create our own factory.
  • Loading branch information
JssDWt committed Nov 30, 2024
1 parent 0f2c827 commit d70e242
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
83 changes: 71 additions & 12 deletions itest/tests/cln.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
wait_for,
)

from pyln.testing.db import Sqlite3Db

import os
import random
import string
import shutil
import threading

FUNDAMOUNT = 10**6

Expand All @@ -19,7 +24,12 @@ def __init__(self, node, bitcoindproxy, port, grpc_port):
self.node = node
self.info = {}

def start(self):
def start(self, wait_for_bitcoind_sync=True):
try:
self.node.start(wait_for_bitcoind_sync)
except Exception:
self.stop()
raise
self.info = {"id": self.node.info["id"]}

def stop(self, timeout=10):
Expand Down Expand Up @@ -109,18 +119,67 @@ def list_utxos(self):
class ClnNodeFactory(object):
"""A factory to setup and start wrapped `lightningd` daemons."""

def __init__(self, node_factory):
def __init__(self, bitcoind, directory):
self.next_id = 1
self.nodes = []
self.reserved_ports = []
self.node_factory = node_factory

def get_node(self):
cln_grpc_port = reserve_unused_port()
self.reserved_ports.append(cln_grpc_port)
node = self.node_factory.get_node(options={"grpc-port": cln_grpc_port})
cln_node = ClnNode(node, node.bitcoin, node.port, cln_grpc_port)
cln_node.start()
self.bitcoind = bitcoind
self.directory = directory
self.lock = threading.Lock()

def get_node(
self, node_id=None, start=True, cleandir=True, wait_for_bitcoind_sync=True
):
node_id = self.get_node_id() if not node_id else node_id
port = reserve_unused_port()
grpc_port = reserve_unused_port()
self.reserved_ports.append(port)
self.reserved_ports.append(grpc_port)

lightning_dir = os.path.join(self.directory, "lightning-{}/".format(node_id))

if cleandir and os.path.exists(lightning_dir):
shutil.rmtree(lightning_dir)

db_path = os.path.join(lightning_dir, "lightningd.sqlite3")
db = Sqlite3Db(db_path)
node = LightningNode(
node_id,
lightning_dir,
self.bitcoind,
None,
False,
port=port,
db=db,
options={"grpc-port": grpc_port},
feerates=(15000, 11000, 7500, 3750),
)

cln_node = ClnNode(node, self.bitcoind, port, grpc_port)
if start:
try:
cln_node.start(wait_for_bitcoind_sync)
except Exception:
node.daemon.stop()
raise

return cln_node

def killall(self):
for reserved_port in self.reserved_ports:
drop_unused_port(reserved_port)
"""Returns true if every node we expected to succeed actually succeeded"""

for i in range(len(self.nodes)):
try:
self.nodes[i].stop()
except Exception:
pass

for p in self.reserved_ports:
drop_unused_port(p)

def get_node_id(self):
"""Generate a unique numeric ID for a lightning node"""
with self.lock:
node_id = self.next_id
self.next_id += 1
return node_id
8 changes: 4 additions & 4 deletions itest/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def postgres_factory(test_name, teardown_checks):


@pytest.fixture
def cln_factory(pyln_node_factory):
nf = ClnNodeFactory(pyln_node_factory)
def cln_factory(directory, bitcoind):
nf = ClnNodeFactory(bitcoind, directory)
yield nf
nf.killall()

Expand All @@ -149,8 +149,8 @@ def lnd_options(directory, bitcoind):


@pytest.fixture()
def node_factory(pyln_node_factory):
return ClnNodeFactory(pyln_node_factory)
def node_factory(cln_factory):
return cln_factory


@pytest.fixture()
Expand Down
8 changes: 0 additions & 8 deletions itest/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
from fixtures import *
from pyln.testing.fixtures import (
directory,
db_provider,
executor,
jsonschemas,
node_cls,
setup_logging,
teardown_checks,
test_base_dir,
Expand All @@ -19,10 +15,6 @@
__all__ = [
"bitcoind",
"directory",
"db_provider",
"executor",
"jsonschemas",
"node_cls",
"node_factory",
"setup_logging",
"teardown_checks",
Expand Down

0 comments on commit d70e242

Please sign in to comment.