Blockchain and Web3 course Β· Module 1: Blockchain Fundamentals
How Blockchain Works - Anatomy of a Block
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. Which element is NOT part of the block header?
2. What happens if someone changes a transaction in block #100?
3. Since 2022, Ethereum has used:
4. How many confirmations are considered safe?
5. The main difference between Bitcoin and Ethereum is:
6. On a testnet (e.g. Sepolia) you can:
7. Ethereum is an example of a blockchain that is:
8. A node in a blockchain network is:
9. A hash function (e.g. SHA-256) always:
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
Tasks after this lesson
- QuestionQuiz: What Does a Block Contain?
- Coding taskChain Validation
- Put in orderThe Block Creation Process
- QuestionQuiz: The Block Chain
- Put in orderTransaction Components
- Coding taskSimple Proof of Work
- QuestionQuiz: PoW vs PoS
- Click in orderClick the PoW Elements
- Put in orderComparing PoW and PoS
- Coding taskP2P Network Simulation
- QuestionQuiz: Confirmations
- Put in orderBuilding the Genesis Block
- QuestionQuiz: Bitcoin vs Ethereum
- Click in orderClick the Blockchain Networks
- Coding taskNetwork Comparison
- QuestionQuiz: Mainnet vs Testnet
- Put in orderNetwork Hierarchy
- Coding taskNetwork Configuration
- QuestionQuiz: Types of Blockchain
- Put in orderTypes of Blockchain
- QuestionQuiz: Node
- Click in orderClick the Node Types
- Coding taskThe Blockchain Class
- QuestionQuiz: Hash
- Put in orderProperties of a Hash
- Coding taskHashing Simulation
- QuestionQuiz: Immutability
- Put in orderCalling Blockchain Methods
- Click in orderClick in the Order of Building a Blockchain
- Coding taskFull Implementation
- Project to submitSend Your Project to a Mentor
- ConversationEnd of Module 1