#!/usr/bin/env bash # Fix for the "upgrade rebuild" loop of BDAG community stack v0.1.2 # (the node rescans ~14M headers every ~20 minutes and never stays in sync). # # bash bdag-node-loopfix.sh builds the fixed node program, changes nothing else # bash bdag-node-loopfix.sh install builds it if needed, then swaps it in (safe stop between scans) # bash bdag-node-loopfix.sh undo goes back to the original program # # What it does: takes the published v0.1.2 source, applies a 4-file patch written by the # network operators (thanks to Mr Wilson) and compiles it in the Go container the project itself # pins in its Dockerfile. Dependencies come from the offline module cache inside the source zip. # The original program and Docker image are not modified: the fixed program is mounted over it # by a small docker-compose.override.yml, so "undo" is always possible. # # Guide: https://bdagnode.org/#loopfix set -euo pipefail STACK=${STACK:-/opt/bdag/stack} WORK=${WORK:-/opt/bdag/loopfix} SRC_URL=https://github.com/bdag-community/bdag-stack-releases/releases/download/stack-v0.1.2/bdag-community-stack-v0.1.2-source.zip SRC_SHA=0c441927c5fde691cdd1bf810bcba323c06d0a127ce1dd2c5d03156430a96e19 DIFF_URL=${DIFF_URL:-https://bdagnode.org/files/bdag-v0.1.2-loopfix.diff} DIFF_SHA=8c826f3ca075817105817fbcad7df6f5d1e3c0a8a19b9afc34770e1873afc44d IMG='golang:1.26.9-bookworm@sha256:d9c68c2c51161e12fd77e4c6320687c9cd86e1af1e3ad6e6cd63ff970641453c' OUT=$WORK/blockdag-node.loopfix OVERRIDE=$STACK/docker-compose.override.yml MARK='# bdag-node-loopfix' say() { echo "[loopfix] $*"; } die() { echo "[loopfix] ERROR: $*" >&2; exit 1; } need() { command -v "$1" >/dev/null || die "'$1' is missing. Install it first: apt-get install -y $2"; } build() { need docker docker.io; need curl curl; need unzip unzip; need patch patch mkdir -p "$WORK" && cd "$WORK" [ -f source.zip ] || { say "downloading the v0.1.2 source"; curl -fL --retry 3 -o source.zip "$SRC_URL"; } echo "$SRC_SHA source.zip" | sha256sum -c - >/dev/null || die "source.zip has the wrong checksum - delete $WORK/source.zip and try again" curl -fsSL --retry 3 -o loopfix.diff "$DIFF_URL" echo "$DIFF_SHA loopfix.diff" | sha256sum -c - >/dev/null || die "the patch has the wrong checksum - do not continue" say "checksums OK, unpacking" rm -rf src build && mkdir src build unzip -q source.zip -d src S=$WORK/src/bdag-community-stack-v0.1.2-source cp -a "$S/projects/node" build/node patch -s -p1 -d build/node < loopfix.diff || die "the patch did not apply" cpus=$(( $(nproc) / 2 )); [ "$cpus" -ge 1 ] || cpus=1 say "compiling on $cpus CPU core(s) - takes 3 to 10 minutes, the running node is not touched" docker run --rm --cpus "$cpus" \ -v "$WORK/build/node":/bdag -v "$S/go-module-proxy":/proxy:ro \ -v bdag-loopfix-gocache:/root/.cache/go-build -v bdag-loopfix-gomod:/go/pkg/mod \ -e GOPROXY=file:///proxy -e GOSUMDB=off -e GOTOOLCHAIN=local -e CGO_ENABLED=1 -w /bdag "$IMG" sh -ec ' nice -n 15 go build -p 2 -trimpath \ -ldflags "-X github.com/BlockdagNetworkLabs/bdag/version.ReleaseTag=stack-v0.1.2" \ -o build/bin/bdag ./cmd/bdag ./build/bin/bdag --version' cp build/node/build/bin/bdag "$OUT" && chmod 755 "$OUT" rm -rf src build say "built: $OUT" say "sha256: $(sha256sum "$OUT" | cut -d' ' -f1)" } running_sha() { docker exec node sha256sum /usr/local/bin/blockdag-node 2>/dev/null | cut -d' ' -f1; } wait_between_scans() { local last last=$(docker logs --since 40m node 2>&1 | grep -E 'header scan (started|finished)' | tail -1 || true) if echo "$last" | grep -q 'scan started'; then say "a header scan is running - waiting for it to finish (up to 60 minutes). Never stop the node in the middle of a scan." timeout 3600 docker logs -f --since 5s node 2>&1 | grep -m1 'header scan finished' >/dev/null || die "the scan did not finish in time; run 'install' again later" sleep 3 fi } restart_node() { cd "$STACK" if docker ps --format '{{.Names}}' | grep -qx node; then wait_between_scans say "stopping the node gently (it gets up to 15 minutes to close its database)" docker compose stop -t 900 node docker inspect node --format '{{.State.ExitCode}}' | grep -qx 137 && say "WARNING: the node had to be killed. If it does not start cleanly, restore the chain data from a snapshot." fi docker compose up -d node } install() { [ -x "$OUT" ] || build [ -f "$STACK/docker-compose.yml" ] || die "no docker-compose.yml in $STACK (set STACK=/path/to/stack)" if [ -f "$OVERRIDE" ] && ! grep -q "$MARK" "$OVERRIDE"; then die "$OVERRIDE already exists and was not made by this script. Add this line to the node's volumes yourself: $OUT:/usr/local/bin/blockdag-node:ro" fi cat > "$OVERRIDE" </dev/null || { rm -f "$OVERRIDE"; die "docker compose rejected the override file"; } restart_node sleep 20 [ "$(running_sha)" = "$(sha256sum "$OUT" | cut -d' ' -f1)" ] || die "the node is not running the fixed program - check 'docker compose ps'" say "done. The node now runs the fixed program." say "In a few minutes check: docker logs --since 30m node 2>&1 | grep -c 'header scan started' (should stay at 0 or grow very rarely)" } undo() { [ -f "$OVERRIDE" ] && grep -q "$MARK" "$OVERRIDE" || die "nothing to undo ($OVERRIDE was not made by this script)" rm -f "$OVERRIDE" restart_node say "done. The node runs the original program again." } case "${1:-build}" in build) build; say "nothing was changed on the node. To use it: bash $0 install" ;; install) install ;; undo) undo ;; *) die "usage: bash $0 [build|install|undo]" ;; esac