init.sh

Overview

`init.sh` is a shell script designed to initialize and run a Bor node, a blockchain client used in the Polygon network ecosystem. The script handles environment preparation, optionally downloads and extracts a blockchain snapshot for fast synchronization, sets up required data directories and configuration files, and finally launches the Bor node with specified runtime parameters. It also manages graceful shutdown on termination signals.

This script ensures that the Bor node is running with appropriate dependencies, data, and configuration, facilitating the operation of a Polygon network node either in a fresh or resumed state.


Detailed Breakdown

Environment Setup

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

Variables

DATA_DIR=/data
CHAINDATA_DIR=$DATA_DIR/bor/chaindata

Snapshot Download and Extraction

if [ -n "$SNAPSHOT" ] && [ ! -d "$CHAINDATA_DIR" ]; then
  rm -rf $DATA_DIR/bor;
  mkdir -p $CHAINDATA_DIR;
  curl -L $SNAPSHOT | bash -s -- --network mainnet --client bor --extract-dir $CHAINDATA_DIR --validate-checksum true
fi

**Implementation Note:** This approach leverages Polygon's snapshot system to speed up node synchronization by downloading a pre-synced chain state instead of syncing from genesis.


Genesis File Setup

cp /var/lib/bor/genesis-mainnet-v1.json $DATA_DIR/bor/genesis.json

Functions

start()

Starts the Bor node with specified options.

start() {
  bor server \
    --chain=$DATA_DIR/bor/genesis.json \
    --syncmode=full \
    --datadir=/data \
    --bootnodes="enode://b8f1cc9c5d4403703fbf377116469667d2b1823c0daf16b7250aa576bacf399e42c3930ccfcb02c5df6879565a2b8931335565f0e8d3f8e72385ecf4a4bf160a@3.36.224.80:30303,enode://8729e0c825f3d9cad382555f3e46dcff21af323e89025a0e6312df541f4a9e73abfa562d64906f5e59c51fe6f0501b3e61b07979606c56329c020ed739910759@54.194.245.5:30303" \
    --maxpeers=150 \
    --http \
    --http.addr=0.0.0.0 \
    --http.api="eth,net,web3,debug,txpool,bor" \
    --http.vhosts="*" \
    --http.corsdomain="*" \
    --ws \
    --ws.addr=0.0.0.0 \
    --ws.api="eth,net,web3,debug,txpool,bor" \
    --ws.origins="*" \
    --txlookuplimit=0 \
    --cache=8192 \
    --nat=none &
  PID="$!"
}

**Parameters and Flags:**

**Usage Example:**

start

Starts the Bor node with the above configuration.


stop()

Gracefully stops the Bor node process.

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

**Usage:** Called automatically on termination signals.


Signal Handling

trap 'stop' TERM INT
start
wait $PID

Interaction with Other System Components


Important Implementation Details


Workflow Diagram

Below is a flowchart illustrating the main operational flow of the script:

flowchart TD
    A[Start Script] --> B{Is DEBUG=true?}
    B -- Yes --> C[Enable Shell Debug Mode]
    B -- No --> D[Install Dependencies]
    C --> D
    D --> E{Is SNAPSHOT set and chaindata missing?}
    E -- Yes --> F[Remove old Bor data]
    F --> G[Create chaindata dir]
    G --> H[Download and extract snapshot]
    E -- No --> I[Skip snapshot download]
    H --> J[Copy genesis file]
    I --> J
    J --> K[Start Bor node]
    K --> L[Trap TERM/INT signals]
    L --> M[On signal: Stop Bor node gracefully]
    M --> N[Exit script]

Summary

`init.sh` is a utility script to bootstrap and manage a Polygon Bor blockchain node. It automates environment setup, optionally restores the node state from a snapshot, and launches the node with necessary configurations and network parameters. It also incorporates signal handling to ensure graceful termination. This script is typically used in containerized or automated environments to run a fully functional Polygon node with minimal manual intervention.