Last active
January 20, 2024 22:39
-
-
Save clemensgg/23a56c9750f788c302c35f5c3b483eb7 to your computer and use it in GitHub Desktop.
TM sync info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if RPC URLs are provided | |
if [ -z "$1" ] || [ -z "$2" ]; then | |
echo "Please provide the primary and reference RPC URLs as arguments." | |
exit 1 | |
fi | |
PRIMARY_RPC_URL="$1" | |
REFERENCE_RPC_URL="$2" | |
# Polling interval in seconds | |
INTERVAL=5 | |
# Maximum number of measurements to consider for averaging | |
MAX_MEASUREMENTS=10 | |
# Function to get current block height from an RPC URL | |
get_current_block_height() { | |
local rpc_url=$1 | |
local info=$(curl -s "$rpc_url/status") | |
local has_result=$(echo "$info" | jq -r '.result != null') | |
local latest_block_height | |
if [ "$has_result" = "true" ]; then | |
latest_block_height=$(echo "$info" | jq -r '.result.sync_info.latest_block_height') | |
else | |
latest_block_height=$(echo "$info" | jq -r '.sync_info.latest_block_height') | |
fi | |
if [ "$latest_block_height" == "null" ]; then | |
echo "null" | |
else | |
echo "$latest_block_height" | |
fi | |
} | |
# Arrays to store block heights and times for averaging for both nodes | |
primary_block_heights=() | |
primary_measurement_times=() | |
reference_block_heights=() | |
reference_measurement_times=() | |
# Function to update arrays and maintain size | |
update_measurements() { | |
local -n heights=$1 | |
local -n times=$2 | |
local new_height=$3 | |
local new_time=$4 | |
heights+=($new_height) | |
times+=($new_time) | |
# Remove oldest entry if the arrays exceed the max size | |
if [ ${#heights[@]} -gt $MAX_MEASUREMENTS ]; then | |
heights=("${heights[@]:1}") | |
times=("${times[@]:1}") | |
fi | |
} | |
# Function to calculate average sync speed | |
calculate_average_sync_speed() { | |
local -n heights=$1 | |
local -n times=$2 | |
local length=${#heights[@]} | |
if [ $length -le 1 ]; then | |
echo 0 | |
return | |
fi | |
local height_diff=$(( heights[length-1] - heights[0] )) | |
local time_diff=$(( times[length-1] - times[0] )) | |
if [ $time_diff -eq 0 ]; then | |
echo 0 | |
else | |
echo "scale=2; $height_diff / $time_diff" | bc | |
fi | |
} | |
# Function to estimate ETA to match reference block height | |
estimate_eta() { | |
local primary_speed=$1 | |
local reference_speed=$2 | |
local height_diff=$3 | |
# Using bc for floating point comparison | |
local catch_up=$(echo "$primary_speed > $reference_speed" | bc) | |
if [ $catch_up -eq 0 ]; then | |
echo "ETA: Primary node unlikely to catch up at current speeds" | |
else | |
local net_speed=$(echo "$primary_speed - $reference_speed" | bc) | |
local eta_seconds=$(echo "scale=0; $height_diff / $net_speed" | bc) | |
local eta_date=$(date -u -d "@$(( $(date +%s) + eta_seconds ))") | |
echo "ETA: $eta_date" | |
fi | |
} | |
# Main script | |
previous_primary_time=$(date +%s) | |
previous_primary_height=$(get_current_block_height $PRIMARY_RPC_URL) | |
previous_reference_time=$(date +%s) | |
previous_reference_height=$(get_current_block_height $REFERENCE_RPC_URL) | |
while true; do | |
current_time=$(date +%s) | |
new_primary_height=$(get_current_block_height $PRIMARY_RPC_URL) | |
new_reference_height=$(get_current_block_height $REFERENCE_RPC_URL) | |
# Handle cases where sync info is not available | |
if [ "$new_primary_height" == "null" ] || [ "$new_reference_height" == "null" ]; then | |
echo -ne "\nUnable to fetch sync info. Ensure RPC URLs are correct and servers are reachable." | |
sleep $INTERVAL | |
continue | |
fi | |
# Update measurement arrays for both nodes | |
update_measurements primary_block_heights primary_measurement_times $new_primary_height $current_time | |
update_measurements reference_block_heights reference_measurement_times $new_reference_height $current_time | |
# Calculate average sync speed for both nodes | |
avg_primary_sync_speed=$(calculate_average_sync_speed primary_block_heights primary_measurement_times) | |
avg_reference_sync_speed=$(calculate_average_sync_speed reference_block_heights reference_measurement_times) | |
# Calculate height difference | |
height_diff=$(( new_reference_height - new_primary_height )) | |
# Display sync info | |
echo -ne "\rPrimary Node Block Height: $new_primary_height | Avg Sync Speed: $avg_primary_sync_speed blocks/second\n" | |
echo -ne "Reference Node Block Height: $new_reference_height | Avg Sync Speed: $avg_reference_sync_speed blocks/second\n" | |
echo -ne "$(estimate_eta $avg_primary_sync_speed $avg_reference_sync_speed $height_diff)\n" | |
# Sleep for interval | |
sleep $INTERVAL | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if RPC URL is provided | |
if [ -z "$1" ]; then | |
echo "Please provide the RPC URL as an argument." | |
exit 1 | |
fi | |
RPC_URL="$1" | |
# Polling interval in seconds | |
INTERVAL=1 | |
# Maximum number of measurements to consider for averaging | |
MAX_MEASUREMENTS=10 | |
# Function to get current block height | |
get_current_block_height() { | |
local info=$(curl -s "$RPC_URL/status") | |
local has_result=$(echo "$info" | jq -r '.result != null') | |
local latest_block_height | |
if [ "$has_result" = "true" ]; then | |
latest_block_height=$(echo "$info" | jq -r '.result.sync_info.latest_block_height') | |
else | |
latest_block_height=$(echo "$info" | jq -r '.sync_info.latest_block_height') | |
fi | |
if [ "$latest_block_height" == "null" ]; then | |
echo "null" | |
else | |
echo "$latest_block_height" | |
fi | |
} | |
# Arrays to store block heights and times for averaging | |
block_heights=() | |
measurement_times=() | |
# Function to update arrays and maintain size | |
update_measurements() { | |
block_heights+=($1) | |
measurement_times+=($2) | |
# Remove oldest entry if the arrays exceed the max size | |
if [ ${#block_heights[@]} -gt $MAX_MEASUREMENTS ]; then | |
block_heights=("${block_heights[@]:1}") | |
measurement_times=("${measurement_times[@]:1}") | |
fi | |
} | |
# Function to calculate average sync speed | |
calculate_average_sync_speed() { | |
local length=${#block_heights[@]} | |
if [ $length -le 1 ]; then | |
echo 0 | |
return | |
fi | |
local height_diff=$(( block_heights[length-1] - block_heights[0] )) | |
local time_diff=$(( measurement_times[length-1] - measurement_times[0] )) | |
if [ $time_diff -eq 0 ]; then | |
echo 0 | |
else | |
echo "scale=2; $height_diff / $time_diff" | bc | |
fi | |
} | |
# Main script | |
previous_time=$(date +%s) | |
previous_height=$(get_current_block_height) | |
while true; do | |
current_time=$(date +%s) | |
new_height=$(get_current_block_height) | |
# Handle cases where sync info is not available | |
if [ "$new_height" == "null" ]; then | |
echo -ne "\nUnable to fetch sync info. Ensure RPC URL is correct and server is reachable." | |
sleep $INTERVAL | |
continue | |
fi | |
# Update measurement arrays | |
update_measurements $new_height $current_time | |
# Calculate average sync speed | |
avg_sync_speed=$(calculate_average_sync_speed) | |
# Display sync info | |
echo -ne "\rLatest Block Height: $new_height | Avg Sync Speed: $avg_sync_speed blocks/second" | |
# Sleep for interval | |
sleep $INTERVAL | |
# Update previous measurements | |
previous_height=$new_height | |
previous_time=$current_time | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment