blockchain.rs - Chain Logic

The blockchain module serves as the central nervous system of Bunkercoin, orchestrating all blockchain operations including block validation, mining coordination, transaction management, and peer synchronization. It implements a thread-safe, deterministic blockchain with specialized optimizations for disconnected environments.

Architecture Overview

Key Design Principles

  • Deterministic Mining: Blocks are produced at predictable 30-second intervals

  • Thread Safety: Arc wrapper enables safe concurrent access

  • Persistent State: Automatic JSON serialization for blockchain persistence

  • Longest Chain Rule: Automatic chain replacement for network consensus

Core Data Structures

// src/blockchain.rs (lines 7-26)
#[derive(Clone)]
pub struct Blockchain {
    inner: Arc<Mutex<State>>,
}

struct State {
    chain: Vec<Block>,
    mempool: Vec<Transaction>,
    data_dir: PathBuf,
    difficulty: u32,
}

#[derive(Serialize, Deserialize)]
pub struct SyncInfo {
    pub height: usize,
    pub latest_hash: String,
}

Thread Safety Architecture: The blockchain uses Arc to wrap the internal state, allowing safe sharing across multiple async tasks while maintaining data consistency during concurrent mining, networking, and transaction processing operations.

Initialization and Persistence

The initialization process demonstrates Bunkercoin's resilient design. If no existing blockchain is found, it starts with an empty chain. The difficulty is hardcoded to 10,000 iterations, balancing security with the need for deterministic timing in radio environments.

Deterministic Mining Algorithm

Block Construction Process

Mining Process Breakdown

Step
Operation
Deterministic Factor

1. Transaction Ordering

Sort mempool by transaction hash

Ensures all nodes produce identical blocks

2. Timestamp Calculation

Previous block + 30 seconds

Predictable timing for radio synchronization

3. VDF Computation

Blake3^10000(prev_hash)

Proof-of-work with non-parallelizable computation

4. Nonce Generation

Derived from prev_hash + timestamp

Deterministic but unpredictable values

Peer Synchronization

The blockchain module implements sophisticated peer synchronization for network consensus:

Chain Replacement Logic

Mempool Management

  • Deduplication: Transactions are stored by hash to prevent duplicates

  • Persistence: Mempool survives process restarts through file-based storage

  • Broadcasting: New transactions automatically propagate to HTTP peers

Thread Safety Implementation

The blockchain uses advanced Rust concurrency patterns:

  • Arc (Atomic Reference Counting): Enables shared ownership across threads

  • Mutex: Provides exclusive access to mutable state

  • Clone semantics: Arc cloning is cheap (only increments reference count)

Radio-Optimized Features

Deterministic Block Production

Unlike Bitcoin's competitive mining, Bunkercoin produces blocks on a fixed schedule, enabling:

  • Predictable transmission windows for HF radio

  • Reduced power consumption (no competitive hashing)

  • Synchronized operation across disconnected networks

Compact State Management

  • 10KB maximum block size fits within JS8Call frame limits

  • JSON serialization enables human-readable debugging

  • Atomic file updates prevent corruption during power failures

Error Handling and Recovery

The blockchain module implements comprehensive error handling:

  • Non-fatal mining errors don't stop the blockchain

  • Automatic recovery from file system issues

  • Graceful degradation during network partitions

Integration Points

The blockchain module integrates with other system components:

  • web.rs: Provides HTTP endpoints for peer communication

  • vdf.rs: Computes proof-of-work for new blocks

  • transaction.rs: Validates and processes value transfers

  • block.rs: Manages block structure and hashing

Last updated