Bitcoin Cycle
Bitcoin v0.1 · January 2009

Inside the code Satoshi shipped.

On 9 January 2009 Satoshi Nakamoto released the first Bitcoin client: a Windows program written in C++. This page opens it up and shows how it works. Every hash on this page is computed live in your browser, using the same algorithm and the same bytes as the original, and every code excerpt is copied word for word from that release.

Lines of C++
The core
main.cpp
2,660 lines: blocks, chain, mining
First reward
50 BTC
halving every 210,000 blocks
Block 0, re-hashed just now
…
1

The whole codebase on one screen

Each tile is a source file, sized by line count. Almost half is the Windows interface. The ideas that matter fit in two files.

2

A coin is a chain of signatures

There are no balances in the code. A CTransaction spends earlier outputs (vin) and creates new ones (vout). Each input points back at one output by its transaction hash and index: a COutPoint.

Follow 50 BTC from a mined block

Hover any input to see the output it spends.

Outputs are spent whole. To pay Bob 10 out of 50, Alice spends the whole 50 output and sends 40 back to herself as change.
3

The genesis block, rebuilt byte by byte

A block's identity is the hash of just its 80-byte header. The page below rebuilds block 0 from the values hard-coded in main.cpp and hashes it with SHA-256 twice, as CBlock::GetHash() does.

The 80-byte header
Hash(header) = SHA-256(SHA-256(80 bytes)), printed backwards
The message Satoshi left in block 0

The first transaction in a block (the coinbase) has no real input, so its scriptSig can hold anything. Satoshi put a newspaper headline there, which proves the block was not mined before 3 January 2009. Edit it and watch the change ripple up into the block hash.

Mine your edited block ↓
4

Mining is guessing a number

BitcoinMiner() hashes the header, checks the result against a target, adds one to nNonce, and repeats. The first hash at or below the target wins the block. Your browser is running that loop below.

Mine the block from chapter 3

nNonce
0
Hashes tried
0
Speed
–
Expected
–
5

One hash that commits to every transaction

BuildMerkleTree() hashes transactions in pairs, then pairs of pairs, until one hash is left: the Merkle root in the header. Change any transaction and the root changes, so the proof of work changes too.

Transactions in the block:

Click a transaction to tamper with it. Red marks every hash that changes. With an odd count the last hash is paired with itself (dashed), which is what min(i+1, nSize-1) does.

6

Script: a tiny stack machine guards every coin

An output doesn't name an owner. It holds a small program, scriptPubKey. To spend it you supply scriptSig, and VerifySignature() runs the two joined together. If the stack ends with true, the spend is valid.

stack
7

Ten minutes, enforced by arithmetic

Every 2,016 blocks, GetNextWorkRequired() checks how long those blocks actually took and scales the target so the next 2,016 take two weeks. One adjustment is capped at 4× either way.

How long did the last 2,016 blocks take?
nActualTimespan
after the 4× clamp
mining gets

8

The halving is one bit-shift

The four-year cycle on the dashboard starts here. The block reward is 50 coins shifted right by one bit for every 210,000 blocks mined. That one line caps supply just under 21 million.

Block height
9

When two miners disagree, the longer chain wins

Two blocks can be found at nearly the same time, splitting the network. AddToBlockIndex() settles it: when a block arrives that makes a branch longer than the current best, the node switches to that branch with Reorganize().

Your node's view

In v0.1 "longer" means more blocks (nHeight). Later versions compare total work instead, so a branch of easier blocks can't win just by being longer.