init.sh
Overview
`init.sh` is a Bash initialization script designed to prepare and launch an **op-reth** blockchain node for the Base network (a Layer 2 Ethereum rollup). The script automates environment setup, optionally restores a blockchain snapshot, and manages the lifecycle of the node process with clean startup and shutdown procedures.
Key functionalities include:
Installing necessary system dependencies (
wget,zstd,curl,jq).Downloading and extracting a blockchain snapshot if specified and not already present.
Starting the op-reth node with configured parameters for HTTP and WebSocket RPC endpoints.
Handling termination signals to gracefully stop the node process.
This script is intended to be run in a container or server environment where the Base rollup node is deployed.
Detailed Explanation
Variables
DATA_DIR=/data
Directory where node data and blockchain snapshots are stored.CHAINDATA_DIR=$DATA_DIR/db
Specific subdirectory for the blockchain database.SNAPSHOT(environment variable)
URL of a blockchain snapshot archive to download and restore if the node data directory is empty.
Installation and Preparation
apt update && apt install -y wget zstd curl jq
Updates package lists and installs required command-line utilities:
wget: for downloading files.zstd: for decompression of snapshots compressed in Zstandard format.curl: general purpose transfer tool (not currently used directly here).jq: JSON processor (not currently used here but often useful in scripting blockchain nodes).
Snapshot Restoration Logic
if [[ -n $SNAPSHOT && ! -d "$CHAINDATA_DIR" ]]; then
wget -c $SNAPSHOT -O - | zstd -d | tar -xvf - --strip-components=3 -C $DATA_DIR
fi
Checks if the environment variable
SNAPSHOTis set and if the chain data directory does not exist.Downloads the snapshot archive using
wgetwith resume support.Pipes the downloaded data through
zstddecompressor.Extracts the tar archive to
$DATA_DIR, stripping the top 3 directory levels to place the files correctly.This step restores blockchain data to speed up node synchronization.
Functions
start()
Starts the **op-reth** node with specified runtime options.
start() {
op-reth node \
-vvv \
--datadir $DATA_DIR \
--authrpc.jwtsecret /jwt.hex \
--authrpc.port 8551 \
--http \
--http.addr 0.0.0.0 \
--http.port 8545 \
--http.api eth,net,debug,txpool \
--http.corsdomain "*" \
--ws \
--ws.addr 0.0.0.0 \
--ws.port 8546 \
--ws.api eth,net,debug,txpool \
--ws.origins "*" \
--chain base \
--rollup.disable-tx-pool-gossip \
--rollup.sequencer-http https://mainnet-sequencer.base.org \
--max-outbound-peers=100 &
PID="$!"
}
**Parameters:**
No parameters; uses environment variables and hardcoded options.
**Functionality:**
Runs
op-reth nodewith very verbose logging (-vvv).Specifies the data directory.
Configures authenticated RPC communication with JWT secret located at
/jwt.hex.Enables HTTP and WebSocket RPC endpoints, listening on all interfaces (
0.0.0.0), with standard Ethereum ports (8545 for HTTP, 8546 for WS).Enables APIs:
eth,net,debug,txpool.Configures CORS and WS origins to accept requests from any origin (
*).Sets the chain to
base, indicating the Base rollup network.Disables transaction pool gossip to optimize rollup behavior.
Configures the rollup sequencer HTTP endpoint.
Limits maximum outbound peers to 100 to control network connections.
Runs the node process in the background and records its PID for later control.
**Usage Example:**
start
Starts the node with the above configuration.
stop()
Gracefully stops the running node process.
stop() {
echo "Catching signal and sending to PID: $PID" && kill $PID
while $(kill -0 $PID 2>/dev/null); do sleep 1; done
}
**Functionality:**
Prints a message indicating shutdown signal reception.
Sends
SIGTERMto the node process using its PID.Waits in a loop until the process has exited (
kill -0checks if the process exists).
**Usage Example:**
stop
Will terminate the running node process cleanly.
Signal Handling and Main Execution Flow
trap 'stop' TERM INT
start
wait $PID
Sets up trap handlers for
TERMandINTsignals (e.g.,docker stop, Ctrl+C).On receiving these signals, calls
stop()to cleanly terminate the node.Starts the node process by calling
start().Waits for the node process to exit, keeping the script running as long as the node is active.
Implementation Details and Algorithms
Uses Unix signal trapping (
trap) to intercept termination signals and allow graceful shutdown.Downloads and restores blockchain snapshots using streaming decompression and extraction, which avoids writing the full archive to disk.
Runs the node as a background process, capturing its PID for management.
Configures node networking and RPC endpoints with security and accessibility in mind (e.g., JWT authentication for AuthRPC, CORS allowing all origins for HTTP/WS).
Interaction with Other Parts of the System
Relies on the
op-rethbinary being installed and accessible in the environment.Uses a JWT secret file at
/jwt.hexwhich must be mounted or created externally for securing authenticated RPC calls.The
$SNAPSHOTenvironment variable must be set externally to provide a URL for snapshot restoration.The
$DATA_DIR(/data) directory is expected to be a persistent volume or mounted directory where the blockchain data is stored.Interacts with the Base rollup network via configured sequencer HTTP endpoint.
Provides RPC endpoints that other components (wallets, dApps, monitoring tools) can connect to.
Diagram: Workflow of init.sh
flowchart TD
A[Start Script] --> B[Install Dependencies]
B --> C{Is $SNAPSHOT set and chain data missing?}
C -- Yes --> D[Download & Extract Snapshot]
C -- No --> E[Skip Snapshot]
D --> E
E --> F[Start op-reth Node]
F --> G[Set PID]
G --> H[Wait for Signals]
H -->|TERM or INT| I[Call stop()]
I --> J[Send SIGTERM to PID]
J --> K[Wait for Process to Exit]
K --> L[Exit Script]
Summary
`init.sh` is a robust startup and shutdown helper script for running an **op-reth** node on the Base rollup network. It automates dependency installation, optional snapshot restoration, node startup with comprehensive RPC configuration, and graceful termination handling. It is intended for use in containerized or server environments to simplify node management and ensure reliable operation.
Example Usage
To use this script in a typical deployment:
export SNAPSHOT=https://example.com/base_snapshot.tar.zst
./init.sh
This will:
Install required utilities.
Download and extract the snapshot if needed.
Launch the Base rollup node exposing RPC endpoints.
Handle termination signals to stop the node cleanly.
*End of Documentation*