A comprehensive blockchain simulation implementing fundamental data structures and algorithms concepts in C++. This educational project demonstrates blockchain technology principles through both Command-Line Interface (CLI) and Graphical User Interface (GUI) applications.
This project serves as a semester project for Data Structures and Algorithms (DSA) course, demonstrating practical application of core DSA concepts through blockchain technology simulation. It provides hands-on experience with:
- Advanced data structures implementation
- Algorithm design and analysis
- Object-oriented programming in C++
- Software architecture and design patterns
- GUI development with SFML
AlgoChain is an educational blockchain simulation that demonstrates how blockchain technology works under the hood. It implements core blockchain concepts including proof-of-work mining, transaction processing, hash-based security, Merkle tree verification, and decentralized ledger management.
The project features:
- Dual interfaces: Command-line and graphical user interface
- Real blockchain mechanics: Mining, transaction validation, chain verification
- Educational focus: Clear demonstration of DSA concepts in a real-world application
- Professional logging: Timestamped, colored console output with file logging
- Genesis Block: Automatic blockchain initialization
- Transaction Management: Create and validate transactions with balance verification
- Proof of Work Mining: Configurable difficulty mining algorithm
- Chain Validation: Complete blockchain integrity verification
- Merkle Tree: Efficient transaction verification using binary tree
- Undo Functionality: Stack-based block reversal mechanism
- Transaction Pool: Queue-based pending transaction management
- Balance Tracking: Real-time account balance using hash maps
- Mining Rewards: Incentive system for successful block mining
- CLI Application: Enhanced console interface with colored output and professional logging
- GUI Application: Modern SFML-based graphical interface with animations and interactive controls
-
Vector (Dynamic Array):
- Stores blockchain as a linked list
- Transaction pool management
- Time: O(1) access, O(n) traversal
-
Unordered Map (Hash Table):
- Account balance tracking
- Time: O(1) average case for lookups
- Space: O(u) where u = unique addresses
-
Stack (LIFO):
- Undo functionality for blocks
- Time: O(1) push/pop operations
-
Queue (FIFO):
- Pending transaction pool management
- Time: O(1) enqueue/dequeue
-
Binary Tree (Merkle Tree):
- Transaction verification
- Time: O(log n) verification
- Space: O(n) for n transactions
-
Hashing Algorithm:
- Custom hash function for blocks and transactions
- Creates unique fingerprints for data integrity
- Time: O(n) where n = data size
-
Proof of Work:
- Mining algorithm with nonce finding
- Adjustable difficulty (leading zeros)
- Time: O(n * d) where d = difficulty level
-
Merkle Tree Construction:
- Binary tree traversal for hash computation
- Bottom-up tree building
- Time: O(n) for n transactions
-
Chain Validation:
- Recursive validation of all blocks
- Hash verification and chain integrity
- Time: O(n * m) where n = blocks, m = transactions per block
-
Balance Calculation:
- Hash map lookup and updates
- Transaction validation
- Time: O(1) average case
Time Complexities:
- Add Transaction: O(1)
- Mine Block: O(n * d) where n = transactions, d = difficulty
- Validate Chain: O(n * m) where n = blocks, m = transactions per block
- Get Balance: O(1) average case
- Undo Block: O(n) where n = transactions in block
- Search Transaction: O(n) linear search
Space Complexities:
- Blockchain Storage: O(n * m) where n = blocks, m = transactions
- Balance Map: O(u) where u = unique addresses
- Undo Stack: O(k) where k = number of undoable blocks
- Merkle Tree: O(n) where n = transactions
- Transaction Pool: O(p) where p = pending transactions
- C++ Compiler: GCC 7+ or Clang 5+ with C++17 support
- SFML Library: Required only for GUI version (2.5+ recommended)
- Operating System: Linux, Windows, or macOS
# Install SFML for GUI support
sudo apt-get update
sudo apt-get install libsfml-dev
# Navigate to project directory
cd blockchain-simulationDownload SFML from sfml-dev.org and extract to project directory.
g++ cli/main.cpp blockchain.cpp block.cpp transaction.cpp merkle_tree.cpp logger.cpp -o cli/algochain_console -std=c++17Linux:
g++ main_gui.cpp gui.cpp blockchain.cpp block.cpp transaction.cpp merkle_tree.cpp logger.cpp -o algochain_gui -lsfml-graphics -lsfml-window -lsfml-system -std=c++17Windows:
g++ -I"SFML/include" -L"SFML/lib" main_gui.cpp gui.cpp blockchain.cpp block.cpp transaction.cpp merkle_tree.cpp logger.cpp -lsfml-graphics -lsfml-window -lsfml-system -o algochain_gui.exe# CLI Version
./cli/algochain_console
# GUI Version
./algochain_guiMain Menu Options:
- Add Transaction: Create transactions between addresses
- Mine Block: Process pending transactions into new block
- Display Blockchain: View entire blockchain with details
- Validate Blockchain: Verify blockchain integrity
- Display Transaction Pool: View pending transactions
- Display Balance: Check account balance
- Undo Last Block: Revert most recent block
- Display Statistics: View blockchain metrics
- Exit: Close application
- Visual blockchain explorer with scrolling
- Interactive transaction creation forms
- Real-time mining animation
- Balance checking interface
- Transaction pool visualization
- Professional visual feedback
- Start Application: Launch CLI or GUI version
- Add Transactions: Create multiple transactions between addresses
- Mine Block: Process transactions and add to blockchain
- View Blockchain: Examine the created blocks and transactions
- Check Balances: Verify account balances after transactions
- Validate Chain: Ensure blockchain integrity
- Undo (Optional): Revert last block if needed
blockchain-simulation/
βββ Core Components
β βββ blockchain.cpp/.h # Main blockchain implementation
β βββ block.cpp/.h # Block structure and mining logic
β βββ transaction.cpp/.h # Transaction handling and validation
β βββ merkle_tree.cpp/.h # Merkle tree implementation
β βββ logger.cpp/.h # Logging system with colors
β
βββ User Interfaces
β βββ cli/
β β βββ main.cpp # Console application entry point
β βββ gui.cpp/.h # GUI implementation with SFML
β βββ main_gui.cpp # GUI application entry point
β
βββ Executables (after compilation)
β βββ algochain_gui # GUI executable
β βββ cli/algochain_console # CLI executable
β
βββ Documentation
β βββ README.md # This file
β βββ LICENSE # Project license
β
βββ Logs (generated at runtime)
βββ blockchain.log # Transaction and mining logs
- Each block contains cryptographic hash of its contents
- Any tampering changes the hash, breaking the chain
- Previous hash links create immutable chain structure
- Efficient proof of transaction inclusion
- Tamper-evident data structure
- O(log n) verification complexity
- Balance verification before transaction
- Prevents double-spending
- Maintains accurate account states
- Input validation for all user data
- Linked Lists: Blockchain as linked block structure
- Hash Tables: Fast balance lookups and tracking
- Binary Trees: Merkle tree for transaction verification
- Stacks: LIFO operations for undo functionality
- Queues: FIFO transaction pool management
- Dynamic Arrays: Flexible transaction storage
- Hashing: Cryptographic data integrity
- Proof of Work: Computational puzzle solving
- Tree Traversal: Binary tree operations
- Search Algorithms: Transaction and balance lookups
- Validation Algorithms: Chain integrity verification
- Time/Space Complexity: Real-world trade-offs
- Object-oriented design and encapsulation
- Separation of concerns (MVC-like pattern)
- Code reusability and modularity
- Error handling and input validation
- Professional logging and debugging
- Cross-platform compatibility
Located in blockchain initialization:
- Mining Difficulty: 2-5 (number of leading zeros in hash)
- Mining Reward: Default 10 coins per block
- Initial Balances: Pre-configured test accounts
- Log Level: DEBUG, INFO, WARN, ERROR, SUCCESS
- Implement actual SHA-256 cryptographic hashing
- Add digital signatures for transaction authentication
- Implement consensus mechanisms (PoS, DPoS)
- Add network simulation with multiple nodes
- Implement smart contract functionality
- Database persistence (SQLite integration)
- RESTful API for blockchain interaction
- Web-based dashboard interface
Compilation Errors:
- Ensure C++17 support is enabled (
-std=c++17) - Verify SFML installation for GUI version
- Check file paths are correct
SFML-Related (GUI):
- Install SFML development libraries
- Ensure library paths are correct in compilation command
- Font warnings are non-critical (GUI works with default)
Performance Issues:
- Lower mining difficulty for faster mining (2-3)
- Limit transaction pool size for better performance
- Monitor memory usage with large blockchains
Linux-Specific:
- May need to install g++ compiler:
sudo apt-get install g++ - SFML libraries:
sudo apt-get install libsfml-dev
The blockchain initializes with:
- Genesis Block: Block 0 with initial state
- Test Accounts: Pre-funded accounts for testing
- Zain: 1000 coins
- Ali: 1000 coins
- Usman: 1000 coins
- SYSTEM: 1,000,000 coins (for mining rewards)
- Basic Transaction Flow: Add transactions β Mine β Verify balances
- Chain Validation: Mine multiple blocks β Validate integrity
- Undo Functionality: Mine blocks β Undo β Verify state restoration
- Balance Validation: Attempt transaction with insufficient funds
- Mining Difficulty: Test with different difficulty levels
Copyright Β© 2025 Sanaullah Turab. All Rights Reserved.
This project is created as a semester project for educational purposes. All rights are reserved by the author.
Restrictions:
- This code may not be copied, modified, or distributed without explicit permission
- Commercial use is strictly prohibited
- Academic use requires proper attribution
For permissions or inquiries, please contact the author.
Sanaullah Turab
- Data Structures and Algorithms Semester Project
- University Semester Project - 2025
- Course: Data Structures and Algorithms
- Technologies: C++17, SFML Graphics Library
- Inspiration: Bitcoin and Ethereum blockchain implementations
- Educational Resources: Various blockchain tutorials and documentation
For questions, bug reports, or suggestions regarding this semester project:
- Open an issue on the repository
- Contact through university channels
AlgoChain - A Comprehensive Data Structures and Algorithms Project Demonstrating Blockchain Technology
Built with C++ β’ SFML β’ Data Structures β’ Algorithms β’ Educational Purpose