init.sh
Overview
`init.sh` is a shell script designed to initialize and manage the lifecycle of an `op-node` process, which is a node client for the Optimism Layer 2 Ethereum scaling solution. The script handles the installation of necessary dependencies, starts the `op-node` with specified network and RPC configurations, and gracefully stops the node upon receiving termination signals.
Its primary purpose is to automate the setup and controlled execution of the `op-node` to ensure it runs reliably with appropriate parameters and can be cleanly shut down when required.
Functionality Summary
Installs required packages:
bash,curl, andjq.Defines two main functions:
start: Launches theop-nodeprocess with network and RPC settings.stop: Handles termination signals, sends kill signals to the running node, and waits for its exit.
Sets traps to catch termination (
TERM) and interrupt (INT) signals and triggers thestopfunction.Starts the
op-nodein the background and waits for its termination.
Detailed Explanation of Script Components
Package Installation
apk add bash curl jq
Uses Alpine Linux package manager
apkto install:bash: Bourne Again Shell, providing enhanced scripting capabilities.curl: Command-line tool for transferring data with URLs, useful for network requests.jq: A lightweight and flexible command-line JSON processor.
**Note:** These dependencies are likely needed by the node process or for potential future extensions of this script.
Function: start
start() {
op-node \
--network op-mainnet \
--rpc.addr 0.0.0.0 \
--rpc.port 9545 \
--l1 $L1_RPC_ENDPOINT \
--l1.beacon $L1_BEACON_ENDPOINT \
--l1.trustrpc \
--l1.rpckind debug_geth \
--l2 http://localhost:8551 \
--l2.jwt-secret /jwt.hex &
PID="$!"
}
**Purpose:** Starts the `op-node` process with specific configuration parameters required for connecting to Layer 1 (L1) and Layer 2 (L2) Ethereum components.
**Parameters used:**
--network op-mainnet
Specifies the network environment as Optimism Mainnet.--rpc.addr 0.0.0.0
Binds the RPC service to all network interfaces.--rpc.port 9545
Sets the RPC port to9545, where clients can communicate with this node.--l1 $L1_RPC_ENDPOINT
Environment variable specifying the L1 Ethereum RPC endpoint URL.--l1.beacon $L1_BEACON_ENDPOINT
Environment variable specifying the L1 beacon chain endpoint URL.--l1.trustrpc
Enables trusted RPC calls to L1, assumes the L1 endpoint is trustworthy.--l1.rpckind debug_geth
Indicates the type of L1 RPC node; here,debug_gethrefers to a Geth node with debug capabilities.--l2 http://localhost:8551
Specifies the local L2 execution engine endpoint.--l2.jwt-secret /jwt.hex
Points to the JWT secret file for authenticated communication with the L2 endpoint.
**Behavior:**
The node process is started in the background (
&).The process ID (
PID) of the started node is captured for later management.
**Example usage:**
Assuming environment variables are set:
export L1_RPC_ENDPOINT="https://mainnet.infura.io/v3/your_project_id"
export L1_BEACON_ENDPOINT="https://beacon-chain-endpoint"
./init.sh
The script will start the `op-node` with these settings.
Function: stop
stop() {
echo "Catching signal and sending to PID: $PID" && kill $PID
while $(kill -0 $PID 2>/dev/null); do sleep 1; done
}
**Purpose:** Gracefully stops the running `op-node` process upon receiving termination or interrupt signals.
**Details:**
Prints a message indicating the signal caught and the PID targeted for termination.
Sends a termination signal (
kill $PID) to theop-nodeprocess.Enters a loop that checks if the process is still alive (
kill -0 $PIDchecks for process existence without sending a signal).Waits (
sleep 1) until the process exits, ensuring clean shutdown before the script exits.
Signal Trapping and Script Execution Flow
trap 'stop' TERM INT
start
wait $PID
trap 'stop' TERM INT
Sets up signal handlers so that when the script receivesSIGTERMorSIGINT(e.g., Ctrl+C), it invokes thestopfunction to terminate theop-node.start
Calls thestartfunction to launch the node.wait $PID
Causes the script to wait for the backgroundop-nodeprocess to finish before exiting. This keeps the script running to maintain the node process lifecycle.
Important Implementation Details
Error Handling:
The script starts withset -e, which causes the script to exit immediately if any command fails, preventing further execution in error states.Background Process Management:
By runningop-nodein the background and capturing its PID, the script can control and monitor the node process lifecycle.Graceful Shutdown:
Signal trapping ensures that the node can perform cleanup and shutdown procedures rather than being abruptly killed.Environment Variables:
The script depends on the environment variablesL1_RPC_ENDPOINTandL1_BEACON_ENDPOINTto specify external endpoints, making it flexible to different deployment environments.
Interaction with Other System Components
op-nodeexecutable:
This script is a wrapper to start and manage theop-nodebinary, which connects to Ethereum L1 and L2 nodes.Ethereum Layer 1 and Layer 2 nodes:
The script expects connection endpoints for L1 and L2 nodes to be provided via environment variables and local addresses.JWT Secret File (
/jwt.hex):
Used for authenticating with the L2 execution engine; this file must be provisioned externally.System Init or Container Environment:
This script is suitable as an entrypoint in containerized environments (e.g., Docker) to reliably launch the node and handle shutdown signals.
Usage Example
export L1_RPC_ENDPOINT="https://mainnet.infura.io/v3/your_project_id"
export L1_BEACON_ENDPOINT="https://beacon-chain-mainnet.example.com"
./init.sh
This will install dependencies (if needed), start the `op-node`, and keep it running until interrupted.
Mermaid Flowchart Diagram
flowchart TD
A[Start Script] --> B[Install Dependencies: bash, curl, jq]
B --> C[Define Functions: start(), stop()]
C --> D[Set Trap for SIGTERM and SIGINT -> stop()]
D --> E[start() launches op-node in background & saves PID]
E --> F[wait for op-node process ($PID) to exit]
F -->|Signal received| G[stop() sends kill signal to $PID]
G --> H[Wait for op-node to terminate]
H --> I[Script exits gracefully]
Summary
The `init.sh` script is a lightweight, robust utility for initializing and managing the lifecycle of the `op-node` process in an Optimism Layer 2 Ethereum environment. It automates dependency installation, configures the node with necessary endpoints, and ensures clean startup and shutdown aligned with system signals. This makes it particularly useful for containerized deployments or automated node management workflows.