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
set -e: The script exits immediately if any command exits with a non-zero status.Installs required packages using Alpine Linux package manager (
apk):bash: Shell interpreter.curl: For downloading files.jq: JSON processor.wget: Network downloader.zstd: Compression tool.tar: Archive utility.pv: Pipe viewer (progress monitor).aria2: Download utility supporting multiple protocols.
Enables debug mode (
set -x) if the environment variableDEBUGis set to"true".
Variables
DATA_DIR=/data
CHAINDATA_DIR=$DATA_DIR/bor/chaindata
DATA_DIR: Base directory for data storage.CHAINDATA_DIR: Directory specifically for Bor's chaindata (blockchain data).
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
Checks if the environment variable
SNAPSHOTis set (non-empty) and if the chaindata directory does not exist.If true:
Deletes any existing Bor data directory to start fresh.
Creates the chaindata directory.
Downloads the snapshot from the URL in
$SNAPSHOT.Pipes the downloaded snapshot into a shell script with specific arguments:
--network mainnet: Uses mainnet configuration.--client bor: Specifies the client.--extract-dir $CHAINDATA_DIR: Directory to extract snapshot data.--validate-checksum true: Validates data integrity.
**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
Copies the Bor genesis file (the initial blockchain state) into the data directory.
This file configures the blockchain parameters and is essential for node startup.
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:**
--chain: Path to genesis configuration.--syncmode=full: Full synchronization mode (process all blocks).--datadir: Directory for all node data.--bootnodes: List of peer nodes to bootstrap the network connection.--maxpeers=150: Maximum number of peers to connect.HTTP RPC server options:
Enabled (
--http)Bind address
0.0.0.0(all interfaces)APIs exposed:
eth, net, web3, debug, txpool, borCORS and vhosts allowed from any origin.
WebSocket options:
Enabled (
--ws)Bind address
0.0.0.0Same APIs as HTTP.
Origins allowed from anywhere.
--txlookuplimit=0: Unlimited transaction lookup.--cache=8192: Memory cache size (8192 MB).--nat=none: Disables NAT traversal.Runs the Bor node in the background and captures its PID.
**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
}
Sends the
SIGINTsignal (kill -2) to the Bor node process.Waits in a loop until the process exits.
**Usage:** Called automatically on termination signals.
Signal Handling
trap 'stop' TERM INT
start
wait $PID
Sets up a trap to handle
TERMandINTsignals (termination and interrupt).When such a signal is received, it invokes the
stopfunction to gracefully shutdown the Bor node.Starts the Bor node by calling
start.Waits for the Bor process to end.
Interaction with Other System Components
Polygon Snapshot Service: Downloads blockchain snapshots from
https://snapshot.polygon.technology/to speed up node syncing.Bor Client: The script launches the Bor client binary (
bor server), which is the core blockchain node executable.Filesystem: Uses
/datadirectory for persistent storage of chain data and configuration.Genesis File: Copies a predefined genesis file from
/var/lib/bor/genesis-mainnet-v1.json, which must be present in the image or host system.Networking: Connects to bootnodes in the Polygon network to join the P2P network and exposes RPC and WebSocket endpoints.
Important Implementation Details
The script supports a
SNAPSHOTenvironment variable to conditionally restore blockchain state, avoiding full sync from genesis.Runs the Bor client with extensive RPC APIs enabled for full node operation and developer interaction.
Uses background process management with PID tracking for clean start/stop.
Installs all dependencies at runtime, assuming an Alpine Linux base environment.
Debug mode can be enabled with
DEBUG=trueto trace script execution.
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.