readiness.sh


Overview

The `readiness.sh` script is a **readiness probe** designed to monitor the synchronization and connectivity status of a Tendermint-based blockchain node daemon. This script is intended to be executed periodically by Kubernetes to determine if the daemon is fully synchronized, connected to peers, and ready to serve requests.

By performing multi-layered checks — including local node sync status, peer connectivity, and cross-validation against external reference nodes — the script ensures that traffic is only routed to healthy, fully synced blockchain nodes. This protects the system from premature traffic routing to lagging or disconnected nodes, enhancing overall stability and reliability.


Detailed Explanation

Script Purpose

Key Functional Sections


1. Disable Readiness Probe Check

DISABLE_READINESS_PROBE=/root/disable_readiness

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

2. Environment Setup

source /tendermint.sh
BLOCK_HEIGHT_TOLERANCE=5

3. Local Node Status Queries

SYNCING=$(curl -sf http://localhost:1317/cosmos/base/tendermint/v1beta1/syncing) || exit 1
NET_INFO=$(curl -sf http://localhost:27147/net_info) || exit 1
STATUS=$(curl -sf http://localhost:27147/status) || exit 1

4. Parsing Status Values

IS_SYNCING=$(echo $SYNCING | jq -r '.syncing')
CATCHING_UP=$(echo $STATUS | jq -r '.result.sync_info.catching_up')
NUM_PEERS=$(echo $NET_INFO | jq -r '.result.n_peers')

5. External Reference Nodes Setup

status_curls=(
  "curl -sf -m 3 https://rpc.ninerealms.com/status"
  "curl -sf -m 3 -H \"Referer: https://app.thorswap.finance\" https://rpc.thorswap.net/status"
)

6. Readiness Decision Logic

if [[ $IS_SYNCING == false && $CATCHING_UP == false ]]; then
  if (( $NUM_PEERS > 0 )); then
    latest_block_height=$(echo $STATUS | jq -r '.result.sync_info.latest_block_height')
    best_reference_block_height=$(get_best_reference_block_height_eval "${status_curls[@]}")

    reference_validation $latest_block_height $best_reference_block_height $BLOCK_HEIGHT_TOLERANCE

    echo "daemon is synced with $NUM_PEERS peers"
    exit 0
  fi

  echo "daemon is synced, but has no peers"
  exit 1
fi

echo "daemon is still syncing"
exit 1

Functions (from sourced /tendermint.sh)

The script sources `/tendermint.sh` which provides the following key functions:

These functions encapsulate the logic for external reference validation, critical for avoiding false readiness signals due to local node misreporting or network delays.


Usage Example

The script is designed to be used as a Kubernetes readiness probe command:

readinessProbe:
  exec:
    command:
    - /bin/bash
    - /path/to/readiness.sh
  initialDelaySeconds: 10
  periodSeconds: 15
  failureThreshold: 3

On invocation, the script performs all checks and exits with appropriate status codes to inform Kubernetes whether the node is ready.


Important Implementation Details


Interaction with Other System Components


Mermaid Flowchart: Readiness Probe Workflow

flowchart TD
  A[Kubernetes Probe Triggered] --> B{Disable File Present?}
  B -- Yes --> C[Exit 0: Probe Disabled]
  B -- No --> D[Query Local Syncing Status]
  D --> E[Query Local Peer Info]
  E --> F[Query Local Node Status]
  F --> G{Is Node Synced?}
  G -- No --> H[Exit 1: Node Syncing]
  G -- Yes --> I{Peers > 0?}
  I -- No --> J[Exit 1: No Peers]
  I -- Yes --> K[Get Latest Local Block Height]
  K --> L[Query External Reference Nodes]
  L --> M[Get Best Reference Block Height]
  M --> N[Validate Local Height vs Reference]
  N -- Fail --> O[Exit 1: Validation Failed]
  N -- Pass --> P[Exit 0: Node Ready]

Summary

`readiness.sh` is a robust readiness probe script customized for Tendermint blockchain nodes. It combines local sync checks, peer connectivity validation, and cross-node block height verification using external references to confidently determine node readiness. This script is critical in Kubernetes environments to prevent routing traffic to out-of-sync or isolated nodes, thereby maintaining service stability and integrity.


Appendix: Key Variables and Constants

Name

Description

`DISABLE_READINESS_PROBE`

File path to disable readiness checks when present

`BLOCK_HEIGHT_TOLERANCE`

Maximum allowed block height difference between local and reference nodes (5)

`SYNCING`

JSON response indicating if node is syncing

`NET_INFO`

JSON response containing network and peer info

`STATUS`

General node status containing sync info

`IS_SYNCING`

Boolean extracted from `SYNCING`

`CATCHING_UP`

Boolean extracted from `STATUS` indicating catch-up state

`NUM_PEERS`

Number of connected peers

`status_curls`

Array of curl commands for querying external reference nodes


If you require further details on the sourced utility functions in `/tendermint.sh` or related probe scripts for other chains, please refer to their respective documentation.