Skip to content

Commit c1ccbc3

Browse files
committed
devtools: add utxo_snapshot.sh
to allow easy (if not time-consuming) generation and verification of snapshots.
1 parent 57cf74c commit c1ccbc3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

contrib/devtools/utxo_snapshot.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
 (0)