init.sh


Overview

`init.sh` is a Bash shell script designed to manage the lifecycle of a Dogecoin daemon process (`dogecoind`). Its primary purpose is to start the Dogecoin node with specific runtime parameters and ensure proper shutdown handling upon receiving termination signals. The script encapsulates the startup and graceful termination logic, making it suitable for initializing Dogecoin nodes in containerized environments or as part of automated deployment workflows.


Detailed Explanation

Script Behavior and Flow

  1. Set Bash Options

    set -e
    
    • Causes the script to exit immediately if any command exits with a non-zero status, ensuring early failure detection.

  2. Function: start_coin

    start_coin() {
      dogecoind \
        -port=22556 \
        -rpcuser=user \
        -rpcpassword=password \
        -rpcallowip=0.0.0.0/0 \
        -rpcbind=0.0.0.0 \
        -datadir=/data \
        -printtoconsole=1 \
        -server=1 \
        -nolisten=1 \
        -txindex=1 \
        -disablewallet=1 \
        -zmqpubhashtx=tcp://127.0.0.1:28332 \
        -zmqpubhashblock=tcp://127.0.0.1:28332 \
        -rpcworkqueue=1100 \
        -rpcport=8332 \
        -maxmempool=2000 \
        -dbcache=4000 &
      PID="$!"
    }
    
    • Purpose: Starts the Dogecoin daemon (dogecoind) with a predefined set of parameters.

    • Parameters passed to dogecoind:

      • -port=22556: Listen port for P2P connections (default Dogecoin port).

      • -rpcuser=user and -rpcpassword=password: Basic authentication for RPC interface.

      • -rpcallowip=0.0.0.0/0: Allow RPC connections from any IP.

      • -rpcbind=0.0.0.0: Bind RPC server on all network interfaces.

      • -datadir=/data: Use /data directory for blockchain data.

      • -printtoconsole=1: Print logs to console (stdout).

      • -server=1: Enable server mode for RPC commands.

      • -nolisten=1: Do not accept P2P connections (node is non-listening).

      • -txindex=1: Maintain an index of all transactions, enabling richer queries.

      • -disablewallet=1: Disable wallet functionality (node is running in a wallet-less mode).

      • -zmqpubhashtx and -zmqpubhashblock: Publish transaction and block hashes over ZMQ at TCP port 28332.

      • -rpcworkqueue=1100: Set RPC work queue size.

      • -rpcport=8332: Listen RPC server on port 8332.

      • -maxmempool=2000: Set max mempool size to 2000 MB.

      • -dbcache=4000: Set database cache size to 4000 MB.

    • Execution: Runs the command in the background (&), and captures the background process PID in variable PID.

  3. Function: 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 Dogecoin daemon process.

    • Behavior:

      • Prints a message indicating the signal catch and targeted PID.

      • Sends a SIGTERM signal to the process with kill $PID.

      • Enters a loop to check if the process is still alive using kill -0 $PID. This does not send a signal but checks for process existence.

      • Sleeps 1 second between checks until the process exits, ensuring a clean shutdown.

  4. Signal Trap Setup

    trap 'stop_coin' TERM INT
    
    • Sets up signal handlers for TERM and INT signals.

    • When the script receives termination (SIGTERM) or interrupt (SIGINT) signals, it runs the stop_coin function to stop the Dogecoin daemon gracefully.

  5. Main Execution Flow

    start_coin
    wait $PID
    
    • Calls start_coin to launch the Dogecoin daemon.

    • wait $PID suspends the script execution until the Dogecoin process terminates, ensuring the script stays alive as long as the daemon is running.


Usage Examples


Important Implementation Details


Interaction with Other System Components


Diagram: Workflow of init.sh

flowchart TD
    A[Start Script] --> B[start_coin function]
    B --> C[dogecoind process starts in background]
    C --> D[PID stored in $PID]
    D --> E[wait $PID - script waits for dogecoind to finish]

    subgraph Signal Handling
        F[Receive SIGTERM or SIGINT] --> G[trap calls stop_coin]
        G --> H[stop_coin sends SIGTERM to $PID]
        H --> I[Loop until dogecoind process exits]
        I --> J[dogecoind terminates]
        J --> E
    end

Summary

This script is a lightweight, robust entrypoint utility designed to start and manage a Dogecoin node daemon with a specific set of configurations. It ensures proper startup, exposes interfaces for RPC and ZeroMQ notification, and handles termination signals gracefully to avoid data corruption or orphaned processes. It is particularly suited for containerized environments where controlled start/stop of the node process is necessary.


End of Documentation for init.sh