Bitcoin
You study how Bitcoin works from the 2008 whitepaper to the mining loop: UTXOs, transactions, blocks, proof of work and the fixed supply. You write a toy miner in Python to feel what proof of work costs.
You Will Be Able To
- Explain the UTXO model and trace a transaction from inputs to outputs.
- Describe what is inside a Bitcoin block and how blocks link together.
- Explain how mining and proof of work secure the ledger.
- Calculate Bitcoin's supply schedule from the 50 coin starting reward and the halving rule.
- Write a toy proof-of-work miner in Python and vary its difficulty.
The Whitepaper And The First Block
On 31 October 2008 an author using the name Satoshi Nakamoto published a nine-page paper called 'Bitcoin: A Peer-to-Peer Electronic Cash System'. Its opening line states the goal: online payments sent directly from one party to another without going through a financial institution.
The paper's contribution was a combination. It used the tools from week 2, hashes and digital signatures, together with a network of volunteers and a costly puzzle called proof of work. The result was a ledger that strangers could agree on without a boss. The network began running on 3 January 2009 when the first block, the genesis block, was created.
Satoshi's identity remains unknown. This is less important than it sounds, because anyone can read the code and the paper and check that the system follows its rules. That openness is the point of the design.
Coins Are Unspent Outputs: The UTXO Model
Bitcoin does not store account balances. It stores unspent transaction outputs, or UTXOs. A UTXO is a chunk of bitcoin locked to an owner, created by an earlier transaction and not yet used. Your wallet balance is the sum of the UTXOs your keys can unlock.
Think of UTXOs as banknotes of odd sizes. If you hold a note worth 7 and need to pay 5, you hand over the whole note of 7. You receive a new note worth 2 back as change. A transaction does exactly this: it consumes whole outputs and creates new ones.
A transaction has inputs and outputs. Each input points to an earlier unspent output and carries a signature proving the spender controls it. Each output states an amount and the conditions needed to spend it later, usually a public key hash. The sum of outputs must not exceed the sum of inputs. The small leftover difference is the transaction fee, collected by the miner.
- Input: a reference to a past output, plus a signature.
- Output: an amount and a lock that names who may spend it next.
- Change output: returns the surplus to the sender.
- Fee: inputs minus outputs, paid to the miner.
How Double Spending Is Prevented
Each UTXO can be spent once. When a transaction uses an output as an input, nodes mark that output as spent. A second transaction that tries to use the same output is invalid, and nodes reject it. This makes the check mechanical: every full node keeps the set of unspent outputs and tests each new transaction against it.
The difficult part remains the one from week 1: when two conflicting transactions appear at about the same time, which one comes first? Bitcoin answers through blocks. The transaction included in the accepted chain of blocks wins, and the other becomes invalid. So the real question is how the network agrees on the next block.
Blocks And The Chain
A block bundles transactions. Its header holds a version, the hash of the previous block, the Merkle root of the block's transactions, a timestamp, a difficulty target and a number called the nonce. Hashing the header twice with SHA-256 gives the block's identifier.
Because the header contains the previous block's hash, every block commits to the full history behind it. The first transaction in each block is special. It is called the coinbase transaction and it creates new bitcoin as a reward for the miner who built the block.
The target block interval is about ten minutes. Blocks have a size limit, so only a limited number of transactions fit each time. Users compete by attaching higher fees, and miners tend to include the best-paying transactions first.
Mining And Proof Of Work
Mining is the process of proposing the next block. A miner collects pending transactions, builds a header and tries to find a nonce such that the hash of the header is lower than the difficulty target. In practice that means the hash must begin with a certain number of zero bits.
No shortcut is known. The miner changes the nonce, hashes again and checks, billions of times per second across the network. Finding a valid answer is slow and random. Checking an answer takes one hash. This asymmetry is the heart of proof of work: expensive to produce, cheap to verify.
Proof of work does two jobs. It picks who gets to write the next block, so the network agrees on a single order of transactions. It also makes rewriting history expensive. To change an old block, an attacker must redo the work for that block and every block after it, while the honest network keeps extending the chain. Nodes follow the valid chain with the most accumulated work.
- Collect pending transactions
- Build a block header
- Change the nonce and hash
- Hash below target?
- Broadcast the block
Miners repeat the hash step until one result falls below the target.
Difficulty Adjustment
More miners joining or leaving would change block speed. Bitcoin handles this with a rule: every 2,016 blocks, nodes compare how long those blocks took to the expected two weeks and adjust the difficulty target. If blocks arrived too fast, the puzzle becomes harder. If they arrived too slowly, it becomes easier.
The result is a steady rhythm of one block roughly every ten minutes, whatever the total computing power. This also means mining power and security can grow without changing how the system feels to users.
| Parameter | Value |
|---|---|
| Target block time | About 10 minutes |
| Adjustment interval | Every 2016 blocks |
| Typical period | About two weeks |
| Effect | Hash rate up, difficulty up |
The network keeps block time steady as miners come and go. Source: Bitcoin Developer Documentation
Supply: 21 Million And The Halving
Every block's coinbase transaction creates new bitcoin. The first reward was 50 BTC per block. The protocol cuts the reward in half every 210,000 blocks, which is about four years. Halvings took the reward to 25, then 12.5, then 6.25 and then 3.125 BTC, the last in April 2024.
Add up the geometric series: 210,000 blocks times 50 BTC, plus half of that, plus a quarter, and so on. The total approaches 21 million BTC and never exceeds it. The smallest unit is a satoshi, one hundred millionth of a bitcoin, so the reward eventually rounds down to zero. Around the year 2140 the last new coins are issued.
After that, miners earn only transaction fees. Whether fees alone will pay for enough security is a topic of active debate, and you should treat confident predictions on either side with care. The supply schedule itself is not up for debate: it is written in the software, and every node checks it.
The reward halves every 210,000 blocks, about four years. Source: Bitcoin protocol rules (Bitcoin Developer Documentation)
Nodes, Wallets And What To Take Away
A full node downloads every block and validates every rule, including the supply cap and the signatures. Miners propose blocks, but full nodes decide whether to accept them. A wallet is software that stores your private keys, builds and signs transactions and shows your balance by scanning UTXOs.
Bitcoin demonstrates one working answer to the problem from week 1. It uses signatures for authority, hashes and Merkle trees for integrity, proof of work for ordering and a public schedule for issuance. Next week we examine consensus more broadly, including proof of stake, forks and attacks. This week's code shows the mining loop at toy scale, so you can feel what the cost of a puzzle is.
One more habit is worth building now: look at real data. Open a public block explorer, pick a recent block and find its height, its timestamp, the number of transactions and the coinbase reward. Check that the previous block hash matches the block before it. Find one transaction and list its inputs, outputs and fee. Seeing the structure you studied inside a live block turns the vocabulary of this week into something you can read on your own. Compare what you see with the toy miner's output: the real hash begins with many more zeros, because the real difficulty is far higher than anything a laptop would attempt.
Key Terms
- UTXO
- An unspent transaction output: a chunk of bitcoin that a key can still spend.
- Transaction fee
- The difference between inputs and outputs, paid to the miner who includes the transaction.
- Coinbase transaction
- The first transaction in a block, which creates the block reward for the miner.
- Nonce
- A number miners change to search for a header hash below the target.
- Proof of work
- Evidence that a miner spent computing effort, cheap for others to verify.
- Difficulty
- How hard the hash puzzle is, adjusted every 2,016 blocks.
- Halving
- The cut of the block reward in half every 210,000 blocks.
- Satoshi
- The smallest bitcoin unit, equal to one hundred millionth of a bitcoin.
- Full node
- A computer that downloads and checks every block and transaction against the rules.
Exercise: Write A Toy Bitcoin Miner
- Create miner.py and write a function mine(data, difficulty) that tries nonces from 0 upward.
- For each nonce, compute SHA-256 of data plus the nonce as text and test whether the hex digest starts with difficulty zeros.
- Run it with difficulty 1, 2, 3, 4 and 5, and record the nonce found and the time taken for each using time.perf_counter.
- Write a verify function that checks a given nonce with one hash, and compare its time with mining time.
- Add a small chain: three blocks, each storing the previous block's hash, mined at difficulty 4. Change the first block and show that the links break.
- Calculate the total supply by summing the reward over each halving period in a loop, and confirm it approaches 21 million.
Further Reading
- Bitcoin: A Peer-to-Peer Electronic Cash System, Satoshi Nakamoto, 2008 whitepaper (read in full, nine pages)
- Mastering Bitcoin, 3rd edition, Andreas M. Antonopoulos and David A. Harding, O'Reilly Media, chapters on transactions and mining
- Bitcoin and Cryptocurrency Technologies, Arvind Narayanan et al., Princeton University Press, chapters 2 to 5
- Bitcoin Developer Documentation, bitcoin.org, developer guides on transactions and block chain