Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/node-status-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Node status test

on:
pull_request:

permissions:
contents: read

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
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion dappnode_package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"architectures": [
"linux/amd64"
Expand All @@ -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"
Expand All @@ -38,7 +49,7 @@
{
"arg": "STAKER_SCRIPTS_VERSION",
"repo": "dappnode/staker-package-scripts",
"version": "v0.1.2"
"version": "v0.1.3"
}
]
}
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: {}
5 changes: 5 additions & 0 deletions package_variants/hoodi/dappnode_package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"links": {
"api": "http://hoodi-reth.dappnode:8545",
Expand All @@ -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"
}
5 changes: 5 additions & 0 deletions package_variants/mainnet/dappnode_package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"links": {
"api": "http://reth.dappnode:8545",
Expand All @@ -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"
}
5 changes: 5 additions & 0 deletions package_variants/sepolia/dappnode_package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"links": {
"api": "http://sepolia-reth.dappnode:8545",
Expand All @@ -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"
}
6 changes: 4 additions & 2 deletions reth/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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} \
Expand Down
62 changes: 55 additions & 7 deletions reth/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
181 changes: 181 additions & 0 deletions reth/node-status.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
$(printf '%s\n' "$syncing" | analyze_stages)
EOF

if [ "$awaiting" = true ]; then
if [ "$rpc_ok" = true ] && headers_downloading; then
set -- "$@" phase="Downloading block headers"
else
set -- "$@" awaitingTarget=true
fi
fi

# shellcheck disable=SC2086 # $head holds two key=value words
ns_emit syncing $head peers="$peers" "$@"
exit $?
Loading
Loading