This repo contains some utility functions in shell script to help with the correct usage and configuration of any execution or consensus dappnode staker package
Consensus tools:
Shell script including a set of handy functions to be used in the entrypoint of both beacon-chain and validator services of all consensus client packages
Execution tools:
Shell script including a set of handy functions to be used in the entrypoint of any execution package
- Add script to
/etc/profile.dand load it in the entrypoint script:
ADD https://raw.githubusercontent.com/dappnode/staker-package-scripts/${STAKER_SCRIPTS_VERSION}/consensus-tools.sh /etc/profile.d/consensus-tools.sh
#!/bin/sh
. /etc/profilenode_status_tools.sh holds the shared helpers for a package's Docker healthcheck script that prints a node status report: one compact JSON line, the last line of stdout, describing what the node is doing (state, phase, progress, blocks, peers, issues). Each package keeps its client-specific logic in its own script; these functions only call the node's JSON-RPC or HTTP API on 127.0.0.1 with short timeouts, read Prometheus metrics and print a report that always respects the size and field limits. They are POSIX sh (busybox ash, dash), depend only on curl and jq (1.6 or later), define functions only and print nothing on failure.
ADD ${STAKER_SCRIPTS_URL}/node_status_tools.sh /etc/profile.d/
Environment: NS_RPC_PORT is the JSON-RPC port used by ns_rpc (default 8545), NS_TIMEOUT the timeout in seconds of every request, also used as connect timeout (default 3).
| Function | Arguments | Output (stdout) | Exit status |
|---|---|---|---|
ns_rpc |
<method> [params_json] (default []) |
The JSON result, compact (false and null are valid results) |
0 ok, 1 no answer / not JSON / error in the reply, 2 invalid arguments |
ns_http_get |
<url> starting with http://127.0.0.1 or http://localhost |
The body | 0 ok, 1 request failed or status not 2xx, 2 URL not local |
ns_metric |
<name> [label=value ...], Prometheus text on stdin |
Value of the first sample of name carrying all given labels, as a plain number (2.4E7 gives 24000000) |
0 found, 1 absent or NaN/Inf, 2 no name |
ns_hex_to_dec |
<0x… or decimal> |
Decimal integer, up to 2^63-1 | 0 ok, 1 anything else |
ns_json_get |
<json> [jq_filter] (default .) |
jq -r output (compact for objects), nothing when null or absent |
0 ok, non-zero for empty or invalid JSON or a failing filter |
ns_block_head |
none | height=<n> headTime=<n> of the latest block, to pass unquoted to ns_emit |
0 ok, 1 no answer or no block |
ns_emit |
<state> [key=value ...] |
The report as one JSON line | 0 for starting, syncing, synced; 1 for error; 2 invalid state (nothing printed) |
ns_emit states are starting, syncing, synced and error. Keys: phase, progress, height, headTime, position, target, unit, peers, awaitingTarget (only true is emitted), background, plus repeatable issue=<text> and detail=<Label>:<Value>. It adds "version":1, drops empty, invalid and unknown values (unknown keys are reported on stderr), accepts hex quantities for height, headTime, position, target and peers, rounds progress to one decimal within 0-99.9 (0-100 when synced), cuts phase and background to 60 characters, unit to 20, issues to 120 (at most 5) and detail labels and values to 40 and 120 (at most 8), and drops details, then issues, until the line fits in 2,048 bytes. When a key is given twice the last value wins. End the script with ns_emit ...; exit $?.
Example only, the healthcheck script of an execution client (the metric name is illustrative):
#!/bin/sh
. /etc/profile.d/node_status_tools.sh
# The JSON-RPC does not answer yet: the client is still starting
if ! syncing=$(ns_rpc eth_syncing); then
ns_emit starting phase="Opening database"
exit $?
fi
peers=$(ns_json_get "$(ns_rpc net_peerCount)" .)
metrics=$(ns_http_get http://127.0.0.1:6060/debug/metrics/prometheus)
txindex=$(printf '%s\n' "$metrics" | ns_metric txindex_progress)
# eth_syncing is false once the client follows the head
if [ "$syncing" = "false" ]; then
background=""
[ -n "$txindex" ] && background="Indexing transactions"
ns_emit synced peers="$peers" background="$background"
exit $?
fi
current=$(ns_json_get "$syncing" .currentBlock)
highest=$(ns_json_get "$syncing" .highestBlock)
awaiting=""
[ "$highest" = "0x0" ] && awaiting=true
ns_emit syncing phase="Downloading blocks" position="$current" target="$highest" \
peers="$peers" awaitingTarget="$awaiting" detail="Highest block:$(ns_hex_to_dec "$highest")"
exit $?Run the tests with sh test/node_status_tools_test.sh.