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


Detailed Explanation of Script Components

Package Installation

apk add bash curl jq

**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:**

**Behavior:**

**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:**


Signal Trapping and Script Execution Flow

trap 'stop' TERM INT
start
wait $PID

Important Implementation Details


Interaction with Other System Components


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.