Skip to content

Instantly share code, notes, and snippets.

@Klazomenai
Created August 15, 2024 09:48
Show Gist options
  • Save Klazomenai/2458f9289a44ab1555460f79f9ab689a to your computer and use it in GitHub Desktop.
Save Klazomenai/2458f9289a44ab1555460f79f9ab689a to your computer and use it in GitHub Desktop.
Estimation date of completion for syncing an Autonity node, based on velocity increase of the block number
#!/bin/bash
# Function to convert seconds to human-readable time
function convert_seconds_to_readable() {
local T=$1
local D=$((T/86400))
local H=$(( (T%86400)/3600 ))
local M=$(( (T%3600)/60 ))
local S=$(( T%60 ))
printf '%d days, %02d hours, %02d minutes, %02d seconds\n' $D $H $M $S
}
# Function to display help
function display_help() {
echo "Usage: $0 -w WAIT_TIME -c CALL_COUNT -l LATEST_BLOCK_HEIGHT [-s]"
echo
echo " -w WAIT_TIME Time to wait between each aut node info call (in seconds)"
echo " -c CALL_COUNT Number of calls to make"
echo " -l LATEST_BLOCK_HEIGHT Latest block height in the network"
echo " -s Silent mode (omit iteration outputs)"
echo " -h Display this help message"
exit 1
}
# Initialize the silent flag as false
silent_mode=false
# Parse command-line arguments
while getopts "w:c:l:sh" opt; do
case "$opt" in
w) wait_time=$OPTARG ;;
c) call_count=$OPTARG ;;
l) latest_block_height=$OPTARG ;;
s) silent_mode=true ;; # Set silent mode to true if -s flag is provided
h) display_help ;;
*) display_help ;; # If an unknown flag is provided, show help and exit
esac
done
# Check if all required arguments are provided
if [ -z "$wait_time" ] || [ -z "$call_count" ] || [ -z "$latest_block_height" ]; then
echo "Error: Missing required arguments."
display_help
fi
# Collect the block heights at each interval
declare -a block_heights
for ((i=0; i<=call_count; i++)); do
# Get block number from the output of aut node info
block_number=$(aut node info | grep '"eth_blockNumber"' | grep -oE '[0-9]+')
# Check if block_number is valid
if [[ -z "$block_number" ]]; then
echo "Error: Unable to retrieve eth_blockNumber."
exit 1
fi
block_heights[$i]=$block_number
# Output current block height only if silent mode is not enabled
if ! $silent_mode; then
echo "Calling aut node info... (iteration $i)"
echo "Current block height: $block_number"
fi
# Wait for the specified amount of time, unless this is the last iteration
if [[ $i -lt $call_count ]]; then
sleep $wait_time
fi
done
# Calculate the velocity (block number change per second)
block_start=${block_heights[0]}
block_end=${block_heights[$call_count]}
total_blocks_increased=$((block_end - block_start))
total_time_elapsed=$((wait_time * call_count))
if [ $total_blocks_increased -le 0 ]; then
echo "Error: Block height did not increase or a negative value was calculated. Check if sync is happening correctly."
exit 1
fi
velocity=$(echo "scale=4; $total_blocks_increased / $total_time_elapsed" | bc)
# Calculate remaining blocks to sync
remaining_blocks=$((latest_block_height - block_end))
# Check if node is fully synced
if [ $remaining_blocks -le 0 ]; then
echo "Node is already synced or very close to being synced."
exit 0
fi
# Calculate the estimated time to sync completion
time_remaining=$(echo "scale=0; $remaining_blocks / $velocity" | bc)
# Get the current date and add the estimated time to complete
current_date=$(date +%s)
estimated_completion_time=$(($current_date + $time_remaining))
# Convert estimated completion time to human-readable format
estimated_completion_date=$(date -d @$estimated_completion_time)
time_remaining_readable=$(convert_seconds_to_readable $time_remaining)
# Output results
echo "Average block velocity: $velocity blocks per second."
echo "Remaining blocks to sync: $remaining_blocks"
echo "Estimated time remaining for sync completion: $time_remaining_readable"
echo "Estimated completion date: $estimated_completion_date"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment