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.
What this part covered
Section titled “What this part covered”- Setup, hashing & the block — a
cargoproject with a lib/main split, aHashtype 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
Vecof 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 model —
OutPoint, inputs and outputs, the distinction betweentxidandsighash, and a UTXO set that represents the chain’s current state and lets you compute balances by summing unspent outputs. - Keys, signatures & addresses — a
Walletbuilt 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 andArc<Mutex>to gossip blocks between peers, and a CLI tyingdemoandservetogether, plus where to go next (difficulty retargeting, reorgs, Schnorr).
The takeaway
Section titled “The takeaway”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.