Skip to content

Revision · Build Your Own Blockchain in Rust

This part is the capstone: a small but complete blockchain built from scratch in Rust, one file at a time — the shape first (the block, the chain), then the details that make it real (Proof of Work, transactions, signatures, validation). Each chapter added one working piece, and the book’s recurring question stopped being abstract — you saw the answer in the hash links, the Proof of Work loop, the signature checks, and the validation rules.

  • Setup, hashing & the block — a cargo project with a lib/main split, a Hash type built on double SHA-256 (sha256d), and a block split into a header and body whose canonical bytes hash to its identity.
  • Linking blocks into a chain — a Vec of blocks starting from a genesis block, where each header commits to the previous hash, so any tampering with history becomes immediately obvious downstream.
  • Proof of Work & mining — the difficulty rule counts leading zero bits, and Block::mine() grinds the nonce until the hash passes; because each attempt is independent, mining is a lottery — and that randomness is the point.
  • Transactions & the UTXO modelOutPoint, inputs and outputs, the distinction between txid and sighash, and a UTXO set that represents the chain’s current state and lets you compute balances by summing unspent outputs.
  • Keys, signatures & addresses — a Wallet built on the secp256k1 crate generates keypairs, signs transactions, and maps a public key to an address, so only the owner can spend a coin.
  • Merkle trees & proofs — folding txids into a single root by recursively hashing pairs, then producing and verifying a compact proof that a transaction belongs to a block.
  • Mempool & mining real transactions — minting the coinbase reward, collecting fees, and assembling waiting transactions into a mined block.
  • Full chain validation — the complete rule set (validate_tx, validate_block) that rejects double-spends, forged signatures, and inflation — the code equivalent of consensus.
  • Persistence, networking & the CLI — saving blocks with serde/JSON, a real TCP P2P node using threads and Arc<Mutex> to gossip blocks between peers, and a CLI tying demo and serve together, plus where to go next (difficulty retargeting, reorgs, Schnorr).

Building the chain locks in everything the book taught: consensus is just hash links plus a Proof of Work loop plus signature and validation rules, all of which you now have in code that compiles, runs, and rejects cheating. The pointers at the end — real difficulty retargeting, the longest-chain rule, Taproot signatures — are the on-ramp from this toy chain to the real protocol you’ve spent the whole book understanding.