This is tool to demonstrate how blockchain technology works. It provides a visual, interactive way to understand the core concepts of blockchain, including mining, hashing, and chain validation.
While blockchain shares some characteristics with traditional data structures, it's important to understand its unique properties:
-
Similarities to Linked Lists:
- Like a linked list, blockchain uses pointers (hashes) to connect elements
- Each block references the previous block, similar to a singly-linked list
- The chain maintains a strict order of elements
-
Key Differences from Linked Lists:
- Immutability: Once a block is added, its data cannot be changed without invalidating the chain
- Cryptographic Links: Instead of simple memory pointers, blocks are linked using cryptographic hashes
- Proof of Work: Each block requires computational work (mining) to be added
- Consensus Mechanism: The chain's validity is determined by network consensus, not just local state
-
Why It's Not Just a Doubly-Linked List:
- A doubly-linked list allows bidirectional traversal and easy modification
- Blockchain is designed for immutability and distributed consensus
- The mining process adds a unique computational barrier to modification
- The chain's integrity is verified through cryptographic proofs, not just pointer validity
-
Unique Properties:
- Distributed: Multiple copies exist across a network
- Consensus-Based: Changes require network agreement
- Cryptographically Secure: Links are verified through mathematical proofs
- Immutable History: Past blocks cannot be modified without affecting all subsequent blocks
This architecture makes blockchain more than just a data structure - it's a distributed system that uses cryptographic principles to maintain data integrity across a network.
https://blockchain-learner.netlify.app/
-
Clone the repository:
git clone https://github.com/yourusername/blockchain-demo.git cd blockchain-demo
-
Using a Local Server (recommended):
- Using Python:
# Python 3 python -m http.server 8000 # Python 2 python -m SimpleHTTPServer 8000
- Using Node.js:
# Install http-server globally npm install -g http-server # Run the server http-server
- Using VS Code:
- Install the "Live Server" extension
- Right-click on
index.html
- Select "Open with Live Server"
- Using Python:
-
Open in Browser:
- Navigate to
http://localhost:8000
(or the port shown in your terminal) - The demo should now be running!
- Navigate to
While you can open index.html
directly in your browser, some features (like the Web Crypto API) may not work due to security restrictions. Using a local server is recommended.
If you encounter issues:
- Make sure you're using a modern browser (Chrome, Firefox, Safari, or Edge)
- Check the browser's console (F12) for any error messages
- Ensure you're accessing the site through
http://
orhttps://
(notfile://
) - Try clearing your browser cache if you see unexpected behavior
-
Adding Blocks:
- Click the "Add new block" button on the last block to add a new block
- Alternatively, use the "Add New Block" button at the top
- Each block starts unmined (red in the visualization)
-
Mining Blocks:
- Click "Mine Block" on any unmined block
- Watch the nonce change as it searches for a valid hash
- When successful, the block turns green
- The "Add new block" button will appear on the last block in the chain
-
Adjusting Difficulty:
- Use the difficulty selector to change mining difficulty
- More leading zeros = longer mining time
- Each additional zero makes it 16x harder to find a valid hash
-
Editing Data:
- Change the data in any block's text area
- Notice how the hash changes immediately
- The block becomes invalid until re-mined
- Invalidating a block breaks the chain
-
Chain Validation:
- The chain status shows if all blocks are valid
- Invalid blocks are highlighted in red
- Previous hash mismatches are visually indicated
- Mining a block updates all subsequent blocks
-
Export/Import:
- Save your blockchain state using "Export Chain"
- Load a previous state using "Import Chain"
- Create and mine blocks in real-time
- Visual representation of the blockchain network
- Chain validation and integrity checking
- Export/import blockchain state
- Mining statistics and performance metrics
- Interactive block data editing
-
Block: A container that holds:
- Data (any information you want to store)
- Timestamp (when the block was created)
- Previous block's hash (creates the chain)
- Nonce (a number used in mining)
- Hash (a unique digital fingerprint)
-
Hash: A unique digital fingerprint (SHA-256) of the block's contents. It:
- Changes if any content changes
- Must start with "00" to be considered valid (mined)
- Links blocks together in the chain
-
Mining: The process of finding a valid hash by:
- Adjusting the nonce (a random number)
- Recalculating the hash until it starts with "00"
- This simulates the computational work in real blockchains
-
Chain Validation: The process of ensuring:
- Each block's hash starts with "00"
- Each block correctly references the previous block's hash
- No data has been tampered with
- Nonce: A number that can be changed to create different hashes. In mining, we try different nonces until we find one that creates a valid hash.
- Genesis Block: The first block in the chain, which has no previous block to reference.
- Previous Hash: A reference to the previous block's hash, creating the "chain" in blockchain.
- Difficulty: In this demo, the requirement that a block's hash must start with "00" to be considered valid. In real blockchains, this is much more complex.
Mining is the process of finding a valid hash for a block. Let's break down how it works and why it's important:
In blockchain technology, mining is like solving a complex mathematical puzzle. In this demo, the puzzle is simple: find a hash that starts with "00". In real cryptocurrencies like Bitcoin, the puzzle is much more complex, requiring many more leading zeros.
-
The Process:
- Start with a block containing data, timestamp, and previous block's hash
- Add a random number (nonce)
- Calculate the hash of all this information
- If the hash doesn't meet the requirements (starts with "00" in this demo), change the nonce and try again
- Keep trying until you find a valid hash
-
Why It's Hard:
- Hash functions are one-way: you can't predict what input will give you a specific hash
- The only way to find a valid hash is to try different nonces
- Each attempt requires computing a new hash
- The more leading zeros required, the harder it becomes
-
In This Demo:
- We require hashes to start with "00"
- This is a simplified version of real blockchain difficulty
- Mining time varies based on:
- Random chance (you might get lucky with an early nonce)
- Your computer's processing power
- The current state of the block's data
-
In Real Blockchains:
- Difficulty automatically adjusts to maintain a target block time
- For example, Bitcoin aims for 10 minutes per block
- As more miners join the network:
- The difficulty increases
- The required number of leading zeros goes up
- Mining time stays roughly constant
- This is called "difficulty adjustment"
-
Security:
- Makes it computationally expensive to create new blocks
- Prevents malicious actors from easily creating fake blocks
- Protects the blockchain's integrity
-
Consensus:
- Miners compete to find valid hashes
- The first to find a valid hash gets to add their block
- This creates a fair, decentralized system
-
Resource Investment:
- Mining requires significant computational power
- This investment makes it costly to attack the network
- Creates economic incentives for honest behavior
-
Similarities:
- Uses cryptographic hashing
- Requires finding a specific hash pattern
- Links blocks together securely
-
Differences:
- This demo uses a simple "00" requirement
- Real blockchains use much more complex requirements
- This demo doesn't include rewards or competition
- Real blockchains have network-wide difficulty adjustment
This project is licensed under the MIT License - see the LICENSE file for details.