Skip to content

Instantly share code, notes, and snippets.

@st3b1t
Created May 16, 2024 22:16
Show Gist options
  • Save st3b1t/5226bf92488e1c4d15078284f64c5bd2 to your computer and use it in GitHub Desktop.
Save st3b1t/5226bf92488e1c4d15078284f64c5bd2 to your computer and use it in GitHub Desktop.
Calculate the avg time of last NBLOCKS bitcoin block in your full node
#!/bin/bash
# copyright 2024 st3b1t
# Calculate the avg time of last NBLOCKS
NBLOCKS=10
#pick last NBLOCKS blocks
PREV_TIMESTAMP=0
DIFF_SUM=0
COUNT=0
CURRENT_HEIGHT=$(bitcoin-cli getblockcount)
# Itera sugli ultimi NUM_BLOCKS blocchi per calcolare la differenza di tempo
for (( i = CURRENT_HEIGHT - NBLOCKS + 1; i <= CURRENT_HEIGHT; i++ ))
do
# Ottieni l'hash del blocco all'altezza i
BLOCKHASH=$(bitcoin-cli getblockhash $i)
# Ottieni il timestamp del blocco tramite l'hash
TIMESTAMP=$(bitcoin-cli getblock $BLOCKHASH | jq -r '.time')
# Calcola la differenza di tempo solo se non è il primo ciclo
if [ $PREV_TIMESTAMP -ne 0 ]; then
DIFF=$((TIMESTAMP - PREV_TIMESTAMP))
DIFF_SUM=$((DIFF_SUM + DIFF))
((COUNT++))
fi
PREV_TIMESTAMP=$TIMESTAMP
done
if [ $COUNT -gt 0 ]; then
AVG_DIFF=$((DIFF_SUM / COUNT)) #seconds
AVG_MIN=$(((AVG_DIFF + 30) / 60)) #minutes
echo "AVG Block Time: $AVG_MIN minutes"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment