run.sh


Overview

`run.sh` is a robust shell script designed to bootstrap and launch a blockchain node daemon within a containerized environment, typically Kubernetes. It automates the node initialization, configuration, snapshot restoration, and runtime environment setup based on dynamic chain metadata and environment variables.

This script enables flexible deployment across multiple blockchain networks by dynamically fetching chain-specific parameters, managing peer configurations, handling genesis file setup, and accelerating node sync with snapshots. It also integrates optional features like Polkachu seed nodes and debug support, ensuring a seamless and reliable node lifecycle management.


Detailed Explanation

Script Purpose and Functionality


Implementation Details and Workflow

1. Dynamic Chain Metadata Loading

If the environment variable `CHAIN_JSON` is set, the script fetches the chain metadata JSON and extracts:

Extraction uses `jq` to parse JSON fields, supporting multiple fallback JSON paths to handle different metadata versions/formats.

2. Environment Variables and Paths Setup

3. Chain ID Validation

The script exits immediately if `CHAIN_ID` is missing, as it is essential for node initialization.

4. Configuring Node Settings

5. Snapshot Quick Sync Handling

When `SNAPSHOT_QUICKSYNC` is set and the private validator state file is missing, the script:

6. Polkachu Seed Node Integration

If `P2P_POLKACHU` is enabled:

7. Peer Seeds Environment Setup

Exports peer seeds and persistent peers as environment variables prefixed with the namespace (uppercase daemon name), ensuring proper usage by the daemon.

8. Chain Initialization

If the config directory does not exist:

9. Overwrite Seeds in Config

If `OVERWRITE_SEEDS` equals `1`, forcibly overwrites the seeds and persistent peers in the `config.toml` file to ensure the correct P2P configuration.

10. Preseed Private Validator State

Creates a minimal `priv_validator_state.json` file with initial height, round, and step if it does not exist—this avoids startup errors related to missing validator state.

11. Debugging Information

If `DEBUG` is set to `1`, prints all environment variables for troubleshooting.

12. Final Execution

Executes the blockchain daemon binary with all passed arguments (using `exec`), replacing the shell process with the daemon process.


Parameters and Usage

Environment Variable

Purpose

Default / Notes

`CHAIN_JSON`

URL to a JSON metadata file describing the blockchain network.

Optional; enables dynamic metadata loading

`CHAIN_ID`

Blockchain chain identifier.

Required if `CHAIN_JSON` not set

`P2P_SEEDS`

Comma-separated list of seed peers.

Can be overridden by metadata or Polkachu integration

`P2P_PERSISTENT_PEERS`

Comma-separated list of persistent peers.

Same as above

`GENESIS_URL`

URL to the genesis file.

Can be specified or extracted from metadata

`PROJECT_BIN`

Name of the daemon binary executable.

Extracted from metadata or derived from `PROJECT` variable

`PROJECT_DIR`

Node home directory (usually under `/root`).

Extracted from metadata or defaulted

`MONIKER`

Node moniker/name used in initialization.

Optional

`MAX_NUM_OUTBOUND_PEERS`

Maximum number of outbound peers; modifies config.toml.

Optional

`SNAPSHOT_QUICKSYNC`

URL to snapshot index JSON for fast sync snapshots.

Optional

`SNAPSHOT_PRUNING`

Snapshot pruning strategy name (e.g., [default](/projects/291/69304)).

Defaults to [default](/projects/291/69304)

`SNAPSHOT_FORMAT`

Compression format of snapshot archive (`tar.gz`, `tar.lz4`, `tar.zst`, etc.).

Auto-detected from URL if not set

`SNAPSHOT_WASM_PATH`

Directory path inside snapshot containing WASM files; moved to `../wasm`.

Optional

`P2P_POLKACHU`

Enable Polkachu seed node integration.

Optional

POLKACHU_NETWORK

Polkachu network identifier for fetching seed nodes.

Required if `P2P_POLKACHU` enabled

`OVERWRITE_SEEDS`

If set to `1`, forcibly overwrite seeds in config.toml.

Optional

`DEBUG`

Set to `1` or `2` to enable debug output and shell tracing (`set -x`).

Optional


Usage Example

export CHAIN_JSON="https://example.com/chain-metadata.json"
export MONIKER="mynode"
export SNAPSHOT_QUICKSYNC="https://snapshots.example.com/index.json"
export MAX_NUM_OUTBOUND_PEERS=50
export DEBUG=1

./run.sh start

This example will:


Interaction with Other System Components


Important Implementation Notes


Mermaid Diagram: Flowchart of Daemon Initialization and Startup Process

flowchart TD
  Start[Start Container / Script] --> LoadMetadata{Is CHAIN_JSON set?}
  LoadMetadata -- Yes --> FetchMetadata[Fetch Chain Metadata JSON]
  FetchMetadata --> ExtractConfig[Extract Chain ID, Peers, Genesis URL, etc.]
  LoadMetadata -- No --> SkipMetadata[Skip Metadata Loading]

  ExtractConfig --> CheckConfigDir{Config directory exists?}
  SkipMetadata --> CheckConfigDir

  CheckConfigDir -- No --> InitChain[Run daemon init with moniker and chain ID]
  InitChain --> DownloadGenesis{Is GENESIS_URL set?}
  CheckConfigDir -- Yes --> SkipInit[Skip Chain Initialization]
  SkipInit --> DownloadGenesis

  DownloadGenesis -- Yes --> DownloadAndExtract[Download & extract genesis file]
  DownloadGenesis -- No --> SkipGenesis[Skip Genesis Download]

  DownloadAndExtract --> ConfigurePeers[Set seeds and persistent peers in config.toml]
  SkipGenesis --> ConfigurePeers

  ConfigurePeers --> CheckSnapshot{Is SNAPSHOT_QUICKSYNC set?}
  CheckSnapshot -- Yes --> DownloadSnapshot[Download and extract snapshot]
  CheckSnapshot -- No --> SkipSnapshot[Skip Snapshot]

  DownloadSnapshot --> PreseedPrivVal[Preseed priv_validator_state.json if missing]
  SkipSnapshot --> PreseedPrivVal

  PreseedPrivVal --> PolkachuCheck{Is P2P_POLKACHU enabled?}
  PolkachuCheck -- Yes --> PolkachuFetch[Fetch Polkachu seed nodes and append]
  PolkachuCheck -- No --> SkipPolkachu[Skip Polkachu Integration]

  PolkachuFetch --> ExecuteDaemon[Execute daemon binary with arguments]
  SkipPolkachu --> ExecuteDaemon

  ExecuteDaemon --> End[Daemon Running]

Summary

The `run.sh` script is a critical infrastructure component facilitating automated, flexible, and reliable blockchain node deployment. It abstracts complex initialization workflows, dynamically configures node parameters from metadata, manages fast synchronization via snapshots, and integrates with external services such as Polkachu. Its design emphasizes idempotency, portability, and seamless integration with container orchestration platforms, making it a foundational piece in the system's operational lifecycle.