checkpoint.sh raw

   1  #!/bin/sh
   2  # checkpoint.sh prints the chain-profile checkpoint for a fork height.
   3  #
   4  # The checkpoint is the last block the two chains share. Both chain profiles carry the same three
   5  # values, because below the fork there is only one chain: the light client starts verifying
   6  # above it instead of downloading Bitcoin's history a second time, and the wallet treats every
   7  # coin at or below it as Bitcoin on both chains.
   8  #
   9  # The filter header is the value getblockfilter returns as "header": the running commitment the
  10  # client continues from, not the hash of that one block's filter.
  11  #
  12  # usage: checkpoint.sh <fork-height>
  13  # env:   BITCOIN_CLI (default: bitcoin-cli), DATADIR, RPCPORT, RPCUSER, RPCPASSWORD
  14  #
  15  # Example, on the regtest rig:
  16  #
  17  #   DATADIR=$HOME/.blake-swap-test/btc scripts/checkpoint.sh 20000
  18  #   {"height":20000,"block_hash":"...","filter_header":"..."}
  19  #
  20  # Put that object under "checkpoint" in the profile JSON: the bridge and the channel both read
  21  # it from there, and -checkpoint-height/-checkpoint-hash/-checkpoint-filter-header override it.
  22  set -eu
  23  
  24  HEIGHT=${1:?usage: checkpoint.sh <fork-height>}
  25  CLI=${BITCOIN_CLI:-bitcoin-cli}
  26  
  27  if [ -z "${DATADIR:-}" ]; then
  28    echo "checkpoint: set DATADIR to the node's data directory" >&2
  29    exit 2
  30  fi
  31  
  32  ARGS="-datadir=$DATADIR"
  33  if [ -n "${RPCPORT:-}" ]; then ARGS="$ARGS -rpcport=$RPCPORT"; fi
  34  if [ -n "${RPCUSER:-}" ]; then ARGS="$ARGS -rpcuser=$RPCUSER"; fi
  35  if [ -n "${RPCPASSWORD:-}" ]; then ARGS="$ARGS -rpcpassword=$RPCPASSWORD"; fi
  36  
  37  HASH=$($CLI $ARGS getblockhash "$HEIGHT")
  38  FILTER=$($CLI $ARGS getblockfilter "$HASH")
  39  
  40  HEADER=$(printf '%s' "$FILTER" | sed -n 's/.*"header": *"\([0-9a-fA-F]*\)".*/\1/p')
  41  
  42  if [ -z "$HEADER" ]; then
  43    echo "checkpoint: the node returned no filter header for $HASH (is -blockfilterindex=1 set?)" >&2
  44    exit 1
  45  fi
  46  
  47  printf '{"height":%s,"block_hash":"%s","filter_header":"%s"}\n' "$HEIGHT" "$HASH" "$HEADER"
  48