Skip to content

Commit

Permalink
Wait for lnd and stagger startup time for lndk
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Jun 10, 2024
1 parent 363bf69 commit cab8421
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
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", "1"]
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", "3"]
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 "$@"
45 changes: 45 additions & 0 deletions docker/lndk/wait-for-lnd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/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.

# Function to check if lnd is ready
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

# Delay startup of lndk nodes
# The sleep command is used to pause the script for a specified number of seconds before starting lndk.
# The number of seconds is specified by the second parameter to the script.
echo "Waiting for another $2 seconds before starting lndk..."
sleep "$2"

# 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 "${@:3}"

0 comments on commit cab8421

Please sign in to comment.