Skip to content

plan: Code Audit Cleanup - Dead Code, Logging & Security Fixes #79

@AlphaB135

Description

@AlphaB135

Overview

Comprehensive code cleanup based on Linus Torvalds-style security audit. Focus areas: dead code removal, logging migration, hardcoded values, and P2P bug fixes.

Priority: P0 (Blocker for mainnet)
Estimated Time: 4-6 hours


Audit Summary

Critical (P0) - Must Fix Before Mainnet

Issue File Severity
P2P connection bug Issue #72 Critical
#[allow(dead_code)] modules vardiff.rs, pool_template.rs, block_submit.rs, chainstate.rs, metrics.rs, miner.rs High
expect() in production metrics.rs Medium
println!/eprintln! commands/.rs, network/.rs Medium
Hardcoded credentials rpc/src/jwt/secret.rs Critical

High Priority (P1)

Issue Description
Hardcoded RPC URL 127.0.0.1:29443 hardcoded
Dependency PRs #71, #40, #41, #17 (breaking changes)

Implementation Plan

Phase 1: Dead Code Removal (30 min)

Objective: Remove or feature-gate all #[allow(dead_code)] modules

Files to audit:

crates/node/src/vardiff.rs        # Pool mining vardiff
crates/node/src/pool_template.rs  # Stratum pool templates
crates/node/src/block_submit.rs   # Pool block submission
crates/node/src/chainstate.rs     # Chainstate queries
crates/node/src/metrics.rs        # Mining metrics stub
crates/node/src/miner.rs          # Various unused methods

Action Items:

  • Add pool feature flag to Cargo.toml
  • Move pool-related modules behind #[cfg(feature = "pool")]
  • Remove truly dead code
  • Update lib.rs mod declarations

Verification:

cargo build --all-features  # Must pass
cargo build                 # Must pass (no pool code)
rg "allow\(dead_code\)" --type rust  # Should be minimal

Phase 2: Logging Migration (45 min)

Objective: Replace all println!/eprintln! with tracing crate

Target files:

crates/node/src/commands/rpc.rs
crates/node/src/commands/p2p.rs
crates/node/src/ws_dashboard.rs
crates/network/src/sync.rs

Pattern:

// BEFORE
println!("Connected to {} peers", count);
eprintln!("Error: {}", err);

// AFTER
use tracing::{info, error, instrument};

#[instrument]
fn connect_peers(count: usize) {
    info!(peer_count = count, "Connected to peers");
}

error!(error = ?err, "Connection failed");

Verification:

rg "println!|eprintln!" --type rust -g '!*test*' -g '!*bench*'
# Expected: 0 results in production code

Phase 3: Remove Hardcoded Values (20 min)

Objective: Remove hardcoded credentials and URLs

Actions:

  1. Remove test credentials from rpc/src/jwt/secret.rs
  2. Move RPC URL to config struct
  3. Add config validation

Verification:

rg -i "admin.*password|secret.*=" --type rust -g '!*test*'
# Expected: 0 results

Phase 4: Fix P2P Bootstrap (45 min)

Objective: Add timeout and better error handling to peer bootstrap (fixes #72)

Changes:

// crates/network/src/bootstrap.rs

use tokio::time::{timeout, Duration};
use tracing::{info, warn, error};

pub async fn bootstrap_peers(&self, seeds: &[String]) -> Result<usize, NetworkError> {
    let mut connected = 0;
    
    for seed in seeds {
        info!(seed = %seed, "Attempting to connect to seed peer");
        
        match timeout(Duration::from_secs(30), self.connect_peer(seed)).await {
            Ok(Ok(_)) => {
                info!(seed = %seed, "Successfully connected to seed");
                connected += 1;
            }
            Ok(Err(e)) => {
                warn!(seed = %seed, error = ?e, "Failed to connect to seed");
            }
            Err(_) => {
                warn!(seed = %seed, "Connection timeout after 30s");
            }
        }
    }
    
    if connected == 0 {
        error!("Failed to connect to any seed peers!");
        return Err(NetworkError::NoSeedPeersAvailable);
    }
    
    Ok(connected)
}

Verification:

  • Manual P2P connection test
  • Node can bootstrap from empty peer list

Phase 5: Dependency Updates (60 min)

Objective: Review and merge dependency PRs (carefully - breaking changes)

PRs to review:

Action:

# Test each PR carefully
gh pr checkout 41
cargo build --all-features
cargo test --all

# Verify crypto signatures still work
cargo test -p bq-crypto

Acceptance Criteria

  • cargo build --all-features passes
  • cargo test --all passes
  • cargo clippy --all-features -- -D warnings passes
  • No #[allow(dead_code)] in production (except feature-gated)
  • No println!/eprintln! in production code
  • No hardcoded credentials
  • P2P bootstrap works with timeout handling
  • Dependency PRs reviewed and merged

Related Issues


Created: 2026-01-21
Priority: P0 (Blocker)
Status: Planning

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions