readiness.sh


Overview

`readiness.sh` is a Bash script designed to serve as a readiness probe for a blockchain node daemon—specifically, an Avalanche C-Chain node. The script determines if the node is ready to serve traffic by checking its synchronization status and network connectivity. It performs several checks including:

Based on these checks, the script exits with appropriate status codes indicating readiness (exit 0) or unavailability (exit 1).


Detailed Explanation

Variables

Variable

Description

`DISABLE_READINESS_PROBE`

Path to a file that, if present, disables readiness checking (`/data/disable_readiness`).

`BLOCK_HEIGHT_TOLERANCE`

Allowed difference in block height between the node and reference nodes (default: 5).


Script Workflow and Commands

  1. Disable readiness probe check

    if [[ -f "$DISABLE_READINESS_PROBE" ]]; then
      echo "readiness probe disabled"
      exit 0
    fi
    
    • If the file /data/disable_readiness exists, the script prints a message and immediately exits with success.

    • This allows manual override to disable readiness checks.

  2. Source environment variables

    source /evm.sh
    
    • Sources external script /evm.sh which presumably sets necessary environment variables or functions (e.g., get_best_reference_block_number and reference_validation).

    • Note: The definitions of these functions are external and expected to be available.

  3. Retrieve syncing status and peers info

    ETH_SYNCING=$(curl -sf -d '{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}' -H 'Content-Type:application/json;' http://localhost:9650/ext/bc/C/rpc) || exit 1
    INFO_PEERS=$(curl -sf -d '{"jsonrpc":"2.0","method":"info.peers","params":[],"id":1}' -H 'Content-Type:application/json;' http://localhost:9650/ext/info) || exit 1
    
    • Uses curl to send JSON-RPC requests to local Avalanche node endpoints.

    • eth_syncing returns syncing status (false if fully synced).

    • info.peers returns info about connected peers, including peer count.

  4. Parse results

    SYNCING=$(echo $ETH_SYNCING | jq -r '.result')
    NUM_PEERS=$(echo $INFO_PEERS | jq -r '.result.numPeers')
    
    • Parses JSON responses using jq to extract:

      • SYNCING: boolean or syncing info.

      • NUM_PEERS: number of connected peers.

  5. Logic based on syncing and peer status

    • If the node is not syncing (SYNCING == false) and has peers:

      • Fetch current block number from the node:

        eth_blockNumber=$(curl -sf -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' -H 'Content-Type: application/json' http://localhost:9650/ext/bc/C/rpc) || exit 1
        current_block_number_hex=$(echo $eth_blockNumber | jq -r '.result')
        current_block_number=$(($current_block_number_hex))
        
        • Block number is returned as a hexadecimal string and converted to decimal.

      • Fetch the highest block number from trusted public Avalanche C-Chain RPC endpoints:

        best_reference_block_number=$(get_best_reference_block_number https://api.avax.network/ext/bc/C/rpc https://avalanche-c-chain.publicnode.com https://avalanche.public-rpc.com)
        
        • get_best_reference_block_number is an external function expected to query the given RPCs and return the highest block height.

      • Validate the node's block height against the best reference with tolerance:

        reference_validation daemon $current_block_number $best_reference_block_number $BLOCK_HEIGHT_TOLERANCE
        
        • reference_validation checks if the node's block number is within BLOCK_HEIGHT_TOLERANCE of the reference block number.

        • Exits with failure if validation fails.

      • If all checks pass, print readiness success:

        echo "daemon is synced, with $NUM_PEERS peers"
        exit 0
        
    • If the node is synced but has no peers:

      echo "daemon is synced, but has no peers"
      exit 1
      
    • If still syncing:

      echo "daemon is still syncing"
      exit 1
      

Important Implementation Details


Interaction with Other System Components


Usage Example

This script is usually run by the container orchestration platform as a readiness probe:

./readiness.sh

A manual override can be done by creating the disable file:

touch /data/disable_readiness
./readiness.sh
# Output: readiness probe disabled
# Exit code: 0

Mermaid Flowchart Diagram

flowchart TD
    A[Start] --> B{File /data/disable_readiness exists?}
    B -- Yes --> C[Print "readiness probe disabled"]
    C --> D[Exit 0]
    B -- No --> E[Source /evm.sh]
    E --> F[Query eth_syncing via JSON-RPC]
    F --> G[Query info.peers via JSON-RPC]
    G --> H[Parse SYNCING and NUM_PEERS]
    H --> I{SYNCING == false?}
    I -- No --> J[Print "daemon is still syncing"]
    J --> K[Exit 1]
    I -- Yes --> L{NUM_PEERS > 0?}
    L -- No --> M[Print "daemon is synced, but has no peers"]
    M --> K
    L -- Yes --> N[Query eth_blockNumber]
    N --> O[Convert block number hex to decimal]
    O --> P[get_best_reference_block_number from public RPCs]
    P --> Q[reference_validation (local vs reference block number)]
    Q --> R[Print "daemon is synced, with NUM_PEERS peers"]
    R --> D

Summary

The `readiness.sh` script is a critical health check utility that verifies an Avalanche C-Chain node's readiness by combining syncing status, peer connectivity, and block height validation against trusted sources. It is designed for use in production environments to ensure that only healthy and synchronized nodes are marked ready for service. The script is modular and extensible by sourcing external functions from `/evm.sh`, allowing for maintainability and flexibility.