Skip to content

Instantly share code, notes, and snippets.

@tyzbit
Last active January 24, 2018 00:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyzbit/45757fd4a7258b51a9aa00a5e34db604 to your computer and use it in GitHub Desktop.
Save tyzbit/45757fd4a7258b51a9aa00a5e34db604 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Prints out information from bitcoin-cli about the previous Bitcoin difficulty
# adjustment and the projected difficulty adjustment.
#
# Requires: bitcoin-cli
# jq
# bc
#
tempfile=$PWD/.tempfile
currentblockheight=$(bitcoin-cli getblockcount)
bestblockhash=$(bitcoin-cli getbestblockhash)
currentblockjson=$(bitcoin-cli getblock $bestblockhash)
currentdiff=$(echo $currentblockjson | jq '.difficulty')
currenttime=$(echo $currentblockjson | jq '.time')
# set up for the loop below
previousblockheight=$currentblockheight
previousdiff=$currentdiff
blockdifference=0
timedifference=0
combinedtime=0
# iterate backwards through the blocks until we find a difficulty change
# while doing so, collect the following
# difference in block heights ($blockdifference)
# previous block time ($previoustime)
# combined block times ($combinedtime)
# combined block times for the last 50 blocks ($lastfifty)
while [[ "$previousdiff" == "$currentdiff" ]]; do
let previousblockheight=$previousblockheight-1
let blockdifference=$blockdifference+1
previousblockhash=$(bitcoin-cli getblockhash $previousblockheight)
previousblockjson=$(bitcoin-cli getblock $previousblockhash)
previousdiff=$(echo "$previousblockjson" | jq '.difficulty')
previoustime=$(echo "$previousblockjson" | jq '.time')
timedifference=$currenttime-$previoustime
let combinedtime=$combinedtime+$timedifference
let currenttime=$previoustime
# stop counting combined block times at 50 blocks for more recent calculations
if [[ $blockdifference -eq 50 ]]; then
lastfifty=$combinedtime
fi
done
# get the average block time in seconds
averagetime=$(( $combinedtime / $blockdifference ))
# convert to minutes
averagetimemin=$(echo "$averagetime / 60" | bc -l)
# get the average block time for the last 50 blocks (in seconds)
averagefifty=$(( $lastfifty / 50 ))
averagetimeminfifty=$(echo "$averagefifty / 60" | bc -l)
# block adjustments are every 2016 blocks
nextdifficultyadjustment=$(( 2016 - $blockdifference ))
# make a pretty timestamp for the previous difficulty change
previousblocktimestamp=$(date --date="@$previoustime" "+%F▕▏%T")
# get the estimated number of seconds until the next adjustment by taking
# the average block time plus the number of blocks left until the adjustment
estimatenextblock=$(($averagetime * $nextdifficultyadjustment))
estimatenextblockfifty=$(( $averagefifty * $nextdifficultyadjustment ))
# get the UNIX epoc time of the projected next adjustment
nextblocktime=$(( $currenttime + $estimatenextblock ))
nextblocktimefifty=$(( $currenttime + $estimatenextblockfifty ))
# get a pretty timestamp for the projected next adjustment
nextblocktimestamp=$(date --date="@$nextblocktime" "+%F▕▏%T")
nextblocktimestampfifty=$(date --date="@$nextblocktimefifty" "+%F▕▏%T")
# convert the project seconds till next adjustment into days, hours, minutes etc
fiftyblockest=$(printf '%dd:%dh:%dm:%ds\n' \
$(($estimatenextblockfifty/86400)) \
$(($estimatenextblockfifty%24)) \
$(($estimatenextblockfifty%3600/60)) \
$(($estimatenextblockfifty%60)))
nextblockin=$(printf '%dd:%dh:%dm:%ds\n' \
$(($estimatenextblock/86400)) \
$(($estimatenextblock%24)) \
$(($estimatenextblock%3600/60)) \
$(($estimatenextblock%60)))
# print it all out to a temporary file
echo "prev: -$blockdifference $previousblocktimestamp" >> $tempfile
echo "next: +$nextdifficultyadjustment $nextblocktimestamp" >> $tempfile
echo "next(50): +$nextdifficultyadjustment $nextblocktimestampfifty" >> $tempfile
echo "blocktimes: ${averagetimemin:0:4}m" >> $tempfile
echo "blocktimes(50): ${averagetimeminfifty:0:4}m" >> $tempfile
echo "nextadjust: $nextblockin" >> $tempfile
echo "nextadjust(50): $fiftyblockest" >> $tempfile
# output from temp file, format into columns, delete temp file
cat $tempfile | column -t
rm $tempfile
@tyzbit
Copy link
Author

tyzbit commented Jan 23, 2018

example output:

$ ./next-difficulty.sh
prev:            -1737           2018-01-13▕▏02:11:12
next:            +279            2018-01-14▕▏18:05:57
next(50):        +279            2018-01-14▕▏15:18:33
blocktimes:      8.58m
blocktimes(50):  7.98m
nextadjust:      1d:21h:54m:45s
nextadjust(50):  1d:9h:7m:21s

(50) is calculations just using the last 50 blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment