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:

This script is intended to be run in a container or server environment where the Base rollup node is deployed.


Detailed Explanation

Variables


Installation and Preparation

apt update && apt install -y wget zstd curl jq

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

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

**Functionality:**

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

**Usage Example:**

stop

Will terminate the running node process cleanly.


Signal Handling and Main Execution Flow

trap 'stop' TERM INT
start
wait $PID

Implementation Details and Algorithms


Interaction with Other Parts of the System


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:


*End of Documentation*