Skip to content

Add CLN support #713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
May 13, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e0def8f
add eclair chart
danvergara Feb 23, 2025
1f44794
add eclair as a dependency
danvergara Feb 23, 2025
6c54c7b
add cln chart
danvergara Mar 25, 2025
80c3211
add improvements to the cln node
danvergara Mar 25, 2025
02609bc
fixes for eclair
danvergara Mar 27, 2025
814793c
checkpoint cln integrated into ln_framework
macgyver13 Apr 5, 2025
4df0c33
move certs from cln nodes to simln
macgyver13 Apr 14, 2025
4c409fd
adjust cln to start grpc
macgyver13 Apr 15, 2025
7831413
enable grpc plugin to respond to remote nodes
macgyver13 Apr 18, 2025
9fdd5f8
Add styling to standard out for improved readability
macgyver13 Apr 19, 2025
a6fc662
Fix wait for gossip loop
macgyver13 Apr 19, 2025
915b70e
Merge branch 'main' into add-ln-nodes
macgyver13 Apr 20, 2025
5767109
fix linter findings
macgyver13 Apr 20, 2025
ff9d32e
don't setup a ln connection to itself
macgyver13 Apr 20, 2025
cda355b
Add ln_mixed node test
macgyver13 Apr 23, 2025
f1a8b37
switch from print to logger
macgyver13 Apr 23, 2025
6355e85
correct formatting ruff finding
macgyver13 Apr 23, 2025
f1d7b39
revert -lnd changes based on comments
macgyver13 Apr 23, 2025
10531d5
Remove all dependences on -cln or -lnd name to determine type - use p…
macgyver13 Apr 23, 2025
3d846d3
address comments
macgyver13 Apr 25, 2025
f5aba7c
remove RPC requirements for CLN - fetch rune via http transfer from pod
macgyver13 Apr 25, 2025
4fb0225
Add debug logs to deploy step
macgyver13 Apr 25, 2025
7890267
try wait_for_pod_ready
macgyver13 Apr 25, 2025
76023bb
remove debug logging
macgyver13 May 1, 2025
f21bfdd
remove unused test files
macgyver13 May 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions resources/charts/bitcoincore/Chart.yaml
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@ dependencies:
- name: lnd
version: 0.1.0
condition: ln.lnd
- name: cln
version: 0.1.0
condition: ln.cln

# A chart can be either an 'application' or a 'library' chart.
#
23 changes: 23 additions & 0 deletions resources/charts/bitcoincore/charts/cln/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions resources/charts/bitcoincore/charts/cln/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: cln
description: A Helm chart for CLN

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.1.0"
78 changes: 78 additions & 0 deletions resources/charts/bitcoincore/charts/cln/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{{/*
Expand the name of the PARENT chart.
*/}}
{{- define "bitcoincore.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified PARENT app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "bitcoincore.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s" .Release.Name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}


{{/*
Expand the name of the chart.
*/}}
{{- define "cln.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}-ln
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "cln.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s" .Release.Name | trunc 63 | trimSuffix "-" }}-ln
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "cln.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "cln.labels" -}}
helm.sh/chart: {{ include "cln.chart" . }}
{{ include "cln.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "cln.selectorLabels" -}}
app.kubernetes.io/name: {{ include "cln.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "cln.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "cln.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
36 changes: 36 additions & 0 deletions resources/charts/bitcoincore/charts/cln/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "cln.fullname" . }}
labels:
{{- include "cln.labels" . | nindent 4 }}
data:
config: |
{{- .Values.baseConfig | nindent 4 }}
{{- .Values.defaultConfig | nindent 4 }}
{{- .Values.config | nindent 4 }}
network={{ .Values.global.chain }}
addr=0.0.0.0:{{ .Values.P2PPort }}
bitcoin-rpcconnect={{ include "bitcoincore.fullname" . }}
bitcoin-rpcport={{ index .Values.global .Values.global.chain "RPCPort" }}
bitcoin-rpcpassword={{ .Values.global.rpcpassword }}
alias={{ include "cln.fullname" . }}
announce-addr=dns:{{ include "cln.fullname" . }}:{{ .Values.P2PPort }}
database-upgrade=true
bitcoin-retry-timeout=600
grpc-port={{ .Values.RPCPort }}
grpc-host=0.0.0.0
clnrest-host=0.0.0.0
clnrest-port=3010
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "cln.fullname" . }}-channels
labels:
channels: "true"
{{- include "cln.labels" . | nindent 4 }}
data:
source: {{ include "cln.fullname" . }}
channels: |
{{ .Values.channels | toJson }}
100 changes: 100 additions & 0 deletions resources/charts/bitcoincore/charts/cln/templates/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
apiVersion: v1
kind: Pod
metadata:
name: {{ include "cln.fullname" . }}
labels:
{{- include "cln.labels" . | nindent 4 }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 4 }}
{{- end }}
app: {{ include "cln.fullname" . }}
{{- if .Values.collectLogs }}
collect_logs: "true"
{{- end }}
chain: {{ .Values.global.chain }}
annotations:
kubectl.kubernetes.io/default-container: "cln"
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 4 }}
{{- end }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 4 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 8 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: p2p
containerPort: {{ .Values.P2PPort }}
protocol: TCP
- name: rpc
containerPort: {{ .Values.RPCPort }}
protocol: TCP
- name: rest
containerPort: {{ .Values.RestPort }}
protocol: TCP
command:
- /bin/sh
- -c
- |
lightningd --conf=/root/.lightning/config &
sleep 1
lightning-cli createrune > /working/rune.json
echo "Here is the rune file contents"
cat /working/rune.json
wait
livenessProbe:
{{- toYaml .Values.livenessProbe | nindent 8 }}
readinessProbe:
{{- toYaml .Values.readinessProbe | nindent 8 }}
startupProbe:
{{- toYaml .Values.startupProbe | nindent 8 }}
resources:
{{- toYaml .Values.resources | nindent 8 }}
volumeMounts:
{{- with .Values.volumeMounts }}
{{- toYaml . | nindent 8 }}
{{- end }}
- mountPath: /root/.lightning/config
name: config
subPath: config
{{- with .Values.extraContainers }}
{{- toYaml . | nindent 4 }}
{{- end }}
- name: http-server
image: busybox
command: ["/bin/sh", "-c"]
args:
- |
echo "Starting HTTP server..."
busybox httpd -f -p 8080 -h /working
ports:
- containerPort: 8080
name: http
volumeMounts:
{{- with .Values.volumeMounts }}
{{- toYaml . | nindent 8 }}
{{- end }}
volumes:
{{- with .Values.volumes }}
{{- toYaml . | nindent 4 }}
{{- end }}
- configMap:
name: {{ include "cln.fullname" . }}
name: config
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 4 }}
{{- end }}
24 changes: 24 additions & 0 deletions resources/charts/bitcoincore/charts/cln/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "cln.fullname" . }}
labels:
{{- include "cln.labels" . | nindent 4 }}
app: {{ include "cln.fullname" . }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.P2PPort }}
targetPort: p2p
protocol: TCP
name: p2p
- port: {{ .Values.RPCPort }}
targetPort: rpc
protocol: TCP
name: rpc
- port: {{ .Values.RestPort }}
targetPort: rest
protocol: TCP
name: rest
selector:
{{- include "cln.selectorLabels" . | nindent 4 }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{- if .Values.metricsExport }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ include "cln.fullname" . }}
labels:
app.kubernetes.io/name: cln-metrics
release: prometheus
spec:
endpoints:
- port: prometheus-metrics
selector:
matchLabels:
app: {{ include "cln.fullname" . }}
{{- end }}
102 changes: 102 additions & 0 deletions resources/charts/bitcoincore/charts/cln/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Default values for cln.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
namespace: warnet

image:
repository: elementsproject/lightningd
pullPolicy: IfNotPresent
tag: "v25.02"

nameOverride: ""
fullnameOverride: ""

podAnnotations: {}
podLabels:
app: "warnet"
mission: "lightning"

podSecurityContext: {}

securityContext: {}

service:
type: ClusterIP

P2PPort: 9735
RPCPort: 9736
RestPort: 3010

ingress:
enabled: false
className: ""
annotations: {}
hosts:
- host: chart-example.local
paths:
- path: /
pathType: ImplementationSpecific
tls: []

resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi

livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "lightning-cli getinfo >/dev/null 2>&1"
failureThreshold: 3
initialDelaySeconds: 5
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 1
readinessProbe:
failureThreshold: 10
periodSeconds: 30
successThreshold: 1
timeoutSeconds: 60
exec:
command:
- "/bin/sh"
- "-c"
- "lightning-cli getinfo 2>/dev/null | grep -q 'id' || exit 1"

# Additional volumes on the output Deployment definition.
volumes:
- name: working
emptyDir: {}

# Additional volumeMounts on the output Deployment definition.
volumeMounts:
- name: working
mountPath: "/working"

nodeSelector: {}

tolerations: []

affinity: {}

baseConfig: |
log-level=info
developer
dev-fast-gossip
bitcoin-rpcuser=user
# bitcoind.rpcpass are set in configmap.yaml
config: ""

defaultConfig: ""

channels: []
3 changes: 2 additions & 1 deletion resources/charts/bitcoincore/values.yaml
Original file line number Diff line number Diff line change
@@ -141,4 +141,5 @@ loadSnapshot:
url: ""

ln:
lnd: false
lnd: false
cln: false
16 changes: 13 additions & 3 deletions resources/plugins/simln/README.md
Original file line number Diff line number Diff line change
@@ -19,8 +19,18 @@ SimLN also requires access details for each node; however, the SimLN plugin will
"cert": <path_to_tls_cert>
}
````
SimLN plugin also supports Core Lightning (CLN). CLN nodes connection details are transfered from the CLN node to SimLN node during launch-activity processing.
```` JSON
{
"id": <node_id>,
"address": https://<domain:port>,
"ca_cert": /working/<node_id>-ca.pem,
"client_cert": /working/<node_id>-client.pem,
"client_key": /working/<node_id>-client-key.pem
}
````

Since SimLN already has access to those LND connection details, it means you can focus on the "activity" definitions.
Since SimLN already has access to those LND and CLN connection details, it means you can focus on the "activity" definitions.

### Launch activity definitions from the command line
The SimLN plugin takes "activity" definitions like so:
@@ -73,8 +83,8 @@ nodes:
addnode:
- tank-0000
ln:
lnd: true
lnd:
cln: true
cln:
channels:
- id:
block: 300
55 changes: 48 additions & 7 deletions resources/plugins/simln/plugin.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@

from warnet.constants import LIGHTNING_MISSION, PLUGIN_ANNEX, AnnexMember, HookValue, WarnetContent
from warnet.k8s import (
copyfile,
download,
get_default_namespace,
get_mission,
@@ -145,6 +146,8 @@ def _launch_activity(activity: Optional[list[dict]], plugin_dir: str) -> str:
activity_json = _generate_activity_json(activity)
wait_for_init(name, namespace=get_default_namespace(), quiet=True)

# write cert files to container
transfer_cln_certs(name)
if write_file_to_container(
name,
"init",
@@ -162,13 +165,18 @@ def _generate_activity_json(activity: Optional[list[dict]]) -> str:
nodes = []

for i in get_mission(LIGHTNING_MISSION):
name = i.metadata.name
node = {
"id": name,
"address": f"https://{name}:10009",
"macaroon": "/working/admin.macaroon",
"cert": "/working/tls.cert",
}
ln_name = i.metadata.name
port = 10009
node = {"id": ln_name}
if "cln" in i.metadata.labels["app.kubernetes.io/name"]:
port = 9736
node["ca_cert"] = f"/working/{ln_name}-ca.pem"
node["client_cert"] = f"/working/{ln_name}-client.pem"
node["client_key"] = f"/working/{ln_name}-client-key.pem"
else:
node["macaroon"] = "/working/admin.macaroon"
node["cert"] = "/working/tls.cert"
node["address"] = f"https://{ln_name}:{port}"
nodes.append(node)

if activity:
@@ -179,6 +187,39 @@ def _generate_activity_json(activity: Optional[list[dict]]) -> str:
return json.dumps(data, indent=2)


def transfer_cln_certs(name):
dst_container = "init"
for i in get_mission(LIGHTNING_MISSION):
ln_name = i.metadata.name
if "cln" in i.metadata.labels["app.kubernetes.io/name"]:
chain = i.metadata.labels["chain"]
cln_root = f"/root/.lightning/{chain}"
copyfile(
ln_name,
"cln",
f"{cln_root}/ca.pem",
name,
dst_container,
f"/working/{ln_name}-ca.pem",
)
copyfile(
ln_name,
"cln",
f"{cln_root}/client.pem",
name,
dst_container,
f"/working/{ln_name}-client.pem",
)
copyfile(
ln_name,
"cln",
f"{cln_root}/client-key.pem",
name,
dst_container,
f"/working/{ln_name}-client-key.pem",
)


def _sh(pod, method: str, params: tuple[str, ...]) -> str:
namespace = get_default_namespace()

41 changes: 33 additions & 8 deletions resources/scenarios/commander.py
Original file line number Diff line number Diff line change
@@ -13,8 +13,7 @@
from time import sleep

from kubernetes import client, config
from ln_framework.ln import LND

from ln_framework.ln import CLN, LND, LNNode
from test_framework.authproxy import AuthServiceProxy
from test_framework.p2p import NetworkThread
from test_framework.test_framework import (
@@ -71,7 +70,10 @@
)

if pod.metadata.labels["mission"] == "lightning":
WARNET["lightning"].append(pod.metadata.name)
lnnode = LND(pod.metadata.name, pod.status.pod_ip)
if "cln" in pod.metadata.labels["app.kubernetes.io/name"]:
lnnode = CLN(pod.metadata.name, pod.status.pod_ip)
WARNET["lightning"].append(lnnode)

for cm in cmaps.items:
if not cm.metadata.labels or "channels" not in cm.metadata.labels:
@@ -92,6 +94,30 @@ def auth_proxy_request(self, method, path, postdata):
AuthServiceProxy._request = auth_proxy_request


# Create a custom formatter
class ColorFormatter(logging.Formatter):
"""Custom formatter to add color based on log level."""

# Define ANSI color codes
RED = "\033[91m"
YELLOW = "\033[93m"
GREEN = "\033[92m"
RESET = "\033[0m"

FORMATS = {
logging.DEBUG: f"{RESET}%(name)-8s - Thread-%(thread)d - %(message)s{RESET}",
logging.INFO: f"{RESET}%(name)-8s - %(message)s{RESET}",
logging.WARNING: f"{YELLOW}%(name)-8s - %(message)s{RESET}",
logging.ERROR: f"{RED}%(name)-8s - %(message)s{RESET}",
logging.CRITICAL: f"{RED}##%(name)-8s - %(message)s##{RESET}",
}

def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)


class Commander(BitcoinTestFramework):
# required by subclasses of BitcoinTestFramework
def set_test_params(self):
@@ -132,7 +158,7 @@ def tank_connected(self, tank):
if count >= tank.init_peers:
break
else:
sleep(1)
sleep(5)

conn_threads = [
threading.Thread(target=tank_connected, args=(self, tank)) for tank in self.nodes
@@ -164,13 +190,12 @@ def setup(self):
# Scenarios log directly to stdout which gets picked up by the
# subprocess manager in the server, and reprinted to the global log.
ch = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(fmt="%(name)-8s %(message)s")
ch.setFormatter(formatter)
ch.setFormatter(ColorFormatter())
self.log.addHandler(ch)

# Keep a separate index of tanks by pod name
self.tanks: dict[str, TestNode] = {}
self.lns: dict[str, LND] = {}
self.lns: dict[str, LNNode] = {}
self.channels = WARNET["channels"]

for i, tank in enumerate(WARNET["tanks"]):
@@ -203,7 +228,7 @@ def setup(self):
self.tanks[tank["tank"]] = node

for ln in WARNET["lightning"]:
self.lns[ln] = LND(ln)
self.lns[ln.name] = ln

self.num_nodes = len(self.nodes)

415 changes: 385 additions & 30 deletions resources/scenarios/ln_framework/ln.py

Large diffs are not rendered by default.

90 changes: 55 additions & 35 deletions resources/scenarios/ln_init.py
Original file line number Diff line number Diff line change
@@ -42,18 +42,12 @@ def gen(n):
ln_addrs = []

def get_ln_addr(self, ln):
while True:
res = ln.newaddress()
if "address" in res:
addr = res["address"]
ln_addrs.append(addr)
self.log.info(f"Got wallet address {addr} from {ln.name}")
break
else:
self.log.info(
f"Couldn't get wallet address from {ln.name}:\n {res}\n wait and retry..."
)
sleep(1)
success, address = ln.newaddress()
if success:
ln_addrs.append(address)
self.log.info(f"Got wallet address {address} from {ln.name}")
else:
self.log.info(f"Couldn't get wallet address from {ln.name}")

addr_threads = [
threading.Thread(target=get_ln_addr, args=(self, ln)) for ln in self.lns.values()
@@ -166,15 +160,18 @@ def connect_ln(self, pair):
)
sleep(1)
else:
self.log.info(
self.log.error(
f"Unexpected response attempting to connect {pair[0].name} -> {pair[1].name}:\n {res}\n ABORTING"
)
break
raise Exception(
f"Unable to connect {pair[0].name} -> {pair[1].name}:\n {res}"
)

p2p_threads = [
threading.Thread(target=connect_ln, args=(self, pair)) for pair in connections
]
for thread in p2p_threads:
sleep(0.25)
thread.start()

all(thread.join() is None for thread in p2p_threads)
@@ -188,7 +185,8 @@ def connect_ln(self, pair):
# so their channel ids are deterministic
ch_by_block = {}
for ch in self.channels:
# TODO: if "id" not in ch ...
if "id" not in ch or "block" not in ch["id"]:
raise Exception(f"LN Channel {ch} not found")
block = ch["id"]["block"]
if block not in ch_by_block:
ch_by_block[block] = [ch]
@@ -214,24 +212,24 @@ def open_channel(self, ch, fee_rate):
f"Sending channel open from {ch['source']} -> {ch['target']} with fee_rate={fee_rate}"
)
res = src.channel(
pk=self.hex_to_b64(tgt_pk),
pk=tgt_pk,
capacity=ch["capacity"],
push_amt=ch["push_amt"],
fee_rate=fee_rate,
)
if "result" not in res:
if res and "txid" in res:
ch["txid"] = res["txid"]
self.log.info(
"Unexpected channel open response:\n "
+ f"From {ch['source']} -> {ch['target']} fee_rate={fee_rate}\n "
+ f"{res}"
f"Channel open {ch['source']} -> {ch['target']}\n "
+ f"outpoint={res['outpoint']}\n "
+ f"expected channel id: {ch['id']}"
)
else:
txid = self.b64_to_hex(res["result"]["chan_pending"]["txid"], reverse=True)
ch["txid"] = txid
ch["txid"] = "N/A"
self.log.info(
f"Channel open {ch['source']} -> {ch['target']}\n "
+ f"outpoint={txid}:{res['result']['chan_pending']['output_index']}\n "
+ f"expected channel id: {ch['id']}"
"Unexpected channel open response:\n "
+ f"From {ch['source']} -> {ch['target']} fee_rate={fee_rate}\n "
+ f"{res}"
)

channels = sorted(ch_by_block[target_block], key=lambda ch: ch["id"]["index"])
@@ -244,6 +242,7 @@ def open_channel(self, ch, fee_rate):
assert index == ch["id"]["index"], "Channel ID indexes are not consecutive"
assert fee_rate >= 1, "Too many TXs in block, out of fee range"
t = threading.Thread(target=open_channel, args=(self, ch, fee_rate))
sleep(0.25)
t.start()
ch_threads.append(t)

@@ -260,6 +259,7 @@ def open_channel(self, ch, fee_rate):
block_txs = block["tx"]
block_height = block["height"]
for ch in channels:
assert ch["txid"] != "N/A", f"Channel:{ch} did not receive txid"
assert ch["id"]["block"] == block_height, f"Actual block:{block_height}\n{ch}"
assert block_txs[ch["id"]["index"]] == ch["txid"], (
f"Actual txid:{block_txs[ch['id']['index']]}\n{ch}"
@@ -273,14 +273,26 @@ def open_channel(self, ch, fee_rate):

def ln_all_chs(self, ln):
expected = len(self.channels)
while len(ln.graph()["edges"]) != expected:
sleep(1)
self.log.info(f"LN {ln.name} has graph with all {expected} channels")
attempts = 0
actual = 0
while actual != expected:
actual = len(ln.graph()["edges"])
if attempts > 10:
break
attempts += 1
sleep(5)
if actual == expected:
self.log.info(f"LN {ln.name} has graph with all {expected} channels")
else:
self.log.error(
f"LN {ln.name} graph is INCOMPLETE - {actual} of {expected} channels"
)

ch_ann_threads = [
threading.Thread(target=ln_all_chs, args=(self, ln)) for ln in self.lns.values()
]
for thread in ch_ann_threads:
sleep(0.25)
thread.start()

all(thread.join() is None for thread in ch_ann_threads)
@@ -311,6 +323,7 @@ def update_policy(self, ln, txid_hex, policy, capacity):
ch["capacity"],
),
)
sleep(0.25)
ts.start()
update_threads.append(ts)
if "target_policy" in ch:
@@ -324,6 +337,7 @@ def update_policy(self, ln, txid_hex, policy, capacity):
ch["capacity"],
),
)
sleep(0.25)
tt.start()
update_threads.append(tt)
count = len(update_threads)
@@ -337,16 +351,23 @@ def policy_equal(pol1, pol2, capacity):
return pol1.to_lnd_chanpolicy(capacity) == pol2.to_lnd_chanpolicy(capacity)

def matching_graph(self, expected, ln):
while True:
done = False
while not done:
actual = ln.graph()["edges"]
assert len(expected) == len(actual)
done = True
self.log.debug(f"LN {ln.name} channel graph edges: {actual}")
if len(actual) > 0:
done = True
assert len(expected) == len(actual), (
f"Expected edges {len(expected)}, actual edges {len(actual)}\n{actual}"
)
for i, actual_ch in enumerate(actual):
expected_ch = expected[i]
capacity = expected_ch["capacity"]
# We assert this because it isn't updated as part of policy.
# If this fails we have a bigger issue
assert int(actual_ch["capacity"]) == capacity
assert int(actual_ch["capacity"]) == capacity, (
f"LN {ln.name} graph capacity mismatch:\n actual: {actual_ch['capacity']}\n expected: {capacity}"
)

# Policies were not defined in network.yaml
if "source_policy" not in expected_ch or "target_policy" not in expected_ch:
@@ -367,19 +388,18 @@ def matching_graph(self, expected, ln):
):
continue
done = False
break
if done:
self.log.info(f"LN {ln.name} graph channel policies all match expected source")
break
else:
sleep(1)
sleep(5)

expected = sorted(self.channels, key=lambda ch: (ch["id"]["block"], ch["id"]["index"]))
policy_threads = [
threading.Thread(target=matching_graph, args=(self, expected, ln))
for ln in self.lns.values()
]
for thread in policy_threads:
sleep(0.25)
thread.start()

all(thread.join() is None for thread in policy_threads)
3 changes: 1 addition & 2 deletions src/warnet/bitcoin.py
Original file line number Diff line number Diff line change
@@ -6,10 +6,9 @@
from typing import Optional

import click
from urllib3.exceptions import MaxRetryError

from test_framework.messages import ser_uint256
from test_framework.p2p import MESSAGEMAP
from urllib3.exceptions import MaxRetryError

from .constants import BITCOINCORE_CONTAINER
from .k8s import get_default_namespace_or, get_mission, pod_log
1 change: 0 additions & 1 deletion src/warnet/constants.py
Original file line number Diff line number Diff line change
@@ -65,7 +65,6 @@ class AnnexMember(Enum):

# Helm charts
BITCOIN_CHART_LOCATION = str(CHARTS_DIR.joinpath("bitcoincore"))
LND_CHART_LOCATION = str(CHARTS_DIR.joinpath("lnd"))
FORK_OBSERVER_CHART = str(CHARTS_DIR.joinpath("fork-observer"))
COMMANDER_CHART = str(CHARTS_DIR.joinpath("commander"))
NAMESPACES_CHART_LOCATION = CHARTS_DIR.joinpath("namespaces")
18 changes: 14 additions & 4 deletions src/warnet/deploy.py
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@
get_mission,
get_namespaces_by_type,
wait_for_ingress_controller,
wait_for_pod,
wait_for_pod_ready,
)
from .process import run_command, stream_command
@@ -365,11 +364,22 @@ def deploy_network(directory: Path, debug: bool = False, namespace: Optional[str
network_file = yaml.safe_load(f)

needs_ln_init = False
supported_ln_projects = ["lnd", "cln"]
for node in network_file["nodes"]:
if "lnd" in node and "channels" in node["lnd"] and len(node["lnd"]["channels"]) > 0:
needs_ln_init = True
ln_config = node.get("ln", {})
for key in supported_ln_projects:
if ln_config.get(key, False) and key in node and "channels" in node[key]:
needs_ln_init = True
break
if needs_ln_init:
break

default_file_path = directory / DEFAULTS_FILE
with default_file_path.open() as f:
default_file = yaml.safe_load(f)
if any(default_file.get("ln", {}).get(key, False) for key in supported_ln_projects):
needs_ln_init = True

processes = []
for node in network_file["nodes"]:
p = Process(target=deploy_single_node, args=(node, directory, debug, namespace))
@@ -388,7 +398,7 @@ def deploy_network(directory: Path, debug: bool = False, namespace: Optional[str
admin=False,
namespace=namespace,
)
wait_for_pod(name, namespace=namespace)
wait_for_pod_ready(name, namespace=namespace)
_logs(pod_name=name, follow=True, namespace=namespace)


55 changes: 55 additions & 0 deletions src/warnet/k8s.py
Original file line number Diff line number Diff line change
@@ -596,3 +596,58 @@ def download(
os.remove(tar_file)

return destination_path


def read_file_from_container(
pod_name,
source_path: Path,
container_name: str = "",
namespace: Optional[str] = None,
quiet: bool = False,
) -> str:
"""Download the file from the `source_path` to the `destination_path`"""

namespace = get_default_namespace_or(namespace)

v1 = get_static_client()

command = ["cat", str(source_path)]

resp = stream(
v1.connect_get_namespaced_pod_exec,
name=pod_name,
namespace=namespace,
container=container_name,
command=command,
stderr=True,
stdin=False,
stdout=True,
tty=False,
_preload_content=False,
)

result = ""
while resp.is_open():
resp.update(timeout=5)
if resp.peek_stdout():
result += resp.read_stdout()
if resp.peek_stderr():
raise Exception(resp.read_stderr())
resp.close()
return result


def copyfile(pod_name, src_container, source_path, dst_name, dst_container, dst_path):
namespace = get_default_namespace()
file_data = read_file_from_container(pod_name, source_path, src_container, namespace)
if write_file_to_container(
dst_name,
dst_container,
dst_path,
file_data,
namespace=namespace,
quiet=True,
):
print(f"Copied {source_path} to {dst_path}")
else:
print(f"Failed to copy {source_path} from {pod_name} to {dst_name}:{dst_path}")
31 changes: 21 additions & 10 deletions src/warnet/ln.py
Original file line number Diff line number Diff line change
@@ -31,7 +31,10 @@ def _rpc(pod_name: str, method: str, params: str = "", namespace: Optional[str]
pod = get_pod(pod_name)
namespace = get_default_namespace_or(namespace)
chain = pod.metadata.labels["chain"]
cmd = f"kubectl -n {namespace} exec {pod_name} -- lncli --network {chain} {method} {' '.join(map(str, params))}"
ln_client = "lncli"
if "cln" in pod.metadata.labels["app.kubernetes.io/name"]:
ln_client = "lightning-cli"
cmd = f"kubectl -n {namespace} exec {pod_name} -- {ln_client} --network {chain} {method} {' '.join(map(str, params))}"
return run_command(cmd)


@@ -46,9 +49,13 @@ def pubkey(
print(_pubkey(pod))


def _pubkey(pod: str):
info = _rpc(pod, "getinfo")
return json.loads(info)["identity_pubkey"]
def _pubkey(pod_name: str):
info = _rpc(pod_name, "getinfo")
pod = get_pod(pod_name)
pubkey_key = "identity_pubkey"
if "cln" in pod.metadata.labels["app.kubernetes.io/name"]:
pubkey_key = "id"
Comment on lines +56 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this kind of thing is ok for now and probably will be ok for eclair as well -- but its a tad codesmelly. feels like implementation-specific methods should stay in their classes, even as jsut static functions

return json.loads(info)[pubkey_key]


@ln.command()
@@ -62,10 +69,14 @@ def host(
print(_host(pod))


def _host(pod):
info = _rpc(pod, "getinfo")
uris = json.loads(info)["uris"]
if uris and len(uris) >= 0:
return uris[0].split("@")[1]
def _host(pod_name: str):
info = _rpc(pod_name, "getinfo")
pod = get_pod(pod_name)
if "cln" in pod.metadata.labels["app.kubernetes.io/name"]:
return json.loads(info)["alias"]
else:
return ""
uris = json.loads(info)["uris"]
if uris and len(uris) >= 0:
return uris[0].split("@")[1]
else:
return ""
4 changes: 2 additions & 2 deletions test/ln_basic_test.py
Original file line number Diff line number Diff line change
@@ -47,8 +47,8 @@ def setup_network(self):

def fund_wallets(self):
outputs = ""
for lnd in self.lns:
addr = json.loads(self.warnet(f"ln rpc {lnd} newaddress p2wkh"))["address"]
for ln in self.lns:
addr = json.loads(self.warnet(f"ln rpc {ln} newaddress p2wkh"))["address"]
outputs += f',"{addr}":10'
# trim first comma
outputs = outputs[1:]