Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnzweng/f730df872c34e6e7e3061b4bd04e29c8 to your computer and use it in GitHub Desktop.
Save johnzweng/f730df872c34e6e7e3061b4bd04e29c8 to your computer and use it in GitHub Desktop.
Export difficulty adjustments in testnet from BTC testnet node
#!/bin/bash
# Description:
# Analyze testnet difficulty changes.
# This script iterates over the blocks and exports blockheight, time, mediantime,
# difficulty, bits and blockhash for all blocks around difficulty adjustments
# (5 blocks before and 5 blocks afterwards).
#
# Author: John Zweng,30.04.2024
###############################################################################
## config:
###############################################################################
start_block=2570390 # some block from January 2024, before the block storm
end_block=2808470 # chaintip at time of creating this script
##
csv_file="testnet_difficulty_adjustments_2024.csv"
###############################################################################
###############################################################################
# write csv header
echo "height,time,mediantime,difficulty,bits,blockhash,blknum_in_diff_period" >> $csv_file
counter=0
# Loop through the specified range of block numbers
for (( block=$start_block; block<=$end_block; block++ ))
do
# index of current block within difficulty period (starts with 0, up to 2015)
block_in_diff_period=$((block % 2016))
# we are only interested in the blocks around difficulty adjustment
if (( block_in_diff_period > 2010 || block_in_diff_period < 5 )); then
# just a gimmick, repeat header in terminal every 20 lines (not in CSV)
if (( counter % 20 == 0 )); then
echo "height,time,mediantime,difficulty,bits,blockhash,blknum_in_diff_period"
fi
# Fetch the block hash for the current block number
blockhash=$(bitcoin-cli getblockhash $block)
header_csv=$(bitcoin-cli getblockheader $blockhash | jq -r '[.height, .time, .mediantime, .difficulty, .bits, .hash] | @csv')
echo $header_csv,$block_in_diff_period
# write ino csv file
echo $header_csv,$block_in_diff_period >> $csv_file
# Increment the counter
((counter++))
fi
done
echo "Finished. :)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment