Episode 6 — Scaling Reliability Microservices Web3 / 6.10 — Web3 Basics

6.10.b — Blockchain Fundamentals

In one sentence: A blockchain is a distributed, immutable ledger where data is stored in cryptographically linked blocks, maintained by a network of nodes that agree on the state of the ledger through consensus mechanisms — eliminating the need for a central authority.

Navigation: <- 6.10.a — Understanding Web3 | 6.10.c — Decentralized Applications ->


1. What Is a Blockchain?

At its simplest, a blockchain is a database — but a very unusual one. Instead of a single company running MySQL on a server, a blockchain is a database that is:

  • Distributed — copied across thousands of computers (nodes) worldwide
  • Append-only — you can add data but never modify or delete existing data
  • Cryptographically secured — each entry is mathematically linked to the previous one
  • Consensus-driven — all nodes must agree before new data is added
Traditional Database:                  Blockchain:

  ┌──────────────┐                     ┌───┐ ┌───┐ ┌───┐ ┌───┐
  │   Company     │                     │ N1│ │ N2│ │ N3│ │ N4│  ← thousands of nodes
  │   Database    │                     └─┬─┘ └─┬─┘ └─┬─┘ └─┬─┘
  │ (single copy) │                       │     │     │     │
  └──────┬───────┘                     ┌──┴─────┴─────┴─────┴──┐
         │                             │  Same data on every    │
    One admin                          │  node. No single admin.│
    controls everything                └────────────────────────┘

Think of it like a Google Doc that everyone can read, anyone can add to, but no one can edit or delete previous entries — and every participant has a complete copy.


2. Blocks, Chains, and Hashes

What Is a Block?

A block is a container that holds a batch of transactions (data). Each block contains:

┌───────────────────────────────────────────┐
│  BLOCK #17,456,789                         │
│                                            │
│  Header:                                   │
│    - Previous Block Hash: 0x8a3f...c29d    │
│    - Timestamp: 2024-03-15 14:23:07 UTC    │
│    - Nonce: 2,847,291                      │
│    - Merkle Root: 0x5b2e...a81f            │
│                                            │
│  Body (Transactions):                      │
│    TX1: Alice sends 1 ETH to Bob           │
│    TX2: Charlie calls swap() on Uniswap    │
│    TX3: Dave mints an NFT                  │
│    TX4: Eve deploys a smart contract       │
│    ... (hundreds of transactions)          │
│                                            │
│  Block Hash: 0x7d4f...e612                 │
│  (computed from ALL of the above)          │
└───────────────────────────────────────────┘

What Is a Hash?

A cryptographic hash is a function that takes any input and produces a fixed-size output (a "fingerprint"). The key properties:

Properties of cryptographic hashes (SHA-256):

1. DETERMINISTIC — same input always produces same output
   SHA256("Hello") → "185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969"
   SHA256("Hello") → "185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969"  (same!)

2. AVALANCHE EFFECT — tiny change in input = completely different output
   SHA256("Hello")  → "185f8db3..."
   SHA256("Hello!") → "334d016f..."  (totally different!)

3. ONE-WAY — cannot reverse-engineer the input from the output
   "185f8db3..." → ??? (computationally impossible to find input)

4. COLLISION-RESISTANT — practically impossible to find two inputs with same output

How Blocks Form a Chain

Each block contains the hash of the previous block, creating an unbreakable chain:

  Block #1              Block #2              Block #3
┌──────────────┐    ┌──────────────┐    ┌──────────────┐
│ Prev: 0x0000 │    │ Prev: 0xAABB │    │ Prev: 0xCCDD │
│ Data: ...    │    │ Data: ...    │    │ Data: ...    │
│ Hash: 0xAABB │───>│ Hash: 0xCCDD │───>│ Hash: 0xEEFF │
└──────────────┘    └──────────────┘    └──────────────┘

If you change ANY data in Block #1:
  - Block #1's hash changes (avalanche effect)
  - Block #2's "previous hash" no longer matches
  - Block #2's hash changes
  - Block #3's "previous hash" no longer matches
  - ... the ENTIRE chain after the tampered block breaks

This is why blockchains are tamper-evident: changing historical data requires recomputing every subsequent block, which is computationally infeasible on a large network.


3. How Consensus Works

The fundamental challenge: if thousands of nodes maintain copies of the same database, how do they agree on which transactions are valid and in what order?

This is the consensus problem, and different blockchains solve it differently.

Proof of Work (PoW)

Used by Bitcoin (and Ethereum before September 2022).

How Proof of Work works:

1. Pending transactions sit in a "mempool" (waiting room)
2. Miners (specialized computers) compete to solve a math puzzle:
   "Find a number (nonce) where SHA256(block_data + nonce) starts with N zeros"
3. This requires TRILLIONS of random guesses (brute force)
4. First miner to find a valid nonce wins:
   - Their block is added to the chain
   - They receive a block reward (e.g., 3.125 BTC)
   - Other miners verify the solution (instant to verify, hard to find)
5. All nodes accept the new block and start working on the next one

Security: To tamper with a block, you'd need >50% of the network's computing power
Energy: Enormous — Bitcoin uses as much electricity as some countries

Proof of Stake (PoS)

Used by Ethereum (since "The Merge", September 2022), Solana, Polygon, Cardano.

How Proof of Stake works:

1. Validators lock up (stake) cryptocurrency as collateral
   (Ethereum requires 32 ETH minimum to be a validator)
2. The protocol randomly selects a validator to propose the next block
   (probability proportional to stake amount)
3. Other validators attest (vote) that the block is valid
4. If the block is accepted:
   - Proposer earns transaction fees + tips
5. If a validator acts maliciously:
   - Their stake is "slashed" (partially or fully destroyed)

Security: To attack, you'd need to control >33% of all staked ETH (worth billions)
Energy: ~99.95% less energy than Proof of Work

PoW vs PoS Comparison

FeatureProof of Work (PoW)Proof of Stake (PoS)
Used byBitcoinEthereum, Solana, Polygon
Who validatesMiners (hardware)Validators (staked tokens)
Security modelComputational powerEconomic stake
Energy usageExtremely highVery low
Hardware neededSpecialized ASICsStandard servers
Attack costBuy >50% hash powerBuy >33% staked tokens
Block time~10 min (Bitcoin)~12 sec (Ethereum)
RewardsBlock reward + feesFees + tips
Centralization riskMining poolsLarge stakers
FinalityProbabilistic (6 blocks)Economic (2 epochs ~13 min)

4. Immutability

Immutability means that once data is written to the blockchain, it cannot be changed or deleted. This is a fundamental property, not a feature that can be toggled off.

Why immutability matters:

  TRADITIONAL DATABASE:
    UPDATE users SET balance = 1000000 WHERE id = 'hacker';
    -- A database admin can change any record at any time

  BLOCKCHAIN:
    -- Once Alice sends 1 ETH to Bob, that transaction is permanent
    -- No admin, no company, no government can reverse it
    -- The entire network would need to agree to a "hard fork" (extremely rare)

  Advantages:
    - Audit trail — perfect for financial records
    - Trust — no one can secretly change history
    - Censorship resistance — transactions cannot be blocked after inclusion

  Disadvantages:
    - Mistakes are permanent — send ETH to wrong address? Gone forever.
    - Buggy contracts — a deployed bug lives on-chain forever
    - No "right to be forgotten" — all data is permanent and public

5. Decentralized vs Distributed

These terms are related but distinct:

CENTRALIZED:        DISTRIBUTED:         DECENTRALIZED:

    ┌───┐              ┌───┐  ┌───┐         ┌───┐  ┌───┐
    │ S │              │ S1│──│ S2│         │ N1│──│ N2│
    └─┬─┘              └─┬─┘  └─┬─┘         └─┬─┘  └─┬─┘
  ┌───┼───┐              │      │              │      │
  C1  C2  C3          ┌──┴──────┴──┐        ┌──┴──────┴──┐
                      │ S3│──│ S4  │        │ N3│──│ N4  │
  One server,         └───┘  └─────┘        └───┘  └─────┘
  one owner.
                      Many servers,         Many nodes,
                      ONE owner             NO single owner
                      (e.g., Netflix CDN)   (e.g., Bitcoin)
PropertyCentralizedDistributedDecentralized
ServersOneManyMany
ControlOne entityOne entityNo single entity
ExampleTraditional DBNetflix CDNBitcoin network
FailureSingle pointRedundantHighly resilient
TrustTrust the operatorTrust the operatorTrust the protocol

Key insight: Distributed systems can be centrally controlled (Google has millions of servers but one company controls them). Decentralized systems distribute both the infrastructure and the control.


6. Public vs Private Blockchains

FeaturePublic BlockchainPrivate Blockchain
AccessAnyone can joinInvitation only
TransparencyAll data visibleRestricted visibility
ExamplesBitcoin, EthereumHyperledger, R3 Corda
SpeedSlower (global consensus)Faster (fewer nodes)
Use caseOpen finance, public DAppsEnterprise, supply chain
DecentralizationHighLow (consortium controls)
TrustTrustless (math-based)Trust the consortium

For Web3 development, we focus on public blockchains — they are the ones powering DeFi, NFTs, and the applications most developers interact with.


7. Major Blockchains Compared

BlockchainConsensusTPSBlock TimeNative TokenStrengthsUse Case Focus
BitcoinPoW~7~10 minBTCMost secure, most decentralizedStore of value, digital gold
EthereumPoS~15-30~12 secETHLargest smart contract ecosystemDeFi, NFTs, DApps, DAOs
SolanaPoS + PoH~4,000~0.4 secSOLHigh speed, low feesDeFi, gaming, high-frequency apps
PolygonPoS (L2)~7,000~2 secMATICLow cost, Ethereum-compatibleScaling Ethereum apps
ArbitrumOptimistic Rollup (L2)~4,000~0.25 secARBLow cost, inherits Ethereum securityScaling Ethereum DeFi
BaseOptimistic Rollup (L2)~2,000~2 secETHCoinbase-backed, low feesConsumer DApps

TPS = Transactions Per Second. Compare to Visa at ~65,000 TPS.


8. Gas Fees

Gas is the unit of measurement for computational effort on a blockchain. Every operation on Ethereum (or similar chains) costs gas.

Why gas exists:
  - Prevents spam (every transaction costs real money)
  - Compensates validators for processing transactions
  - Prioritizes important transactions (higher gas = faster inclusion)

Gas calculation (Ethereum):
  Gas Used × Gas Price = Transaction Fee

  Example:
    Simple ETH transfer:     21,000 gas units
    ERC-20 token transfer:   ~65,000 gas units
    Uniswap swap:            ~150,000 gas units
    NFT mint:                ~100,000-200,000 gas units
    Contract deployment:     ~1,000,000+ gas units

  If gas price = 30 gwei (1 gwei = 0.000000001 ETH):
    ETH transfer fee = 21,000 × 30 gwei = 630,000 gwei = 0.00063 ETH ≈ $2
    Uniswap swap fee = 150,000 × 30 gwei = 0.0045 ETH ≈ $14

Gas prices fluctuate based on network demand. During congestion, gas prices spike dramatically.

Layer 2 solutions drastically reduce gas costs:

  Ethereum mainnet:  Simple transfer ≈ $2-50
  Polygon:           Simple transfer ≈ $0.001-0.01
  Arbitrum:          Simple transfer ≈ $0.01-0.10
  Base:              Simple transfer ≈ $0.001-0.01

9. Block Explorers (Etherscan)

A block explorer is a search engine for the blockchain. It lets you look up any transaction, address, block, or smart contract.

Etherscan (https://etherscan.io) — Ethereum's primary block explorer

What you can look up:
  - Transaction hash → See sender, recipient, amount, gas, status
  - Wallet address  → See balance, all transactions, token holdings
  - Block number    → See all transactions in that block
  - Contract address → See source code, read/write functions, events

Popular block explorers:
  Ethereum   → etherscan.io
  Polygon    → polygonscan.com
  Solana     → solscan.io
  Arbitrum   → arbiscan.io
  Base       → basescan.org
  Bitcoin    → blockchain.com/explorer

Reading a Transaction on Etherscan

Transaction Hash: 0x7d4f...e612

  Status:         Success (green checkmark)
  Block:          17,456,789
  Timestamp:      Mar 15, 2024 2:23:07 PM UTC
  From:           0xAlice...
  To:             0xBob...
  Value:          1.5 ETH ($4,500.00)
  Transaction Fee: 0.00063 ETH ($1.89)
  Gas Price:      30 Gwei
  Gas Used:       21,000 / 21,000 (100%)

10. Transaction Lifecycle

Every blockchain transaction goes through a predictable lifecycle:

TRANSACTION LIFECYCLE:

  1. CREATE         User signs a transaction with their private key
       │
       ▼
  2. BROADCAST      Transaction is sent to the network (mempool)
       │
       ▼
  3. MEMPOOL        Transaction waits in the "waiting room"
       │            (validators pick transactions, usually highest gas first)
       ▼
  4. INCLUSION      A validator includes the transaction in a new block
       │
       ▼
  5. PROPAGATION    The new block is shared across the network
       │
       ▼
  6. CONFIRMATION   Other validators confirm the block is valid
       │            (1 confirmation = included in 1 block)
       │            (6 confirmations on Bitcoin = ~1 hour = "final")
       │            (2 epochs on Ethereum PoS = ~13 min = "finalized")
       ▼
  7. FINALITY       Transaction is considered irreversible

Transaction States

StateMeaning
PendingIn the mempool, waiting to be included
Confirmed (1)Included in one block
Confirmed (N)Included in block with N-1 blocks built on top
FinalizedIrreversible (varies by chain)
FailedExecuted but reverted (gas still consumed!)
DroppedRemoved from mempool (gas too low, replaced)

11. Merkle Trees Basics

A Merkle tree is a data structure that allows efficient verification of large datasets. Every block uses a Merkle tree to organize its transactions.

MERKLE TREE:

  Transactions:  TX1     TX2     TX3     TX4

  Step 1 — Hash each transaction:
                H(TX1)  H(TX2)  H(TX3)  H(TX4)

  Step 2 — Hash pairs together:
                H(TX1+TX2)      H(TX3+TX4)

  Step 3 — Hash the pair hashes (Merkle Root):
                    H(TX1+TX2 + TX3+TX4)
                         │
                    MERKLE ROOT  ← stored in block header

Why this matters:

To verify TX3 is in the block, you only need:
  - H(TX3)           ← hash of the transaction
  - H(TX4)           ← its sibling
  - H(TX1+TX2)       ← uncle hash

  Verify: compute H(TX3+TX4), then H(TX1+TX2 + TX3+TX4), compare to Merkle Root

  For a block with 1,000,000 transactions:
    Without Merkle tree: check all 1,000,000 transactions
    With Merkle tree:    check only ~20 hashes (log2 of 1,000,000)

12. Simple Blockchain Concept in JavaScript

Here is a minimal blockchain implementation to illustrate the core concepts:

import crypto from 'crypto';

// A single block in the chain
class Block {
  constructor(index, timestamp, data, previousHash = '') {
    this.index = index;
    this.timestamp = timestamp;
    this.data = data;
    this.previousHash = previousHash;
    this.nonce = 0;
    this.hash = this.calculateHash();
  }

  // Create a SHA-256 hash of the block's contents
  calculateHash() {
    return crypto
      .createHash('sha256')
      .update(
        this.index +
        this.previousHash +
        this.timestamp +
        JSON.stringify(this.data) +
        this.nonce
      )
      .digest('hex');
  }

  // Simple Proof of Work: find a hash that starts with N zeros
  mineBlock(difficulty) {
    const target = '0'.repeat(difficulty);
    while (this.hash.substring(0, difficulty) !== target) {
      this.nonce++;
      this.hash = this.calculateHash();
    }
    console.log(`Block mined: ${this.hash}`);
  }
}

// The blockchain itself
class Blockchain {
  constructor() {
    this.chain = [this.createGenesisBlock()];
    this.difficulty = 4; // number of leading zeros required
  }

  // The first block in every blockchain (hardcoded)
  createGenesisBlock() {
    return new Block(0, Date.now(), 'Genesis Block', '0');
  }

  getLatestBlock() {
    return this.chain[this.chain.length - 1];
  }

  addBlock(newBlock) {
    newBlock.previousHash = this.getLatestBlock().hash;
    newBlock.mineBlock(this.difficulty);
    this.chain.push(newBlock);
  }

  // Verify the entire chain is valid (no tampering)
  isChainValid() {
    for (let i = 1; i < this.chain.length; i++) {
      const current = this.chain[i];
      const previous = this.chain[i - 1];

      // Recalculate the hash — does it match?
      if (current.hash !== current.calculateHash()) {
        console.log('Current hash mismatch at block', i);
        return false;
      }

      // Does the previous hash pointer match?
      if (current.previousHash !== previous.hash) {
        console.log('Previous hash mismatch at block', i);
        return false;
      }
    }
    return true;
  }
}

// Usage
const myChain = new Blockchain();

console.log('Mining block 1...');
myChain.addBlock(new Block(1, Date.now(), { from: 'Alice', to: 'Bob', amount: 10 }));

console.log('Mining block 2...');
myChain.addBlock(new Block(2, Date.now(), { from: 'Bob', to: 'Charlie', amount: 5 }));

console.log('Is chain valid?', myChain.isChainValid()); // true

// Attempt to tamper with data
myChain.chain[1].data = { from: 'Alice', to: 'Hacker', amount: 1000 };
console.log('Is chain valid after tampering?', myChain.isChainValid()); // false!

Key observations from this code:

  1. Each block's hash depends on its content AND the previous block's hash
  2. Mining (Proof of Work) requires finding a nonce that produces a hash with leading zeros
  3. Tampering with any block invalidates the entire chain from that point forward
  4. Verification is fast (recalculate hashes), but mining is slow (brute-force search)

13. Key Takeaways

  1. A blockchain is a distributed, append-only ledger — data is stored in cryptographically linked blocks across thousands of nodes, making tampering practically impossible.
  2. Hashes are the glue — SHA-256 creates unique fingerprints for each block, and each block references the previous block's hash, forming an unbreakable chain.
  3. Consensus mechanisms solve the agreement problem — PoW uses computational effort (energy-intensive), PoS uses economic stake (energy-efficient). Ethereum switched from PoW to PoS in 2022.
  4. Immutability is both a feature and a limitation — it provides audit trails and censorship resistance, but mistakes are permanent.
  5. Gas fees compensate validators and prevent spam — they vary by operation complexity and network congestion. Layer 2 solutions dramatically reduce costs.
  6. Block explorers are your debugging tool — Etherscan lets you inspect any transaction, address, or contract on Ethereum.
  7. Merkle trees enable efficient verification — you can prove a transaction is in a block by checking ~log(N) hashes instead of all N transactions.

Explain-It Challenge

  1. Explain to a non-technical person why you cannot "hack" a blockchain by changing historical records.
  2. A colleague says "Proof of Stake is less secure than Proof of Work." Construct an argument for and against this claim.
  3. Your company processes 10,000 transactions per hour. Would Ethereum mainnet be suitable? What about a Layer 2? Calculate the approximate gas costs for both.

Navigation: <- 6.10.a — Understanding Web3 | 6.10.c — Decentralized Applications ->