From 9f7d768fab292f2e56d14b1384c9c993c36b57fe Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 30 Nov 2018 00:07:49 +0900 Subject: [PATCH] Improve save/load utxo --- contrib/load-utxo-set.sh | 58 +++++++++++++++++++++------- contrib/save-utxo-set-in-bitcoind.sh | 28 ++++++++++++++ contrib/save-utxo-set.sh | 45 ++------------------- 3 files changed, 77 insertions(+), 54 deletions(-) create mode 100644 contrib/save-utxo-set-in-bitcoind.sh diff --git a/contrib/load-utxo-set.sh b/contrib/load-utxo-set.sh index cd15df5..8911311 100644 --- a/contrib/load-utxo-set.sh +++ b/contrib/load-utxo-set.sh @@ -2,26 +2,51 @@ # This script shows the steps to download and update an archive of the current UTXO Set -exit # This script is not meant to run automatically +if ! [ "$0" = "$BASH_SOURCE" ]; then + echo "This script must not be sourced" + exit 1 +fi +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root after running \"sudo su -\"" + exit 1 +fi -## ARGS# -NETWORK="testnet" -DOWNLOAD_LINK="http://utxosets.blob.core.windows.net/public/utxo-snapshot-bitcoin-testnet-1445586.tar" -TAR_FILE_HASH="eabaaa717bb8eeaf603e383dd8642d9d34df8e767fccbd208b0c936b79c82742" -####### +: "${UTXO_DOWNLOAD_LINK:=http://utxosets.blob.core.windows.net/public/utxo-snapshot-bitcoin-mainnet-551636.tar}" +NETWORK="" +[[ $DOWNLOAD_LINK == *-testnet-* ]] && NETWORK="testnet" +[[ $DOWNLOAD_LINK == *-mainnet-* ]] && NETWORK="mainnet" +[[ $DOWNLOAD_LINK == *-regtest-* ]] && NETWORK="regtest" BITCOIN_DATA_DIR="/var/lib/docker/volumes/generated_bitcoin_datadir/_data" [ ! -d "$BITCOIN_DATA_DIR" ] && mkdir -p "$BITCOIN_DATA_DIR" -TAR_FILE="$BITCOIN_DATA_DIR/snapshot.tar" +TAR_NAME="$(basename $DOWNLOAD_LINK)" +TAR_FILE="$BITCOIN_DATA_DIR/$TAR_NAME" + echo "Downloading $DOWNLOAD_LINK to $TAR_FILE" -wget "$DOWNLOAD_LINK" -q --show-progress -O "$TAR_FILE" +cd "$BITCOIN_DATA_DIR" +if [ ! -f "$TAR_FILE" ]; then + wget "$DOWNLOAD_LINK" -q --show-progress +else + echo "$TAR_FILE already exists" +fi -if ! echo "$TAR_FILE_HASH" "$TAR_FILE" | sha256sum -c -; then - echo "Invalid hash" +echo " +fab994299273080bf7124c8c45c4ada867974ca747900178496a69e450cf713f utxo-snapshot-bitcoin-mainnet-551636.tar +eabaaa717bb8eeaf603e383dd8642d9d34df8e767fccbd208b0c936b79c82742 utxo-snapshot-bitcoin-testnet-1445586.tar +" > "trusted-utxo-sets.asc" + +grep "$TAR_NAME" "trusted-utxo-sets.asc" | tee "sig.asc" +rm "trusted-utxo-sets.asc" +if ! sha256sum -c "sig.asc"; then + echo "$TAR_FILE is not trusted" + rm "sig.asc" + cd - exit 1 fi +rm "sig.asc" +cd - NETWORK_DIRECTORY=$NETWORK if [[ $NETWORK == "mainnet" ]]; then @@ -37,7 +62,14 @@ NETWORK_DIRECTORY="$BITCOIN_DATA_DIR/$NETWORK_DIRECTORY" [ ! -d "$NETWORK_DIRECTORY" ] && mkdir "$NETWORK_DIRECTORY" echo "Extracting..." -tar -xf "$TAR_FILE" -C "$BITCOIN_DATA_DIR" +if ! tar -xf "$TAR_FILE" -C "$BITCOIN_DATA_DIR"; then + echo "Failed extracting, did you turned bitcoin off? (btcpay-down.sh)" + exit 1 +fi +rm "$TAR_FILE" + +BTCPAY_DATA_DIR="/var/lib/docker/volumes/generated_btcpay_datadir/_data" +[ ! -d "$BTCPAY_DATA_DIR" ] && mkdir -p "$BTCPAY_DATA_DIR" +echo "$TAR_NAME" > "$BTCPAY_DATA_DIR/FastSynced" -echo "Extracted" -rm "$TAR_FILE" \ No newline at end of file +echo "Successfully downloaded and extracted, you can run btcpay again (btcpay-up.sh)" \ No newline at end of file diff --git a/contrib/save-utxo-set-in-bitcoind.sh b/contrib/save-utxo-set-in-bitcoind.sh new file mode 100644 index 0000000..7f4dde5 --- /dev/null +++ b/contrib/save-utxo-set-in-bitcoind.sh @@ -0,0 +1,28 @@ +BITCOIND="bitcoind -datadir=/data" +BITCOIN_CLI="bitcoin-cli -datadir=/data" + +$BITCOIND & +BITCOIND_PID=$! +CURRENT_HEIGHT="$($BITCOIN_CLI -rpcwait getblockcount)" +let "PRUNED_HEIGHT=$CURRENT_HEIGHT - 289" + +echo "Pruning to $PRUNED_HEIGHT" +$BITCOIN_CLI pruneblockchain "$PRUNED_HEIGHT" + +echo "Waiting bitcoind to stop..." +$BITCOIN_CLI stop +wait $BITCOIND_PID + +NETWORK_DIRECTORY=$NETWORK +if [[ $NETWORK == "mainnet" ]]; then + NETWORK_DIRECTORY="." +fi +if [[ $NETWORK == "testnet" ]]; then + NETWORK_DIRECTORY="testnet3" +fi + +cd /data +TAR_NAME="utxo-snapshot-bitcoin-$NETWORK-$PRUNED_HEIGHT.tar" +echo "Creating $TAR_NAME..." +tar -cf "$TAR_NAME" "$NETWORK_DIRECTORY/blocks/" +tar -rf "$TAR_NAME" "$NETWORK_DIRECTORY/chainstate/" diff --git a/contrib/save-utxo-set.sh b/contrib/save-utxo-set.sh index 42baac9..aeee683 100644 --- a/contrib/save-utxo-set.sh +++ b/contrib/save-utxo-set.sh @@ -2,11 +2,11 @@ # This script shows the steps to create an archive of the current UTXO Set -exit # This script is not meant to run automatically +exit 1 # This script is not meant to run automatically ## ARGS# -NETWORK="testnet" +NETWORK="mainnet" export AZURE_STORAGE_CONTAINER="public" export AZURE_STORAGE_CONNECTION_STRING="" ####### @@ -17,46 +17,9 @@ export AZURE_STORAGE_CONNECTION_STRING="" btcpay-down.sh # Run only bitcoind and connect to it +SCRIPT="$(cat save-utxo-set-in-bitcoind.sh)" cd "`dirname $BTCPAY_ENV_FILE`" -docker-compose -f $BTCPAY_DOCKER_COMPOSE run -e "NETWORK=$NETWORK" bitcoind bash - -# IN THE CONTAINER ############################################################# -ENVIRONMENT="" -BITCOIND="bitcoind -datadir=/data" -BITCOIN_CLI="bitcoin-cli -datadir=/data" - -$BITCOIND & -BITCOIND_PID=$! -CURRENT_HEIGHT="$($BITCOIN_CLI -rpcwait getblockcount)" -let "PRUNED_HEIGHT=$CURRENT_HEIGHT - 289" - -echo "Pruning to $PRUNED_HEIGHT" -$BITCOIN_CLI pruneblockchain "$PRUNED_HEIGHT" - -echo "Waiting bitcoind to stop..." -$BITCOIN_CLI stop -wait $BITCOIND_PID - -NETWORK_DIRECTORY=$NETWORK -if [[ $NETWORK == "mainnet" ]]; then - NETWORK_DIRECTORY="." -fi -if [[ $NETWORK == "testnet" ]]; then - NETWORK_DIRECTORY="testnet3" -fi - -cd /data -TAR_NAME="utxo-snapshot-bitcoin-$NETWORK-$PRUNED_HEIGHT.tar" -echo "Creating $TAR_NAME..." -tar -cf "$TAR_NAME" "$NETWORK_DIRECTORY/blocks/" -tar -rf "$TAR_NAME" "$NETWORK_DIRECTORY/chainstate/" - -# Exit from the container -exit - -# IN THE HOST ############################################################# - -# Restart btcpay +docker-compose -f $BTCPAY_DOCKER_COMPOSE run -e "NETWORK=$NETWORK" bitcoind bash -c "$SCRIPT" btcpay-up.sh TAR_FILE="$(echo /var/lib/docker/volumes/generated_bitcoin_datadir/_data/utxo-snapshot-*)"