Skip to content
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

Wait for lnd and stagger startup time for lndk #22

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions conf/lnd/lnd.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rpclisten=0.0.0.0:10009
restlisten=0.0.0.0:8080
trickledelay=1000
noseedbackup=true
debuglevel=debug
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ services:
restart: unless-stopped
depends_on:
- lnd1
entrypoint: ["/wait-for-lnd.sh", "lnd1"]
command: --address=https://lnd1:10009 --cert-path=/root/.lnd/tls.cert --macaroon-path=/root/.lnd/data/chain/bitcoin/regtest/admin.macaroon --log-level=trace --grpc-host=0.0.0.0
environment:
- RUST_BACKTRACE=1
volumes:
- "lndk1:/root/.lndk"
- "lnd1:/root/.lnd:ro"
- "./docker/lndk/wait-for-lnd.sh:/wait-for-lnd.sh"
networks:
testing_net:
ipv4_address: 172.30.1.3
Expand Down Expand Up @@ -87,12 +89,14 @@ services:
restart: unless-stopped
depends_on:
- lnd2
entrypoint: ["/wait-for-lnd.sh", "lnd2"]
command: --address=https://lnd2:10009 --cert-path=/root/.lnd/tls.cert --macaroon-path=/root/.lnd/data/chain/bitcoin/regtest/admin.macaroon --log-level=trace --grpc-host=0.0.0.0
environment:
- RUST_BACKTRACE=1
volumes:
- "lndk2:/root/.lndk"
- "lnd2:/root/.lnd:ro"
- "./docker/lndk/wait-for-lnd.sh:/wait-for-lnd.sh"
networks:
testing_net:
ipv4_address: 172.30.2.2
Expand Down
14 changes: 14 additions & 0 deletions docker/cln/wait-for-bitcoind.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
#!/bin/bash

# Usage:
# wait-for-bitcoind.sh <cln-args>
#
# Arguments:
# cln-args The arguments to be passed to the lightningd command.
#
# This script is used to delay the startup of cln nodes until bitcoind is ready.
# It uses the bitcoin-cli command to check the status of bitcoind.
# Once bitcoind is ready, it starts the cln nodes with the provided arguments.

# Function to check if bitcoind is ready
is_bitcoind_ready() {
bitcoin-cli -rpcconnect=bitcoind -rpcport=43782 -rpcuser=user -rpcpassword=pass -conf=/root/.lightning/bitcoin/bitcoin.conf getblockchaininfo &> /dev/null
return $?
}

# Wait for bitcoind to be ready
# The until loop will keep looping as long as the is_bitcoind_ready function returns a non-zero value (i.e., bitcoind is not ready).
until is_bitcoind_ready; do
echo "Waiting for bitcoind to be ready..."
# The sleep command is used to pause the script for 5 seconds between each check.
sleep 5
done

# Start cln
# The exec command is used to replace the current shell process with the lightningd command.
# The "$@" part is used to pass all arguments to the lightningd command.
exec lightningd "$@"
38 changes: 38 additions & 0 deletions docker/lndk/wait-for-lnd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Usage:
# wait-for-lnd.sh <host> <delay> <lndk-args>
#
# Arguments:
# host The host of the lnd's gRPC service.
# delay The delay (in seconds) for the startup of lndk nodes after lnd's gRPC port is ready.
# lndk-args The arguments to be passed to the lndk command.

is_lnd_ready() {
macaroon=$(base64 /root/.lnd/data/chain/bitcoin/regtest/readonly.macaroon | tr -d '\n')
response=$(curl --cacert /root/.lnd/tls.cert -Ss -H "Grpc-Metadata-macaroon: $macaroon" "https://$1:8080/v1/state")
if [ $? -ne 0 ]; then
echo "Error: curl command failed"
echo "Response from curl: $response"
return 1
fi
if echo "$response" | grep -q '"state":"SERVER_ACTIVE"'; then
return 0
else
echo "Error: lnd node state is not SERVER_ACTIVE"
echo "Response from lnd: $response"
return 1
fi
}

# Wait for lnd to be ready
# The until loop will keep looping as long as the is_lnd_ready function returns a non-zero value (i.e., the port is not open).
until is_lnd_ready $1; do
echo "Waiting for lnd to be ready..."
sleep 2
done

# Start lndk
# The exec command is used to replace the current shell process with the lndk command.
# The "${@:3}" part is used to pass all arguments starting from the third one to the lndk command.
exec lndk "${@:2}"