Skip to content

Part 11 · Build Your Own Blockchain in Rust

You’ve spent ten parts learning how Bitcoin works. Now you’ll build a working blockchain yourself, in Rust — and that’s where the understanding truly locks in. By the end you’ll have a small but complete chain that mines blocks with Proof of Work, moves coins between wallets with real secp256k1 signatures, validates everything, saves to disk, and gossips blocks between nodes over a real network.

This part doubles as a Rust tutorial. We assume you’ve seen a little Rust (say, the first few chapters of the Rust Book) but we explain every concept as it comes up — ownership, structs, traits, Result, modules — so you learn the language by building something real.

A real run of the finished demo looks like this — note every block hash starts with 0000, the Proof of Work we’ll implement:

genesis hash: 0000cd8509b9b185b342c1eff4a73cfed4320208bb487f6a1cbbe93122cb0bd0
mined block 1 → Alice (height 1)
Alice: 50 coins
mined block 2 with Alice → Bob (height 2)
Alice: 90 coins
Bob: 10 coins

We keep asking the book’s thread — how does this help untrusting strangers agree on one ledger? — but now you’ll see the answer in code: it’s the hash links, the Proof of Work loop, the signature checks, and the validation rules you’re about to write with your own hands.

We build in dependency order, each chapter adding one working piece:

  1. Setup & Project Layout — install Rust, cargo new, how the crate is organized.
  2. Hashing & Fingerprints — SHA-256 in Rust; the Hash type.
  3. The Block & the Header — structs, deriving traits, encoding bytes, computing a block hash.
  4. The Blockchain: Linking BlocksVec, the genesis block, prev-hash links.
  5. Proof of Work & Mining — the difficulty target and the nonce loop.
  6. Transactions & the UTXO Model — inputs, outputs, txids, the UTXO set; enums and HashMap.
  7. Keys, Signatures & Addresses — secp256k1 keypairs, signing, verifying; external crates and Result.
  8. Merkle Trees & Proofs — folding txids into a root; recursion and slices.
  9. The Mempool & Mining Real Transactions — coinbase, fees, assembling a block; iterators.
  10. Full Chain Validation — the complete rule set that rejects double-spends, forged signatures, and inflation.
  11. Persistence: Saving the Chainserde, JSON, the ? operator.
  12. Networking: A Real P2P Node — TCP, threads, Arc<Mutex>, gossiping blocks between nodes.
  13. The CLI & Where to Go Next — tying it together, and what to build beyond this.

Start with Setup & Project Layout →