Skip to content

Instantly share code, notes, and snippets.

@mutatrum
Last active Jun 12, 2021
Embed
What would you like to do?
Print a taproot signalling block diagram
#!/bin/bash
BLOCKCHAININFO=$(bitcoin-cli getblockchaininfo)
BLOCKS=$(echo "$BLOCKCHAININFO" | jq .blocks)
TAPROOT=$(echo "$BLOCKCHAININFO" | jq .softforks.taproot.bip9)
SINCE=$(echo "$TAPROOT" | jq .since)
PERIOD=$(echo "$TAPROOT" | jq .statistics.period)
BLOCKS=$(echo "$BLOCKCHAININFO" | jq .blocks)
PERIOD_COUNT=$(((BLOCKS - SINCE) / PERIOD))
SINCE=$((SINCE + (PERIOD * PERIOD_COUNT)))
ELAPSED=$(echo "$TAPROOT" | jq .statistics.elapsed)
for BLOCK in $(seq "$SINCE" $((SINCE + ELAPSED - 1))); do
HASH=$(bitcoin-cli getblockhash "$BLOCK")
HEADER=$(bitcoin-cli getblockheader "$HASH")
VERSION=$(echo "$HEADER" | jq .version)
SIGNAL=$(((VERSION & 3758096388) == 536870916))
case $SIGNAL in
(1) echo -n "🟩";;
(0) echo -n "🟥";;
esac
if ((($BLOCK + 1) % 48 == 0)); then echo; fi
done
echo
PERCENTAGE=`echo $BLOCKCHAININFO | jq '.softforks.taproot.bip9.statistics | .count / .elapsed * 100'`
echo $PERCENTAGE
@pysiak

This comment has been minimized.

Copy link

@pysiak pysiak commented May 3, 2021

Nice going! I ran the script through https://www.shellcheck.net/ - it's a linter that finds potentially problematic code.
It didn't find anything aweful, just:

Here's the linted version:

#!/bin/bash
BLOCKCHAININFO=$(bitcoin-cli getblockchaininfo)
TAPROOT=$(echo "$BLOCKCHAININFO" | jq .softforks.taproot.bip9)
SINCE=$(echo "$TAPROOT" | jq .since)
ELAPSED=$(echo "$TAPROOT" | jq .statistics.elapsed)
for BLOCK in $(seq "$SINCE" $((SINCE + ELAPSED - 1))); do
  HASH=$(bitcoin-cli getblockhash "$BLOCK")
  HEADER=$(bitcoin-cli getblockheader "$HASH")
  VERSION=$(echo "$HEADER" | jq .version)
  SIGNAL=$(((VERSION & 3758096388) == 536870916))
  case $SIGNAL in
    (1) echo -n "🟩";;
    (0) echo -n "🟥";;
  esac
done
@Sjors

This comment has been minimized.

Copy link

@Sjors Sjors commented May 21, 2021

$SINCE refers to the first period, so you need something slightly more complicated to deal with subsequent periods.

@Sjors

This comment has been minimized.

Copy link

@Sjors Sjors commented May 21, 2021

bitcoin/bitcoin#22016 to the rescue:

#!/bin/bash
BLOCKCHAININFO=$(bitcoin-cli getblockchaininfo)
TAPROOT=$(echo "$BLOCKCHAININFO" | jq .softforks.taproot.bip9)
PERIOD_START=$(echo "$TAPROOT" | jq .statistics.period_start)
ELAPSED=$(echo "$TAPROOT" | jq .statistics.elapsed)
for BLOCK in $(seq "$PERIOD_START" $((PERIOD_START + ELAPSED - 1))); do
  HASH=$(bitcoin-cli getblockhash "$BLOCK")
  HEADER=$(bitcoin-cli getblockheader "$HASH")
  VERSION=$(echo "$HEADER" | jq .version)
  SIGNAL=$(((VERSION & 3758096388) == 536870916))
  case $SIGNAL in
    (1) echo -n "🟩";;
    (0) echo -n "🟥";;
  esac
done
@mutatrum

This comment has been minimized.

Copy link
Owner Author

@mutatrum mutatrum commented May 21, 2021

The version at the top was updated with the slightly more complicated stuff. But having that field in the API is really nice, it surprised my a bit after the first difficulty period.

@Sjors

This comment has been minimized.

Copy link

@Sjors Sjors commented May 21, 2021

Also for testnet you need something like seq -f "%.10g" to prevent it from messing up the block numbers.

@Sjors

This comment has been minimized.

Copy link

@Sjors Sjors commented May 21, 2021

This version uses the new (proposed) period_start fields and adds testnet support (./taproot.sh -testnet)

#!/bin/bash
NETWORK=$1
BLOCKCHAININFO=$(bitcoin-cli $NETWORK getblockchaininfo)
BLOCKS=$(echo "$BLOCKCHAININFO" | jq .blocks)
TAPROOT=$(echo "$BLOCKCHAININFO" | jq .softforks.taproot.bip9)
PERIOD=$(echo "$TAPROOT" | jq .statistics.period)
PERIOD_START=$(echo "$TAPROOT" | jq .statistics.period_start)
BLOCKS=$(echo "$BLOCKCHAININFO" | jq .blocks)
ELAPSED=$(echo "$TAPROOT" | jq .statistics.elapsed)
for BLOCK in $(seq -f "%.10g" "$PERIOD_START" $((PERIOD_START + ELAPSED - 1))); do
  HASH=$(bitcoin-cli $NETWORK getblockhash "$BLOCK")
  HEADER=$(bitcoin-cli $NETWORK getblockheader "$HASH")
  VERSION=$(echo "$HEADER" | jq .version)
  SIGNAL=$(((VERSION & 3758096388) == 536870916))
  case $SIGNAL in
    (1) echo -n "🟩";;
    (0) echo -n "🟥";;
  esac
  if ((($BLOCK + 1) % 48 == 0)); then echo; fi
done
echo

PERCENTAGE=`echo $BLOCKCHAININFO | jq '.softforks.taproot.bip9.statistics | .count / .elapsed * 100'`
echo $PERCENTAGE

Testnet isn't going too well this period:
Schermafbeelding 2021-05-21 om 18 15 54

@mutatrum

This comment has been minimized.

Copy link
Owner Author

@mutatrum mutatrum commented Jun 12, 2021

Infinite version

#!/bin/bash
BLOCKCHAININFO=$(bitcoin-cli getblockchaininfo)
BLOCKS=$(echo "$BLOCKCHAININFO" | jq .blocks)
TAPROOT=$(echo "$BLOCKCHAININFO" | jq .softforks.taproot.bip9)
SINCE=$(echo "$TAPROOT" | jq .since)
PERIOD=$(echo "$TAPROOT" | jq .statistics.period)
BLOCKS=$(echo "$BLOCKCHAININFO" | jq .blocks)
PERIOD_COUNT=$(((BLOCKS - SINCE) / PERIOD))
START=$((SINCE + (PERIOD * PERIOD_COUNT)))
ELAPSED=$(echo "$TAPROOT" | jq .statistics.elapsed)
COUNT=0
for BLOCK in $(seq "$SINCE" $((START + ELAPSED - 1))); do
  HASH=$(bitcoin-cli getblockhash "$BLOCK")
  HEADER=$(bitcoin-cli getblockheader "$HASH")
  VERSION=$(echo "$HEADER" | jq .version)
  SIGNAL=$(((VERSION & 3758096388) == 536870916))
  case $SIGNAL in
    (1) echo -n "🟧"
        ((COUNT++))
    ;;
    (0) echo -n "🟦"
    ;;
  esac
  if (( ($BLOCK + 1) % 56 == 0 )); then echo "   "; COUNT=0; fi
done
echo " $COUNT"

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