|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +################################################################# |
| 4 | +# Measures a validator syncing 1000 blocks from another validator |
| 5 | +################################################################# |
| 6 | + |
| 7 | +set -eo pipefail # error on any command failure |
| 8 | + |
| 9 | +network_id=1 |
| 10 | +min_height=250 |
| 11 | + |
| 12 | +# The total number of validators in the beacon committee. |
| 13 | +# This must match the number of validators used when generating the snapshot. |
| 14 | +num_validators=40 |
| 15 | + |
| 16 | +# The number of validators that are syncing |
| 17 | +num_nodes=1 |
| 18 | + |
| 19 | +# Adjust this to show more/less log messages |
| 20 | +log_filter="info,snarkos_node_sync=trace,snarkos_node_bft::sync=trace,snarkos_node_bft::primary=warn,snarkos_node_rest=warn" |
| 21 | + |
| 22 | +max_wait=1800 # Wait for up to 30 minutes |
| 23 | +poll_interval=1 # Check block heights every second |
| 24 | + |
| 25 | +. ./.ci/utils.sh |
| 26 | + |
| 27 | +branch_name=$(git rev-parse --abbrev-ref HEAD) |
| 28 | +echo "On branch: ${branch_name}" |
| 29 | + |
| 30 | +network_name=$(get_network_name $network_id) |
| 31 | +echo "Using network: $network_name (ID: $network_id)" |
| 32 | + |
| 33 | +snapshot_info=$(<info.txt) |
| 34 | +echo "Snapshot_info: ${snapshot_info}" |
| 35 | + |
| 36 | +# Create log directory |
| 37 | +log_dir=".logs-$(date +"%Y%m%d%H%M%S")" |
| 38 | +mkdir -p "$log_dir" |
| 39 | + |
| 40 | +# Define a trap handler that cleans up all processes on exit. |
| 41 | +function exit_handler() { |
| 42 | + stop_nodes |
| 43 | +} |
| 44 | +trap exit_handler EXIT |
| 45 | +trap child_exit_handler CHLD |
| 46 | + |
| 47 | +# Define a trap handler that prints a message when an error occurs |
| 48 | +trap 'echo "⛔️ Error in $BASH_SOURCE at line $LINENO: \"$BASH_COMMAND\" failed (exit $?)"' ERR |
| 49 | + |
| 50 | +# Shared flags betwen all nodes |
| 51 | +common_flags=( |
| 52 | + --nobanner --noupdater --nodisplay \ |
| 53 | + "--network=$network_id" |
| 54 | + --nocdn |
| 55 | + "--dev-num-validators=$num_validators" |
| 56 | + --no-dev-txs |
| 57 | + "--log-filter=$log_filter" |
| 58 | +) |
| 59 | + |
| 60 | +# The validator that has the ledger to by synced from. |
| 61 | +$TASKSET1 snarkos start --dev 0 --validator "${common_flags[@]}" \ |
| 62 | + --logfile="$log_dir/validator-0.log" & |
| 63 | +PIDS[0]=$! |
| 64 | + |
| 65 | +# Stores the list of all validators. |
| 66 | +validators="127.0.0.1:5000" |
| 67 | + |
| 68 | +# Spawn the clients that will sync the ledger |
| 69 | +for node_index in $(seq 1 "$num_nodes"); do |
| 70 | + name="validator-$node_index" |
| 71 | + |
| 72 | + # Ensure there are no old ledger files and the node syncs from scratch |
| 73 | + snarkos clean "--dev=$node_index" "--network=$network_id" || true |
| 74 | + |
| 75 | + $TASKSET2 snarkos start "--dev=$node_index" --validator \ |
| 76 | + "${common_flags[@]}" "--validators=$validators" \ |
| 77 | + "--logfile=$log_dir/$name.log" & |
| 78 | + PIDS[node_index]=$! |
| 79 | + |
| 80 | + # Add the validators BFT address to the validators list. |
| 81 | + bft_port=$((5000 + node_index)) |
| 82 | + validators="$validators,127.0.0.1:$bft_port" |
| 83 | + |
| 84 | + # Add 1-second delay between starting nodes to avoid hitting rate limits |
| 85 | + sleep 1 |
| 86 | +done |
| 87 | + |
| 88 | +# Block until nodes are running and connected to each other. |
| 89 | +wait_for_nodes $((num_nodes+1)) 0 |
| 90 | + |
| 91 | +SECONDS=0 |
| 92 | + |
| 93 | +# TODO add API call for number of connected validators. |
| 94 | +#for ((node_index = 0; node_index < num_nodes+1; node_index++)); do |
| 95 | +# if ! (wait_for_peers "$node_index" $num_nodes); then |
| 96 | +# exit 1 |
| 97 | +# fi |
| 98 | +#done |
| 99 | + |
| 100 | +connect_time=$SECONDS |
| 101 | +echo "ℹ️ Nodes are fully connected (took $connect_time secs). Starting block sync measurement." |
| 102 | + |
| 103 | +# Check heights periodically with a timeout |
| 104 | +SECONDS=0 |
| 105 | +while (( SECONDS < max_wait )); do |
| 106 | + # The last block cannot be fully applied to the ledger yet as there is no next block to confirm it. |
| 107 | + # However, we know that the sync height of a node is always at least one more than the ledger height. |
| 108 | + expected_height=$((min_height-1)) |
| 109 | + |
| 110 | + if check_heights 1 $((num_nodes+1)) $expected_height "$network_name" "$SECONDS"; then |
| 111 | + total_wait=$SECONDS |
| 112 | + throughput=$(compute_throughput "$min_height" "$total_wait") |
| 113 | + |
| 114 | + echo "🎉 BFT sync benchmark done! Waited $total_wait seconds for $min_height blocks. Throughput was $throughput blocks/s." |
| 115 | + |
| 116 | + # Append data to results file. |
| 117 | + printf "{ \"name\": \"bft-sync\", \"unit\": \"blocks/s\", \"value\": %.3f, \"extra\": \"total_wait=%is, target_height=%i, connect_time=%i, branch=%s, %s\" },\n" \ |
| 118 | + "$throughput" "$total_wait" "$min_height" "$connect_time" "$branch_name" "$snapshot_info"| tee -a results.json |
| 119 | + exit 0 |
| 120 | + fi |
| 121 | + |
| 122 | + # Continue waiting |
| 123 | + sleep $poll_interval |
| 124 | +done |
| 125 | + |
| 126 | +echo "❌ Benchmark failed! Validators did not sync within 30 minutes." |
| 127 | + |
| 128 | +# Print logs for debugging |
| 129 | +echo "Last 20 lines of validators logs:" |
| 130 | +for ((node_index = 0; node_index <= num_nodes; node_index++)); do |
| 131 | + echo "=== Node $node_index logs ===" |
| 132 | + tail -n 20 "$log_dir/validator-$node_index.log" |
| 133 | +done |
| 134 | + |
| 135 | +exit 1 |
0 commit comments