init.sh


Overview

`init.sh` is a shell script designed to manage the lifecycle of the Thornode blockchain daemon process within a container or server environment. Its primary purpose is to start the Thornode daemon with specific configuration flags, handle termination signals gracefully, and ensure clean shutdown of the daemon process. This script acts as the entrypoint for the Thornode node container, maintaining the daemon's uptime and facilitating reliable integration with container orchestration systems like Kubernetes.


Detailed Explanation

Environment Setup

At the start, the script sets strict error handling to immediately exit on any command failure:

set -e

It then exports two environment variables that configure buffer sizes for the Tendermint RPC experimental WebSocket subscriptions:

export THOR_TENDERMINT_RPC_EXPERIMENTAL_SUBSCRIPTION_BUFFER_SIZE=5000
export THOR_TENDERMINT_RPC_EXPERIMENTAL_WEBSOCKET_WRITE_BUFFER_SIZE=5000

These variables tune the capacity of internal buffers related to RPC communication, potentially optimizing throughput or preventing message drops in high-load scenarios.


Functions

start_coin

start_coin() {
  /scripts/fullnode.sh thornode start \
    --p2p.laddr=tcp://0.0.0.0:27146 \
    --proxy_app=tcp://127.0.0.1:27148 \
    --rpc.laddr=tcp://0.0.0.0:27147 &
  PID="$!"
}

**Purpose:** Launches the Thornode full node daemon process as a background job.

**Parameters:** None.

**Behavior:**

**Usage Example:**

start_coin
echo "Thornode started with PID: $PID"

stop_coin

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

**Purpose:** Gracefully stops the Thornode daemon process triggered by termination signals.

**Parameters:** None.

**Behavior:**

**Usage Example:**

stop_coin
echo "Thornode process stopped."

Signal Handling

trap 'stop_coin' TERM INT

Script Execution Flow

start_coin
wait $PID

Important Implementation Details


Interaction With Other System Components


Visual Diagram: Workflow of init.sh

flowchart TD
    Start[Script Start] --> SetEnv[Set Environment Variables]
    SetEnv --> StartDaemon[start_coin()]
    StartDaemon --> CapturePID[Capture Thornode PID]
    CapturePID --> WaitProcess[wait $PID]
    WaitProcess -->|SIGTERM or SIGINT| SignalTrap[trap signal]
    SignalTrap --> StopDaemon[stop_coin()]
    StopDaemon --> SendKill[Send SIGTERM to PID]
    SendKill --> CheckProcess[Check if PID still running]
    CheckProcess -->|Yes| Sleep1s[Sleep 1 second]
    Sleep1s --> CheckProcess
    CheckProcess -->|No| Exit[Exit Script]

Summary

The `init.sh` script is a specialized utility for orchestrating the Thornode blockchain daemon lifecycle inside a container. It ensures the node starts with correct network configurations, remains running until externally terminated, and shuts down gracefully on signals. This management helps maintain node health, prevents data corruption, and integrates smoothly with container orchestration systems, making it a critical part of the Daemon Node Management subsystem for Thornode in the broader multi-blockchain platform.


Example Usage in a Container

#!/bin/sh
# Typical container entrypoint usage
exec /init.sh

This delegates container process management to `init.sh`, which in turn manages the Thornode daemon process lifecycle robustly.


End of Documentation for init.sh