Skip to content

Commit 7575b63

Browse files
committed
chore: make it so non-Debian systems can run the block-replay script
1 parent a86c232 commit 7575b63

File tree

1 file changed

+75
-20
lines changed

1 file changed

+75
-20
lines changed

contrib/tools/block-replay.sh

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ set -o pipefail
1616
## for 20 slices, this is about 1.8TB
1717

1818
NETWORK="mainnet" ## network to replay
19-
REPO_DIR="$HOME/stacks-inspect" ## where to build the source
19+
REPO_DIR="$HOME/stacks-core" ## where to build the source
2020
REMOTE_REPO="stacks-network/stacks-core" ## remote git repo to build stacks-inspect from
2121
SCRATCH_DIR="$HOME/scratch" ## root folder for the replay slices
2222
TIMESTAMP=$(date +%Y-%m-%d-%s) ## use a simple date format year-month-day-epoch
23-
LOG_DIR="/tmp/replay_${TIMESTAMP}" ## location of logfiles for the replay
23+
LOG_DIR="$HOME/replay_${TIMESTAMP}" ## location of logfiles for the replay
2424
SLICE_DIR="${SCRATCH_DIR}/slice" ## location of slice dirs
2525
TMUX_SESSION="replay" ## tmux session name to run the replay
2626
TERM_OUT=false ## terminal friendly output
2727
TESTING=false ## only run a replay on a few thousand blocks
2828
BRANCH="develop" ## default branch to build stacks-inspect from
2929
CORES=$(grep -c processor /proc/cpuinfo) ## retrieve total number of CORES on the system
30-
RESERVED=10 ## reserve this many CORES for other processes as default
30+
RESERVED=8 ## reserve this many CORES for other processes as default
31+
LOCAL_CHAINSTATE= ## path to local chainstate to use instead of snapshot download
3132

3233
## ansi color codes for terminal output
3334
COLRED=$'\033[31m' ## Red
@@ -94,19 +95,25 @@ configure_replay_slices() {
9495
echo "${COLRED}Error${COLRESET} creating dir ${SLICE_DIR}"
9596
exit 1
9697
}
97-
echo "Downloading latest ${NETWORK} chainstate archive ${COLYELLOW}https://archive.hiro.so/${NETWORK}/stacks-blockchain/${NETWORK}-stacks-blockchain-latest.tar.gz${COLRESET}"
98-
## curl had some random issues retrying the download when network issues arose. wget has resumed more consistently, so we'll use that binary
99-
# curl -L --proto '=https' --tlsv1.2 https://archive.hiro.so/${NETWORK}/stacks-blockchain/${NETWORK}-stacks-blockchain-latest.tar.gz -o ${SCRATCH_DIR}/${NETWORK}-stacks-blockchain-latest.tar.gz || {
100-
wget -O "${SCRATCH_DIR}/${NETWORK}-stacks-blockchain-latest.tar.gz" "https://archive.hiro.so/${NETWORK}/stacks-blockchain/${NETWORK}-stacks-blockchain-latest.tar.gz" || {
101-
echo "${COLRED}Error${COLRESET} downlaoding latest ${NETWORK} chainstate archive"
102-
exit 1
103-
}
104-
## extract downloaded archive
105-
echo "Extracting downloaded archive: ${COLYELLOW}${SCRATCH_DIR}/${NETWORK}-stacks-blockchain-latest.tar.gz${COLRESET}"
106-
tar --strip-components=1 -xzf "${SCRATCH_DIR}/${NETWORK}-stacks-blockchain-latest.tar.gz" -C "${SLICE_DIR}0" || {
107-
echo "${COLRED}Error${COLRESET} extracting ${NETWORK} chainstate archive"
108-
exit
109-
}
98+
99+
if [[ -n "${LOCAL_CHAINSTATE}" ]]; then
100+
echo "Copying local chainstate '${LOCAL_CHAINSTATE}'"
101+
cp -r "${LOCAL_CHAINSTATE}"/* "${SLICE_DIR}0"
102+
else
103+
echo "Downloading latest ${NETWORK} chainstate archive ${COLYELLOW}https://archive.hiro.so/${NETWORK}/stacks-blockchain/${NETWORK}-stacks-blockchain-latest.tar.gz${COLRESET}"
104+
## curl had some random issues retrying the download when network issues arose. wget has resumed more consistently, so we'll use that binary
105+
# curl -L --proto '=https' --tlsv1.2 https://archive.hiro.so/${NETWORK}/stacks-blockchain/${NETWORK}-stacks-blockchain-latest.tar.gz -o ${SCRATCH_DIR}/${NETWORK}-stacks-blockchain-latest.tar.gz || {
106+
wget -O "${SCRATCH_DIR}/${NETWORK}-stacks-blockchain-latest.tar.gz" "https://archive.hiro.so/${NETWORK}/stacks-blockchain/${NETWORK}-stacks-blockchain-latest.tar.gz" || {
107+
echo "${COLRED}Error${COLRESET} downlaoding latest ${NETWORK} chainstate archive"
108+
exit 1
109+
}
110+
## extract downloaded archive
111+
echo "Extracting downloaded archive: ${COLYELLOW}${SCRATCH_DIR}/${NETWORK}-stacks-blockchain-latest.tar.gz${COLRESET}"
112+
tar --strip-components=1 -xzf "${SCRATCH_DIR}/${NETWORK}-stacks-blockchain-latest.tar.gz" -C "${SLICE_DIR}0" || {
113+
echo "${COLRED}Error${COLRESET} extracting ${NETWORK} chainstate archive"
114+
exit
115+
}
116+
fi
110117
echo "Moving marf database: ${SLICE_DIR}0/chainstate/vm/clarity/marf.sqlite.blobs -> ${COLYELLOW}${SCRATCH_DIR}/marf.sqlite.blobs${COLRESET}"
111118
mv "${SLICE_DIR}"0/chainstate/vm/clarity/marf.sqlite.blobs "${SCRATCH_DIR}"/
112119
echo "Symlinking marf database: ${SCRATCH_DIR}/marf.sqlite.blobs -> ${COLYELLOW}${SLICE_DIR}0/chainstate/vm/clarity/marf.sqlite.blobs${COLRESET}"
@@ -377,6 +384,8 @@ usage() {
377384
echo " ${COLYELLOW}-t|--terminal${COLRESET}: more terminal friendly output"
378385
echo " ${COLYELLOW}-n|--network${COLRESET}: run block replay against specific network (default: mainnet)"
379386
echo " ${COLYELLOW}-b|--branch${COLRESET}: branch of stacks-core to build stacks-inspect from (default: develop)"
387+
echo " ${COLYELLOW}-c|--chainstate${COLRESET}: local chainstate copy to use instead of downloading a chainstaet snapshot"
388+
echo " ${COLYELLOW}-l|--logdir${COLRESET}: use existing log directory"
380389
echo " ${COLYELLOW}-r|--reserved${COLRESET}: how many cpu cores to reserve for system tasks"
381390
echo
382391
echo " ex: ${COLCYAN}${0} -t -u ${COLRESET}"
@@ -386,9 +395,30 @@ usage() {
386395

387396

388397
## install missing dependencies
389-
for cmd in curl tmux git wget tar gzip grep cargo pgrep; do
398+
HAS_APT=1
399+
HAS_SUDO=1
400+
for cmd in apt-get sudo curl tmux git wget tar gzip grep cargo pgrep tput find; do
401+
# in Alpine, `find` might be linked to `busybox` and won't work
402+
if [ "${cmd}" == "find" ] && [ -L "${cmd}" ]; then
403+
local rp="$(readlink "$(command -v "${cmd}" || echo "NOTLINK")")"
404+
if [ "${rp}" == "/bin/busybox" ]; then
405+
echo "${COLRED}ERROR${COLRESET} Busybox 'find' is not supported. Please install 'findutils' or similar."
406+
exit 1
407+
fi
408+
fi
409+
390410
command -v "${cmd}" >/dev/null 2>&1 || {
391411
case "${cmd}" in
412+
"apt-get")
413+
echo "${COLYELLOW}WARN${COLRESET} 'apt-get' not found; automatic package installation will fail"
414+
HAS_APT=0
415+
continue
416+
;;
417+
"sudo")
418+
echo "${COLYELLOW}WARN${COLRESET} 'sudo' not found; automatic package installation will fail"
419+
HAS_SUDO=0
420+
continue
421+
;;
392422
"cargo")
393423
install_cargo
394424
;;
@@ -399,6 +429,11 @@ for cmd in curl tmux git wget tar gzip grep cargo pgrep; do
399429
package="${cmd}"
400430
;;
401431
esac
432+
433+
if [[ ${HAS_APT} = 0 ]] || [[ ${HAS_SUDO} = 0 ]]; then
434+
echo "${COLRED}Error${COLRESET} Missing command '${cmd}'"
435+
exit 1
436+
fi
402437
(sudo apt-get update && sudo apt-get install "${package}") || {
403438
echo "${COLRED}Error${COLRESET} installing $package"
404439
exit 1
@@ -422,6 +457,7 @@ while [ ${#} -gt 0 ]; do
422457
# required if not mainnet
423458
if [ "${2}" == "" ]; then
424459
echo "Missing required value for ${1}"
460+
exit 1
425461
fi
426462
NETWORK=${2}
427463
shift
@@ -430,10 +466,29 @@ while [ ${#} -gt 0 ]; do
430466
# build from specific branch
431467
if [ "${2}" == "" ]; then
432468
echo "Missing required value for ${1}"
469+
exit 1
433470
fi
434471
BRANCH=${2}
435472
shift
436-
;;
473+
;;
474+
-c|--chainstate)
475+
# use a local chainstate
476+
if [ "${2}" == "" ]; then
477+
echo "Missing required value for ${1}"
478+
exit 1
479+
fi
480+
LOCAL_CHAINSTATE="${2}"
481+
shift
482+
;;
483+
-l|--logdir)
484+
# use a given logdir
485+
if [ "${2}" == "" ]; then
486+
echo "Missing required value for ${1}"
487+
exit 1
488+
fi
489+
LOG_DIR="${2}"
490+
shift
491+
;;
437492
-r|--RESERVED)
438493
# reserve this many cpus for the system (default is 10)
439494
if [ "${2}" == "" ]; then
@@ -458,8 +513,8 @@ done
458513
## clear display before starting
459514
tput reset
460515
echo "Replay Started: ${COLYELLOW}$(date)${COLRESET}"
461-
build_stacks_inspect ## comment if using an existing chainstate/slice dir (ex: replay was performed already, and a second run is desired)
462-
configure_replay_slices ## comment if using an existing chainstate/slice dir (ex: replay was performed already, and a second run is desired)
516+
build_stacks_inspect ## comment if using an existing chainstate/slice dir (ex: replay was performed already, and a second run is desired)
517+
configure_replay_slices ## comment if using an existing chainstate/slice dir (ex: replay was performed already, and a second run is desired)
463518
setup_replay ## configure logdir and tmux sessions
464519
start_replay ## replay pre-nakamoto blocks (2.x)
465520
start_replay nakamoto ## replay nakamoto blocks

0 commit comments

Comments
 (0)