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:**
Calls the
fullnode.shscript located in/scriptswith the argumentsthornode startto start the Thornode blockchain daemon.Passes several command-line flags:
--p2p.laddr=tcp://0.0.0.0:27146: Binds the P2P network listener to all network interfaces on port 27146.--proxy_app=tcp://127.0.0.1:27148: Sets the proxy app address for the Tendermint consensus engine communication.--rpc.laddr=tcp://0.0.0.0:27147: Binds the RPC server to all interfaces on port 27147.
Runs the node process in the background (
&).Captures the daemon’s process ID (PID) in the variable
PIDfor later use.
**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:**
Prints a message indicating that it is sending a termination signal to the Thornode process.
Sends a
SIGTERM(default signal forkill) to the daemon process using its PID.Enters a loop where it periodically checks if the process is still running (
kill -0tests for process existence without sending a signal).Sleeps 1 second between checks until the process has exited, ensuring a graceful shutdown before the script exits.
**Usage Example:**
stop_coin
echo "Thornode process stopped."
Signal Handling
trap 'stop_coin' TERM INT
Sets up signal traps for
SIGTERMandSIGINT(termination and interrupt signals).Upon receiving these signals, the
stop_coinfunction is invoked to cleanly stop the daemon process.This is crucial for container environments where Kubernetes or Docker sends termination signals to stop containers gracefully.
Script Execution Flow
start_coin
wait $PID
Starts the Thornode daemon using
start_coin.Uses
wait $PIDto block the script execution until the daemon process exits.This keeps the container running as long as the daemon is active.
When a termination signal is received, the trap calls
stop_coin, which kills the daemon and exits the script, allowing container shutdown.
Important Implementation Details
Background Process Management:
The daemon is started in the background so the script can monitor it asynchronously. Capturing the PID allows precise control over the process lifecycle.Signal Trapping:
The use oftrapis critical for ensuring the daemon receives termination signals, allowing it to close open connections, flush buffers, and save state properly.Buffer Size Environment Variables:
These exported variables are likely specific to the Tendermint RPC implementation used by Thornode, allowing tuning of WebSocket communication buffers to handle event subscription loads effectively.Port Bindings:
The script exposes the P2P networking, RPC, and proxy app interfaces on specific ports bound to all interfaces (0.0.0.0), enabling external access and inter-container communication within a cluster.
Interaction With Other System Components
/scripts/fullnode.sh:
This script relies on the helper scriptfullnode.shto actually invoke the Thornode daemon executable with all necessary chain-specific configurations.init.shacts as a wrapper controlling its lifecycle.Container Orchestration (Kubernetes/Docker):
The script is designed to run inside a containerized environment where lifecycle signals (SIGTERM,SIGINT) are sent to facilitate shutdowns.Thornode Blockchain Node:
The daemon started is the Thornode full node, which participates in the blockchain network by handling P2P communication, RPC requests, and Tendermint consensus proxying.Monitoring and Health Checks:
External monitoring tools or Kubernetes probes can monitor the exposed RPC ports or the container’s running status, withinit.shensuring the daemon remains running or shuts down cleanly on demand.
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.