Learning Bitcoin Through Source Code: A Deep Dive into Mining

·

Bitcoin mining is the process of generating new blocks while simultaneously introducing new bitcoins into circulation. This comprehensive guide explores the technical intricacies behind mining, from block rewards to security considerations.

How Bitcoin Mining Works

  1. The Mining Reward System
    Miners compete to solve complex cryptographic puzzles by finding a valid hash value (explained in detail later). Successfully mining a block grants Bitcoin rewards:

    • New blocks are generated approximately every 10 minutes
    • Block rewards halve every 210,000 blocks (~4 years)
    • Total Bitcoin supply will cap at ~21 million by 2140
    • Post-2140, miner income will rely solely on transaction fees
  2. Miner Revenue Composition
    Rewards consist of two components:

    • Coinbase transaction (block subsidy)
    • Cumulative transaction fees within the block

    Bitcoin Core implementation:

    int64_t GetBlockValue(int nHeight, int64_t nFees) {
      int64_t nSubsidy = 50 * COIN; // Initial 50 BTC reward
      int nHalving = nHeight / 210000;
      if (nHalving >= 64) return nFees;
      nSubsidy >>= nHalving; // Halving via bit-shift
      return nSubsidy + nFees;
    }

Transaction Processing Mechanics

  1. Network Propagation & Validation
    Transactions undergo independent verification by nodes through core functions:

    CheckInputs();
    CheckTransaction();
    AcceptToMemoryPool();

    Valid transactions join the mempool and propagate network-wide.

  2. Transaction Priority System
    Mining nodes prioritize transactions using:

    priority = sum(vin[i].value * vin[i].age)/tx_size

    Key factors:

    • Higher priority given to transactions with:

      • Larger amounts
      • Older UTXOs
      • Smaller byte size
    • First 50KB reserved for high-priority transactions
    • Remaining space allocated by fee-per-byte rate

👉 Discover how Bitcoin's security model prevents double-spending

Mining Technicals

  1. Coinbase Transaction Structure
    Special characteristics:

    • Input references null transaction (0x000...)
    • Output index set to 0xFFFFFFFF
    • Contains block height + optional miner data (≤100 bytes)
    • Output sends subsidy to miner's address
  2. Proof-of-Work Algorithm
    Miners iterate nonce values until:

    SHA256(SHA256(block_header)) ≤ target_hash
    • Target difficulty adjusts dynamically via nBits
    • Maintains ~10 minute block intervals
    • Adjustment function: GetNextWorkRequired()

Blockchain Security Considerations

  1. Chain Reorganization Events

    • Nodes follow the longest valid chain (most accumulated work)
    • Temporary forks resolve naturally through network propagation
    • Orphan blocks remain in pool until parent block arrives
  2. 51% Attack Dynamics

    • Possible with >30% hashpower (higher probability with more)
    • Current global hashrate: ~40 EH/s (exa-hashes per second)
    • Attack vectors include:

      • Double-spending
      • Transaction censorship
      • Chain reorganization
  3. Double-Spend Prevention
    Critical practices:

    • Wait for 6+ confirmations for high-value transactions
    • Merchants shouldn't deliver goods for unconfirmed payments
    • Monitor blockchain depth for transaction finality
  4. Denial-of-Service Resistance
    While theoretically possible to censor transactions through:

    • Selective transaction exclusion
    • Chain reorganization
      Bitcoin's decentralized nature makes sustained attacks impractical

FAQ: Bitcoin Mining Explained

Q: Why does mining difficulty adjust?
A: To maintain consistent 10-minute block intervals despite changing network hashpower.

Q: How are transaction fees determined?
A: Market-driven—users bid for block space by attaching fees. Miners typically prioritize higher-fee transactions.

Q: What happens when all 21 million BTC are mined?
A: Miners will earn income solely from transaction fees, estimated to become economically viable through:

  1. Increased transaction volume
  2. Higher fee market competition

👉 Explore advanced Bitcoin mining strategies

Q: Can mining be done profitably at home?
A: With industrial ASIC dominance, individual mining requires:

Q: How does mining secure the network?
A: By making:

Q: What's the environmental impact of Bitcoin mining?
A: Mining energy usage is often compared to: