Bitcoin mining is a lottery where each ticket is a hash. Miners repeatedly hash an 80-byte block header, changing a few fields each time, until the double SHA-256 of that header is numerically at or below a target. Whoever finds such a header first gets to add their block to the chain and collect the reward. The work proves nothing about the transactions. What it does is make rewriting history expensive.
What actually gets hashed
A miner never hashes the whole block. It hashes the block header, which is always 80 bytes:
| Field | Size | What it is |
|---|---|---|
version | 4 bytes | Block version, also used for soft-fork signalling |
prev_block_hash | 32 bytes | Hash of the previous block's header |
merkle_root | 32 bytes | Root of the Merkle tree of all transactions in the block |
time | 4 bytes | Unix timestamp, loosely constrained |
bits | 4 bytes | Compact encoding of the current target |
nonce | 4 bytes | A free counter the miner changes |
Because prev_block_hash is inside the header, every block commits to the full history before it. Because merkle_root is inside the header, changing any transaction changes the header hash.
You can check this yourself with the genesis block:
import hashlib, struct
def header_hash(version, prev_hash, merkle_root, timestamp, bits, nonce):
# Hashes are displayed big-endian but serialized little-endian
header = (
struct.pack("<I", version)
+ bytes.fromhex(prev_hash)[::-1]
+ bytes.fromhex(merkle_root)[::-1]
+ struct.pack("<III", timestamp, bits, nonce)
)
assert len(header) == 80
return hashlib.sha256(hashlib.sha256(header).digest()).digest()[::-1].hex()
def bits_to_target(bits):
exponent, mantissa = bits >> 24, bits & 0xFFFFFF
return mantissa * 256 ** (exponent - 3)
h = header_hash(
version=1,
prev_hash="00" * 32,
merkle_root="4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
timestamp=1231006505,
bits=0x1D00FFFF,
nonce=2083236893,
)
print(h) # 000000000019d6689c...8ce26f
print(int(h, 16) <= bits_to_target(0x1D00FFFF)) # True
The leading zeros are simply what "numerically small" looks like in hex. A lower target means more leading zeros, so each hash is less likely to be a winner.
Why nobody can shortcut it
SHA-256 has no known way to aim for a small output. The only strategy is to try inputs. Each attempt has the same tiny chance of success, whatever came before, so mining is memoryless. A miner with 1% of the network's hash rate finds about 1% of the blocks over time. On any single block they have nothing like "90% of the work done".
The expected number of hashes per block is about difficulty × 2^32. Difficulty is just the ratio between the easiest-ever target (the genesis target above) and the current one.
The nonce runs out almost instantly
The 32-bit nonce gives only about 4.3 billion tries. A modern ASIC goes through that in a fraction of a second. So miners also change:
- extranonce: bytes inside the coinbase transaction's input script. Changing them changes the coinbase txid, the Merkle root, and so the header.
- time: the timestamp can move within consensus limits. It must be greater than the median of the previous 11 blocks, and nodes reject blocks more than two hours ahead of their own adjusted clock.
- version bits: some bits of
versioncan be rolled by hardware ("version rolling").
Difficulty adjustment
Bitcoin targets one block every 10 minutes on average. Every 2016 blocks (about two weeks), each node compares how long those blocks actually took with 20,160 minutes and scales the target to match. The adjustment is clamped to a factor of 4 in either direction. If hash rate doubles, blocks come faster for up to two weeks, and then difficulty roughly doubles.
This is why adding miners does not make Bitcoin faster. It only makes blocks more expensive to produce.
Where the reward comes from
The first transaction in every block is the coinbase transaction. It has no real inputs and may pay out at most:
block subsidy + sum of fees of included transactions
The subsidy halves every 210,000 blocks. It has been 3.125 BTC since the April 2024 halving. Coinbase outputs cannot be spent until they are 100 blocks deep, so a miner whose block is later orphaned never gets to spend a reward that no longer exists.
Miners generally fill blocks by feerate (sats per virtual byte), not by absolute fee. They also count unconfirmed parents together with their children, which is why child-pays-for-parent works. Bitcoin Core exposes a ready-made block template:
bitcoin-cli getmininginfo
bitcoin-cli getblocktemplate '{"rules": ["segwit"]}'
"Longest chain" really means "most work"
Nodes follow the valid chain with the most cumulative proof of work, not the most blocks. When two miners find blocks at almost the same time, the network briefly splits. The tie breaks when the next block extends one side, and the other block becomes stale. Its transactions go back to the mempool unless they are already in the winning chain.
To rewrite a transaction buried under n blocks, an attacker must redo those n blocks and then overtake the honest chain. As n grows, the chance of pulling that off falls exponentially unless the attacker controls most of the hash rate. That is the whole basis for confirmation counts.
Pools and shares
A single miner might wait years for a block. Pools smooth out the income. The pool hands out work (usually over the Stratum protocol) with the pool's payout address in the coinbase. Miners submit shares: headers that meet a much easier pool-set target. Shares prove how much work each miner did, and the pool splits rewards in proportion. Occasionally a share also meets the real network target, and that one becomes a block.
Summary
- Mining means searching for an 80-byte header whose double SHA-256 is at or below the target encoded in
bits. - Nonce, extranonce, timestamp and version bits provide the search space. There is no shortcut.
- Difficulty retargets every 2016 blocks to keep roughly 10-minute blocks.
- The coinbase pays out subsidy plus fees and matures after 100 blocks.
- Nodes follow the most-work valid chain, which is what makes deep blocks hard to reverse.
Get the weekly commit
New blockchain deep dives every week.