From 7f65de94dda8b336f4afe6b651bbd31a2866006e Mon Sep 17 00:00:00 2001
From: Vesa-Ville <v-v@equilibrium.co>
Date: Mon, 26 Feb 2024 16:16:28 +0200
Subject: [PATCH 1/2] feat: read custom snarkos binary from SNARKOS_BIN env var

---
 devnet.sh | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/devnet.sh b/devnet.sh
index e91627335a..1070b29dcb 100755
--- a/devnet.sh
+++ b/devnet.sh
@@ -1,5 +1,12 @@
 #!/bin/bash
 
+# Check if using a non-installed binary via environment variable (e.g. SNARKOS_BIN=target/release/snarkos)
+executable=${SNARKOS_BIN:-"snarkos"}
+
+if [[ $executable != "snarkos" ]]; then
+    echo "Using custom executable: $executable"
+fi
+
 # Read the total number of validators from the user or use a default value of 4
 read -p "Enter the total number of validators (default: 4): " total_validators
 total_validators=${total_validators:-4}
@@ -8,9 +15,12 @@ total_validators=${total_validators:-4}
 read -p "Enter the total number of clients (default: 2): " total_clients
 total_clients=${total_clients:-2}
 
-# Ask the user if they want to run 'cargo install --locked --path .' or use a pre-installed binary
-read -p "Do you want to run 'cargo install --locked --path .' to build the binary? (y/n, default: y): " build_binary
-build_binary=${build_binary:-y}
+# If using the installed executable,
+if [[ $executable == "snarkos" ]]; then
+    # Ask the user if they want to run 'cargo install --locked --path .' or use a pre-installed binary
+    read -p "Do you want to run 'cargo install --locked --path .' to build the binary? (y/n, default: y): " build_binary
+    build_binary=${build_binary:-y}
+fi
 
 # Ask the user whether to clear the existing ledger history
 read -p "Do you want to clear the existing ledger history? (y/n, default: n): " clear_ledger
@@ -28,7 +38,7 @@ if [[ $clear_ledger == "y" ]]; then
 
   for ((index = 0; index < $((total_validators + total_clients)); index++)); do
     # Run 'snarkos clean' for each node in the background
-    snarkos clean --dev $index &
+    $executable clean --dev $index &
 
     # Store the process ID of the background task
     clean_processes+=($!)
@@ -64,12 +74,12 @@ for validator_index in "${validator_indices[@]}"; do
 
   # Send the command to start the validator to the new window and capture output to the log file
   if [ "$validator_index" -eq 0 ]; then
-    tmux send-keys -t "devnet:window$validator_index" "snarkos start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file --metrics" C-m
+    tmux send-keys -t "devnet:window$validator_index" "$executable start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file --metrics" C-m
   else
     # Create a new window with a unique name
     window_index=$((validator_index + index_offset))
     tmux new-window -t "devnet:$window_index" -n "window$validator_index"
-    tmux send-keys -t "devnet:window$validator_index" "snarkos start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file" C-m
+    tmux send-keys -t "devnet:window$validator_index" "$executable start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file" C-m
   fi
 done
 
@@ -87,7 +97,7 @@ for client_index in "${client_indices[@]}"; do
   tmux new-window -t "devnet:$window_index" -n "window-$window_index"
 
   # Send the command to start the validator to the new window and capture output to the log file
-  tmux send-keys -t "devnet:window-$window_index" "snarkos start --nodisplay --dev $window_index --client --logfile $log_file" C-m
+  tmux send-keys -t "devnet:window-$window_index" "$executable start --nodisplay --dev $window_index --client --logfile $log_file" C-m
 done
 
 # Attach to the tmux session to view and interact with the windows

From 986e95a988df04df898a104fcc341dc0d773ee57 Mon Sep 17 00:00:00 2001
From: Vesa-Ville <v-v@equilibrium.co>
Date: Wed, 6 Mar 2024 19:08:21 +0200
Subject: [PATCH 2/2] feat: add auto-restart option

---
 devnet.sh | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/devnet.sh b/devnet.sh
index 1070b29dcb..e2dc0f7fdf 100755
--- a/devnet.sh
+++ b/devnet.sh
@@ -26,6 +26,11 @@ fi
 read -p "Do you want to clear the existing ledger history? (y/n, default: n): " clear_ledger
 clear_ledger=${clear_ledger:-n}
 
+# Read the automatic restart delay in seconds
+read -p "Enter the automatic validator restart delay in seconds, 0 to disable (default: 0): " restart_delay
+restart_delay=${restart_delay:-0}
+
+
 if [[ $build_binary == "y" ]]; then
   # Build the binary using 'cargo install --path .'
   cargo install --locked --path . || exit 1
@@ -74,12 +79,22 @@ for validator_index in "${validator_indices[@]}"; do
 
   # Send the command to start the validator to the new window and capture output to the log file
   if [ "$validator_index" -eq 0 ]; then
-    tmux send-keys -t "devnet:window$validator_index" "$executable start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file --metrics" C-m
+    if [ "$restart_delay" -gt 0 ]; then
+      tmux send-keys -t "devnet:window$validator_index" "while true; do $executable start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file --metrics; echo '--- Restarting in $restart_delay seconds ---'; sleep $restart_delay; done" C-m
+    else
+      tmux send-keys -t "devnet:window$validator_index" "$executable start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file --metrics" C-m
+    fi
+
   else
     # Create a new window with a unique name
     window_index=$((validator_index + index_offset))
     tmux new-window -t "devnet:$window_index" -n "window$validator_index"
-    tmux send-keys -t "devnet:window$validator_index" "$executable start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file" C-m
+
+    if [ "$restart_delay" -gt 0 ]; then
+      tmux send-keys -t "devnet:window$validator_index" "while true; do $executable start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file; echo '--- Restarting in $restart_delay seconds ---'; sleep $restart_delay; done" C-m
+    else
+      tmux send-keys -t "devnet:window$validator_index" "$executable start --nodisplay --dev $validator_index --dev-num-validators $total_validators --validator --logfile $log_file" C-m
+    fi
   fi
 done