web.rs - HTTP Interface

The web module provides the HTTP interface layer for Bunkercoin, implementing both RESTful endpoints for peer-to-peer communication and real-time event streaming via Server-Sent Events (SSE). This design enables firewall-friendly networking and live monitoring capabilities essential for radio-based deployments.

Global Event Broadcasting System

// src/web.rs (lines 10-22)
// Global broadcast channel for log lines / events
pub static EVENT_TX: Lazy<Sender<String>> = Lazy::new(|| {
    let (tx, _rx) = channel(100);
    tx
});

/// Broadcast helper prints to stdout and pushes to SSE listeners
pub fn broadcast<S: Into<String>>(msg: S) {
    let text: String = msg.into();
    println!("{}", text);
    let _ = EVENT_TX.send(text);
}

Event Architecture: The system uses a global broadcast channel that simultaneously outputs to stdout and pushes events to all connected SSE clients. This dual approach ensures both console logging and real-time web monitoring.

Server-Sent Events Implementation

The SSE endpoint creates a new subscription to the global broadcast channel for each client connection. The stream filtering ensures only successful messages are forwarded, with automatic keep-alive to maintain connections through firewalls and proxies.

P2P Synchronization Endpoints

Chain Data Access

The chain endpoint serves the blockchain data directly from the filesystem. This approach eliminates memory overhead and provides atomic consistency since the JSON file is only updated after successful block validation.

Peer Synchronization Info

Block Broadcasting Protocol

Transaction Processing

Mempool Inspection

Transaction Submission

Server Configuration

Complete API Reference

GET /events

  • Purpose: Real-time event streaming via Server-Sent Events

  • Response: text/event-stream

  • Use Case: Provides live updates for mining progress, transaction processing, and peer synchronization events. Essential for monitoring radio-bridge operations.

GET /chain

  • Purpose: Download complete blockchain data

  • Response: JSON array of blocks

  • Use Case: Peer synchronization - allows nodes to fetch the entire chain when joining the network or recovering from partitions.

GET /sync

  • Purpose: Lightweight synchronization status

  • Response: {"height": number, "latest_hash": "hex"}

  • Use Case: Quick check to determine if peer has newer blocks without downloading the full chain.

POST /broadcast

  • Purpose: Accept new blocks from peers

  • Request Body: Single Block object in JSON

  • Use Case: Network-wide block propagation when a node successfully mines a new block.

GET /mempool

  • Purpose: List pending transactions

  • Response: JSON array of transactions

  • Use Case: Peer synchronization of pending transactions to ensure all nodes have the same transaction pool.

POST /tx

  • Purpose: Accept single transaction from peer

  • Request Body: Single Transaction object in JSON

  • Use Case: Transaction propagation throughout the network before block inclusion.

Radio-Optimized Design Features

HTTP-Only Protocol

  • Firewall Friendly: HTTP traverses NAT and firewalls without configuration

  • Stateless: No persistent connections that can break during radio blackouts

  • Simple: Minimal protocol overhead for bandwidth-constrained radio links

Event Broadcasting for Monitoring

The SSE system enables real-time monitoring of radio bridge operations:

Atomic Operations

All endpoints are designed for atomic operations that can safely handle connection failures:

  • GET operations are idempotent

  • POST operations either succeed completely or fail cleanly

  • No partial state updates that could corrupt the blockchain

Integration with Blockchain Core

The web module acts as the network interface for the blockchain:

This pattern ensures:

  • Thread Safety: All blockchain access goes through Arc

  • Performance: No blocking of HTTP responses during blockchain operations

  • Reliability: Failed operations don't affect the web server

Last updated