From de2618346ac138c207c3bc3e4a1d329df0361e08 Mon Sep 17 00:00:00 2001 From: 3alpha <15694175+3alpha@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:24:35 +0100 Subject: [PATCH 1/2] feat(reth): report the sync phase through the healthcheck The healthcheck prints a status report with the client's sync phase and progress, declared in the manifest `nodes` field, so the dappmanager no longer needs client-specific parsing. --- .github/workflows/node-status-test.yml | 18 + README.md | 4 + dappnode_package.json | 13 +- docker-compose.yml | 8 +- package_variants/hoodi/dappnode_package.json | 5 + .../mainnet/dappnode_package.json | 5 + .../sepolia/dappnode_package.json | 5 + reth/Dockerfile | 6 +- reth/entrypoint.sh | 62 ++- reth/node-status.sh | 181 ++++++++ test/node_status_test.sh | 434 ++++++++++++++++++ 11 files changed, 730 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/node-status-test.yml create mode 100755 reth/node-status.sh create mode 100755 test/node_status_test.sh diff --git a/.github/workflows/node-status-test.yml b/.github/workflows/node-status-test.yml new file mode 100644 index 0000000..3738efc --- /dev/null +++ b/.github/workflows/node-status-test.yml @@ -0,0 +1,18 @@ +name: Node status test + +on: + pull_request: + +jobs: + node-status: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/checkout@v4 + with: + repository: dappnode/staker-package-scripts + path: staker-package-scripts + + - name: Test with dash + run: NODE_STATUS_TOOLS=staker-package-scripts/scripts/node_status_tools.sh sh test/node_status_test.sh diff --git a/README.md b/README.md index 35113e7..63d36e5 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ Dappnode Package for execution client Reth in Ethereum mainnet. +## Node status report + +The package reports what Reth is doing (snapshot download, pipeline stage and its progress, waiting for a consensus client) through its Docker healthcheck, which the Dappnode dashboard reads. The script is [`reth/node-status.sh`](reth/node-status.sh), installed as `/usr/local/bin/node-status`; its tests are in [`test/node_status_test.sh`](test/node_status_test.sh) (`sh test/node_status_test.sh`, with `dappnode/staker-package-scripts` checked out next to this repository). + ## License This project is licensed under the GNU General Public License v3.0 - see the [LICENSE](LICENSE) file for details diff --git a/dappnode_package.json b/dappnode_package.json index e55bf65..34dbc92 100644 --- a/dappnode_package.json +++ b/dappnode_package.json @@ -20,6 +20,17 @@ "execution" ], "license": "GPL-3.0", + "nodes": { + "reth": { + "role": "execution", + "probe": { + "kind": "ethereum-execution", + "rpcPort": 8545, + "enginePort": 8551 + }, + "report": "healthcheck" + } + }, "repository": { "type": "git", "url": "https://github.com/dappnode/DAppNodePackage-reth-generic.git" @@ -38,7 +49,7 @@ { "arg": "STAKER_SCRIPTS_VERSION", "repo": "dappnode/staker-package-scripts", - "version": "v0.1.2" + "version": "v0.1.3" } ] } diff --git a/docker-compose.yml b/docker-compose.yml index 546260d..4116a1d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: context: reth args: UPSTREAM_VERSION: v2.10.1 - STAKER_SCRIPTS_VERSION: v0.1.2 + STAKER_SCRIPTS_VERSION: v0.1.3 DATA_DIR: /data/reth volumes: - reth:/data/reth @@ -15,6 +15,12 @@ services: ARCHIVE_NODE: "false" DOWNLOAD_SNAPSHOT: "true" STORAGE_MODE: full + healthcheck: + test: ["CMD", "/usr/local/bin/node-status"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 60s restart: unless-stopped volumes: reth: {} diff --git a/package_variants/hoodi/dappnode_package.json b/package_variants/hoodi/dappnode_package.json index 3e069ef..6703bc9 100644 --- a/package_variants/hoodi/dappnode_package.json +++ b/package_variants/hoodi/dappnode_package.json @@ -5,5 +5,10 @@ "homepage": "https://github.com/dappnode/DAppNodePackage-reth-generic#readme" }, "name": "hoodi-reth.dnp.dappnode.eth", + "nodes": { + "reth": { + "network": "hoodi" + } + }, "version": "0.1.10" } diff --git a/package_variants/mainnet/dappnode_package.json b/package_variants/mainnet/dappnode_package.json index 0d11f95..b560acc 100644 --- a/package_variants/mainnet/dappnode_package.json +++ b/package_variants/mainnet/dappnode_package.json @@ -5,5 +5,10 @@ "homepage": "https://github.com/dappnode/DAppNodePackage-reth-generic#readme" }, "name": "reth.dnp.dappnode.eth", + "nodes": { + "reth": { + "network": "mainnet" + } + }, "version": "0.1.8" } diff --git a/package_variants/sepolia/dappnode_package.json b/package_variants/sepolia/dappnode_package.json index 0e43622..187a0d1 100644 --- a/package_variants/sepolia/dappnode_package.json +++ b/package_variants/sepolia/dappnode_package.json @@ -5,5 +5,10 @@ "homepage": "https://github.com/dappnode/DAppNodePackage-reth-generic#readme" }, "name": "sepolia-reth.dnp.dappnode.eth", + "nodes": { + "reth": { + "network": "sepolia" + } + }, "version": "0.1.3" } diff --git a/reth/Dockerfile b/reth/Dockerfile index 59aa206..6ec526c 100644 --- a/reth/Dockerfile +++ b/reth/Dockerfile @@ -7,17 +7,19 @@ ARG P2P_PORT ARG STAKER_SCRIPTS_VERSION ARG DATA_DIR -RUN apt-get update && apt-get --yes install --no-install-recommends curl && apt-get clean +RUN apt-get update && apt-get --yes install --no-install-recommends curl jq && apt-get clean ENV DATA_DIR=${DATA_DIR} \ JWT_PATH=/jwtsecret.hex \ STAKER_SCRIPTS_URL=https://github.com/dappnode/staker-package-scripts/releases/download/${STAKER_SCRIPTS_VERSION} COPY entrypoint.sh /usr/local/bin/entrypoint.sh +COPY node-status.sh /usr/local/bin/node-status ADD ${STAKER_SCRIPTS_URL}/execution_tools.sh /etc/profile.d/ +ADD ${STAKER_SCRIPTS_URL}/node_status_tools.sh /etc/profile.d/ -RUN chmod +rx /usr/local/bin/entrypoint.sh /etc/profile.d/execution_tools.sh +RUN chmod +rx /usr/local/bin/entrypoint.sh /usr/local/bin/node-status /etc/profile.d/execution_tools.sh /etc/profile.d/node_status_tools.sh # Placed at the end to regenerate the least amount of layers ENV NETWORK=${NETWORK} \ diff --git a/reth/entrypoint.sh b/reth/entrypoint.sh index 70faa65..9f1a6f6 100755 --- a/reth/entrypoint.sh +++ b/reth/entrypoint.sh @@ -66,6 +66,60 @@ is_data_dir_initialized() { return 1 } +# Read by the healthcheck (node-status) to report the snapshot download before Reth serves RPC +NODE_STATUS_DIR=/tmp/node-status +SNAPSHOT_MARKER="${NODE_STATUS_DIR}/snapshot-download" + +# A marker left by a container that was killed mid-download must not outlive that run +rm -f "${SNAPSHOT_MARKER}" + +# Writes the snapshot download marker atomically +# Arguments: PID of reth download, phase, progress in percent (may be empty) +write_snapshot_marker() { + printf 'pid=%s\nphase=%s\nprogress=%s\n' "$1" "$2" "$3" >"${SNAPSHOT_MARKER}.tmp" && + mv -f "${SNAPSHOT_MARKER}.tmp" "${SNAPSHOT_MARKER}" +} + +# Copies the output of reth download (stdin) to stdout and records the phase and progress of its +# periodic progress lines, e.g. "Downloading snapshot archives archives=3/40 progress=12.3% ..." +# Arguments: PID of reth download +follow_snapshot_download() { + esc=$(printf '\033') + while IFS= read -r line; do + printf '%s\n' "${line}" + case "${line}" in + *"Downloading snapshot archives"*) phase="Downloading snapshot" ;; + *"Extracting snapshot archives"*) phase="Extracting snapshot" ;; + *"Verifying snapshot archives"*) phase="Verifying snapshot" ;; + *) continue ;; + esac + # Field names carry color codes unless the output is plain + progress=$(printf '%s\n' "${line}" | sed -e "s/${esc}\[[0-9;]*m//g" -n -e 's/.*[[:space:]]progress=\([0-9][0-9.]*\)%.*/\1/p') + write_snapshot_marker "$1" "${phase}" "${progress}" + done +} + +# Runs reth download in the background, so its PID can vouch for the marker, and follows its output. +# Returns the exit status of reth download. +download_snapshot() { + fifo="${NODE_STATUS_DIR}/snapshot-download.fifo" + rm -f "${fifo}" + if ! mkdir -p "${NODE_STATUS_DIR}" || ! mkfifo "${fifo}"; then + # shellcheck disable=SC2086 + reth download -y ${SNAPSHOT_MODE_FLAG} --chain "${NETWORK}" --datadir "${DATA_DIR}" + return $? + fi + # shellcheck disable=SC2086 + reth download -y ${SNAPSHOT_MODE_FLAG} --chain "${NETWORK}" --datadir "${DATA_DIR}" >"${fifo}" 2>&1 & + download_pid=$! + write_snapshot_marker "${download_pid}" "Downloading snapshot" "" + follow_snapshot_download "${download_pid}" <"${fifo}" + wait "${download_pid}" + download_status=$? + rm -f "${fifo}" "${SNAPSHOT_MARKER}" "${SNAPSHOT_MARKER}.tmp" + return "${download_status}" +} + echo "[INFO - entrypoint] Running Reth client for network: ${NETWORK}" if [ "${STORAGE_MODE}" = custom ]; then @@ -78,13 +132,7 @@ elif [ "${DOWNLOAD_SNAPSHOT}" = true ]; then echo "[INFO - entrypoint] No Reth snapshot available for ${NETWORK}; syncing from genesis" else echo "[INFO - entrypoint] Downloading ${NETWORK} ${SNAPSHOT_MODE_FLAG#--} snapshot before starting Reth" - # shellcheck disable=SC2086 - if ! reth \ - download \ - -y \ - ${SNAPSHOT_MODE_FLAG} \ - --chain "${NETWORK}" \ - --datadir "${DATA_DIR}"; then + if ! download_snapshot; then echo "[WARN - entrypoint] Snapshot download failed; falling back to syncing ${NETWORK} from genesis" fi fi diff --git a/reth/node-status.sh b/reth/node-status.sh new file mode 100755 index 0000000..b24bd49 --- /dev/null +++ b/reth/node-status.sh @@ -0,0 +1,181 @@ +#!/bin/sh + +# Node status report of the Reth package, run as the Docker healthcheck. +# Prints one JSON line (see node_status_tools.sh in dappnode/staker-package-scripts): +# what Reth is doing, from its own JSON-RPC, metrics and the snapshot download +# marker the entrypoint leaves. Whether the node is synced is decided by the +# dappmanager, not here. +# +# Environment: +# NODE_STATUS_TOOLS: Helper library to source (default /etc/profile.d/node_status_tools.sh) +# NODE_STATUS_DIR: Directory of the snapshot download marker (default /tmp/node-status) + +# Every call gets 2 s, so even a node that stops answering midway stays well inside the 10 s timeout +NS_TIMEOUT=${NS_TIMEOUT:-2} + +# shellcheck source=/dev/null +. "${NODE_STATUS_TOOLS:-/etc/profile.d/node_status_tools.sh}" || exit 1 + +SNAPSHOT_MARKER="${NODE_STATUS_DIR:-/tmp/node-status}/snapshot-download" +METRICS_URL=http://127.0.0.1:6060/metrics + +# Blocks a stage may trail the pipeline target and still count as done. Reth only runs the +# pipeline for larger gaps; smaller ones are filled block by block by the live sync. +STAGE_TOLERANCE_BLOCKS=32 + +# The JSON-RPC does not answer: Reth is opening its database, or the entrypoint is still +# downloading a snapshot. The download marker counts only while the process it names runs, so +# a marker left by a killed download never makes a starting node look like it downloads. +report_starting() { + pid="" + phase="" + progress="" + if [ -f "$SNAPSHOT_MARKER" ]; then + while IFS='=' read -r key value; do + case $key in + pid) pid=$value ;; + phase) phase=$value ;; + progress) progress=$value ;; + esac + done <"$SNAPSHOT_MARKER" + case $pid in + '' | *[!0-9]*) phase="" ;; + *) grep -qa download "/proc/$pid/cmdline" 2>/dev/null || phase="" ;; + esac + fi + if [ -n "$phase" ]; then + ns_emit starting phase="$phase" progress="$progress" + else + ns_emit starting + fi +} + +# Reads the eth_syncing object on stdin and prints ns_emit arguments, one per line. +# +# Reth's currentBlock and highestBlock are always the same block: the latest block only moves +# when the whole staged pipeline finishes. Progress comes from `stages`, the checkpoint of each +# pipeline stage, weighted by the stage's estimated share of the sync time. The pipeline target +# is not reported, so the highest checkpoint (normally the Headers stage) stands for it. +# +# Prints "awaiting=true" when every stage is at the latest block: the pipeline has nothing to +# do, so either no consensus client drives the node, or it just restarted, or the Headers stage +# is downloading (it saves its checkpoint only once all headers are in). +analyze_stages() { + jq -r --argjson tolerance "$STAGE_TOLERANCE_BLOCKS" ' + def num: if type == "number" then floor + elif type == "string" and test("^0[xX][0-9a-fA-F]+$") then .[2:] | ascii_downcase | explode + | reduce .[] as $c (0; . * 16 + (if $c >= 97 then $c - 87 else $c - 48 end)) + elif type == "string" and test("^[0-9]+$") then tonumber + else null end; + def commas: tostring | if length > 3 then (.[:-3] | commas) + "," + .[-3:] else . end; + def near($a; $b): ($a - $b) as $d | $d <= $tolerance and $d >= -$tolerance; + # Pipeline stages in run order (crates/stages/stages/src/sets.rs at the pinned Reth), + # with their estimated share of the sync time. Stages not listed are ignored. + [ + {name: "Era", weight: 0, label: "Importing history files"}, + {name: "Headers", weight: 5, label: "Downloading block headers"}, + {name: "Bodies", weight: 10, label: "Downloading blocks"}, + {name: "SenderRecovery", weight: 5, label: "Recovering transaction senders"}, + {name: "Execution", weight: 60, label: "Executing blocks"}, + {name: "PruneSenderRecovery", weight: 0, label: "Pruning"}, + {name: "MerkleUnwind", weight: 0, label: "Building the state trie"}, + {name: "AccountHashing", weight: 3, label: "Building the state trie"}, + {name: "StorageHashing", weight: 3, label: "Building the state trie"}, + {name: "MerkleExecute", weight: 4, label: "Building the state trie"}, + {name: "TransactionLookup", weight: 4, label: "Indexing transactions"}, + {name: "IndexStorageHistory", weight: 3, label: "Indexing state history"}, + {name: "IndexAccountHistory", weight: 3, label: "Indexing state history"}, + {name: "Prune", weight: 0, label: "Pruning"}, + {name: "Finish", weight: 0, label: "Finishing"} + ] as $table + | ((.currentBlock | num) // 0) as $current + | ((.startingBlock | num) // 0) as $start + | (if (.stages | type) == "array" then .stages else [] end + | map(select(type == "object") + | {name: (.name // .stage_name), block: ((.block // .block_number) | num)} + | select((.name | type) == "string" and .block != null)) + | reduce .[] as $s ({}; .[$s.name] = $s.block)) as $blocks + | [$table[] | select($blocks[.name] != null) | .block = $blocks[.name]] as $known + | ([$known[] | select(.name != "Era") | .block] | max) as $highest + | ([$known[] | select(.name == "Era") | .block] | first) as $era + | ([$known[] | select(.name == "MerkleUnwind") | .block] | first) as $unwind + # Optional stages that are not part of this pipeline keep an old checkpoint (genesis when + # they never ran): Era runs only with --era.enable, ahead of every other stage, and + # PruneSenderRecovery only with sender recovery pruning, ahead of MerkleUnwind + | [$known[] | select(.name != "Era") + | select(.name != "PruneSenderRecovery" or $unwind == null or .block >= $unwind)] as $stages + | if $stages == [] then empty + elif $era != null and $era > $highest then "phase=Importing history files" + elif all($stages[]; near(.block; $current)) then "awaiting=true" + else + # Stages rebuilt from scratch (the indices after a snapshot download) start below the + # block this run started from + reduce $stages[] as $s ({done: 0, total: 0, stage: null}; + (if $s.block >= $start then $start else 0 end) as $base + | (if $highest > $base then ([1, ([0, ($s.block - $base) / ($highest - $base)] | max)] | min) + else 1 end) as $fraction + | .done += $s.weight * $fraction + | .total += $s.weight + | if .stage == null and ($highest - $s.block) > $tolerance then .stage = $s else . end) + | "phase=\(.stage.label // "Finishing")", + (if .total > 0 then "progress=\(.done / .total * 100)" else empty end), + (if .stage != null + then "detail=Pipeline stage:\(.stage.name), block \(.stage.block | commas) of \($highest | commas)" + else empty end) + end + ' 2>/dev/null +} + +# The Headers stage downloads from the target down to the local head and saves its checkpoint only +# at the end, so while it runs every stage still sits at the latest block. Its downloader has +# requests in flight only during that download. +headers_downloading() { + metrics=$(ns_http_get "$METRICS_URL") || return 1 + in_flight=$(printf '%s\n' "$metrics" | ns_metric reth_downloaders_headers_in_flight_requests) || return 1 + case $in_flight in + '' | 0 | -*) return 1 ;; + esac + return 0 +} + +if ! syncing=$(ns_rpc eth_syncing); then + report_starting + exit $? +fi + +# Stop at the first call that fails: each one can take the full timeout +head="" +peers="" +rpc_ok=false +if head=$(ns_block_head); then + peers=$(ns_json_get "$(ns_rpc net_peerCount)" .) && [ -n "$peers" ] && rpc_ok=true +fi + +if [ "$syncing" = "false" ]; then + # shellcheck disable=SC2086 # $head holds two key=value words + ns_emit synced $head peers="$peers" + exit $? +fi + +set -- +awaiting=false +while IFS= read -r arg; do + case $arg in + awaiting=true) awaiting=true ;; + ?*) set -- "$@" "$arg" ;; + esac +done <). +# The network is stubbed by a curl function. + +cd "$(dirname "$0")/.." || exit 1 +NODE_STATUS_TOOLS=${NODE_STATUS_TOOLS:-../staker-package-scripts/scripts/node_status_tools.sh} +if [ ! -f "$NODE_STATUS_TOOLS" ]; then + echo "Helper library not found: $NODE_STATUS_TOOLS (set NODE_STATUS_TOOLS)" >&2 + exit 1 +fi +SCRIPT=./reth/node-status.sh +ENTRYPOINT=./reth/entrypoint.sh + +PASS=0 +FAIL=0 +TMP=$(mktemp -d) || exit 1 +NODE_STATUS_DIR="$TMP/status" +mkdir -p "$NODE_STATUS_DIR" +HELPER_PIDS="" +# shellcheck disable=SC2329 # called by the EXIT trap +cleanup() { + for pid in $HELPER_PIDS; do kill "$pid" 2>/dev/null; done + rm -rf "$TMP" +} +trap cleanup EXIT + +# check +check() { + if [ "$2" = "$3" ]; then + PASS=$((PASS + 1)) + else + FAIL=$((FAIL + 1)) + printf 'FAIL: %s\n expected: %s\n actual: %s\n' "$1" "$2" "$3" + fi +} + +# Stubbed node. STUB_RPC: up (default) or down (connection refused). STUB_SYNCING, STUB_BLOCK and +# STUB_PEERS: JSON results of eth_syncing, eth_getBlockByNumber and net_peerCount. STUB_METRICS: +# body of the metrics endpoint, which refuses connections when unset. Each call is logged to +# $TMP/calls as " max-time=". +# shellcheck disable=SC2329 # called by the library under test +curl() { + _url="" + _data="" + _max_time="" + while [ $# -gt 0 ]; do + case $1 in + --data) _data=$2 && shift ;; + --max-time) _max_time=$2 && shift ;; + http://*) _url=$1 ;; + esac + shift + done + _method="" + [ -n "$_data" ] && _method=$(printf '%s' "$_data" | jq -r .method) + echo "$_url $_method max-time=$_max_time" >>"$TMP/calls" + case $_url in + */metrics) + [ -n "${STUB_METRICS+set}" ] || return 7 + printf '%s' "$STUB_METRICS" + return 0 + ;; + esac + [ "${STUB_RPC:-up}" = up ] || return 7 + case $_method in + eth_syncing) _result=$STUB_SYNCING ;; + eth_getBlockByNumber) _result=$STUB_BLOCK ;; + net_peerCount) _result=$STUB_PEERS ;; + *) _result=null ;; + esac + printf '{"jsonrpc":"2.0","id":1,"result":%s}' "$_result" +} + +# Runs the healthcheck script with the stubbed curl: sets OUT (stdout), RC, LINES and CALLS +run_status() { + : >"$TMP/calls" + # shellcheck disable=SC1090 + OUT=$(. "$SCRIPT" 2>"$TMP/stderr") + RC=$? + LINES=$(printf '%s\n' "$OUT" | grep -c .) + CALLS=$(cat "$TMP/calls") +} + +# Fails unless OUT is one JSON object that follows the status report contract +validate() { + _errors=$(printf '%s\n' "$OUT" | jq -r ' + def int: type == "number" and . == floor and . >= 0; + def short($n): type == "string" and length > 0 and length <= $n; + [ + (if .version != 1 then "version" else empty end), + (if (.state | IN("starting", "syncing", "synced", "error")) | not then "state" else empty end), + (keys - ["version", "state", "phase", "progress", "height", "headTime", "position", "target", + "unit", "peers", "awaitingTarget", "background", "issues", "details"] | .[] | "unknown key \(.)"), + (if has("phase") and (.phase | short(60) | not) then "phase" else empty end), + (if has("progress") and ((.progress | type) != "number" or .progress < 0 or .progress > 100 + or (.state != "synced" and .progress > 99.9)) then "progress" else empty end), + (["height", "headTime", "position", "target", "peers"][] as $k + | if has($k) and (.[$k] | int | not) then $k else empty end), + (if has("awaitingTarget") and .awaitingTarget != true then "awaitingTarget" else empty end), + (if has("background") and (.background | short(60) | not) then "background" else empty end), + (if has("issues") and ((.issues | type) != "array" or (.issues | length) > 5 + or any(.issues[]; short(120) | not)) then "issues" else empty end), + (if has("details") and ((.details | type) != "array" or (.details | length) > 8 + or any(.details[]; (.label | short(40) | not) or (.value | short(120) | not))) then "details" else empty end), + (if has("progress") and (has("position") or has("target")) then "progress with position" else empty end), + (if (tojson | utf8bytelength) > 2048 then "size" else empty end) + ] | join(", ")' 2>/dev/null) || _errors="not JSON" + check "$1: one line" 1 "$LINES" + check "$1: follows the contract" "" "$_errors" + check "$1: exit code" 0 "$RC" +} + +# field : value from OUT, compact +field() { + printf '%s\n' "$OUT" | jq -c "$1" 2>/dev/null +} + +hex() { + printf '"0x%x"' "$1" +} + +# block +block() { + printf '{"number":%s,"timestamp":%s,"hash":"0xab"}' "$(hex "$1")" "$(hex "$2")" +} + +# syncing ...: eth_syncing object as Reth builds it +syncing() { + _start=$1 + _current=$2 + shift 2 + _stages="" + for _stage in "$@"; do + _stages="$_stages${_stages:+,}{\"name\":\"${_stage%%:*}\",\"block\":$(hex "${_stage#*:}")}" + done + printf '{"startingBlock":%s,"currentBlock":%s,"highestBlock":%s,"warpChunksAmount":null,"warpChunksProcessed":null,"stages":[%s]}' \ + "$(hex "$_start")" "$(hex "$_current")" "$(hex "$_current")" "$_stages" +} + +# syncing_all [name:block...]: every stage of Reth v2.10.1 +# (StageId::ALL) at , except the ones given +syncing_all() { + _start_all=$1 + _current_all=$2 + _block_all=$3 + shift 3 + _overrides=" $* " + set -- + for _name in Era Headers Bodies SenderRecovery Execution PruneSenderRecovery MerkleUnwind AccountHashing \ + StorageHashing MerkleExecute TransactionLookup IndexStorageHistory IndexAccountHistory Prune Finish; do + _value=$_block_all + case $_overrides in + *" $_name:"*) + _value=${_overrides#*" $_name:"} + _value=${_value%% *} + ;; + esac + set -- "$@" "$_name:$_value" + done + syncing "$_start_all" "$_current_all" "$@" +} + +metrics_in_flight() { + printf '# TYPE reth_downloaders_headers_in_flight_requests gauge\nreth_downloaders_headers_in_flight_requests %s\nreth_sync_checkpoint{stage="Headers"} 0\n' "$1" +} + +# write_marker : marker as the entrypoint writes it +write_marker() { + printf 'pid=%s\nphase=%s\nprogress=%s\n' "$1" "$2" "$3" >"$NODE_STATUS_DIR/snapshot-download" +} + +# Starts a process whose command line names a download, like reth download, and sets DOWNLOAD_PID +start_fake_download() { + sh -c 'sleep 60; :' reth-download & + DOWNLOAD_PID=$! + HELPER_PIDS="$HELPER_PIDS $DOWNLOAD_PID" +} + +NOW=1764500000 +STUB_BLOCK=$(block 21000000 "$NOW") +STUB_PEERS='"0x19"' + +echo "starting" +STUB_RPC=down +run_status +validate "node down" +check "node down: report" '{"version":1,"state":"starting"}' "$OUT" +check "node down: only eth_syncing called" "http://127.0.0.1:8545 eth_syncing max-time=2" "$CALLS" + +start_fake_download +write_marker "$DOWNLOAD_PID" "Downloading snapshot" "12.3" +run_status +validate "snapshot download" +check "snapshot download: report" '{"version":1,"state":"starting","phase":"Downloading snapshot","progress":12.3}' "$OUT" + +write_marker "$DOWNLOAD_PID" "Extracting snapshot" "" +run_status +validate "snapshot extraction without progress" +check "snapshot extraction without progress: report" '{"version":1,"state":"starting","phase":"Extracting snapshot"}' "$OUT" + +write_marker "$DOWNLOAD_PID" "Downloading snapshot" "100" +run_status +check "snapshot download: progress never 100 while running" "99.9" "$(field .progress)" + +sleep 60 & +OTHER_PID=$! +HELPER_PIDS="$HELPER_PIDS $OTHER_PID" +write_marker "$OTHER_PID" "Downloading snapshot" "50" +run_status +check "marker naming a process that is not a download is ignored" '{"version":1,"state":"starting"}' "$OUT" + +sh -c 'exit 0' & +DEAD_PID=$! +wait "$DEAD_PID" +write_marker "$DEAD_PID" "Downloading snapshot" "50" +run_status +check "marker of a finished download is ignored" '{"version":1,"state":"starting"}' "$OUT" + +write_marker "" "Downloading snapshot" "50" +run_status +check "marker without pid is ignored" '{"version":1,"state":"starting"}' "$OUT" + +printf 'garbage\n' >"$NODE_STATUS_DIR/snapshot-download" +run_status +validate "garbage marker" +check "garbage marker: plain starting" '{"version":1,"state":"starting"}' "$OUT" + +echo "synced" +STUB_RPC=up +STUB_SYNCING=false +write_marker "$DOWNLOAD_PID" "Downloading snapshot" "12.3" +run_status +validate "synced" +check "synced: report" "{\"version\":1,\"state\":\"synced\",\"height\":21000000,\"headTime\":$NOW,\"peers\":25}" "$OUT" +check "synced: a marker is ignored once the RPC answers" "null" "$(field .phase)" +check "synced: no metrics call" "" "$(printf '%s\n' "$CALLS" | grep metrics)" +rm -f "$NODE_STATUS_DIR/snapshot-download" + +STUB_PEERS='"0x0"' +run_status +check "synced with no peers: peers 0, no issue" "0 null" "$(field .peers) $(field .issues)" +STUB_PEERS='"0x19"' + +echo "pipeline stages" +# Fixture from the dappmanager's previous parser: 20 of the 84 points of the stages present are +# done, plus half of Execution's 60 +STUB_SYNCING=$(syncing 0 0 Headers:20000000 Bodies:20000000 SenderRecovery:20000000 Execution:10000000 MerkleExecute:0) +STUB_BLOCK=$(block 0 1438269973) +run_status +validate "executing" +check "executing: phase" '"Executing blocks"' "$(field .phase)" +check "executing: weighted progress" "59.5" "$(field .progress)" +check "executing: head and peers" "0 1438269973 25" "$(field .height) $(field .headTime) $(field .peers)" +check "executing: no target or awaitingTarget" "null null null" "$(field .position) $(field .target) $(field .awaitingTarget)" +check "executing: stage detail" '[{"label":"Pipeline stage","value":"Execution, block 10,000,000 of 20,000,000"}]' "$(field .details)" +check "executing: no metrics call" "" "$(printf '%s\n' "$CALLS" | grep metrics)" + +# A genesis sync on Reth v2.10.1: every stage listed in database order, Era never ran (no --era.enable) +# and stays at genesis, bodies at 40 % +STUB_SYNCING=$(syncing 0 0 AccountHashing:0 Bodies:400000 Era:0 Execution:0 Finish:0 Headers:1000000 \ + IndexAccountHistory:0 IndexStorageHistory:0 MerkleExecute:0 MerkleUnwind:0 Prune:0 PruneSenderRecovery:0 \ + SenderRecovery:0 StorageHashing:0 TransactionLookup:0) +run_status +validate "bodies" +check "bodies: phase, Era at genesis is skipped" '"Downloading blocks"' "$(field .phase)" +check "bodies: progress" "9" "$(field .progress)" + +STUB_SYNCING=$(syncing 0 0 Era:0 Headers:1000000 Bodies:1000000 SenderRecovery:250000 Execution:0) +run_status +check "sender recovery: phase" '"Recovering transaction senders"' "$(field .phase)" + +# Archive mode: PruneSenderRecovery is not in the pipeline and keeps its genesis checkpoint +STUB_SYNCING=$(syncing 0 0 Era:0 Headers:1000000 Bodies:1000000 SenderRecovery:1000000 Execution:1000000 \ + PruneSenderRecovery:0 MerkleUnwind:1000000 AccountHashing:500000 StorageHashing:0 MerkleExecute:0 \ + TransactionLookup:0 IndexStorageHistory:0 IndexAccountHistory:0 Prune:0 Finish:0) +run_status +check "state trie: phase, unused PruneSenderRecovery is skipped" '"Building the state trie"' "$(field .phase)" +check "state trie: progress" "81.5" "$(field .progress)" + +# Full mode: PruneSenderRecovery runs right after Execution +STUB_SYNCING=$(syncing 0 0 Headers:1000000 Bodies:1000000 SenderRecovery:1000000 Execution:1000000 \ + PruneSenderRecovery:300000 MerkleUnwind:0 AccountHashing:0) +run_status +check "prune sender recovery: phase" '"Pruning"' "$(field .phase)" + +# After a snapshot download: the index stages were reset to 0 and are rebuilt up to the snapshot block +S=24500000 +STUB_SYNCING=$(syncing_all "$S" "$S" "$S" Era:0 TransactionLookup:0 IndexStorageHistory:0 IndexAccountHistory:0) +STUB_BLOCK=$(block "$S" "$NOW") +run_status +validate "index rebuild" +check "index rebuild: phase" '"Indexing transactions"' "$(field .phase)" +check "index rebuild: progress" "90" "$(field .progress)" +check "index rebuild: not awaiting" "null" "$(field .awaitingTarget)" + +STUB_SYNCING=$(syncing_all "$S" "$S" "$S" Era:0 IndexStorageHistory:12250000 IndexAccountHistory:0) +run_status +check "state history index: phase" '"Indexing state history"' "$(field .phase)" +check "state history index: progress" "95.5" "$(field .progress)" + +# Stage names the table does not know, stale checkpoints of removed stages and malformed entries +STUB_SYNCING=$(printf '%s' "$(syncing 0 0 Headers:20000000 Bodies:20000000 SenderRecovery:20000000 Execution:10000000 MerkleExecute:0 \ + MerkleChangeSets:5 StaticFile:30000000 SomeFutureStage:40000000)" | + jq -c '.stages += [{"name": "Broken", "block": "xyz"}, {"block": "0x1"}, "text", {"stage_name": "Odd", "block_number": "0x10"}]') +run_status +validate "unknown stages" +check "unknown stages are ignored" '"Executing blocks" 59.5' "$(field .phase) $(field .progress)" + +STUB_SYNCING=$(syncing 0 0 MerkleChangeSets:5 SomeFutureStage:40000000) +run_status +validate "only unknown stages" +check "only unknown stages: no phase" "null null null" "$(field .phase) $(field .progress) $(field .awaitingTarget)" + +# Every stage at the target but the latest block not moved yet: the end of the pipeline +STUB_SYNCING=$(syncing_all 0 1000 2000000 Finish:1000) +run_status +validate "finishing" +check "finishing: phase and capped progress" '"Finishing" 99.9' "$(field .phase) $(field .progress)" + +# Era import (--era.enable): Era runs ahead of every other stage and the target is unknown +STUB_SYNCING=$(syncing 0 0 Era:5000000 Headers:0 Bodies:0 Execution:0) +run_status +validate "era import" +check "era import: phase without progress" '"Importing history files" null' "$(field .phase) $(field .progress)" + +# Erigon-style keys, as the previous parser accepted them +STUB_SYNCING='{"startingBlock":"0x0","currentBlock":"0x0","highestBlock":"0x0","stages":[{"stage_name":"Headers","block_number":"0x64"},{"stage_name":"Bodies","block_number":"0x0"}]}' +run_status +check "stage_name and block_number keys" '"Downloading blocks"' "$(field .phase)" + +echo "nothing to sync towards" +# Fixture from the dappmanager's previous parser: every stage at the latest block +STUB_SYNCING=$(syncing 0 15000000 Headers:15000000 Execution:15000000) +STUB_BLOCK=$(block 15000000 "$NOW") +unset STUB_METRICS +run_status +validate "awaiting target" +check "awaiting target: report" "{\"version\":1,\"state\":\"syncing\",\"height\":15000000,\"headTime\":$NOW,\"peers\":25,\"awaitingTarget\":true}" "$OUT" +check "awaiting target: metrics asked once" "1" "$(printf '%s\n' "$CALLS" | grep -c 'http://127.0.0.1:6060/metrics')" + +# Restarted node: every stage of v2.10.1 moved to the latest block by the live sync, Era included +STUB_SYNCING=$(syncing_all 21000000 21000000 21000000) +STUB_METRICS=$(metrics_in_flight 0) +run_status +validate "restarted" +check "restarted: awaiting target, no phase" "true null" "$(field .awaitingTarget) $(field .phase)" + +# A fresh database: everything at genesis, Era included +STUB_SYNCING=$(syncing_all 0 0 0) +STUB_BLOCK=$(block 0 1742212800) +run_status +check "genesis: awaiting target" "true" "$(field .awaitingTarget)" + +# Pipeline after a snapshot that synced Era only up to the headers: Era behind the head is skipped +STUB_SYNCING=$(syncing_all 21000000 21000000 21000000 Era:0) +STUB_BLOCK=$(block 21000000 "$NOW") +run_status +check "Era at genesis does not hide an idle pipeline" "true" "$(field .awaitingTarget)" + +# The Headers stage is downloading: it saves its checkpoint only at the end +STUB_METRICS=$(metrics_in_flight 4) +STUB_SYNCING=$(syncing_all 0 0 0) +run_status +validate "headers" +check "headers: phase, not awaiting" '"Downloading block headers" null null' "$(field .phase) $(field .awaitingTarget) $(field .progress)" + +echo "partial answers" +STUB_SYNCING=$(syncing 0 15000000 Headers:15000000 Execution:15000000) +STUB_BLOCK=null +run_status +validate "no latest block" +check "no latest block: no head, no peers, no further calls" \ + '{"version":1,"state":"syncing","awaitingTarget":true}' "$OUT" +check "no latest block: calls" "2" "$(printf '%s\n' "$CALLS" | grep -c .)" + +STUB_BLOCK=$(block 15000000 "$NOW") +STUB_SYNCING='{"startingBlock":"0x0","currentBlock":"0x1","highestBlock":"0x1"}' +run_status +validate "no stages" +check "no stages: plain syncing" "{\"version\":1,\"state\":\"syncing\",\"height\":15000000,\"headTime\":$NOW,\"peers\":25}" "$OUT" + +STUB_SYNCING='{"startingBlock":"0x0","currentBlock":"0x1","highestBlock":"0x1","stages":[]}' +run_status +check "empty stages: plain syncing" "null null" "$(field .phase) $(field .awaitingTarget)" + +STUB_PEERS=null +STUB_SYNCING=false +run_status +validate "no peer count" +check "no peer count: peers left out" "null 15000000" "$(field .peers) $(field .height)" +check "stdout carries nothing else" "1" "$LINES" + +echo "entrypoint snapshot marker" +# The marker functions of the entrypoint, without running it +eval "$(sed -n '/^write_snapshot_marker() {/,/^}/p; /^follow_snapshot_download() {/,/^}/p; /^download_snapshot() {/,/^}/p' "$ENTRYPOINT")" +SNAPSHOT_MARKER="$NODE_STATUS_DIR/snapshot-download" +ESC=$(printf '\033') +LINE_COLOR="${ESC}[2m2026-11-30T10:00:00.000000Z${ESC}[0m ${ESC}[32m INFO${ESC}[0m Downloading snapshot archives ${ESC}[3marchives${ESC}[0m${ESC}[2m=${ESC}[0m3/40 ${ESC}[3mprogress${ESC}[0m${ESC}[2m=${ESC}[0m12.3% ${ESC}[3melapsed${ESC}[0m${ESC}[2m=${ESC}[0m1h 2m ${ESC}[3meta${ESC}[0m${ESC}[2m=${ESC}[0m3h 4m ${ESC}[3mbytes${ESC}[0m${ESC}[2m=${ESC}[0m1.00 GB/8.00 GB" +LINE_PLAIN="2026-11-30T12:00:00.000000Z INFO Extracting snapshot archives archives=39/40 progress=97.5% elapsed=5h 1m eta=2m 3s bytes=1.20 TB/1.23 TB" +printf '%s\n%s\n' "$LINE_COLOR" "other line" >"$TMP/download1.log" +OUT=$(follow_snapshot_download 4242 <"$TMP/download1.log") +check "follow: output passed through unchanged" "$(cat "$TMP/download1.log")" "$OUT" +check "follow: marker from a colored line" "pid=4242 phase=Downloading snapshot progress=12.3" "$(tr '\n' ' ' <"$SNAPSHOT_MARKER" | sed 's/ $//')" +printf '%s\n' "$LINE_PLAIN" | follow_snapshot_download 4242 >/dev/null +check "follow: marker from a plain line" "pid=4242 phase=Extracting snapshot progress=97.5" "$(tr '\n' ' ' <"$SNAPSHOT_MARKER" | sed 's/ $//')" +printf '%s\n' "x INFO Verifying snapshot archives archives=40/40 bytes=1 GB" | follow_snapshot_download 4242 >/dev/null +check "follow: line without progress" "pid=4242 phase=Verifying snapshot progress=" "$(tr '\n' ' ' <"$SNAPSHOT_MARKER" | sed 's/ $//')" + +# download_snapshot with a stand-in for reth download that fails after a progress line +# shellcheck disable=SC2329 # called by download_snapshot +reth() { + printf '%s\n' "$LINE_PLAIN" + sleep 1 + cp "$SNAPSHOT_MARKER" "$TMP/marker_during_download" + echo "error: connection reset" >&2 + return 3 +} +rm -f "$SNAPSHOT_MARKER" +# shellcheck disable=SC2034 # read by download_snapshot +SNAPSHOT_MODE_FLAG=--full NETWORK=mainnet DATA_DIR="$TMP/data" +OUT=$(download_snapshot) +RC=$? +check "download: exit status of reth download" "3" "$RC" +check "download: output and errors passed through" "$(printf '%s\n%s' "$LINE_PLAIN" "error: connection reset")" "$OUT" +check "download: marker while running" "phase=Extracting snapshot progress=97.5" "$(sed -n '2,3p' "$TMP/marker_during_download" | tr '\n' ' ' | sed 's/ $//')" +check "download: marker and fifo removed at the end" "" "$(ls "$NODE_STATUS_DIR")" + +echo "$PASS passed, $FAIL failed" +[ "$FAIL" -eq 0 ] From 900b6bfebcbc3f1ebf3dca3dbedd6036110cb5a2 Mon Sep 17 00:00:00 2001 From: 3alpha <15694175+3alpha@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:31:49 +0100 Subject: [PATCH 2/2] ci: limit the test workflow token to read access --- .github/workflows/node-status-test.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/node-status-test.yml b/.github/workflows/node-status-test.yml index 3738efc..8465b12 100644 --- a/.github/workflows/node-status-test.yml +++ b/.github/workflows/node-status-test.yml @@ -3,6 +3,9 @@ name: Node status test on: pull_request: +permissions: + contents: read + jobs: node-status: runs-on: ubuntu-latest