#!/bin/bash
# Persistence core-1 PRUNED snapshot restore helper.
# Snapshot height 28401070, published 2026-07-22T14:06:04Z.
# wasm is already included in the main tarball; core-1_wasm.tar.lz4 is a standalone copy.
set -euo pipefail
if ! command -v lz4 >/dev/null; then echo "Error: lz4 required, install lz4!"; exit 1; fi
URL="${SNAPSHOT_URL:-https://persistence-snapshots-public.s3.gra.io.cloud.ovh.net/persistence/core-1_28401070.tar.lz4}"
HOME_DIR="${PERSISTENCE_HOME:-$HOME/.persistenceCore}"
cd "$HOME_DIR"
# preserve a non-zero validator signing state across the restore (double-sign guard)
if [ -f data/priv_validator_state.json ]; then
  VS=$(jq -r '.height + "/" + (.round|tostring) + "/" + (.step|tostring)' data/priv_validator_state.json)
  if [ "$VS" != "0/0/0" ]; then cp data/priv_validator_state.json ./priv_validator_state.json.keep; fi
fi
rm -rf data wasm
wget -qO- "$URL" | lz4 -d | tar -xf -
if [ -f ./priv_validator_state.json.keep ]; then
  mv ./priv_validator_state.json.keep data/priv_validator_state.json
  echo "Retained existing validator state: $VS"
fi
echo "Restore complete (height 28401070). Verify sha256 against ${URL}.sha256 before starting the node."
