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
Primary Goal: Detect if the op-node is live and syncing properly by checking its L1 and L2 block numbers.
Mechanism:
Queries the node’s JSON-RPC endpoint for sync status.
Extracts current L1 and L2 block numbers.
Compares these with previously saved block numbers.
If both current block numbers have increased, the node is considered live.
Otherwise, it reports the node as stalled.
Disabling Probe: The presence of
/data/disable_livenessdisables the probe and causes it to exit successfully immediately.
Variables and Files
Variable/File | Description |
|---|---|
Path to file that disables the liveness check when present (`/data/disable_liveness`). | |
Path to file storing last known L1 and L2 block numbers (`/data/.block_number`). | |
JSON string response from the JSON-RPC call to the node's sync status method. | |
Current L1 block number extracted from [SYNC_STATUS](/projects/291/69231). | |
Current L2 (unsafe) block number extracted from [SYNC_STATUS](/projects/291/69231). | |
Last saved L1 block number read from [FILE](/projects/291/69196). | |
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
Check for Liveness Disable File
If
/data/disable_livenessexists, the script prints "liveness probe disabled" and exits with status0(success).
Fetch Synchronization Status
Makes a JSON-RPC POST request to
http://localhost:9545with methodoptimism_syncStatus.Fails silently (
-sf) and exits with status1if the request fails.
Parse Block Numbers
Extracts
current_l1.numberandunsafe_l2.numberfrom the JSON response usingjq.
Initial Setup
If
/data/.block_numberdoes not exist, it creates the file with current block numbers and exits with status1(failure), indicating the probe is not yet ready.
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 exits1(failure).
Important Implementation Details
JSON-RPC Request:
The script usescurlto POST a JSON-RPC request with method "optimism_syncStatus" to the node's HTTP endpoint on port 9545, expecting a structured JSON response with syncing information.Use of
jq:
JSON parsing is done viajqto extract numeric block numbers safely.File-based State Persistence:
The script stores the last seen block numbers in a hidden file/.block_numberto compare progress between invocations.Exit Codes for Liveness Probes:
0indicates success (node is live or probe disabled).1indicates failure (node stalled or initial setup).
Fail-fast on Request Failure:
If the JSON-RPC call fails, the script exits immediately with failure to signal a liveness problem.
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
The orchestration system will periodically run this script.
If it exits with status
0, the node is considered healthy.If it exits with status
1, the node is considered unhealthy and may be restarted.
Interaction with Other System Components
Optimism Node (op-node):
The script depends on the op-node exposing a JSON-RPC HTTP endpoint on localhost port 9545 with the methodoptimism_syncStatus.Persistent Data Storage:
The file/data/.block_numbermust be persisted across probe executions to maintain state.Disable Mechanism:
Presence of/data/disable_livenessallows manual disabling of the probe, likely managed externally (e.g., administrator intervention or automated maintenance).
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.