readiness.sh
Overview
`readiness.sh` is a Bash script designed to serve as a readiness probe for an **Optimism Layer 2 (L2) Ethereum scaling solution node** (`op-node`). Its primary function is to determine whether the node is fully synced and ready to serve traffic or still in the process of synchronizing.
This script is intended to be used in containerized or orchestrated environments (like Kubernetes), where readiness probes dictate whether the node should receive traffic. It performs the following key checks:
Allows disabling the readiness probe using a specific file flag.
Queries the node’s JSON-RPC endpoint to get sync status.
Validates the node's synchronization progress relative to the Ethereum Layer 1 (L1) chain.
Returns appropriate exit codes to signal readiness or non-readiness.
Detailed Explanation
Constants and Variables
DISABLE_READINESS_PROBE=/data/disable_readiness
Path to a file which, if present, disables the readiness check (always returns ready).BLOCK_HEIGHT_TOLERANCE=5
Numeric tolerance for block height difference allowed between L1 reference block and node's current block.SYNC_STATUS
JSON response from the node's sync status RPC call.QUEUED_UNSAFE_L2_HEIGHT
Extracted L2 block height that is still considered "queued unsafe" (not finalized or synced).
Script Workflow and Logic
Check for disable file:
If /data/disable_readiness file exists, print"readiness probe disabled"and exit with code0(ready).Load environment variables and utility functions:
Sources/evm.sh, assumed to contain utility functions (get_best_reference_block_number,reference_validation) and environment variables (e.g.,L1_RPC_ENDPOINT).Query sync status:
Usescurlto send a JSON-RPC request tohttp://localhost:9545with methodoptimism_syncStatusto retrieve sync status JSON.Parse queued unsafe L2 block height:
Extracts.result.queued_unsafe_l2.numberfrom the JSON response usingjq.Evaluate sync status:
If
QUEUED_UNSAFE_L2_HEIGHT == 0, i.e., no blocks are queued unsafe, the node is nearly or fully synced.Retrieves the current L1 block number from
.result.current_l1.number.Calls
get_best_reference_block_numberwith multiple L1 RPC endpoints to determine the best current L1 block number.Calls
reference_validationwithop-node, current L1 block number, best reference block number, and tolerance to validate sync status.If validation passes, echo
"op-node is synced"and exit0.
If queued unsafe blocks exist:
The node is still syncing; echo"op-node is still syncing"and exit with code1.
Functions and External Calls
This script depends on functions and variables defined in `/evm.sh`:
get_best_reference_block_number
Purpose:
Queries multiple L1 RPC endpoints to determine the best (highest) L1 block number available.Parameters:
Accepts one or more L1 RPC endpoint URLs.Returns:
Numeric block number representing the best L1 block height for reference.Usage Example:
best_block=$(get_best_reference_block_number https://endpoint1 https://endpoint2)
reference_validation
Purpose:
Validates whether the node’s current L1 block number is within an acceptable tolerance of the best reference block number.Parameters:
Node identifier string (e.g.,
op-node)Current L1 block number (numeric)
Best reference block number (numeric)
Block height tolerance (numeric)
Returns:
Exit status indicating validation success or failure. May print validation messages.Usage Example:
reference_validation op-node 1234567 1234570 5
Usage Example
To manually run the readiness check, execute:
./readiness.sh
Output
"readiness probe disabled"and exit0if disable file is present.Output
"op-node is synced"and exit0if node is fully synced.Output
"op-node is still syncing"and exit1if node is not ready.
This script is designed to be integrated as a readiness probe script in container orchestrators (e.g., Kubernetes):
readinessProbe:
exec:
command:
- /bin/bash
- /path/to/readiness.sh
initialDelaySeconds: 10
periodSeconds: 30
Implementation Details and Algorithms
JSON-RPC Query:
The script uses a standard JSON-RPC 2.0 call to the local Optimism node endpoint to fetch sync status. This is a RESTful HTTP POST with a JSON payload specifying theoptimism_syncStatusmethod.Parsing JSON with
jq:
Extracts specific fields from the JSON response to determine synchronization progress.Tolerance-based Validation:
The script uses a tolerance parameter to allow a small lag in block height differences between the node's view and the best known L1 reference block. This prevents false negatives due to minor syncing delays.Disable flag:
Presence of a file disables the readiness check, which can be useful during maintenance or debugging.
Interaction with Other Components
/evm.sh:
This script is dependent on/evm.shfor utility functions and environment variables. It is assumed that/evm.shexportsL1_RPC_ENDPOINTand defines functions likeget_best_reference_block_numberandreference_validation.Optimism Node JSON-RPC Endpoint:
Interacts with the local node athttp://localhost:9545using JSON-RPC to fetch sync information.Ethereum L1 RPC Endpoints:
Calls multiple external Ethereum L1 endpoints (e.g.,https://ethereum.publicnode.com,https://eth-mainnet.g.alchemy.com/v2/demo) to fetch the latest block heights for reference.Container Orchestration:
Typically used as a readiness probe script by container runtime or orchestrators to manage pod lifecycle and traffic routing.
Visual Diagram
The following flowchart illustrates the main functional flow of `readiness.sh`:
flowchart TD
A[Start] --> B{Is disable file present?}
B -- Yes --> C[Print "readiness probe disabled"]
C --> D[Exit 0]
B -- No --> E[Source /evm.sh]
E --> F[Query sync status from op-node]
F --> G[Parse queued_unsafe_l2.number]
G --> H{Is queued_unsafe_l2.number == 0?}
H -- No --> I[Print "op-node is still syncing"]
I --> J[Exit 1]
H -- Yes --> K[Get current_l1_block_number]
K --> L[Get best_reference_block_number from L1 endpoints]
L --> M[Call reference_validation with tolerance]
M --> N[Print "op-node is synced"]
N --> D
Summary
`readiness.sh` is a critical utility script for assessing the readiness of an Optimism L2 node by checking sync status via JSON-RPC. It performs smart validation by comparing the node’s sync state with trusted L1 references and includes a disable mechanism for operational flexibility. The script integrates tightly with the environment via `/evm.sh` and external L1 RPC services, making it a robust component in the deployment and orchestration of Optimism nodes.