#!/usr/bin/env bash # Install a BTCX testnet (btcxtest) node on any Ubuntu 22.04 or 24.04 server, x86-64 or ARM64. # It downloads the release binaries (bitcoind, bitcoin-cli, bitcoin-util) from GitHub releases, # checks them against the release's SHA256SUMS, runs the node as a systemd service and connects it # to the public seed nodes. Safe to run again: it updates the binaries and keeps the data. # # curl -fsSL https://raw.githubusercontent.com/yoniassia/Bitcoin2x/master/contrib/install-node.sh | sudo bash # # Environment (all optional): # BTCX_RELEASE release tag to install, for example btcx-v0.2.0 (default: the latest release) # BTCX_BUILD 1 builds from source instead of downloading a release (20 to 60 minutes) # BTCX_REF git branch or tag to build with BTCX_BUILD=1 (default master) # BTCX_PRUNE prune the block files to this many MB (minimum 550). Unset keeps the full chain. # BTCX_OPEN_PORT 1 (default) opens P2P port 19777 in ufw and in Oracle's iptables rules; 0 skips it # BTCX_TXINDEX 1 keeps a transaction index (for explorers); default 0 set -euo pipefail GITHUB_REPO=yoniassia/Bitcoin2x REF=${BTCX_REF:-master} SRC=/opt/btcx/repo BIN=/opt/btcx/bin SHARE=/opt/btcx/share DATA=/var/lib/bitcoin P2P_PORT=19777 SEEDS="178.105.98.246 5.222.62.161" # btcx-seed1 (Nuremberg), btcx-seed2 (Helsinki) [[ $(id -u) == 0 ]] || { echo "Run as root (sudo bash)." >&2; exit 1; } . /etc/os-release [[ ${ID:-} == ubuntu ]] || echo "Warning: tested on Ubuntu 22.04 and 24.04; this is ${PRETTY_NAME:-unknown}." export DEBIAN_FRONTEND=noninteractive log() { printf '\n==> %s\n' "$*"; } mem_mb=$(awk '/MemTotal/ {print int($2/1024)}' /proc/meminfo) cpus=$(nproc) NEW=$(mktemp -d) trap 'rm -rf "$NEW"' EXIT if [[ ${BTCX_BUILD:-0} != 1 ]]; then log "Packages" apt-get update -q apt-get install -y -q --no-install-recommends ca-certificates curl jq case $(uname -m) in x86_64) arch=x86_64 ;; aarch64|arm64) arch=arm64 ;; *) echo "No release binaries for $(uname -m); run again with BTCX_BUILD=1." >&2; exit 1 ;; esac tag=${BTCX_RELEASE:-} if [[ -z $tag ]]; then tag=$(curl -fsSL "https://api.github.com/repos/$GITHUB_REPO/releases/latest" | jq -r .tag_name) [[ $tag == btcx-v* ]] || { echo "Could not find the latest release; set BTCX_RELEASE or BTCX_BUILD=1." >&2; exit 1; } fi name="btcx-${tag#btcx-v}-linux-$arch" log "Downloading release $tag ($name)" base="https://github.com/$GITHUB_REPO/releases/download/$tag" curl -fsSL -o "$NEW/$name.tar.gz" "$base/$name.tar.gz" curl -fsSL -o "$NEW/SHA256SUMS" "$base/SHA256SUMS" (cd "$NEW" && grep " $name.tar.gz\$" SHA256SUMS | sha256sum -c -) tar -xzf "$NEW/$name.tar.gz" -C "$NEW" mkdir -p "$NEW/bin" "$NEW/share" cp "$NEW/$name/bin/"* "$NEW/bin/" cp "$NEW/$name/share/btcx/"* "$NEW/share/" else log "Packages" apt-get update -q apt-get install -y -q --no-install-recommends build-essential cmake pkgconf python3 git ca-certificates \ libevent-dev libboost-dev libsqlite3-dev jq # Compiling needs about 1.5 GB per job. Small servers get a swap file and fewer jobs. if (( mem_mb < 3000 )) && ! swapon --show | grep -q /swapfile-btcx; then log "Adding a 4 GB swap file (server has ${mem_mb} MB RAM)" fallocate -l 4G /swapfile-btcx && chmod 600 /swapfile-btcx && mkswap /swapfile-btcx >/dev/null swapon /swapfile-btcx && echo '/swapfile-btcx none swap sw 0 0' >> /etc/fstab fi jobs=$(( mem_mb / 1500 )); (( jobs < 1 )) && jobs=1; (( jobs > cpus )) && jobs=$cpus log "Source ($REF)" if [[ -d $SRC/.git ]]; then git -C "$SRC" fetch -q --depth 1 origin "$REF" git -C "$SRC" checkout -q -f FETCH_HEAD else git clone -q --depth 1 --branch "$REF" "https://github.com/$GITHUB_REPO.git" "$SRC" fi echo "commit $(git -C "$SRC" rev-parse --short HEAD)" log "Building bitcoind, bitcoin-cli and bitcoin-util with $jobs jobs (20 to 60 minutes the first time)" cmake -S "$SRC/core" -B "$SRC/core/build" -DCMAKE_BUILD_TYPE=Release -DENABLE_IPC=OFF -DWITH_ZMQ=OFF \ -DBUILD_GUI=OFF -DBUILD_BENCH=OFF -DBUILD_FUZZ_BINARY=OFF -DBUILD_TESTS=OFF -DBUILD_UTIL=ON >/dev/null cmake --build "$SRC/core/build" -j "$jobs" --target bitcoind bitcoin-cli bitcoin-util mkdir -p "$NEW/bin" "$NEW/share" cp "$SRC/core/build/bin/bitcoind" "$SRC/core/build/bin/bitcoin-cli" "$SRC/core/build/bin/bitcoin-util" "$NEW/bin/" strip "$NEW/bin/"* || true cp "$SRC/infra/hetzner/remote/btcx-cli" "$SRC/infra/hetzner/remote/systemd/bitcoind@.service" "$NEW/share/" fi log "Installing" id bitcoin >/dev/null 2>&1 || useradd --system --home-dir "$DATA" --shell /usr/sbin/nologin bitcoin install -d -o bitcoin -g bitcoin -m 750 "$DATA" install -d -m 755 "$BIN" "$SHARE" /etc/bitcoin changed=0 for b in bitcoind bitcoin-cli bitcoin-util; do if ! cmp -s "$NEW/bin/$b" "$BIN/$b"; then install -m 755 "$NEW/bin/$b" "$BIN/$b.new" && mv "$BIN/$b.new" "$BIN/$b" [[ $b == bitcoind ]] && changed=1 fi done "$BIN/bitcoind" -version | head -1 install -m 755 "$NEW/share/btcx-cli" /usr/local/bin/btcx-cli install -m 644 "$NEW/share/bitcoind@.service" /etc/systemd/system/ dbcache=$(( mem_mb / 4 )); (( dbcache > 2000 )) && dbcache=2000; (( dbcache < 300 )) && dbcache=300 { echo "# Written by contrib/install-node.sh. Your own settings go below the last line marked local settings." echo "[btcxtest]" echo "server=1" echo "listen=1" echo "rpcbind=127.0.0.1" echo "rpcallowip=127.0.0.1" echo "dbcache=$dbcache" echo "fallbackfee=0.00002" if [[ ${BTCX_TXINDEX:-0} == 1 ]]; then echo "txindex=1"; fi if [[ -n ${BTCX_PRUNE:-} ]]; then echo "prune=$BTCX_PRUNE"; fi for s in $SEEDS; do echo "addnode=$s:$P2P_PORT"; done echo "# --- local settings ---" if [[ -f /etc/bitcoin/bitcoin.conf ]]; then sed -n '/^# --- local settings ---$/,$p' /etc/bitcoin/bitcoin.conf | tail -n +2 fi } > /etc/bitcoin/bitcoin.conf.new cmp -s /etc/bitcoin/bitcoin.conf.new /etc/bitcoin/bitcoin.conf || changed=1 mv /etc/bitcoin/bitcoin.conf.new /etc/bitcoin/bitcoin.conf && chmod 644 /etc/bitcoin/bitcoin.conf if [[ ${BTCX_OPEN_PORT:-1} == 1 ]]; then log "Opening P2P port $P2P_PORT on this server" if command -v ufw >/dev/null && ufw status | grep -q '^Status: active'; then ufw allow "$P2P_PORT/tcp" >/dev/null && echo "ufw: allowed $P2P_PORT/tcp" fi # Oracle Cloud's Ubuntu images ship iptables rules that reject everything except SSH. if command -v iptables >/dev/null && iptables -S INPUT 2>/dev/null | grep -q -- '-j REJECT'; then if ! iptables -C INPUT -p tcp --dport "$P2P_PORT" -j ACCEPT 2>/dev/null; then pos=$(iptables -L INPUT --line-numbers -n | awk '$2 == "REJECT" {print $1; exit}') iptables -I INPUT "$pos" -p tcp -m state --state NEW --dport "$P2P_PORT" -j ACCEPT fi if command -v netfilter-persistent >/dev/null; then netfilter-persistent save >/dev/null 2>&1 || true elif [[ -d /etc/iptables ]]; then iptables-save > /etc/iptables/rules.v4; fi echo "iptables: allowed $P2P_PORT/tcp" fi echo "Also allow TCP $P2P_PORT from 0.0.0.0/0 in your cloud provider's firewall (see docs/run-a-node.md)." fi log "Starting the node" systemctl daemon-reload systemctl enable -q bitcoind@btcxtest if (( changed )); then systemctl restart bitcoind@btcxtest; else systemctl start bitcoind@btcxtest; fi for _ in $(seq 60); do btcx-cli getblockchaininfo >/dev/null 2>&1 && break; sleep 2; done btcx-cli getblockchaininfo | jq '{chain, blocks, headers, initialblockdownload}' || true cat <