Last active
January 24, 2018 00:03
-
-
Save tyzbit/45757fd4a7258b51a9aa00a5e34db604 to your computer and use it in GitHub Desktop.
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 | |
# | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example output:
(50) is calculations just using the last 50 blocks.