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, typically Kubernetes. Its primary purpose is to verify if the node is **fully synchronized** with the blockchain network and has **active peer connections** before signaling that the node is ready to serve traffic. This check is crucial to prevent routing client requests to a node that is still syncing or disconnected from the network.

The script performs the following key functions:

This script is optimized for minimal dependencies, using standard tools (`curl`, `jq`) and simple logic to integrate seamlessly as a Kubernetes readiness probe.


Detailed Explanation

Variables and Constants

Variable

Description

DISABLE_READINESS_PROBE

Path to the sentinel file (`/data/disable_readiness`) that disables readiness checks if present.

`TOLERANCE`

Numeric tolerance for block height difference allowed between the node and network (set to 1).

`CONNECTION_COUNT`

JSON-RPC response for the number of connected peers.

BLOCKCHAIN_INFO

JSON-RPC response containing blockchain sync info (node block height, headers, etc.).

`PEERS`

Extracted number of peers from `CONNECTION_COUNT`.

`NODE_LATEST_BLOCK_HEIGHT`

Node's current blockchain height from [BLOCKCHAIN_INFO](/projects/291/69210).

`NETWORK_LATEST_BLOCK_HEIGHT`

Network's latest block height (header count) from [BLOCKCHAIN_INFO](/projects/291/69210).

`NOMINAL_BLOCKS`

Calculated minimum block height threshold for readiness (`NETWORK_LATEST_BLOCK_HEIGHT - TOLERANCE`).


Script Workflow and Logic

  1. Disable Readiness Check:

    • The script first checks for the existence of the file /data/disable_readiness.

    • If the file exists, the readiness probe is disabled, and the script prints "readiness probe disabled" and exits with 0 (success).

  2. Query Node for Connection Count:

    • The script sends a JSON-RPC request to the local node on port 8332 to invoke the method getconnectioncount.

    • If the request fails, the script immediately exits with 1 (failure).

  3. Query Node for Blockchain Info:

    • The script sends another JSON-RPC request to invoke the method getblockchaininfo.

    • If this request fails, the script exits with 1 (failure).

  4. Parse Responses:

    • Extracts the number of peers from CONNECTION_COUNT.

    • Extracts the node's latest block height and the network's latest block header count from BLOCKCHAIN_INFO.

  5. Calculate Nominal Block Height:

    • The script subtracts the tolerance (TOLERANCE=1) from the network's latest block height to allow a small lag.

  6. Readiness Decision:

    • If the node's block height is greater than or equal to the nominal block height:

      • If the peer count is greater than 0, it prints "node is synced with $PEERS peers" and exits with 0 (ready).

      • Else, it prints "node is synced, but has no peers" and exits with 1 (not ready).

    • If the node's block height is less than the nominal block height, it prints "node is still syncing" and exits with 1 (not ready).


Usage Example

This script is typically invoked by Kubernetes as a readiness probe configured in the container spec:

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

**Manual execution** for debugging:

chmod +x readiness.sh
./readiness.sh

Possible outputs:

readiness probe disabled
node is synced with 8 peers
node is synced, but has no peers
node is still syncing

Exit code `0` indicates readiness; exit code `1` indicates not ready.


Important Implementation Details


Interaction with Other System Components


Mermaid Diagram — Script Workflow

flowchart TD
    Start[Start readiness.sh] --> CheckDisable{Is /data/disable_readiness \nfile present?}
    CheckDisable -- Yes --> Disabled[Print "readiness probe disabled"\nExit 0]
    CheckDisable -- No --> GetConnections[Query RPC: getconnectioncount]
    GetConnections -->|Success| GetBlockchainInfo[Query RPC: getblockchaininfo]
    GetConnections -->|Fail| ExitFail1[Exit 1]
    GetBlockchainInfo -->|Success| ParseData[Parse peers, blocks, headers]
    GetBlockchainInfo -->|Fail| ExitFail2[Exit 1]
    ParseData --> CalcNominal[Calculate nominal block height\nNETWORK_LATEST_BLOCK_HEIGHT - TOLERANCE]
    CalcNominal --> CheckSync{NODE_LATEST_BLOCK_HEIGHT >= NOMINAL_BLOCKS?}
    CheckSync -- No --> Syncing[Print "node is still syncing"\nExit 1]
    CheckSync -- Yes --> CheckPeers{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

The `readiness.sh` script is a critical component ensuring that a Bitcoin-like blockchain node signals readiness only when it is fully synchronized and connected to peers. By implementing a simple yet robust check using node RPC endpoints, it integrates tightly with Kubernetes health checking mechanisms to maintain system stability and prevent premature traffic routing to unsynced or isolated nodes.


End of Documentation for readiness.sh