liveness.sh

Overview

`liveness.sh` is a Bash script designed to serve as a **liveness probe** for an Optimism node (op-node), a component in a blockchain layer-2 scaling solution. The script verifies whether the node is actively processing new blocks by querying its synchronization status through a JSON-RPC endpoint and comparing the current block numbers to previously recorded values. If the node is progressing, the probe signals success; otherwise, it signals failure, indicating a potential stall.

The script also supports a manual override to disable the liveness check by the presence of a specific file.


Detailed Explanation

Purpose and Functionality


Variables and Files

Variable/File

Description

DISABLE_LIVENESS_PROBE

Path to file that disables the liveness check when present (`/data/disable_liveness`).

FILE

Path to file storing last known L1 and L2 block numbers (`/data/.block_number`).

SYNC_STATUS

JSON string response from the JSON-RPC call to the node's sync status method.

CURRENT_L1_BLOCK_NUMBER

Current L1 block number extracted from [SYNC_STATUS](/projects/291/69231).

CURRENT_L2_BLOCK_NUMBER

Current L2 (unsafe) block number extracted from [SYNC_STATUS](/projects/291/69231).

PREVIOUS_L1_BLOCK_NUMBER

Last saved L1 block number read from [FILE](/projects/291/69196).

PREVIOUS_L2_BLOCK_NUMBER

Last saved L2 block number read from [FILE](/projects/291/69196).

`JSON`

JSON string containing current block numbers, saved to [FILE](/projects/291/69196).


Step-by-Step Workflow

  1. Check for Liveness Disable File

  2. Fetch Synchronization Status

    • Makes a JSON-RPC POST request to http://localhost:9545 with method optimism_syncStatus.

    • Fails silently (-sf) and exits with status 1 if the request fails.

  3. Parse Block Numbers

    • Extracts current_l1.number and unsafe_l2.number from the JSON response using jq.

  4. Initial Setup

    • If /data/.block_number does not exist, it creates the file with current block numbers and exits with status 1 (failure), indicating the probe is not yet ready.

  5. Compare Current and Previous Block Numbers

    • Reads previous block numbers from /data/.block_number.

    • Overwrites the file with current block numbers.

    • If both current L1 and L2 block numbers are strictly greater than previous ones, prints "op-node is running" and exits 0 (success).

    • Otherwise, prints "op-node is stalled" and exits 1 (failure).


Important Implementation Details


Usage Example

This script is typically used in Kubernetes or similar container orchestration environments as a **liveness probe** command.

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

Interaction with Other System Components


Mermaid Flowchart Diagram

flowchart TD
    Start([Start])
    CheckDisable{File "/data/disable_liveness" exists?}
    ExitDisabled["Print \"liveness probe disabled\"\nExit 0"]
    FetchSyncStatus["Fetch sync status via JSON-RPC\n(curl POST to localhost:9545)"]
    CurlFail{"curl request success?"}
    ParseBlocks["Extract current L1 and L2 block numbers\nusing jq"]
    CheckFileExists{File "/data/.block_number" exists?}
    WriteFileFirstTime["Write current block numbers to file\nExit 1 (initial setup)"]
    ReadPrevBlocks["Read previous block numbers from file"]
    WriteCurrentBlocks["Overwrite file with current block numbers"]
    CompareBlocks{"Current L1 & L2 > Previous L1 & L2?"}
    ExitSuccess["Print \"op-node is running\"\nExit 0"]
    ExitStalled["Print \"op-node is stalled\"\nExit 1"]

    Start --> CheckDisable
    CheckDisable -- Yes --> ExitDisabled
    CheckDisable -- No --> FetchSyncStatus
    FetchSyncStatus --> CurlFail
    CurlFail -- No --> ExitStalled
    CurlFail -- Yes --> ParseBlocks
    ParseBlocks --> CheckFileExists
    CheckFileExists -- No --> WriteFileFirstTime
    CheckFileExists -- Yes --> ReadPrevBlocks
    ReadPrevBlocks --> WriteCurrentBlocks
    WriteCurrentBlocks --> CompareBlocks
    CompareBlocks -- Yes --> ExitSuccess
    CompareBlocks -- No --> ExitStalled

Summary

`liveness.sh` is a lightweight, robust liveness probe script for monitoring the health of an Optimism blockchain node by verifying block synchronization progress. It uses a combination of JSON-RPC querying, JSON parsing, and persistent state comparison to accurately determine if the node is operating correctly or stalled. The script's design allows easy integration with container orchestration liveness checks and supports manual disabling when needed.