Infrastructure Scripts

Infrastructure Scripts in this project encompass shell scripts and utilities that support the operational lifecycle of blockchain node daemons and related Kubernetes services. These scripts are primarily focused on three key areas: Kubernetes probe checks, daemon lifecycle management, and snapshot/restore operations. They provide essential automation and health monitoring capabilities to ensure the reliability, readiness, and consistency of blockchain nodes deployed within Kubernetes environments.


Overview

Infrastructure scripts serve as foundational tools for the smooth running of blockchain node services (daemons) and their integration into Kubernetes-managed clusters. They address the following problems:

These scripts abstract away manual operational steps and embed best practices for stateful blockchain node management on Kubernetes.


Key Functionalities and Workflows

1. Kubernetes Probe Scripts

The probe scripts implement monitoring hooks designed for Kubernetes liveness, readiness, and startup probes. These scripts typically execute simple status checks or wait indefinitely to keep a container alive during startup.


2. Daemon Lifecycle and Initialization (run.sh)

The core daemon bootstrap script (`go/scripts/run.sh`) manages environment configuration, dynamic chain metadata loading, snapshot restoration, and node initialization before launching the blockchain binary.

**Workflow highlights:**

**Excerpt illustrating dynamic chain metadata loading and snapshot handling:**

if [ -n "$CHAIN_JSON" ]; then
  CHAIN_METADATA=$(curl -s $CHAIN_JSON)

  export CHAIN_ID="${CHAIN_ID:-$(echo $CHAIN_METADATA | jq -r .chain_id)}"
  export P2P_SEEDS="${P2P_SEEDS:-$(echo $CHAIN_METADATA | jq -r '.peers.seeds | map(.id+"@"+.address) | join(",")')}"
  export P2P_PERSISTENT_PEERS="${P2P_PERSISTENT_PEERS:-$(echo $CHAIN_METADATA | jq -r '.peers.persistent_peers | map(.id+"@"+.address) | join(",")')}"
  export GENESIS_URL="${GENESIS_URL:-$(echo $CHAIN_METADATA | jq -r '.codebase.genesis.genesis_url? // .genesis.genesis_url? // .genesis?')}"
  export PROJECT_BIN="${PROJECT_BIN:-$(echo $CHAIN_METADATA | jq -r '.codebase.daemon_name? // .daemon_name?')}"
  export PROJECT_DIR="${PROJECT_DIR:-$(echo $CHAIN_METADATA | jq -r '.codebase.node_home? // .node_home?')}"
fi

# Snapshot download and extraction
if [[ -n $SNAPSHOT_QUICKSYNC && ! -f "$PROJECT_ROOT/data/priv_validator_state.json" ]]; then
  SNAPSHOT_PRUNING="${SNAPSHOT_PRUNING:-default}"
  SNAPSHOT_URL=`curl -s $SNAPSHOT_QUICKSYNC | jq -r --arg FILE "$CHAIN_ID-$SNAPSHOT_PRUNING"  'first(.[] | select(.file==$FILE)) | .url'`
  
  # ... determine snapshot format and extract accordingly ...
fi

Interaction with Other System Components


Important Concepts and Design Patterns


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 --> ExecuteDaemon[Execute daemon binary with arguments]
  ExecuteDaemon --> End[Daemon Running]

This documentation outlines the purpose, workflow, and integration of infrastructure scripts within the system. These scripts are vital for ensuring blockchain daemons start reliably, remain healthy under Kubernetes orchestration, and can sync efficiently using snapshots. Their design embraces flexibility, idempotency, and container lifecycle awareness, enabling robust multi-chain deployments.