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

feat: add local testnet guide #1815

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ function sidebarHome() {
text: "Install celestia-app",
link: "/how-to-guides/celestia-app",
},
{
text: "Set up a local testnet",
link: "/how-to-guides/local-testnet",
},
{ text: "Docker images", link: "/how-to-guides/docker-images" },
],
},
Expand Down
156 changes: 156 additions & 0 deletions how-to-guides/local-testnet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Setting up a Celestia local testnet

This guide walks through setting up a local Celestia testnet with a validator node, bridge node, and light node.

## Prerequisites

- celestia-app installed (based on [compatible versions](./participate.md))
- celestia node binary installed
- jq installed
- nc (netcat) installed

## Bash script

This method will start up the testnet with a bash script. It still assumes you have the prerequisites installed.

```bash
bash -c "$(curl -sL https://docs.celestia.org/start-local.sh)"
```
jcstein marked this conversation as resolved.
Show resolved Hide resolved

To interact with the nodes, you can use the `celestia` and `celestia-appd` CLIs. Use the [funding and testing](#funding-and-testing) section below as a guide.

## Manual setup

### Starting the validator node

First, navigate to the celestia-app scripts directory and run the single node script:

```bash
cd celestia-app/scripts
bash single-node.sh
```
Comment on lines +28 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation steps for successful node startup

Consider adding:

  1. Expected output or success indicators
  2. Commands to verify the validator node is running correctly
  3. Troubleshooting steps for common issues


### Setting up the bridge node

Once your validator node is running, get the genesis block hash:

```bash
curl -X GET "localhost:26657/block?height=1" | jq -r '.result .block_id.hash'
```

This will return a hash like:
```
1D53B32ACB02563E425BA1F8B5178B06A748E0F7B9748A8B1D07C34B454AF595
```

Set this as an environment variable:

```bash
export CELESTIA_CUSTOM=test:1D53B32ACB02563E425BA1F8B5178B06A748E0F7B9748A8B1D07C34B454AF595
```

Initialize the bridge node:

```bash
celestia bridge init \
--node.store $HOME/.celestia-custom-bridge \
--core.ip localhost \
--core.grpc.port 9090 \
--core.rpc.port 26657 \
--p2p.network test
```

Start the bridge node:

```bash
celestia bridge start \
--node.store $HOME/.celestia-custom-bridge \
--core.ip localhost \
--core.grpc.port 9090 \
--core.rpc.port 26657 \
--p2p.network test
```

### Setting up the light node

In a new terminal, set the same environment variable:

```bash
export CELESTIA_CUSTOM=test:1D53B32ACB02563E425BA1F8B5178B06A748E0F7B9748A8B1D07C34B454AF595
```

Initialize the light node:

```bash
celestia light init \
--p2p.network test \
--core.ip localhost:26657 \
--node.store $HOME/.celestia-custom-light/
```

Get the bridge node's peer info:

```bash
celestia p2p info --node.store $HOME/.celestia-custom-bridge
```

From the output, locate the relevant IP4 address and peer ID. The output will look similar to this example output:

```json
{
"result": {
"id": "12D3KooWAVsZ36CdczaEXNNnDpsPcVyAnbeBe5EPG7AjttRGfux7",
"peer_addr": [
"/ip4/10.0.0.125/tcp/2121",
"/ip4/10.0.0.125/udp/2121/webrtc-direct/certhash/uEiBXDYwH1McBsLM2aEc3SdvAuhq2ZQ0RUgbjgTvXMQm7LA",
"/ip4/192.0.2.0/tcp/16279",
"/ip4/192.0.2.0/udp/16279/webrtc-direct/certhash/uEiBXDYwH1McBsLM2aEc3SdvAuhq2ZQ0RUgbjgTvXMQm7LA",
"/ip4/127.0.0.1/udp/2121/webrtc-direct/certhash/uEiBXDYwH1McBsLM2aEc3SdvAuhq2ZQ0RUgbjgTvXMQm7LA",
"/ip6/::1/tcp/2121",
"/ip6/::1/udp/2121/webrtc-direct/certhash/uEiBXDYwH1McBsLM2aEc3SdvAuhq2ZQ0RUgbjgTvXMQm7LA"
]
}
}
```

Start the light node with custom RPC port and trusted peer:

```bash
celestia light start \
--p2p.network test \
--core.ip localhost:26657 \
--node.store $HOME/.celestia-custom-light/ \
--headers.trusted-peers /ip4/10.0.0.125/udp/2121/webrtc-direct/certhash/uEiBXDYwH1McBsLM2aEc3SdvAuhq2ZQ0RUgbjgTvXMQm7LA/p2p/12D3KooWAVsZ36CdczaEXNNnDpsPcVyAnbeBe5EPG7AjttRGfux7 \
--rpc.port 42069
```

### Funding and testing

Get the bridge node's account address:

```bash
celestia state account-address --node.store $HOME/.celestia-custom-bridge
```

Get the light node's account address:

```bash
celestia state account-address --node.store $HOME/.celestia-custom-light --url http://localhost:42069
```

Send funds to the light node:

```bash
celestia-appd tx bank send validator celestia1p8yx4yveuu6ushyccepsuknaqpqn9ppq07m4n3 10000000utia \
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Document address usage

Replace the hardcoded address with a placeholder and explain:

  1. How to obtain the correct address
  2. The expected address format

--chain-id test \
--keyring-backend test \
--fees 500utia
```

Test by submitting a blob from the light node:

```bash
celestia blob submit 0x4772756763686174 '"Simplicity is the ultimate sophistication." -Leonardo da Vinci' \
--node.store $HOME/.celestia-custom-light \
--url http://localhost:42069
```
139 changes: 139 additions & 0 deletions public/start-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/bin/bash

# Warning for default .celestia-app directory deletion
echo "WARNING: This script will DELETE your existing .celestia-app directory and start a new local testnet."
echo "Make sure you have backed up any important data from .celestia-app"
read -p "Do you wish to continue? [y/n] " answer
if [[ ! "$answer" =~ ^[Yy]$ ]]; then
echo "Aborting..."
exit 1
fi

# Function to check if a command exists
check_command() {
if ! command -v "$1" &> /dev/null; then
echo "Error: $1 is required but not installed."
exit 1
fi
}

# Function to wait for a service to be ready
wait_for_service() {
local port=$1
local service=$2
echo "Waiting for $service to be ready..."
while ! nc -z localhost "$port"; do
sleep 1
done
echo "$service is ready!"
sleep 5 # Give it a little extra time to stabilize
}
Comment on lines +21 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add timeout and configurable stabilization delay to wait_for_service

The current implementation has two potential issues:

  1. No timeout mechanism could lead to an infinite wait
  2. Fixed 5-second stabilization delay might not be sufficient for all services
 wait_for_service() {
     local port=$1
     local service=$2
+    local timeout=${3:-60}  # Default 60 seconds timeout
+    local stabilization_delay=${4:-5}  # Default 5 seconds stabilization
+    local start_time=$(date +%s)
     echo "Waiting for $service to be ready..."
     while ! nc -z localhost "$port"; do
+        if [ $(($(date +%s) - start_time)) -gt "$timeout" ]; then
+            echo "Timeout waiting for $service after $timeout seconds"
+            exit 1
+        fi
         sleep 1
     done
     echo "$service is ready!"
-    sleep 5  # Give it a little extra time to stabilize
+    sleep "$stabilization_delay"
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
wait_for_service() {
local port=$1
local service=$2
echo "Waiting for $service to be ready..."
while ! nc -z localhost "$port"; do
sleep 1
done
echo "$service is ready!"
sleep 5 # Give it a little extra time to stabilize
}
wait_for_service() {
local port=$1
local service=$2
local timeout=${3:-60} # Default 60 seconds timeout
local stabilization_delay=${4:-5} # Default 5 seconds stabilization
local start_time=$(date +%s)
echo "Waiting for $service to be ready..."
while ! nc -z localhost "$port"; do
if [ $(($(date +%s) - start_time)) -gt "$timeout" ]; then
echo "Timeout waiting for $service after $timeout seconds"
exit 1
fi
sleep 1
done
echo "$service is ready!"
sleep "$stabilization_delay"
}


# Check for required commands
check_command "celestia-appd"
check_command "celestia"
check_command "jq"
check_command "nc"

# Clean up existing directories
rm -rf "$HOME/.celestia-custom-bridge"
rm -rf "$HOME/.celestia-custom-light"

# Create necessary directories
mkdir -p "$HOME/.celestia-custom-bridge"
mkdir -p "$HOME/.celestia-custom-light"

# Kill any existing celestia processes
pkill celestia-appd
pkill celestia
sleep 2
Comment on lines +38 to +49
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling and process cleanup

The current implementation could be enhanced with better error handling and cleaner process management.

+# Function to safely remove directory
+safe_remove_dir() {
+    if [ -d "$1" ]; then
+        rm -rf "$1" || {
+            echo "Error: Failed to remove directory $1"
+            exit 1
+        }
+    fi
+}
+
+# Function to safely stop process
+stop_process() {
+    if pgrep "$1" >/dev/null; then
+        pkill "$1"
+        sleep 2
+        if pgrep "$1" >/dev/null; then
+            pkill -9 "$1"  # Force kill if still running
+        fi
+    fi
+}
+
 # Clean up existing directories
-rm -rf "$HOME/.celestia-custom-bridge"
-rm -rf "$HOME/.celestia-custom-light"
+safe_remove_dir "$HOME/.celestia-custom-bridge"
+safe_remove_dir "$HOME/.celestia-custom-light"
 
 # Kill any existing celestia processes
-pkill celestia-appd
-pkill celestia
-sleep 2
+stop_process "celestia-appd"
+stop_process "celestia"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Clean up existing directories
rm -rf "$HOME/.celestia-custom-bridge"
rm -rf "$HOME/.celestia-custom-light"
# Create necessary directories
mkdir -p "$HOME/.celestia-custom-bridge"
mkdir -p "$HOME/.celestia-custom-light"
# Kill any existing celestia processes
pkill celestia-appd
pkill celestia
sleep 2
# Function to safely remove directory
safe_remove_dir() {
if [ -d "$1" ]; then
rm -rf "$1" || {
echo "Error: Failed to remove directory $1"
exit 1
}
fi
}
# Function to safely stop process
stop_process() {
if pgrep "$1" >/dev/null; then
pkill "$1"
sleep 2
if pgrep "$1" >/dev/null; then
pkill -9 "$1" # Force kill if still running
fi
fi
}
# Clean up existing directories
safe_remove_dir "$HOME/.celestia-custom-bridge"
safe_remove_dir "$HOME/.celestia-custom-light"
# Create necessary directories
mkdir -p "$HOME/.celestia-custom-bridge"
mkdir -p "$HOME/.celestia-custom-light"
# Kill any existing celestia processes
stop_process "celestia-appd"
stop_process "celestia"


# Start the validator node
echo "Starting validator node..."
cd "$HOME/celestia-app/scripts" || exit 1

# Automatically answer "y" to the prompt
echo "y" | bash single-node.sh &
wait_for_service 26657 "validator node"

# Get the genesis block hash
echo "Getting genesis block hash..."
BLOCK_HASH=$(curl -s "localhost:26657/block?height=1" | jq -r '.result.block_id.hash')
if [ -z "$BLOCK_HASH" ]; then
echo "Error: Could not get block hash"
exit 1
fi
Comment on lines +51 to +65
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Improve validator setup reliability

The current implementation has several potential points of failure that should be addressed.

 # Start the validator node
 echo "Starting validator node..."
-cd "$HOME/celestia-app/scripts" || exit 1
+SCRIPT_PATH="$HOME/celestia-app/scripts/single-node.sh"
+if [ ! -f "$SCRIPT_PATH" ]; then
+    echo "Error: single-node.sh not found at $SCRIPT_PATH"
+    exit 1
+fi
 
 # Automatically answer "y" to the prompt
-echo "y" | bash single-node.sh &
+TMPFILE=$(mktemp)
+trap 'rm -f $TMPFILE' EXIT
+expect << EOF
+spawn bash $SCRIPT_PATH
+expect "Do you want to proceed? (y/n):"
+send "y\r"
+expect eof
+EOF
 wait_for_service 26657 "validator node"
 
 # Get the genesis block hash
 echo "Getting genesis block hash..."
-BLOCK_HASH=$(curl -s "localhost:26657/block?height=1" | jq -r '.result.block_id.hash')
+BLOCK_HASH=$(curl -s --max-time 10 "localhost:26657/block?height=1" | jq -r '.result.block_id.hash')
 if [ -z "$BLOCK_HASH" ]; then
     echo "Error: Could not get block hash"
     exit 1
 fi

Note: This change requires the expect utility. Please add it to the command check list:

 check_command "celestia-appd"
 check_command "celestia"
 check_command "jq"
 check_command "nc"
+check_command "expect"

Committable suggestion skipped: line range outside the PR's diff.


# Export the custom network variable
export CELESTIA_CUSTOM="test:$BLOCK_HASH"
echo "Set CELESTIA_CUSTOM=$CELESTIA_CUSTOM"

# Initialize and start bridge node
echo "Initializing bridge node..."
celestia bridge init \
--node.store "$HOME/.celestia-custom-bridge" \
--core.ip localhost \
--core.grpc.port 9090 \
--core.rpc.port 26657 \
--p2p.network test
echo "Starting bridge node..."
celestia bridge start \
--node.store "$HOME/.celestia-custom-bridge" \
--core.ip localhost \
--core.grpc.port 9090 \
--core.rpc.port 26657 \
--p2p.network test &
wait_for_service 2121 "bridge node"

# Get bridge node info
echo "Getting bridge node peer info..."
BRIDGE_INFO=$(celestia p2p info --node.store "$HOME/.celestia-custom-bridge")
PEER_ID=$(echo "$BRIDGE_INFO" | jq -r '.result.id')
PEER_ADDR=$(echo "$BRIDGE_INFO" | jq -r '.result.peer_addr[] | select(contains("/ip4/10.0.0.125") and contains("/udp/") and contains("/webrtc-direct/"))' | head -n 1)
if [ -z "$PEER_ID" ] || [ -z "$PEER_ADDR" ]; then
echo "Error: Could not get peer information"
exit 1
fi
Comment on lines +71 to +96
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Enhance bridge node setup reliability

The bridge node setup needs better error handling and more robust peer address selection.

+# Function to verify bridge node initialization
+verify_bridge_init() {
+    local store_path="$1"
+    if [ ! -d "$store_path/keys" ] || [ ! -f "$store_path/config.toml" ]; then
+        echo "Error: Bridge node initialization failed"
+        exit 1
+    fi
+}
+
 # Initialize and start bridge node
 echo "Initializing bridge node..."
 celestia bridge init \
     --node.store "$HOME/.celestia-custom-bridge" \
     --core.ip localhost \
     --core.grpc.port 9090 \
     --core.rpc.port 26657 \
-    --p2p.network test
+    --p2p.network test || exit 1
+
+verify_bridge_init "$HOME/.celestia-custom-bridge"
+
 echo "Starting bridge node..."
 celestia bridge start \
     --node.store "$HOME/.celestia-custom-bridge" \
     --core.ip localhost \
     --core.grpc.port 9090 \
     --core.rpc.port 26657 \
-    --p2p.network test &
+    --p2p.network test > bridge.log 2>&1 &
+BRIDGE_PID=$!
+
 wait_for_service 2121 "bridge node"
 
+# Verify bridge node is still running
+if ! kill -0 $BRIDGE_PID 2>/dev/null; then
+    echo "Error: Bridge node failed to start. Check bridge.log for details"
+    exit 1
+fi
+
 # Get bridge node info
 echo "Getting bridge node peer info..."
-BRIDGE_INFO=$(celestia p2p info --node.store "$HOME/.celestia-custom-bridge")
+BRIDGE_INFO=$(celestia p2p info --node.store "$HOME/.celestia-custom-bridge") || {
+    echo "Error: Failed to get bridge node info"
+    exit 1
+}
+
-PEER_ADDR=$(echo "$BRIDGE_INFO" | jq -r '.result.peer_addr[] | select(contains("/ip4/10.0.0.125") and contains("/udp/") and contains("/webrtc-direct/"))' | head -n 1)
+# More robust peer address selection
+PEER_ADDR=$(echo "$BRIDGE_INFO" | jq -r '.result.peer_addr[] | 
+    select(contains("/ip4/") and (contains("/tcp/") or contains("/udp/")))' | head -n 1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Initialize and start bridge node
echo "Initializing bridge node..."
celestia bridge init \
--node.store "$HOME/.celestia-custom-bridge" \
--core.ip localhost \
--core.grpc.port 9090 \
--core.rpc.port 26657 \
--p2p.network test
echo "Starting bridge node..."
celestia bridge start \
--node.store "$HOME/.celestia-custom-bridge" \
--core.ip localhost \
--core.grpc.port 9090 \
--core.rpc.port 26657 \
--p2p.network test &
wait_for_service 2121 "bridge node"
# Get bridge node info
echo "Getting bridge node peer info..."
BRIDGE_INFO=$(celestia p2p info --node.store "$HOME/.celestia-custom-bridge")
PEER_ID=$(echo "$BRIDGE_INFO" | jq -r '.result.id')
PEER_ADDR=$(echo "$BRIDGE_INFO" | jq -r '.result.peer_addr[] | select(contains("/ip4/10.0.0.125") and contains("/udp/") and contains("/webrtc-direct/"))' | head -n 1)
if [ -z "$PEER_ID" ] || [ -z "$PEER_ADDR" ]; then
echo "Error: Could not get peer information"
exit 1
fi
# Function to verify bridge node initialization
verify_bridge_init() {
local store_path="$1"
if [ ! -d "$store_path/keys" ] || [ ! -f "$store_path/config.toml" ]; then
echo "Error: Bridge node initialization failed"
exit 1
fi
}
# Initialize and start bridge node
echo "Initializing bridge node..."
celestia bridge init \
--node.store "$HOME/.celestia-custom-bridge" \
--core.ip localhost \
--core.grpc.port 9090 \
--core.rpc.port 26657 \
--p2p.network test || exit 1
verify_bridge_init "$HOME/.celestia-custom-bridge"
echo "Starting bridge node..."
celestia bridge start \
--node.store "$HOME/.celestia-custom-bridge" \
--core.ip localhost \
--core.grpc.port 9090 \
--core.rpc.port 26657 \
--p2p.network test > bridge.log 2>&1 &
BRIDGE_PID=$!
wait_for_service 2121 "bridge node"
# Verify bridge node is still running
if ! kill -0 $BRIDGE_PID 2>/dev/null; then
echo "Error: Bridge node failed to start. Check bridge.log for details"
exit 1
fi
# Get bridge node info
echo "Getting bridge node peer info..."
BRIDGE_INFO=$(celestia p2p info --node.store "$HOME/.celestia-custom-bridge") || {
echo "Error: Failed to get bridge node info"
exit 1
}
PEER_ID=$(echo "$BRIDGE_INFO" | jq -r '.result.id')
# More robust peer address selection
PEER_ADDR=$(echo "$BRIDGE_INFO" | jq -r '.result.peer_addr[] |
select(contains("/ip4/") and (contains("/tcp/") or contains("/udp/")))' | head -n 1)
if [ -z "$PEER_ID" ] || [ -z "$PEER_ADDR" ]; then
echo "Error: Could not get peer information"
exit 1
fi


# Initialize and start light node
echo "Initializing light node..."
celestia light init \
--p2p.network test \
--core.ip localhost:26657 \
--node.store "$HOME/.celestia-custom-light/"

# Construct the light node start command
LIGHT_NODE_CMD="celestia light start \
--p2p.network test \
--core.ip localhost:26657 \
--node.store \"$HOME/.celestia-custom-light/\" \
--headers.trusted-peers \"$PEER_ADDR/p2p/$PEER_ID\" \
--rpc.port 42069"

# Log the command
echo "Starting light node with command:"
echo "$LIGHT_NODE_CMD"

# Execute the command
eval "$LIGHT_NODE_CMD" &
wait_for_service 42069 "light node"
Comment on lines +98 to +119
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Improve light node setup security and reliability

The current implementation uses eval and lacks proper error handling.

 # Initialize and start light node
 echo "Initializing light node..."
 celestia light init \
     --p2p.network test \
     --core.ip localhost:26657 \
-    --node.store "$HOME/.celestia-custom-light/"
+    --node.store "$HOME/.celestia-custom-light/" || {
+        echo "Error: Light node initialization failed"
+        exit 1
+    }
 
-# Construct the light node start command
-LIGHT_NODE_CMD="celestia light start \
-    --p2p.network test \
-    --core.ip localhost:26657 \
-    --node.store \"$HOME/.celestia-custom-light/\" \
-    --headers.trusted-peers \"$PEER_ADDR/p2p/$PEER_ID\" \
-    --rpc.port 42069"
-
-# Log the command
-echo "Starting light node with command:"
-echo "$LIGHT_NODE_CMD"
-
-# Execute the command
-eval "$LIGHT_NODE_CMD" &
+# Start light node directly without eval
+echo "Starting light node..."
+celestia light start \
+    --p2p.network test \
+    --core.ip localhost:26657 \
+    --node.store "$HOME/.celestia-custom-light/" \
+    --headers.trusted-peers "$PEER_ADDR/p2p/$PEER_ID" \
+    --rpc.port 42069 > light.log 2>&1 &
+LIGHT_PID=$!
+
 wait_for_service 42069 "light node"
+
+# Verify light node is still running
+if ! kill -0 $LIGHT_PID 2>/dev/null; then
+    echo "Error: Light node failed to start. Check light.log for details"
+    exit 1
+fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Initialize and start light node
echo "Initializing light node..."
celestia light init \
--p2p.network test \
--core.ip localhost:26657 \
--node.store "$HOME/.celestia-custom-light/"
# Construct the light node start command
LIGHT_NODE_CMD="celestia light start \
--p2p.network test \
--core.ip localhost:26657 \
--node.store \"$HOME/.celestia-custom-light/\" \
--headers.trusted-peers \"$PEER_ADDR/p2p/$PEER_ID\" \
--rpc.port 42069"
# Log the command
echo "Starting light node with command:"
echo "$LIGHT_NODE_CMD"
# Execute the command
eval "$LIGHT_NODE_CMD" &
wait_for_service 42069 "light node"
# Initialize and start light node
echo "Initializing light node..."
celestia light init \
--p2p.network test \
--core.ip localhost:26657 \
--node.store "$HOME/.celestia-custom-light/" || {
echo "Error: Light node initialization failed"
exit 1
}
# Start light node directly without eval
echo "Starting light node..."
celestia light start \
--p2p.network test \
--core.ip localhost:26657 \
--node.store "$HOME/.celestia-custom-light/" \
--headers.trusted-peers "$PEER_ADDR/p2p/$PEER_ID" \
--rpc.port 42069 > light.log 2>&1 &
LIGHT_PID=$!
wait_for_service 42069 "light node"
# Verify light node is still running
if ! kill -0 $LIGHT_PID 2>/dev/null; then
echo "Error: Light node failed to start. Check light.log for details"
exit 1
fi


# Get addresses and fund light node
echo "Getting node addresses..."
BRIDGE_ADDRESS=$(celestia state account-address --node.store "$HOME/.celestia-custom-bridge" | jq -r '.result')
LIGHT_ADDRESS=$(celestia state account-address --node.store "$HOME/.celestia-custom-light" --url http://localhost:42069 | jq -r '.result')
echo "Funding light node..."
celestia-appd tx bank send validator "$LIGHT_ADDRESS" 10000000utia \
--chain-id test \
--keyring-backend test \
--fees 500utia -y
echo "Waiting for funding transaction to be included in a block..."
sleep 5
echo "Testing blob submission..."
celestia blob submit 0x4772756763686174 '"Simplicity is the ultimate sophistication." -Leonardo da Vinci' \
--node.store "$HOME/.celestia-custom-light" \
--url http://localhost:42069
echo "Setup complete!"
echo "Bridge node address: $BRIDGE_ADDRESS"
echo "Light node address: $LIGHT_ADDRESS"
echo "Custom network: $CELESTIA_CUSTOM"
Comment on lines +121 to +139
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve final setup reliability and add cleanup

The current implementation needs better transaction validation and cleanup handling.

+# Trap for cleanup on script exit
+cleanup() {
+    echo "Cleaning up..."
+    stop_process "celestia-appd"
+    stop_process "celestia"
+    rm -f light.log bridge.log
+}
+trap cleanup EXIT
+
 # Get addresses and fund light node
 echo "Getting node addresses..."
-BRIDGE_ADDRESS=$(celestia state account-address --node.store "$HOME/.celestia-custom-bridge" | jq -r '.result')
-LIGHT_ADDRESS=$(celestia state account-address --node.store "$HOME/.celestia-custom-light" --url http://localhost:42069 | jq -r '.result')
+BRIDGE_ADDRESS=$(celestia state account-address --node.store "$HOME/.celestia-custom-bridge" | jq -r '.result') || {
+    echo "Error: Failed to get bridge address"
+    exit 1
+}
+LIGHT_ADDRESS=$(celestia state account-address --node.store "$HOME/.celestia-custom-light" --url http://localhost:42069 | jq -r '.result') || {
+    echo "Error: Failed to get light address"
+    exit 1
+}
+
 echo "Funding light node..."
-celestia-appd tx bank send validator "$LIGHT_ADDRESS" 10000000utia \
+TX_HASH=$(celestia-appd tx bank send validator "$LIGHT_ADDRESS" 10000000utia \
     --chain-id test \
     --keyring-backend test \
-    --fees 500utia -y
+    --fees 500utia -y | jq -r '.txhash') || {
+    echo "Error: Funding transaction failed"
+    exit 1
+}
+
 echo "Waiting for funding transaction to be included in a block..."
-sleep 5
+for i in {1..30}; do
+    if celestia-appd query tx "$TX_HASH" 2>/dev/null; then
+        break
+    fi
+    if [ "$i" -eq 30 ]; then
+        echo "Error: Funding transaction not confirmed after 30 seconds"
+        exit 1
+    fi
+    sleep 1
+done
+
 echo "Testing blob submission..."
 celestia blob submit 0x4772756763686174 '"Simplicity is the ultimate sophistication." -Leonardo da Vinci' \
     --node.store "$HOME/.celestia-custom-light" \
-    --url http://localhost:42069
+    --url http://localhost:42069 || {
+    echo "Error: Blob submission failed"
+    exit 1
+}
+
 echo "Setup complete!"
 echo "Bridge node address: $BRIDGE_ADDRESS"
 echo "Light node address: $LIGHT_ADDRESS"
 echo "Custom network: $CELESTIA_CUSTOM"
+echo "Logs available at: bridge.log and light.log"

Committable suggestion skipped: line range outside the PR's diff.