diff --git a/.github/workflows/node-status-test.yml b/.github/workflows/node-status-test.yml new file mode 100644 index 0000000..520db39 --- /dev/null +++ b/.github/workflows/node-status-test.yml @@ -0,0 +1,25 @@ +name: Node status test + +on: + pull_request: + +permissions: + contents: read + +jobs: + test: + 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 under dash + run: sh test/node_status_test.sh + env: + NODE_STATUS_TOOLS: ${{ github.workspace }}/staker-package-scripts/scripts/node_status_tools.sh + - name: Test under busybox ash (Alpine, like the Geth image) + run: > + docker run --rm -v "$PWD:/work" -w /work alpine:3 sh -c + "apk add -q jq && NODE_STATUS_TOOLS=/work/staker-package-scripts/scripts/node_status_tools.sh sh test/node_status_test.sh" diff --git a/README.md b/README.md index ae1e6d2..2bf2b4f 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ # DAppNode package Geth + +## Node status report + +The package reports Geth's sync phase (header, block and state download, healing, transaction indexing) through its Docker healthcheck, which the dappmanager reads to show the node's status. The healthcheck script is [`geth/node-status.sh`](geth/node-status.sh), installed in the image as `/usr/local/bin/node-status`; it uses the shared helpers from `dappnode/staker-package-scripts`. Its test is [`test/node_status_test.sh`](test/node_status_test.sh): run `sh test/node_status_test.sh` with a clone of `staker-package-scripts` next to this repository, or point `NODE_STATUS_TOOLS` at its `scripts/node_status_tools.sh`. diff --git a/dappnode_package.json b/dappnode_package.json index d36dbfe..516ca69 100644 --- a/dappnode_package.json +++ b/dappnode_package.json @@ -26,6 +26,17 @@ "execution" ], "license": "GPL-3.0", + "nodes": { + "geth": { + "role": "execution", + "probe": { + "kind": "ethereum-execution", + "rpcPort": 8545, + "enginePort": 8551 + }, + "report": "healthcheck" + } + }, "repository": { "type": "git", "url": "https://github.com/dappnode/DAppNodePackage-geth-generic.git" @@ -44,7 +55,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 df9317b..b487e01 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,8 +5,14 @@ services: context: geth args: UPSTREAM_VERSION: v1.17.10 - STAKER_SCRIPTS_VERSION: v0.1.2 + STAKER_SCRIPTS_VERSION: v0.1.3 environment: EXTRA_OPTS: "" SYNCMODE: snap restart: unless-stopped + healthcheck: + test: ["CMD", "/usr/local/bin/node-status"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 60s diff --git a/geth/Dockerfile b/geth/Dockerfile index 41133b0..9a61054 100644 --- a/geth/Dockerfile +++ b/geth/Dockerfile @@ -26,11 +26,14 @@ ENV GETH_HTTP=true \ STAKER_SCRIPTS_URL=https://github.com/dappnode/staker-package-scripts/releases/download/${STAKER_SCRIPTS_VERSION} ADD ${STAKER_SCRIPTS_URL}/execution_tools.sh /etc/profile.d/ +ADD ${STAKER_SCRIPTS_URL}/node_status_tools.sh /etc/profile.d/ COPY entrypoint.sh /usr/local/bin/entrypoint.sh +COPY node-status.sh /usr/local/bin/node-status -RUN apk update && apk add curl && apk cache clean && \ - chmod +rx /usr/local/bin/entrypoint.sh /etc/profile.d/execution_tools.sh +RUN apk update && apk add curl jq && apk cache clean && \ + 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 # If NETWORK == lukso mkdir /config RUN if [ "${NETWORK}" = "lukso" ]; then \ diff --git a/geth/node-status.sh b/geth/node-status.sh new file mode 100755 index 0000000..7112239 --- /dev/null +++ b/geth/node-status.sh @@ -0,0 +1,146 @@ +#!/bin/sh + +# Node status report of the Geth package, run as the Docker healthcheck. +# Prints one JSON line (see the node status contract) telling which sync phase +# Geth is in and how far along. Whether the node is synced is decided by the +# dappmanager, not here. +# +# Geth snap sync, as of v1.17: +# - Before a consensus client drives it, eth_syncing is an object with +# currentBlock and highestBlock at 0: nothing to sync towards yet. +# - The latest block stays at genesis until the state is complete. Meanwhile +# the chain (headers, bodies, receipts) and the state download run together. +# eth_syncing gives no total for the state; the snap progress metric does +# (v1.17.6+). Healing has no total at all. +# - Snap/2 (only after the Amsterdam fork) replaces healing with a BAL +# catch-up and a trie generation, both measurable from eth_syncing. +# - eth_syncing stays an object after reaching the head until transactions +# (and state history, if enabled) are indexed: background work. +# +# Environment: +# NODE_STATUS_TOOLS: Path of the shared helpers (default /etc/profile.d/node_status_tools.sh) +# GETH_METRICS_PORT: Port of the metrics server, also read by Geth itself (default 6060) +# NS_RPC_PORT, NS_TIMEOUT: See node_status_tools.sh + +# At most four calls, each with its own timeout, and none after the JSON-RPC +# fails, so the script ends well inside the healthcheck timeout even when Geth hangs +NS_TIMEOUT=${NS_TIMEOUT:-2} + +# shellcheck source=/dev/null +. "${NODE_STATUS_TOOLS:-/etc/profile.d/node_status_tools.sh}" || exit 1 + +METRICS_URL="http://127.0.0.1:${GETH_METRICS_PORT:-6060}/debug/metrics/prometheus" + +# Decimal value of a quantity field of the eth_syncing object, 0 when absent +sync_field() { + ns_hex_to_dec "$(ns_json_get "$syncing" ".$1")" 2>/dev/null || echo 0 +} + +# Percentage of $1 over $2 with one decimal, nothing when $2 is not positive +percent() { + awk -v done="$1" -v total="$2" 'BEGIN { if (total > 0) printf "%.1f\n", done * 100 / total }' +} + +# The JSON-RPC does not answer yet: Geth is starting (opening its database) +if ! syncing=$(ns_rpc eth_syncing); then + ns_emit starting + exit $? +fi + +# Latest block and peers, skipped after the first call that fails +head="" +height="" +peers="" +if head=$(ns_block_head); then + height=${head#height=} + height=${height%% *} + peers=$(ns_json_get "$(ns_rpc net_peerCount)" .) || peers="" +else + head="" +fi + +if [ "$syncing" = "false" ]; then + # shellcheck disable=SC2086 # $head holds height= headTime= + ns_emit synced $head peers="$peers" + exit $? +fi + +# Anything but an object tells nothing more +case $syncing in +"{"*) ;; +*) + # shellcheck disable=SC2086 # $head holds height= headTime= + ns_emit syncing $head peers="$peers" + exit $? + ;; +esac + +phase="" +progress="" +awaiting="" +background="" +detail="" + +current=$(sync_field currentBlock) +highest=$(sync_field highestBlock) + +if [ "$current" -eq 0 ] && [ "$highest" -eq 0 ]; then + # Nothing downloaded and no target: waits for the consensus client, or + # fetches the headers it was just given + phase="Downloading block headers" + awaiting=true +elif [ "$(($(sync_field healingTrienodes) + $(sync_field healingBytecode)))" -gt 0 ]; then + phase="Healing state" + pending=$(sync_field healingTrienodes) + [ "$pending" -gt 0 ] && detail="Trie nodes pending:$pending" +elif [ "$height" = "0" ]; then + # Snap sync before the state is complete: the latest block is genesis + synced_lists=$(sync_field syncedAccessLists) + total_lists=$(sync_field totalAccessLists) + trie_gen=$(sync_field trieGenProgress) + accounts=$(sync_field syncedAccounts) + if [ "$total_lists" -gt 0 ] && [ "$synced_lists" -lt "$total_lists" ]; then + phase="Catching up state" + progress=$(percent "$synced_lists" "$total_lists") + elif [ "$trie_gen" -gt 0 ] && [ "$trie_gen" -lt 100 ]; then + phase="Generating state trie" + progress=$trie_gen + else + # Only these two gauges are needed from the (large) metrics page + metrics=$(ns_http_get "$METRICS_URL" | + grep -E '^eth_(protocols_snap_sync_progress|downloader_chain_progress)[ {]') + # Fractions 0-1; both gauges exist from start-up at 0, so 0 means not measured yet + state_fraction=$(printf '%s\n' "$metrics" | ns_metric eth_protocols_snap_sync_progress) + chain_fraction=$(printf '%s\n' "$metrics" | ns_metric eth_downloader_chain_progress) + if awk -v f="${state_fraction:-0}" 'BEGIN { exit !(f > 0) }'; then + phase="Downloading state" + progress=$(percent "$state_fraction" 1) + detail="Accounts downloaded:$accounts" + elif [ "$accounts" -gt 0 ]; then + phase="Downloading state" + detail="Accounts downloaded:$accounts" + elif awk -v f="${chain_fraction:-0}" 'BEGIN { exit !(f > 0) }'; then + phase="Downloading blocks" + progress=$(percent "$chain_fraction" 1) + else + phase="Downloading blocks" + progress=$(percent "$current" "$highest") + fi + fi +fi + +# Work that goes on once the node follows the head +if [ -z "$phase" ]; then + tx_remaining=$(sync_field txIndexRemainingBlocks) + if [ "$tx_remaining" -gt 0 ]; then + background="Indexing transactions" + detail="Blocks left to index:$tx_remaining" + elif [ "$(($(sync_field stateIndexRemaining) + $(sync_field trienodeIndexRemaining)))" -gt 0 ]; then + background="Indexing state history" + fi +fi + +# shellcheck disable=SC2086 # $head holds height= headTime= +ns_emit syncing $head peers="$peers" phase="$phase" progress="$progress" \ + awaitingTarget="$awaiting" background="$background" detail="$detail" +exit $? diff --git a/package_variants/hoodi/dappnode_package.json b/package_variants/hoodi/dappnode_package.json index 60fef59..2b30cf6 100644 --- a/package_variants/hoodi/dappnode_package.json +++ b/package_variants/hoodi/dappnode_package.json @@ -19,5 +19,10 @@ "homepage": "https://github.com/dappnode/DAppNodePackage-geth-generic#readme" }, "name": "hoodi-geth.dnp.dappnode.eth", + "nodes": { + "geth": { + "network": "hoodi" + } + }, "version": "0.1.7" } diff --git a/package_variants/lukso/dappnode_package.json b/package_variants/lukso/dappnode_package.json index 078daa3..5928512 100644 --- a/package_variants/lukso/dappnode_package.json +++ b/package_variants/lukso/dappnode_package.json @@ -19,6 +19,11 @@ "homepage": "https://github.com/dappnode/DAppNodePackage-geth-generic#readme" }, "name": "lukso-geth.dnp.dappnode.eth", + "nodes": { + "geth": { + "network": "lukso" + } + }, "upstream": [ { "arg": "UPSTREAM_VERSION", diff --git a/package_variants/mainnet/dappnode_package.json b/package_variants/mainnet/dappnode_package.json index e93aa6c..6dfd6d1 100644 --- a/package_variants/mainnet/dappnode_package.json +++ b/package_variants/mainnet/dappnode_package.json @@ -19,5 +19,10 @@ "homepage": "https://github.com/dappnode/DAppNodePackage-geth-generic#readme" }, "name": "geth.dnp.dappnode.eth", + "nodes": { + "geth": { + "network": "mainnet" + } + }, "version": "0.1.52" } diff --git a/package_variants/sepolia/dappnode_package.json b/package_variants/sepolia/dappnode_package.json index f0dca5b..a2c3fda 100644 --- a/package_variants/sepolia/dappnode_package.json +++ b/package_variants/sepolia/dappnode_package.json @@ -21,5 +21,10 @@ "homepage": "https://github.com/dappnode/DAppNodePackage-geth-generic#readme" }, "name": "sepolia-geth.dnp.dappnode.eth", + "nodes": { + "geth": { + "network": "sepolia" + } + }, "version": "0.1.7" } diff --git a/test/node_status_test.sh b/test/node_status_test.sh new file mode 100755 index 0000000..4498291 --- /dev/null +++ b/test/node_status_test.sh @@ -0,0 +1,293 @@ +#!/bin/sh + +# Tests for geth/node-status.sh, the healthcheck that prints the node status report +# Run from anywhere with any POSIX sh: sh test/node_status_test.sh +# Needs jq and the shared helpers of dappnode/staker-package-scripts, by default +# from a clone next to this repository; set NODE_STATUS_TOOLS to use another path. +# Geth is stubbed by a curl function that answers per JSON-RPC method. + +cd "$(dirname "$0")/.." || exit 1 +REPO=$PWD +NODE_STATUS_TOOLS=${NODE_STATUS_TOOLS:-$REPO/../staker-package-scripts/scripts/node_status_tools.sh} +export NODE_STATUS_TOOLS +if [ ! -r "$NODE_STATUS_TOOLS" ]; then + echo "Shared helpers not found at $NODE_STATUS_TOOLS (set NODE_STATUS_TOOLS)" >&2 + exit 1 +fi +SCRIPT=$REPO/geth/node-status.sh + +PASS=0 +FAIL=0 +TMP=$(mktemp -d) || exit 1 +trap 'rm -rf "$TMP"' 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 +} + +# Stub curl: records the method (or "metrics") and the URL of each call, then +# answers with R_SYNCING, R_BLOCK or R_PEERS (a JSON-RPC result) or R_METRICS +# (a Prometheus page). An empty reply fails like a refused connection; the +# reply "error" is a JSON-RPC error. +# shellcheck disable=SC2329 # called by the script under test +curl() { + _url="" _body="" _prev="" + for _arg in "$@"; do + [ "$_prev" = "--data" ] && _body=$_arg + [ "$_prev" = "--max-time" ] && printf '%s\n' "$_arg" >>"$TMP/timeouts" + _prev=$_arg + _url=$_arg + done + printf '%s\n' "$_url" >>"$TMP/urls" + case $_url in + */debug/metrics/prometheus) + echo metrics >>"$TMP/calls" + [ -n "$R_METRICS" ] || return 7 + printf '%s\n' "$R_METRICS" + return 0 + ;; + esac + _method=$(printf '%s' "$_body" | jq -r .method) + echo "$_method" >>"$TMP/calls" + case $_method in + eth_syncing) _result=$R_SYNCING ;; + eth_getBlockByNumber) _result=$R_BLOCK ;; + net_peerCount) _result=$R_PEERS ;; + *) _result="" ;; + esac + case $_result in + "") return 7 ;; + error) printf '{"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"boom"}}' ;; + *) printf '{"jsonrpc":"2.0","id":1,"result":%s}' "$_result" ;; + esac +} + +# Replies of a node at the head of mainnet with 40 peers +HEAD_BLOCK='{"number":"0x1312d00","timestamp":"0x6553f100","hash":"0xab"}' +GENESIS_BLOCK='{"number":"0x0","timestamp":"0x0","hash":"0xd4"}' +reset_node() { + R_SYNCING=false + R_BLOCK=$HEAD_BLOCK + R_PEERS='"0x28"' + R_METRICS="" +} + +# Source the script, so that it sees the curl stub; its exit leaves only the subshell it runs in +source_script() { + # shellcheck source=/dev/null + . "$SCRIPT" +} + +# Run the script with the stubbed curl: sets OUT (stdout), RC, CALLS (methods called, comma separated) +run_script() { + : >"$TMP/calls" + : >"$TMP/urls" + : >"$TMP/timeouts" + OUT=$(source_script 2>"$TMP/stderr") + RC=$? + CALLS=$(paste -s -d, "$TMP/calls") +} + +# Prints true when $1 is a status report that respects the contract +report_valid() { + printf '%s' "$1" | jq -r ' + def count: type == "number" and . >= 0 and . == floor; + def text($n): type == "string" and length > 0 and length <= $n; + type == "object" + and .version == 1 + and (.state as $s | ["starting", "syncing", "synced", "error"] | index($s) != null) + and ((keys - ["version", "state", "phase", "progress", "height", "headTime", "position", "target", + "unit", "peers", "awaitingTarget", "background", "issues", "details"]) == []) + and (.phase == null or (.phase | text(60) and (endswith(".") | not))) + and (.progress == null or (.progress | type == "number" and . >= 0) + and .progress <= (if .state == "synced" then 100 else 99.9 end)) + and all(.height, .headTime, .position, .target, .peers; . == null or count) + and (.awaitingTarget == null or .awaitingTarget == true) + and (.background == null or (.background | text(60))) + and (.issues == null or (.issues | length <= 5 and all(.[]; text(120)))) + and (.details == null or (.details | length <= 8 and all(.[]; (.label | text(40)) and (.value | text(120))))) + and ((has("progress") and has("position")) | not) + and (tojson | utf8bytelength <= 2048) + ' 2>/dev/null +} + +# The report must be a single JSON line that respects the contract +check_contract() { + _lines=$(printf '%s\n' "$OUT" | wc -l | tr -d ' ') + check "$1: one line" "1" "$_lines" + check "$1: valid report" "true" "$(report_valid "$OUT")" + check "$1: nothing on stderr" "" "$(cat "$TMP/stderr")" + check "$1: only local calls" "" "$(grep -v '^http://127\.0\.0\.1:' "$TMP/urls")" + check "$1: short timeouts" "" "$(grep -vx 2 "$TMP/timeouts")" +} + +# expect_report +expect_report() { + run_script + check "$1: report" "$2" "$OUT" + check "$1: exit code" "0" "$RC" + check "$1: calls" "$3" "$CALLS" + check_contract "$1" +} + +echo "Contract check" +check "rejects an unknown state" "false" "$(report_valid '{"version":1,"state":"waiting"}')" +check "rejects 100% while syncing" "false" "$(report_valid '{"version":1,"state":"syncing","progress":100}')" +check "rejects an unknown field" "false" "$(report_valid '{"version":1,"state":"synced","percent":3}')" +check "rejects a trailing period" "false" "$(report_valid '{"version":1,"state":"syncing","phase":"Healing state."}')" +check "rejects a hex height" "false" "$(report_valid '{"version":1,"state":"synced","height":"0x1"}')" +check "accepts a full report" "true" "$(report_valid '{"version":1,"state":"syncing","phase":"Healing state","height":1,"peers":2,"details":[{"label":"A","value":"b"}]}')" + +RPC="eth_syncing,eth_getBlockByNumber,net_peerCount" +AT_HEAD='"height":20000000,"headTime":1700000000,"peers":40' +AT_GENESIS='"height":0,"headTime":0,"peers":40' + +echo "Geth not answering" +reset_node +R_SYNCING="" +expect_report "connection refused" '{"version":1,"state":"starting"}' "eth_syncing" +R_SYNCING=error +expect_report "JSON-RPC error" '{"version":1,"state":"starting"}' "eth_syncing" + +echo "Synced" +reset_node +expect_report "eth_syncing false" '{"version":1,"state":"synced",'"$AT_HEAD"'}' "$RPC" +R_PEERS='"0x0"' +expect_report "no peers is not an issue" '{"version":1,"state":"synced","height":20000000,"headTime":1700000000,"peers":0}' "$RPC" +reset_node +R_BLOCK="" +expect_report "stops after the latest block fails" '{"version":1,"state":"synced"}' "eth_syncing,eth_getBlockByNumber" +reset_node +R_PEERS="" +expect_report "peers failing" '{"version":1,"state":"synced","height":20000000,"headTime":1700000000}' "$RPC" +reset_node +R_BLOCK=null +expect_report "no latest block" '{"version":1,"state":"synced"}' "eth_syncing,eth_getBlockByNumber" + +echo "Nothing to sync towards yet" +reset_node +R_BLOCK=$GENESIS_BLOCK +R_SYNCING='{"startingBlock":"0x0","currentBlock":"0x0","highestBlock":"0x0","syncedAccounts":"0x0","healingTrienodes":"0x0","txIndexFinishedBlocks":"0x0","txIndexRemainingBlocks":"0x1","stateIndexRemaining":"0x0","trienodeIndexRemaining":"0x0"}' +expect_report "fresh node without a consensus client" \ + '{"version":1,"state":"syncing","phase":"Downloading block headers",'"$AT_GENESIS"',"awaitingTarget":true}' "$RPC" +R_BLOCK="" +expect_report "fresh node, latest block failing" \ + '{"version":1,"state":"syncing","phase":"Downloading block headers","awaitingTarget":true}' \ + "eth_syncing,eth_getBlockByNumber" + +echo "Downloading state" +reset_node +R_BLOCK=$GENESIS_BLOCK +R_SYNCING='{"startingBlock":"0x0","currentBlock":"0x1312c9c","highestBlock":"0x1312d00","syncedAccounts":"0x100","healingTrienodes":"0x0","healingBytecode":"0x0","txIndexRemainingBlocks":"0x1"}' +R_METRICS='# TYPE eth_downloader_chain_progress gauge +eth_downloader_chain_progress 0.99 +# TYPE eth_protocols_snap_sync_progress gauge +eth_protocols_snap_sync_progress 0.437 +# TYPE eth_protocols_snap_sync_progress_other gauge +eth_protocols_snap_sync_progress_other 0.9' +expect_report "measured by the snap progress metric" \ + '{"version":1,"state":"syncing","phase":"Downloading state","progress":43.7,'"$AT_GENESIS"',"details":[{"label":"Accounts downloaded","value":"256"}]}' \ + "$RPC,metrics" +R_METRICS='eth_protocols_snap_sync_progress 1.0000001e+00 +eth_downloader_chain_progress 1' +expect_report "metric at the end is capped" \ + '{"version":1,"state":"syncing","phase":"Downloading state","progress":99.9,'"$AT_GENESIS"',"details":[{"label":"Accounts downloaded","value":"256"}]}' \ + "$RPC,metrics" +R_METRICS="" +expect_report "without metrics (Geth before v1.17.6, Lukso)" \ + '{"version":1,"state":"syncing","phase":"Downloading state",'"$AT_GENESIS"',"details":[{"label":"Accounts downloaded","value":"256"}]}' \ + "$RPC,metrics" +R_METRICS='eth_protocols_snap_sync_progress 0 +eth_downloader_chain_progress 0' +expect_report "metric not measured yet" \ + '{"version":1,"state":"syncing","phase":"Downloading state",'"$AT_GENESIS"',"details":[{"label":"Accounts downloaded","value":"256"}]}' \ + "$RPC,metrics" + +echo "Downloading blocks" +reset_node +R_BLOCK=$GENESIS_BLOCK +R_SYNCING='{"startingBlock":"0x0","currentBlock":"0x4c4b40","highestBlock":"0x1312d00","syncedAccounts":"0x0","healingTrienodes":"0x0","txIndexRemainingBlocks":"0x1"}' +R_METRICS='eth_protocols_snap_sync_progress 0 +eth_downloader_chain_progress 0.3' +expect_report "measured by the chain progress metric" \ + '{"version":1,"state":"syncing","phase":"Downloading blocks","progress":30,'"$AT_GENESIS"'}' "$RPC,metrics" +R_METRICS='eth_protocols_snap_sync_progress 0 +eth_downloader_chain_progress 0' +expect_report "chain metric not measured yet" \ + '{"version":1,"state":"syncing","phase":"Downloading blocks","progress":25,'"$AT_GENESIS"'}' "$RPC,metrics" +R_METRICS="" +expect_report "without metrics, from current / highest" \ + '{"version":1,"state":"syncing","phase":"Downloading blocks","progress":25,'"$AT_GENESIS"'}' "$RPC,metrics" +R_SYNCING='{"currentBlock":"0x5","highestBlock":"0x0"}' +expect_report "without a target" '{"version":1,"state":"syncing","phase":"Downloading blocks",'"$AT_GENESIS"'}' "$RPC,metrics" + +echo "Healing state" +reset_node +R_BLOCK=$GENESIS_BLOCK +R_SYNCING='{"currentBlock":"0x1312c9c","highestBlock":"0x1312d00","syncedAccounts":"0x100","healingTrienodes":"0x50","healingBytecode":"0x0","txIndexRemainingBlocks":"0x1"}' +expect_report "pending trie nodes" \ + '{"version":1,"state":"syncing","phase":"Healing state",'"$AT_GENESIS"',"details":[{"label":"Trie nodes pending","value":"80"}]}' "$RPC" +R_SYNCING='{"currentBlock":"0x1312c9c","highestBlock":"0x1312d00","healingTrienodes":"0x0","healingBytecode":"0x3"}' +expect_report "pending bytecodes" \ + '{"version":1,"state":"syncing","phase":"Healing state",'"$AT_GENESIS"'}' "$RPC" + +echo "Snap/2 (after the Amsterdam fork)" +reset_node +R_BLOCK=$GENESIS_BLOCK +R_SYNCING='{"currentBlock":"0x1312c9c","highestBlock":"0x1312d00","syncedAccounts":"0x100","syncedAccessLists":"0x0","totalAccessLists":"0x0","trieGenProgress":"0x2a"}' +expect_report "generating the trie" \ + '{"version":1,"state":"syncing","phase":"Generating state trie","progress":42,'"$AT_GENESIS"'}' "$RPC" +R_SYNCING='{"currentBlock":"0x1312c9c","highestBlock":"0x1312d00","syncedAccounts":"0x100","syncedAccessLists":"0x10","totalAccessLists":"0x40","trieGenProgress":"0x0"}' +expect_report "catching up with access lists" \ + '{"version":1,"state":"syncing","phase":"Catching up state","progress":25,'"$AT_GENESIS"'}' "$RPC" +R_SYNCING='{"currentBlock":"0x1312c9c","highestBlock":"0x1312d00","syncedAccounts":"0x100","syncedAccessLists":"0x40","totalAccessLists":"0x40","trieGenProgress":"0x64"}' +R_METRICS='eth_protocols_snap_sync_progress 0.5' +expect_report "finished catch-up and generation fall through" \ + '{"version":1,"state":"syncing","phase":"Downloading state","progress":50,'"$AT_GENESIS"',"details":[{"label":"Accounts downloaded","value":"256"}]}' \ + "$RPC,metrics" + +echo "Background work at the head" +reset_node +R_SYNCING='{"startingBlock":"0x0","currentBlock":"0x1312d00","highestBlock":"0x1312d00","healingTrienodes":"0x0","txIndexFinishedBlocks":"0x100","txIndexRemainingBlocks":"0x10","stateIndexRemaining":"0x0"}' +expect_report "indexing transactions" \ + '{"version":1,"state":"syncing",'"$AT_HEAD"',"background":"Indexing transactions","details":[{"label":"Blocks left to index","value":"16"}]}' "$RPC" +R_SYNCING='{"currentBlock":"0x1312d00","highestBlock":"0x1312d00","txIndexRemainingBlocks":"0x0","stateIndexRemaining":"0x5","trienodeIndexRemaining":"0x0"}' +expect_report "indexing state history" \ + '{"version":1,"state":"syncing",'"$AT_HEAD"',"background":"Indexing state history"}' "$RPC" +R_SYNCING='{"currentBlock":"0x1312d00","highestBlock":"0x1312d00","txIndexRemainingBlocks":"0x0","stateIndexRemaining":"0x0","trienodeIndexRemaining":"0x7"}' +expect_report "indexing trie node history" \ + '{"version":1,"state":"syncing",'"$AT_HEAD"',"background":"Indexing state history"}' "$RPC" + +echo "Nothing more to tell" +reset_node +R_SYNCING='{"startingBlock":"0x1312900","currentBlock":"0x1312a00","highestBlock":"0x1312d00","txIndexRemainingBlocks":"0x0"}' +expect_report "catching up after downtime" '{"version":1,"state":"syncing",'"$AT_HEAD"'}' "$RPC" +R_SYNCING=true +expect_report "eth_syncing not an object" '{"version":1,"state":"syncing",'"$AT_HEAD"'}' "$RPC" +R_SYNCING='{"currentBlock":"0x1312a00","highestBlock":"0x1312d00"}' +R_BLOCK="" +expect_report "latest block failing" '{"version":1,"state":"syncing"}' "eth_syncing,eth_getBlockByNumber" + +echo "Run as a command" +mkdir -p "$TMP/bin" +printf '#!/bin/sh\nexit 7\n' >"$TMP/bin/curl" +chmod +x "$TMP/bin/curl" +OUT=$(PATH="$TMP/bin:$PATH" sh "$SCRIPT" 2>"$TMP/stderr") +RC=$? +check "command: report" '{"version":1,"state":"starting"}' "$OUT" +check "command: exit code" "0" "$RC" +check "command: nothing on stderr" "" "$(cat "$TMP/stderr")" +OUT=$(NODE_STATUS_TOOLS=/nonexistent sh "$SCRIPT" 2>/dev/null) +RC=$? +check "command: missing helpers print nothing" "" "$OUT" +check "command: missing helpers fail" "true" "$([ "$RC" -ne 0 ] && echo true)" + +echo +echo "$PASS passed, $FAIL failed" +[ "$FAIL" -eq 0 ]