main.rs - CLI Interface

The main.rs module serves as the application entry point and command-line interface for Bunkercoin. It orchestrates all system components, handles user commands, and implements the HTTP-based peer synchronization protocol that enables radio-friendly blockchain networking.

CLI Command Structure

// src/main.rs (lines 8-42)
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
    /// Path to local data directory (blockchain & wallet). Defaults to ./data
    #[arg(long, short)]
    data_dir: Option<PathBuf>,
}

#[derive(Subcommand)]
enum Commands {
    /// Generate new wallet keypair
    Keygen,
    /// Show wallet address & balance
    Balance,
    /// Send coins to recipient
    Send {
        /// Recipient public key (hex)
        to: String,
        /// Amount in satoshi-like units
        amount: u64,
    },
    /// Run full node (p2p + optional mining)
    Node {
        /// Enable mining loop (CPU-based)
        #[arg(long)]
        mine: bool,
        /// TCP port to listen on
        #[arg(long, default_value_t = 7000)]
        port: u16,
        /// Bootstrap peer list
        #[arg(long)]
        peers: Option<String>,
    },
}

Design Philosophy: The CLI uses clap's derive macros for type-safe command parsing. Each command encapsulates a complete workflow, from wallet operations to full node deployment, making the system accessible to both developers and operators.

Application Initialization

The main function demonstrates Bunkercoin's resilient design. The data directory is created automatically, and the application gracefully handles missing configuration through sensible defaults.

Command Implementations

Wallet Operations

Node Orchestration

HTTP Peer Synchronization

Synchronization Protocol Breakdown

Step
Operation
Purpose

1. Lightweight Status Check

GET /sync returns only height and latest hash

Minimizes bandwidth for radio links while enabling quick chain comparison

2. Chain Download

GET /chain fetches complete blockchain if peer has more blocks

Uses longest chain rule for automatic conflict resolution

3. Mempool Exchange

Bidirectional transaction propagation via GET /mempool and POST /tx

Ensures all nodes have the same pending transactions

4. Error Tolerance

Failures logged but don't stop the sync loop

Critical for unreliable radio links with intermittent connectivity

Task Lifecycle Management

The main.rs module implements sophisticated task coordination for multi-threaded operation:

Background Task Spawning

Graceful Shutdown

All tasks run indefinitely until the process receives a termination signal. The async runtime ensures proper cleanup of resources and network connections.

Integration with Radio Systems

The main.rs architecture is specifically designed for radio deployment scenarios:

  • Fixed port 10000 for HTTP server simplifies firewall configuration

  • 30-second peer sync interval accommodates HF propagation delays

  • Atomic file operations prevent corruption during power failures

  • HTTP-only networking eliminates complex NAT traversal issues

Error Handling Strategy

The system prioritizes availability over perfection, logging errors but continuing operation to maintain network connectivity even when individual components fail.

Last updated