readiness.sh


Overview

`readiness.sh` is a shell script designed as a **readiness probe** for a Bitcoin (or Bitcoin-like) blockchain node daemon running inside a containerized environment (e.g., Kubernetes). Its primary purpose is to verify whether the node is sufficiently synchronized with the blockchain network and actively connected to peers, thus determining if it is "ready" to serve API requests or participate in further processing.

Key functionalities include:

This script enables Kubernetes to manage pod lifecycle intelligently by routing traffic only to nodes that are ready and healthy.


Detailed Explanation of Script Components

Constants and Variables

Logic Flow

  1. Disable Check

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

    If the disable file exists, the script immediately exits with status 0 (success), indicating readiness is bypassed.

  2. RPC Queries

    Fetches blockchain data via JSON-RPC calls to the local Bitcoin node:

    CONNECTION_COUNT=$(curl -sf -H 'content-type: application/json' -u user:password \
      -d '{ "jsonrpc": "2.0", "id": "probe", "method": "getconnectioncount", "params": [] }' \
      http://localhost:8332) || exit 1
    
    BLOCKCHAIN_INFO=$(curl -sf -H 'content-type: application/json' -u user:password \
      -d '{ "jsonrpc": "2.0", "id": "probe", "method": "getblockchaininfo", "params": [] }' \
      http://localhost:8332) || exit 1
    
    • getconnectioncount returns the number of active P2P connections.

    • getblockchaininfo returns detailed blockchain status, including block height and header count.

    If either `curl` fails (e.g., node down or unreachable), the script exits with `1` indicating failure.

  3. Parsing JSON Responses

    Extracts relevant information from the JSON responses using jq:

    PEERS=$(echo $CONNECTION_COUNT | jq -r '.result')
    NODE_LATEST_BLOCK_HEIGHT=$(echo $BLOCKCHAIN_INFO | jq -r '.result.blocks')
    NETWORK_LATEST_BLOCK_HEIGHT=$(echo $BLOCKCHAIN_INFO | jq -r '.result.headers')
    
    • PEERS: Number of connected peers.

    • NODE_LATEST_BLOCK_HEIGHT: Local node's current block height.

    • NETWORK_LATEST_BLOCK_HEIGHT: Latest known block header on network.

  4. Synchronization Check with Tolerance

    Defines nominal acceptable block height considering tolerance:

    NOMINAL_BLOCKS=$(( $NETWORK_LATEST_BLOCK_HEIGHT - $TOLERANCE ))
    

    The node's block height must be greater than or equal to this value to be considered synced.

  5. Decision Logic

    if (( $NODE_LATEST_BLOCK_HEIGHT >= $NOMINAL_BLOCKS )); then
      if (( $PEERS > 0 )); then
        echo "node is synced with $PEERS peers"
        exit 0
      fi
    
      echo "node is synced, but has no peers"
      exit 1
    fi
    
    echo "node is still syncing"
    exit 1
    
    • If synced and has peers → readiness success (exit 0).

    • If synced but zero peers → readiness failure (exit 1).

    • If not synced → readiness failure (exit 1).


Parameters

This script does not accept command-line parameters; however, it relies on internal constants:

Parameter

Description

Type

Default

`DISABLE_READINESS_PROBE`

Path to disable readiness probe sentinel file

String

`/data/disable_readiness`

`TOLERANCE`

Allowed block height lag between node and network

Integer

`1`

These can be customized by editing the script or mounting files into the container.


Return Values / Exit Codes

Kubernetes interprets these codes to manage pod readiness state.


Usage Example

In Kubernetes Pod spec, this script is typically configured as a readiness probe command. For example:

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

When Kubernetes executes this script:


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram: Flowchart of readiness.sh Logic

flowchart TD
    Start[Start readiness.sh script] --> CheckDisable{Disable File Present?}
    CheckDisable -- Yes --> Disabled[Print "readiness probe disabled"\nExit 0]
    CheckDisable -- No --> QueryConnections[Query getconnectioncount RPC]
    QueryConnections --> QueryBlockchainInfo[Query getblockchaininfo RPC]
    QueryBlockchainInfo --> ParseData[Parse JSON for Peers & Block Heights]
    ParseData --> CalcNominal[Calculate Nominal Block Height\n(NETWORK - TOLERANCE)]
    CalcNominal --> CheckSync{Is NODE_BLOCK >= NOMINAL_BLOCKS?}
    CheckSync -- No --> Syncing[Print "node is still syncing"\nExit 1]
    CheckSync -- Yes --> CheckPeers{Are PEERS > 0?}
    CheckPeers -- Yes --> Ready[Print "node is synced with PEERS peers"\nExit 0]
    CheckPeers -- No --> NoPeers[Print "node is synced, but has no peers"\nExit 1]

Summary

`readiness.sh` is a lightweight, efficient readiness probe script tailored for Bitcoin-compatible blockchain nodes. By validating both blockchain sync status within a tolerance and active peer connectivity, it ensures that only nodes ready to serve valid and timely blockchain data receive traffic. Its integration with Kubernetes probe mechanisms supports automated, reliable container orchestration and fault recovery.

This script exemplifies a straightforward yet robust approach to blockchain node health management within containerized environments.


End of Document