readiness.sh


Overview

`readiness.sh` is a Bash script designed to serve as a readiness probe for a blockchain node, specifically for a Bitcoin-like node exposing JSON-RPC on localhost port 8332. The primary purpose of this script is to check if the node is ready to serve requests by verifying:

The script exits with status code `0` when the node is ready and `1` when it is not, making it suitable for integration into container orchestration platforms like Kubernetes as a readiness probe.


Detailed Explanation

Constants and Variables


Script Workflow

  1. Check for readiness disable file
    If the file /data/disable_readiness exists, the script prints "readiness probe disabled" and exits with status 0. This allows manual or external disabling of readiness checks.

  2. Retrieve connection count
    Uses curl with JSON-RPC to call the getconnectioncount method on the local node. If the call fails, the script exits with status 1 (not ready).

  3. Retrieve blockchain info
    Uses curl with JSON-RPC to call the getblockchaininfo method on the local node. On failure, exits with status 1.

  4. Parse JSON values using jq
    Extracts:

    • Number of peers (.result from getconnectioncount).

    • Node’s current block height (blocks field).

    • Network's latest block height (headers field).

  5. Calculate readiness condition
    Determines the nominal block height threshold by subtracting the tolerance from the network latest block height.

  6. Evaluate readiness

    • If the node’s block height is at least the nominal value:

      • If peers > 0 → print "node is synced with X peers" and exit 0.

      • Else → print "node is synced, but has no peers" and exit 1.

    • Else (node behind nominal blocks) → print "node is still syncing" and exit 1.


Implementation Details


Usage Example

This script can be used as a Kubernetes readiness probe in a Pod spec:

readinessProbe:
  exec:
    command: ["/bin/bash", "/path/to/readiness.sh"]
  initialDelaySeconds: 10
  periodSeconds: 15

When the probe exits with `0`, Kubernetes considers the container ready to serve traffic.


Interactions with Other System Components


Summary

Step

Description

Exit Code

Disable file present

Skip readiness check

0

JSON-RPC `getconnectioncount` fails

Node unreachable or down

1

JSON-RPC `getblockchaininfo` fails

Node unreachable or down

1

Node synced & peers > 0

Node ready

0

Node synced & peers == 0

Node ready but isolated

1

Node syncing (behind network)

Node not ready

1


Mermaid Flowchart Diagram

flowchart TD
    A[Start: readiness.sh script] --> B{Is /data/disable_readiness file present?}
    B -- Yes --> C[Print "readiness probe disabled" and exit 0]
    B -- No --> D[Call getconnectioncount via JSON-RPC]
    D -->|Success| E[Call getblockchaininfo via JSON-RPC]
    D -->|Fail| F[Exit 1]
    E -->|Success| G[Parse peers, node block height, network block height]
    E -->|Fail| F
    G --> H[Calculate nominal blocks = network_latest - tolerance]
    H --> I{Is node_latest_block_height >= nominal_blocks?}
    I -- No --> J[Print "node is still syncing" and exit 1]
    I -- Yes --> K{Are peers > 0?}
    K -- Yes --> L[Print "node is synced with X peers" and exit 0]
    K -- No --> M[Print "node is synced, but has no peers" and exit 1]

Summary

This script provides a simple yet effective readiness check for a Bitcoin-like node by combining network connection status and blockchain sync status. It is easily configurable via the sentinel file and can be integrated into deployment environments to ensure traffic is only directed to fully ready nodes.


End of documentation for readiness.sh