liveness.sh


Overview

`liveness.sh` is a Bash script designed to act as a **liveness probe** for a daemon process that interacts with an Ethereum-compatible blockchain node (specifically Avalanche C-Chain RPC). The script checks whether the daemon is actively progressing by monitoring the increase in the blockchain's block number over time.

The script achieves this by querying the current block number from the local RPC endpoint and comparing it to a previously recorded block number stored on disk. If the block number has increased since the last check, the daemon is considered healthy (running). If the block number stagnates or the probe is disabled via a file flag, appropriate exit statuses and messages are returned to indicate the daemon’s health.

This liveness probe can be integrated with container orchestration platforms (e.g., Kubernetes) to automatically restart or alert if the daemon becomes unresponsive.


File Functionality and Workflow

  1. Disable Condition:
    If the file /data/disable_liveness exists, the script immediately exits with status 0 indicating the probe is disabled and reports "liveness probe disabled".

  2. Fetch Current Block Number:
    The script sends a JSON-RPC request to http://localhost:9650/ext/bc/C/rpc to invoke the eth_blockNumber method, which returns the current highest block number on the blockchain as a hex string.

  3. First Run Initialization:
    If the file /data/.block_number does not exist, the script writes the current block number to this file and exits with status 1, signaling that this is the initial setup and no health determination can be made yet.

  4. Block Number Comparison:
    The script reads the previously stored block number, compares it to the current block number, and updates the stored value.

  5. Health Determination:

    • If the current block number is greater than the previous number, the daemon is deemed healthy, and the script exits with status 0.

    • If not, the daemon is considered stalled, and the script exits with status 1.


Detailed Explanation of Script Sections

Variables

Key Commands and Logic


Usage Example

This script is typically executed as part of a container or process liveness check:

./liveness.sh
daemon is running

and exits with status `0`.

daemon is stalled

and exits with status `1`.

liveness probe disabled

and exits with status `0`.


Important Implementation Details


Interaction with Other System Components


Mermaid Flowchart Diagram

flowchart TD
    A[Start liveness.sh] --> B{Is /data/disable_liveness present?}
    B -- Yes --> C[Print "liveness probe disabled" and exit 0]
    B -- No --> D[Send JSON-RPC request to get current block number]
    D --> E{Request successful?}
    E -- No --> F[Exit 1 (unhealthy)]
    E -- Yes --> G[Parse hex block number to decimal]
    G --> H{Does /data/.block_number exist?}
    H -- No --> I[Store current block number and exit 1]
    H -- Yes --> J[Read previous block number from file]
    J --> K[Store current block number to file]
    K --> L{Is current block number > previous?}
    L -- Yes --> M[Print "daemon is running" and exit 0]
    L -- No --> N[Print "daemon is stalled" and exit 1]

Summary

`liveness.sh` is a lightweight, file-backed liveness probe script tailored for a blockchain daemon that monitors the progress of block height via a local JSON-RPC call. It provides a mechanism to detect stalled states and disable probing when necessary, making it suitable for integration in containerized environments to improve system reliability and observability.