Blockchain and Web3 course Β· Module 1: Blockchain Fundamentals

How Blockchain Works - Anatomy of a Block

4 min read Β·
In this lesson7

Welcome back! Atlas here. Now that you know the basics, we'll dive deeper into the anatomy of a single chamber (block) and see how tunnels (chains) connect them into an unbroken network.

Block Structure

Each chamber in our underground city has a precise structure:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         BLOCK #12345                              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  HEADER                                                          β”‚
β”‚  β”œβ”€β”€ Block Hash: 0x7f8a...3b2c (identifier of this chamber)     β”‚
β”‚  β”œβ”€β”€ Previous Hash: 0x4e2d...9a1f (connection to the previous)  β”‚
β”‚  β”œβ”€β”€ Timestamp: 2024-11-28 14:32:15                             β”‚
β”‚  β”œβ”€β”€ Nonce: 2847593                                              β”‚
β”‚  └── Merkle Root: 0x9c3f...7d8e                                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  BODY (Transactions)                                             β”‚
β”‚  β”œβ”€β”€ TX1: Alice β†’ Bob: 0.5 ETH                                  β”‚
β”‚  β”œβ”€β”€ TX2: Carol β†’ Dave: 1.2 ETH                                 β”‚
β”‚  β”œβ”€β”€ TX3: Eve β†’ Frank: 0.1 ETH                                  β”‚
β”‚  └── ... (hundreds/thousands of transactions)                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Chain of Blocks

Each block contains the hash of the previous block - this creates an unbroken chain:

     BLOCK 100               BLOCK 101               BLOCK 102
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Hash: 0xabc...  β”‚    β”‚ Hash: 0xdef...  β”‚    β”‚ Hash: 0x123...  β”‚
β”‚ Prev: 0x999...  │───▢│ Prev: 0xabc...  │───▢│ Prev: 0xdef...  β”‚
β”‚ Transactions... β”‚    β”‚ Transactions... β”‚    β”‚ Transactions... β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

If someone changed a transaction in Block 100:
- The hash of Block 100 would change
- The prev hash in Block 101 wouldn't match
- The entire chain after Block 100 would be INVALID!

Merkle Tree - Transaction Tree

Transactions in a block are organized in a Merkle Tree structure:

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  Merkle Root β”‚
                    β”‚   0x9c3f...  β”‚
                    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚                         β”‚
        β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”             β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
        β”‚  Hash AB  β”‚             β”‚  Hash CD  β”‚
        β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜             β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
              β”‚                         β”‚
       β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”
       β”‚             β”‚           β”‚             β”‚
   β”Œβ”€β”€β”€β”΄β”€β”€β”€β”     β”Œβ”€β”€β”€β”΄β”€β”€β”€β”   β”Œβ”€β”€β”€β”΄β”€β”€β”€β”     β”Œβ”€β”€β”€β”΄β”€β”€β”€β”
   β”‚ TX A  β”‚     β”‚ TX B  β”‚   β”‚ TX C  β”‚     β”‚ TX D  β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”˜

Thanks to this, you can quickly verify whether a transaction is in a block!

Consensus - How Guardians Agree

In a decentralized city, we need a mechanism for all guardians to agree on the state of the network:

Proof of Work (PoW)

  • Miners solve mathematical puzzles
  • The first one to find a solution adds the block
  • Requires a lot of energy (Bitcoin still uses this)

Proof of Stake (PoS)

  • Validators "stake" their ETH
  • Randomly selected to create blocks
  • Ethereum uses this since 2022 (The Merge)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              PROOF OF WORK vs PROOF OF STAKE                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚       PROOF OF WORK        β”‚        PROOF OF STAKE              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Mining                  β”‚ Staking                         β”‚
β”‚ Requires a lot of energyβ”‚ Energy efficient (~99.95% less)  β”‚
β”‚ Hardware needed         β”‚ 32 ETH needed                   β”‚
β”‚ Slower                  β”‚ Faster                          β”‚
β”‚ Block reward            β”‚ Validation reward               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Transaction Finalization

How many confirmations are needed for a transaction to be "safe"?

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    CONFIRMATIONS                                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                  β”‚
β”‚  Your TX in block #100                                          β”‚
β”‚                                                                  β”‚
β”‚  Block #100 β†’ #101 β†’ #102 β†’ #103 β†’ #104 β†’ #105                 β”‚
β”‚     ↑         1       2       3       4       5                  β”‚
β”‚   Your TX   confirmations                                       β”‚
β”‚                                                                  β”‚
β”‚  Security levels:                                             β”‚
β”‚  β€’ 1 confirmation  - basic (reorganization possible)             β”‚
β”‚  β€’ 6 confirmations - safe for most cases                         β”‚
β”‚  β€’ 12+ confirmations - very safe (large amounts)                 β”‚
β”‚                                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Types of Blockchain Networks

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    BLOCKCHAIN TYPES                               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                  β”‚
β”‚  PUBLIC                         β”‚  PRIVATE                       β”‚
β”‚  β€’ Ethereum, Bitcoin            β”‚  β€’ Hyperledger Fabric          β”‚
β”‚  β€’ Anyone can join              β”‚  β€’ Invitation only             β”‚
β”‚  β€’ Full transparency            β”‚  β€’ Controlled access           β”‚
β”‚  β€’ Trustless                    β”‚  β€’ Requires trust              β”‚
β”‚                                                                  β”‚
β”‚  PERMISSIONLESS                 β”‚  PERMISSIONED                  β”‚
β”‚  β€’ No permission needed         β”‚  β€’ Authorization required      β”‚
β”‚  β€’ Anonymity possible           β”‚  β€’ Known participants          β”‚
β”‚                                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Summary

In this lesson you learned:

  • Block structure (header, body, hash, transactions)
  • How the chain of blocks ensures immutability
  • Merkle Tree and transaction verification
  • Consensus mechanisms (PoW vs PoS)
  • Confirmations and transaction finalization
  • Types of blockchain networks

In the next lesson, we'll explore the practical applications of blockchain!

Code for this lesson: index.js
// Simulation of a blockchain
class Block {
  constructor(index, data, previousHash) {
    this.index = index;
    this.timestamp = Date.now();
    this.data = data;
    this.previousHash = previousHash;
    this.hash = this.calculateHash();
  }

  calculateHash() {
    const str = this.index + this.timestamp + JSON.stringify(this.data) + this.previousHash;
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
      hash = ((hash << 5) - hash) + str.charCodeAt(i);
      hash = hash & hash;
    }
    return Math.abs(hash).toString(16).padStart(16, '0');
  }
}

class Blockchain {
  constructor() {
    this.chain = [this.createGenesisBlock()];
  }

  createGenesisBlock() {
    return new Block(0, "Genesis Block", "0");
  }

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

  addBlock(data) {
    const newBlock = new Block(
      this.chain.length,
      data,
      this.getLatestBlock().hash
    );
    this.chain.push(newBlock);
  }
}

const myBlockchain = new Blockchain();
myBlockchain.addBlock({ from: "Atlas", to: "User", amount: 100 });
myBlockchain.addBlock({ from: "User", to: "Atlas", amount: 50 });

console.log("=== BLOCKCHAIN ===");
myBlockchain.chain.forEach((block, i) => {
  console.log(`\nBlock #${i}:`);
  console.log(`  Hash: ${block.hash}`);
  console.log(`  Previous: ${block.previousHash}`);
  console.log(`  Data: ${JSON.stringify(block.data)}`);
});

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. Which element is NOT part of the block header?

  2. 2. What happens if someone changes a transaction in block #100?

  3. 3. Since 2022, Ethereum has used:

  4. 4. How many confirmations are considered safe?

  5. 5. The main difference between Bitcoin and Ethereum is:

  6. 6. On a testnet (e.g. Sepolia) you can:

  7. 7. Ethereum is an example of a blockchain that is:

  8. 8. A node in a blockchain network is:

  9. 9. A hash function (e.g. SHA-256) always:

  10. 10. Immutability in blockchain means:

Hands-on tasks in the game

  • Code editor

    Complete the isChainValid function that checks whether the chain has been tampered with

  • Vertical ordering

    Arrange the steps for creating a new block:

  • Horizontal ordering

    Arrange the elements of a blockchain transaction:

  • Code editor

    Complete the mineBlock function that finds a hash starting with a specified number of zeros

  • Click in order

    Click, in order, the steps of Proof of Work:

  • Vertical ordering

    Arrange the advantages of Proof of Stake over Proof of Work:

  • Code editor

    Complete the Node class with a broadcastBlock method for broadcasting blocks

  • Horizontal ordering

    Arrange the code that creates the Genesis Block:

  • Click in order

    Click the networks in order from oldest to newest:

  • Code editor

    Complete the comparison object with properties for Bitcoin and Ethereum

  • Vertical ordering

    Arrange the networks from production to test:

  • Code editor

    Complete the networks object with configuration for mainnet, sepolia, and localhost

  • Horizontal ordering

    Arrange: A public blockchain is ...

  • Click in order

    Click the types of nodes in the Ethereum network:

  • Code editor

    Complete the Blockchain class with the methods createGenesisBlock, getLatestBlock, addBlock

  • Vertical ordering

    Arrange the properties of a good hash function:

  • Code editor

    Complete the simpleHash function that converts a string into a number

  • Horizontal ordering

    Arrange the code that adds a block to the chain:

  • Click in order

    Click the steps for building a simple blockchain:

  • Code editor

    Create a blockchain, add 3 blocks, and verify the chain

  1. QuestionQuiz: What Does a Block Contain?
  2. Coding taskChain Validation
  3. Put in orderThe Block Creation Process
  4. QuestionQuiz: The Block Chain
  5. Put in orderTransaction Components
  6. Coding taskSimple Proof of Work
  7. QuestionQuiz: PoW vs PoS
  8. Click in orderClick the PoW Elements
  9. Put in orderComparing PoW and PoS
  10. Coding taskP2P Network Simulation
  11. QuestionQuiz: Confirmations
  12. Put in orderBuilding the Genesis Block
  13. QuestionQuiz: Bitcoin vs Ethereum
  14. Click in orderClick the Blockchain Networks
  15. Coding taskNetwork Comparison
  16. QuestionQuiz: Mainnet vs Testnet
  17. Put in orderNetwork Hierarchy
  18. Coding taskNetwork Configuration
  19. QuestionQuiz: Types of Blockchain
  20. Put in orderTypes of Blockchain
  21. QuestionQuiz: Node
  22. Click in orderClick the Node Types
  23. Coding taskThe Blockchain Class
  24. QuestionQuiz: Hash
  25. Put in orderProperties of a Hash
  26. Coding taskHashing Simulation
  27. QuestionQuiz: Immutability
  28. Put in orderCalling Blockchain Methods
  29. Click in orderClick in the Order of Building a Blockchain
  30. Coding taskFull Implementation
  31. Project to submitSend Your Project to a Mentor
  32. ConversationEnd of Module 1