init.sh


Overview

`init.sh` is a shell script designed to initialize and run the Heimdall node software on a Unix-like system (typically within a containerized environment). Heimdall is a component of the Polygon blockchain architecture that acts as a consensus and validator layer.

The script automates environment setup, dependency installation, configuration initialization, optional snapshot restoration, and node startup with proper signal handling for graceful shutdowns.


Detailed Explanation

Environment Setup and Dependency Installation

set -e
apk add bash curl jq wget zstd tar pv aria2
[ "$DEBUG" = "true" ] && set -x

Directory Variables

HOME_DIR=/root/.heimdalld
CONFIG_DIR=$HOME_DIR/config

Snapshot Restoration Logic

if [ -n "$SNAPSHOT" ] && [ ! -f "$HOME_DIR/data/priv_validator_state.json" ]; then
  rm -rf $HOME_DIR/data;
  mkdir -p $HOME_DIR/data;
  curl -L $SNAPSHOT | bash -s -- --network mainnet --client heimdall --extract-dir $HOME_DIR/data --validate-checksum true
fi

Configuration Initialization

if [ ! -d "$CONFIG_DIR" ]; then
  heimdalld init --home $HOME_DIR
  cp /var/lib/heimdall/genesis-mainnet-v1.json $CONFIG_DIR/genesis.json
fi

Functions

start()

Starts the Heimdall node with configured options.

start() {
  heimdalld start \
    --home $HOME_DIR \
    --chain mainnet \
    --rpc.laddr tcp://0.0.0.0:26657 \
    --bor_rpc_url http://localhost:8545 \
    --eth_rpc_url $ETH_RPC_URL \
    --p2p.seeds '[email protected]:26656,[email protected]:26656,[email protected]:26656,[email protected]:26656,[email protected]:26656' \
    --rest-server \
    --node "tcp://localhost:26657" &
  PID="$!"
}

**Usage example:**

start
echo "Heimdall started with PID $PID"

stop()

Gracefully stops the Heimdall process.

stop() {
  echo "Catching signal and sending to PID: $PID" && kill $PID
  while $(kill -0 $PID 2>/dev/null); do sleep 1; done
}

Signal Trapping and Main Execution

trap 'stop' TERM INT
start
wait $PID

Important Implementation Details


Interactions with Other System Components


Visual Diagram

flowchart TD
    StartScript([Start Script])

    StartScript --> InstallDeps[Install Dependencies (apk add)]
    InstallDeps --> CheckSnapshot{Is $SNAPSHOT set and \npriv_validator_state.json missing?}
    
    CheckSnapshot -- Yes --> RemoveData[Remove Existing Data Dir]
    RemoveData --> CreateDataDir[Create Data Dir]
    CreateDataDir --> DownloadSnapshot[Download & Extract Snapshot]
    DownloadSnapshot --> CheckConfigDir
    
    CheckSnapshot -- No --> CheckConfigDir{Does $CONFIG_DIR exist?}
    
    CheckConfigDir -- No --> InitConfig[Run heimdalld init]
    InitConfig --> CopyGenesis[Copy genesis-mainnet-v1.json]
    CopyGenesis --> StartHeimdall
    
    CheckConfigDir -- Yes --> StartHeimdall[Start heimdalld Process (Background)]
    
    StartHeimdall --> SetPID[Store PID of heimdalld]
    SetPID --> TrapSignal[Set trap for TERM and INT signals]
    TrapSignal --> WaitPID[Wait for PID exit]
    
    WaitPID -->|On SIGTERM or SIGINT| StopHeimdall[Stop heimdalld gracefully]
    StopHeimdall --> WaitStop[Wait until process ends]
    WaitStop --> EndScript([End])

Summary

`init.sh` is a robust, production-grade initialization and startup script for the Heimdall node in the Polygon blockchain ecosystem. It ensures all prerequisites are installed, optionally restores fast sync snapshots, initializes configuration only once, and manages the node lifecycle with graceful shutdown handling. Its design supports running in containerized environments and integrates tightly with other Polygon components such as Bor and Ethereum RPC nodes.

This script is typically part of a container image's entrypoint or startup routine in a larger orchestrated blockchain infrastructure stack.