From 962756ca2cfd67a951cfa1cd6f2f8f28fda17de9 Mon Sep 17 00:00:00 2001 From: mgarciate Date: Sat, 4 May 2024 23:15:02 +0200 Subject: [PATCH] Fix #144 Update snapshot script --- alephium/Dockerfile | 4 ++++ alephium/snapshot-loader.sh | 25 ++++++++++++++++++++----- docker-compose.yml | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/alephium/Dockerfile b/alephium/Dockerfile index 3331125..2b3ea36 100644 --- a/alephium/Dockerfile +++ b/alephium/Dockerfile @@ -9,6 +9,10 @@ RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go install github.com/touilleio/tee-ha # Remove the -adoptjdk suffix when docker updates to 20.10.21 or higher FROM alephium/alephium:${UPSTREAM_VERSION}-adoptjdk +USER root +RUN apt update && apt-get install -y bc && apt-get clean +USER nobody + COPY --from=builder /go/bin/tee-hash /usr/local/bin/tee-hash COPY user.conf /alephium-home/.alephium/user.conf diff --git a/alephium/snapshot-loader.sh b/alephium/snapshot-loader.sh index 1e40f85..ed09302 100755 --- a/alephium/snapshot-loader.sh +++ b/alephium/snapshot-loader.sh @@ -3,6 +3,8 @@ ALEPHIUM_HOME=${ALEPHIUM_HOME:-/alephium-home/.alephium} ALEPHIUM_NETWORK=${ALEPHIUM_NETWORK:-mainnet} ALEPHIUM_FORCE_RELOAD_SNAPSHOT=${ALEPHIUM_FORCE_RELOAD_SNAPSHOT:-0} +# Node type: full or pruned. Any other value might cause unexpected behaviour +NODE_TYPE=${NODE_TYPE:-pruned} # If tee-hash (https://github.com/touilleio/tee-hash) is available, validates the checksum of the downloaded file. # Do not validate the checksum otherwise @@ -15,7 +17,7 @@ then VALIDATE_CHECKSUM=1 fi -# Checking for ALEPHIUM_HOME folder is writable +# Check if ALEPHIUM_HOME folder is writable if [ ! -w "$ALEPHIUM_HOME" ] then echo "Error: Data folder $ALEPHIUM_HOME is not writable by $(whoami). Please change ownership and/or permissions to $ALEPHIUM_HOME or its mount so $(whoami) can write on it, then relaunch" @@ -41,10 +43,23 @@ fi # If the full node network data storage folder does not exist (i.e. first run of the full node), loading the snapshot if [ ! -d "$ALEPHIUM_HOME/$ALEPHIUM_NETWORK" ] then + + # Check if enough disk space available + availableSpace=$(df -B1 "$ALEPHIUM_HOME" | tail -n 1 | awk '{print $4}' | head -n 1) + neededSpace=$(curl -s -I -L "$(curl -sL https://archives.alephium.org/archives/$ALEPHIUM_NETWORK/${NODE_TYPE}-node-data/_latest.txt)" | grep -i 'Content-Length:' | awk '{print $2}' | tr -d '\r') + neededSpaceWithMargin=$(echo "${neededSpace} * 1.2 / 1" | bc) + neededSpaceInGB=$(echo "${neededSpaceWithMargin} / 1000 / 1000 / 1000 / 1" | bc) + availableSpaceInGB=$(echo "${availableSpace} / 1000 / 1000 / 1000 / 1" | bc) + if [ "$neededSpaceWithMargin" -gt "$availableSpace" ]; then + echo "Error: Not enough available storage space in ${ALEPHIUM_HOME}. Only ${availableSpaceInGB} GB (${availableSpace} bytes) available but at least ${neededSpaceInGB} GB (${neededSpaceWithMargin} bytes) are needed." + echo "Please add more storage to ${ALEPHIUM_HOME}." + exit 1 + fi + echo "Loading $ALEPHIUM_NETWORK snapshot from official https://archives.alephium.org" # Creating a temp folder (on the same volume) where snapshot will be loaded mkdir "$ALEPHIUM_HOME/${ALEPHIUM_NETWORK}-snapshot" - curl -L "$(curl -s https://archives.alephium.org/archives/$ALEPHIUM_NETWORK/full-node-data/_latest.txt)" | $TEE_HASH_CMD | tar xf - -C "$ALEPHIUM_HOME/${ALEPHIUM_NETWORK}-snapshot" + curl -L "$(curl -sL https://archives.alephium.org/archives/$ALEPHIUM_NETWORK/${NODE_TYPE}-node-data/_latest.txt)" | $TEE_HASH_CMD | tar xf - -C "$ALEPHIUM_HOME/${ALEPHIUM_NETWORK}-snapshot" res=$? if [ "$res" != "0" ]; # If curl or tar command failed, stopping the load of the snapshot. then @@ -54,11 +69,11 @@ then if [ "${VALIDATE_CHECKSUM}" = "1" ] then # Check sha256 of what has been downloaded - remote_sha256sum="$(curl -s https://archives.alephium.org/archives/$ALEPHIUM_NETWORK/full-node-data/_latest.txt.sha256sum)" + remote_sha256sum="$(curl -sL https://archives.alephium.org/archives/$ALEPHIUM_NETWORK/${NODE_TYPE}-node-data/_latest.txt.sha256sum)" local_sha256sum=$(cat "${CHECKSUM_FILE}") if [ "$remote_sha256sum" != "$local_sha256sum" ] then - echo "Error: Checksum is not good." + echo "Error: Checksum is not good. expected ${remote_sha256sum}, got ${local_sha256sum}" exit 1 fi fi @@ -69,4 +84,4 @@ then echo "Loading $ALEPHIUM_NETWORK snapshot completed successfully in $ALEPHIUM_HOME/${ALEPHIUM_NETWORK}." else echo "Folder $ALEPHIUM_HOME/$ALEPHIUM_NETWORK already exists, not loading the snapshot. If this is not expected, please manually remove the folder $ALEPHIUM_HOME/$ALEPHIUM_NETWORK or set ALEPHIUM_FORCE_RELOAD_SNAPSHOT=1 and restart" -fi +fi \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 0b3275b..d29f890 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,6 +17,7 @@ services: - alephium-wallets:/alephium-home/.alephium-wallets environment: - ALEPHIUM_FORCE_RELOAD_SNAPSHOT=0 + - NODE_TYPE=pruned - INITIALIZING_FROM_SNAPSHOT=disabled - EXTRA_OPTS= healthcheck: