Understanding Bitcoin Mining Through Source Code Analysis

·

Bitcoin mining is the foundational process that creates new blocks and issues new bitcoins into circulation. It secures the network and processes transactions through a decentralized consensus mechanism. This article breaks down the technical aspects of mining, from block rewards to security considerations, using insights derived from Bitcoin’s core source code.


What Is Bitcoin Mining?

Mining involves nodes on the Bitcoin network competing to solve a complex cryptographic puzzle. The first miner to find a valid hash for a new block receives a reward in newly minted bitcoins and collects transaction fees from the included transactions. This process occurs approximately every ten minutes, ensuring a steady and predictable issuance of new coins.


The Block Reward Structure

Miners receive rewards composed of two elements:

The following simplified C++ code from Bitcoin Core illustrates how the block reward is calculated:

int64_t GetBlockValue(int nHeight, int64_t nFees) {
    int64_t nSubsidy = 50 * COIN;
    int nHalving = nHeight / 210000;
    if (nHalving >= 64)
        return nFees;
    nSubsidy >>= nHalving;
    return nSubsidy + nFees;
}

Key points from the code:

👉 Explore real-time Bitcoin mining statistics


Transaction Verification and Mempool

When a user creates a transaction, it is broadcast to neighboring nodes. Each node independently validates the transaction using functions such as:

Valid transactions are added to the node’s memory pool (mempool) and relayed to other nodes. Invalid transactions are rejected.


Transaction Priority and Block Space

Due to block size limits, miners prioritize which transactions to include. Priority is calculated as:

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

Where:

Transactions with high priority (over 57,600,000) are allocated the first 50 KB of block space. The remaining space is filled with transactions offering the highest fee per KB.

If a transaction remains unconfirmed, the sender should consider increasing the fee to incentivize miners.


The Coinbase Transaction

The coinbase transaction is the first transaction in every new block. Key attributes include:

This mechanism is how new bitcoins are created and introduced into the economy.


The Mining Algorithm

Miners repeatedly vary a value in the block header called the nNonce and compute:

SHA256(SHA256(block_header))

The goal is to find a hash value less than or equal to the target hash (nHashTarget). The target is adjusted regularly based on total network hashrate to maintain an average block time of 10 minutes. The function GetNextWorkRequired() handles this difficulty adjustment.


Blockchain Forks

Temporary forks occur when two miners produce blocks at similar times. Nodes follow the longest chain (chain with the most cumulative work) to achieve consensus. Orphan blocks—blocks whose parent is unknown—are stored temporarily until the parent block is received.

The 10-minute block interval represents a trade-off between confirmation speed and fork frequency.


51% Attacks

A 51% attack does not require exactly 51% of the network hashrate—it becomes more feasible as an attacker’s hashrate share increases. Even with 30% of the network power, an attacker might attempt to reorganize the chain.

As of now, the Bitcoin network hashrate exceeds 40 exahashes per second (EH/s), making attacks increasingly expensive and difficult.


Double-Spend Attacks

In a double-spend attack, a user initiates two conflicting transactions. For example:

  1. A customer pays for coffee with transaction A.
  2. Before transaction A confirms, the customer sends the same UTXO to another address in transaction B.

If transaction B is confirmed first, transaction A becomes invalid. To mitigate this, merchants should wait for multiple confirmations for high-value transactions. Attackers with high hashrate could attempt chain reorganizations to reverse confirmed transactions.


Denial-of-Service (DoS) Attacks

A malicious miner with significant computational resources could exclude transactions from specific addresses, effectively preventing them from being confirmed. This type of attack is theoretically possible but economically impractical on a well-secured network like Bitcoin.


Frequently Asked Questions

How often are new bitcoins created?
New blocks are generated approximately every 10 minutes. Each new block currently rewards the miner with 6.25 BTC, plus transaction fees. This reward halves every 210,000 blocks.

What happens when all bitcoins are mined?
Around the year 2140, the last bitcoin will be mined. After that, miners will earn income solely from transaction fees, which will serve as an incentive to continue securing the network.

Can anyone become a Bitcoin miner?
Yes, but competitive mining now requires specialized hardware (ASICs) and access to affordable electricity. Most miners join mining pools to combine hashrate and share rewards.

How does mining protect the network?
Mining requires substantial computational work to add new blocks. This proof-of-work mechanism makes it extremely difficult to alter transaction history or double-spend coins.

What is a orphaned block?
An orphaned block is a valid block that is not part of the main chain. This usually happens when two miners find a block at nearly the same time. The network eventually consensus on one chain, and the other block becomes stale.

Is Bitcoin mining profitable?
Profitability depends on factors like electricity costs, hardware efficiency, Bitcoin’s market price, and network difficulty. It requires careful calculation and often involves significant upfront investment.


Mining is a critical and multifaceted component of the Bitcoin network. It ensures security, processes transactions, and controls the supply of new bitcoins. Understanding its mechanisms offers deeper insight into how decentralized consensus is achieved and maintained.