/ .ci / generate_ledger.sh
generate_ledger.sh
 1  #!/bin/bash
 2  
 3  ####################################################
 4  # Runs a network up to a certain height and stores
 5  # the first node's ledger in a zipfile.
 6  ####################################################
 7  
 8  set -eo pipefail # error on any command failure
 9  
10  # Uncomment this to print commands before executing them for easier debugging.
11  #set -x
12  
13  # Change this to increase/decrease logging
14  log_filter="info,snarkos_node_rest=warn,snarkos_node_bft::primary=error,snarkos_node_router=error,snarkos_node_tcp=off"
15  
16  # Set parameters directly
17  total_validators=$1
18  min_height=$2
19  network_id=$3
20  
21  # How often to poll the network height (in seconds)
22  poll_interval=10
23  
24  # Default values if not provided
25  : "${total_validators:=40}"
26  : "${min_height:=250}"
27  : "${network_id:=1}"
28  
29  #shellcheck source=SCRIPTDIR/utils.sh
30  . ./.ci/utils.sh
31  
32  git_commit=$(git rev-parse --short=10 HEAD)
33  echo "On git commit ${git_commit}"
34  
35  network_name=$(get_network_name "$network_id")
36  echo "Network set to $network_name with $total_validators validators"
37  
38  # Create log directory
39  log_dir="$PWD/.logs-$(date +"%Y%m%d%H%M%S")"
40  mkdir -p "$log_dir"
41  chmod 755 "$log_dir"
42  
43  # Define a trap handler that cleans up all processes on exit.
44  #shellcheck disable=SC2329
45  function exit_handler() {
46    stop_nodes
47  }
48  trap exit_handler EXIT
49  
50  # Define a trap handler that prints a message when an error occurs 
51  trap 'echo "⛔️ Error in $BASH_SOURCE at line $LINENO: \"$BASH_COMMAND\" failed (exit $?)"' ERR
52  
53  # Flags used by all ndoes
54  common_flags=(--nodisplay --nobanner --noupdater "--network=$network_id"
55    "--log-filter=$log_filter" "--dev-num-validators=$total_validators")
56  
57  # Start all validator nodes in the background
58  for ((validator_index = 0; validator_index < total_validators; validator_index++)); do
59    snarkos clean --dev $validator_index "--network=${network_id}"
60  
61    log_file="$log_dir/validator-$validator_index.log"
62    if [ $validator_index -eq 0 ]; then
63      snarkos start "${common_flags[@]}" --dev "$validator_index" \
64        --validator --logfile "$log_file" --metrics --no-dev-txs &
65    else
66      snarkos start "${common_flags[@]}" --dev "$validator_index" \
67        --validator --logfile "$log_file" &
68    fi
69    PIDS[validator_index]=$!
70    echo "Started validator $validator_index with PID ${PIDS[$validator_index]}"
71  
72    # Add 1-second delay between starting nodes to avoid hitting rate limits
73    sleep 1
74  done
75  
76  # Ensure all nodes are up and running.
77  wait_for_nodes "$total_validators" 0
78  
79  # Wait until the first node reaches the given height.
80  total_wait=0
81  while ! check_heights 0 1 "$min_height" "$network_name"; do
82    # Continue waiting
83    sleep $poll_interval
84    total_wait=$((total_wait + poll_interval))
85    echo "Waited $total_wait seconds so far..."
86  done
87  
88  printf "num_validators=%i, git_commit=%s, snapshot_height=%i" "$total_validators" "$git_commit" "$min_height" > info.txt
89  
90  zipname="sync-ledger-val${total_validators}-${min_height}-${git_commit}.zip"
91  echo "Done! Generating zipfile \"$zipname\""
92  zip -r "$zipname" ".ledger-${network_id}-0" info.txt
93  
94  exit 0