Skip to content

Commit

Permalink
Merge pull request #24 from LN-Zap/test-improvements
Browse files Browse the repository at this point in the history
Wait for node graph to sync in init process
  • Loading branch information
mrfelton authored Jun 11, 2024
2 parents 1497508 + 8ae1843 commit 29b7549
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 13 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,33 @@ You can use this to get familiar with [Bolt 12](https://bolt12.org/).

## Setup

**Clone the repository:**
**Clone the repository and submodules:**

```sh
git clone --recursive https://github.com/your/repo.git
```

**Start nodes:**
**Initialise the network:**

Start the docker stack to start the nodes:
Run the following command to initialise the nodes:

```sh
docker compose up
./scripts/init.sh
```

**Initialise the nodes:**
This script sets up a dockerized network of lightning nodes and channels which are a mix of different Lightning Network implementations, including LND, c-lightning (CLN), and Eclair.

In a separate terminal, run the following command to initialise the nodes:
***NOTE:** the init script must be run no later than 60 seconds after starting the nodes, otherwise nodes may crash due to an uninitialised blockchain*

```sh
./scripts/init.sh
```
***NOTE:** It will take a couple of minutes for the network graph to become fully ready through the lnd gosip protocol. The init script will wait for the full sync*

This script sets up a network of nodes and channels for the Lightning Network. The nodes are instances of different Lightning Network implementations, including LND, c-lightning (CLN), and Eclair.
**OR start an already initialised network:**

***NOTE: the init script must be run no later than 60 seconds after starting the nodes, otherwise nodes may crash due to an uninitialised blockchain***
Run the following command to startup the nodes:

```sh
docker compose up
```

### Nodes

Expand Down
5 changes: 5 additions & 0 deletions conf/lnd/lnd.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

[Application Options]
rpclisten=0.0.0.0:10009
restlisten=0.0.0.0:8080
trickledelay=1000
Expand All @@ -23,3 +25,6 @@ protocol.wumbo-channels=true
protocol.custom-message=513
protocol.custom-nodeann=39
protocol.custom-init=39

[gossip]
gossip.sub-batch-delay=1s
105 changes: 103 additions & 2 deletions scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

setup () {
$DIR/setup.sh
docker compose down --volumes
docker compose up -d
}

bitcoind() {
Expand Down Expand Up @@ -112,7 +114,6 @@ getNodeInfo() {
echo LND1_PUBKEY: $LND1_PUBKEY
echo LND1_NODE_URI: $LND1_NODE_URI


CLN1_NODE_INFO=$(cln1 getinfo)
CLN1_PUBKEY=$(echo ${CLN1_NODE_INFO} | jq -r .id)
CLN1_IP_ADDRESS=$(echo ${CLN1_NODE_INFO} | jq -r '.address[0].address')
Expand Down Expand Up @@ -251,7 +252,6 @@ waitBitcoind() {
waitFor bitcoind getnetworkinfo
}


waitForNodes() {
waitFor lnd1 getinfo
waitFor cln1 getinfo
Expand All @@ -265,17 +265,118 @@ waitForNodes() {
waitFor eclair3 getinfo
}

waitForGraphSync() {
# Declare an associative array with node names as keys and public keys as values
declare -A nodes=(
["LND2"]=$LND2_PUBKEY
["ECLAIR1"]=$ECLAIR1_PUBKEY
["ECLAIR2"]=$ECLAIR2_PUBKEY
["ECLAIR3"]=$ECLAIR3_PUBKEY
["CLN1"]=$CLN1_PUBKEY
["CLN2"]=$CLN2_PUBKEY
["CLN3"]=$CLN3_PUBKEY
)

# Get the current time and set a timeout of 10 minutes
start_time=$(date +%s)
timeout=$((start_time + 600))

echo "Starting node graph validation. This could take a few minutes..."

# Loop until all nodes have an address or the timeout is reached
while true; do
all_nodes_have_address=true

# Loop over all nodes
for node in "${!nodes[@]}"; do
# If the public key for a node is not set, exit with an error
if [ -z "${nodes[$node]}" ]; then
echo "Error: Public key for $node is not set"
exit 1
fi

# Get node info and check if the command succeeded
node_info=$(lnd1 getnodeinfo ${nodes[$node]} 2>&1)
if [ $? -ne 0 ]; then
echo "Error: Failed to get node info for $node"
echo "Details: $node_info"
all_nodes_have_address=false
break
fi

# Extract the addresses from the node info
node_addresses=$(echo "$node_info" | jq -c '.node.addresses')
echo "Addresses for $node: $node_addresses"

# Count the number of addresses and check if it's zero
address_count=$(echo "$node_addresses" | jq 'if . == [] then 0 else length end')
if [ $address_count -eq 0 ]; then
echo "Error: $node is missing an address"
all_nodes_have_address=false
break
fi
done

# If all nodes have an address, break the loop
if $all_nodes_have_address; then
break
fi

# If the current time is greater than or equal to the timeout, exit with an error
current_time=$(date +%s)
if [ $current_time -ge $timeout ]; then
echo "Timeout: Not all nodes have an address after 10 minutes"
exit 1
fi

# Wait for 5 seconds before the next iteration
sleep 5
done

# Calculate the elapsed time and print a success message
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
echo "All nodes have an address after $elapsed_time seconds"
}

# Helper function to print colored text
print_section() {
echo -e "\033[1;34m\n==================== $1 ====================\033[0m"
}

main() {
print_section "SETUP"
setup

print_section "WAIT FOR BITCOIND"
waitBitcoind

print_section "CREATE BITCOIND WALLET"
createBitcoindWallet

print_section "GENERATE BITCOIN ADDRESS"
generateBitcoinAddress

print_section "INITIALIZE BITCOIN CHAIN"
initBitcoinChain

print_section "WAIT FOR NODES"
waitForNodes

print_section "GENERATE NODE ADDRESSES"
generateNodeAddresses

print_section "GET NODE INFO"
getNodeInfo

print_section "FUND NODES"
fundNodes

print_section "OPEN CHANNEL"
openChannel

print_section "VALIDATE NODE GRAPH"
waitForGraphSync
}

main
File renamed without changes.
File renamed without changes.

0 comments on commit 29b7549

Please sign in to comment.