|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright (c) 2019 The Bitcoin Core developers |
| 4 | +# Distributed under the MIT software license, see the accompanying |
| 5 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 6 | +# |
| 7 | +export LC_ALL=C |
| 8 | + |
| 9 | +set -ueo pipefail |
| 10 | + |
| 11 | +if (( $# < 3 )); then |
| 12 | + echo 'Usage: utxo_snapshot.sh <generate-at-height> <snapshot-out-path> <bitcoin-cli-call ...>' |
| 13 | + echo |
| 14 | + echo " if <snapshot-out-path> is '-', don't produce a snapshot file but instead print the " |
| 15 | + echo " expected assumeutxo hash" |
| 16 | + echo |
| 17 | + echo 'Examples:' |
| 18 | + echo |
| 19 | + echo " ./contrib/devtools/utxo_snapshot.sh 570000 utxo.dat ./src/bitcoin-cli -datadir=\$(pwd)/testdata" |
| 20 | + echo ' ./contrib/devtools/utxo_snapshot.sh 570000 - ./src/bitcoin-cli' |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +GENERATE_AT_HEIGHT="${1}"; shift; |
| 25 | +OUTPUT_PATH="${1}"; shift; |
| 26 | +# Most of the calls we make take a while to run, so pad with a lengthy timeout. |
| 27 | +BITCOIN_CLI_CALL="${*} -rpcclienttimeout=9999999" |
| 28 | + |
| 29 | +# Block we'll invalidate/reconsider to rewind/fast-forward the chain. |
| 30 | +PIVOT_BLOCKHASH=$($BITCOIN_CLI_CALL getblockhash $(( GENERATE_AT_HEIGHT + 1 )) ) |
| 31 | + |
| 32 | +(>&2 echo "Rewinding chain back to height ${GENERATE_AT_HEIGHT} (by invalidating ${PIVOT_BLOCKHASH}); this may take a while") |
| 33 | +${BITCOIN_CLI_CALL} invalidateblock "${PIVOT_BLOCKHASH}" |
| 34 | + |
| 35 | +if [[ "${OUTPUT_PATH}" = "-" ]]; then |
| 36 | + (>&2 echo "Generating txoutset info...") |
| 37 | + ${BITCOIN_CLI_CALL} gettxoutsetinfo | grep hash_serialized_2 | sed 's/^.*: "\(.\+\)\+",/\1/g' |
| 38 | +else |
| 39 | + (>&2 echo "Generating UTXO snapshot...") |
| 40 | + ${BITCOIN_CLI_CALL} dumptxoutset "${OUTPUT_PATH}" |
| 41 | +fi |
| 42 | + |
| 43 | +(>&2 echo "Restoring chain to original height; this may take a while") |
| 44 | +${BITCOIN_CLI_CALL} reconsiderblock "${PIVOT_BLOCKHASH}" |
0 commit comments