readiness.sh


Overview

`readiness.sh` is a shell script implementing a **readiness probe** for an Ethereum-compatible blockchain node daemon (EVM chain). Its primary purpose is to determine if the local blockchain node is sufficiently synchronized and network-connected to be considered "ready" to serve requests. This script is typically executed by Kubernetes as part of container health checks to control traffic routing and load balancing.

The script performs the following key functions:

This ensures that only fully synchronized and network-connected nodes receive traffic, improving reliability and consistency of blockchain services.


Detailed Explanation

Variables and Constants

Name

Purpose

`DISABLE_READINESS_PROBE`

Path to a file (`/data/disable_readiness`) that disables the readiness check when present.

`BLOCK_HEIGHT_TOLERANCE`

Integer tolerance (set to 15) indicating acceptable block height lag compared to references.

External Dependencies


Script Execution Flow

  1. Disable File Check

    if [[ -f "$DISABLE_READINESS_PROBE" ]]; then
      echo "readiness probe disabled"
      exit 0
    fi
    

    If the file /data/disable_readiness exists, the script immediately exits with 0, signaling readiness disabled.

  2. Source Utility Script

    source /evm.sh
    

    This loads shared functions for EVM chain validation.

  3. Constants

    BLOCK_HEIGHT_TOLERANCE=15
    

    This sets the allowed lag in blocks.

  4. Query Syncing Status

    ETH_SYNCING=$(curl -sf -d '{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}' -H 'Content-Type: application/json' http://localhost:8545) || exit 1
    SYNCING=$(echo $ETH_SYNCING | jq -r '.result')
    

    Calls eth_syncing method. If node is syncing, .result will contain syncing details; if fully synced, it returns false.

  5. Query Peer Count

    NET_PEER_COUNT=$(curl -sf -d '{"jsonrpc":"2.0","method":"net_peerCount","params":[],"id":1}' -H 'Content-Type: application/json' http://localhost:8545) || exit 1
    PEER_COUNT_HEX=$(echo $NET_PEER_COUNT | jq -r '.result')
    PEER_COUNT=$(($PEER_COUNT_HEX))
    

    Retrieves number of connected peers as a hex string and converts to integer.

  6. Evaluate Sync and Peer Status

    • If SYNCING is false and there is at least one peer, proceed to check block height.

    • If SYNCING is false but no peers, exit 1 indicating not ready.

    • If still syncing, exit 1.

  7. Obtain Current Block Number

    eth_blockNumber=$(curl -sf -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' -H 'Content-Type: application/json' http://localhost:8545) || exit 1
    current_block_number_hex=$(echo $eth_blockNumber | jq -r '.result')
    current_block_number=$(($current_block_number_hex))
    
  8. Get Best Reference Block Number

    best_reference_block_number=$(get_best_reference_block_number https://polygon-rpc.com https://polygon-bor.publicnode.com https://polygon-mainnet.g.alchemy.com/v2/demo)
    

    Queries multiple external RPC endpoints and returns the highest block number observed.

  9. Validate Local Block Number

    reference_validation daemon $current_block_number $best_reference_block_number $BLOCK_HEIGHT_TOLERANCE
    

    Ensures local block is within tolerance of the highest reference. Exits non-zero if validation fails.

  10. Print Status and Exit

    • On success: echo "daemon is synced, with $PEER_COUNT peers" and exit 0.

    • If no peers: echo "daemon is synced, but has no peers" and exit 1.

    • If syncing: echo "daemon is still syncing" and exit 1.


Important Implementation Details


Interaction with Other System Components


Usage Example

To manually test the readiness probe:

chmod +x readiness.sh
./readiness.sh

In Kubernetes, this script runs automatically at configured intervals.


Mermaid Diagram: Flowchart of readiness.sh

flowchart TD
    Start[Start Script] --> CheckDisable{Disable File Present?}
    CheckDisable -- Yes --> Disabled[Print "readiness probe disabled"\nExit 0]
    CheckDisable -- No --> SourceEVM[Source /evm.sh]
    SourceEVM --> QuerySync[Query eth_syncing]
    QuerySync --> ParseSync[Parse syncing status]
    ParseSync --> QueryPeers[Query net_peerCount]
    QueryPeers --> ParsePeers[Parse peer count]
    ParseSync --> IsSynced{SYNCING == false?}
    IsSynced -- No --> Syncing[Print "daemon is still syncing"\nExit 1]
    IsSynced -- Yes --> HasPeers{PEER_COUNT > 0?}
    HasPeers -- No --> NoPeers[Print "daemon is synced, but has no peers"\nExit 1]
    HasPeers -- Yes --> GetBlockNumber[Query eth_blockNumber]
    GetBlockNumber --> ParseBlockNumber[Parse current block number]
    ParseBlockNumber --> GetRefBlock[Call get_best_reference_block_number]
    GetRefBlock --> ValidateRef[Call reference_validation]
    ValidateRef --> Valid?{Validation passed?}
    Valid? -- No --> RefFail[Exit 1]
    Valid? -- Yes --> Ready[Print "daemon is synced, with PEER_COUNT peers"\nExit 0]

Summary

The `readiness.sh` script is a critical component ensuring that an Ethereum-compatible blockchain node is fully synchronized and network-connected before being considered ready for traffic. It integrates tightly with Kubernetes readiness probes, local node RPC endpoints, and external reference nodes to provide robust validation. The script's design emphasizes fail-fast semantics, configurable tolerance, and operational flexibility via a disable file mechanism.

This approach helps maintain high availability and consistency for blockchain services running in containerized environments.


End of documentation for readiness.sh