Skip to content

Repository files navigation

AlgoChain - Blockchain Simulation

Data Structures and Algorithms Semester Project

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.


πŸ“‹ Project Overview

Purpose

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

What is AlgoChain?

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

🎯 Core Features

Blockchain Functionality

  • 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

User Interfaces

  1. CLI Application: Enhanced console interface with colored output and professional logging
  2. GUI Application: Modern SFML-based graphical interface with animations and interactive controls

🧠 Data Structures & Algorithms Implementation

Data Structures Used

  1. Vector (Dynamic Array):

    • Stores blockchain as a linked list
    • Transaction pool management
    • Time: O(1) access, O(n) traversal
  2. Unordered Map (Hash Table):

    • Account balance tracking
    • Time: O(1) average case for lookups
    • Space: O(u) where u = unique addresses
  3. Stack (LIFO):

    • Undo functionality for blocks
    • Time: O(1) push/pop operations
  4. Queue (FIFO):

    • Pending transaction pool management
    • Time: O(1) enqueue/dequeue
  5. Binary Tree (Merkle Tree):

    • Transaction verification
    • Time: O(log n) verification
    • Space: O(n) for n transactions

Algorithms Implemented

  1. Hashing Algorithm:

    • Custom hash function for blocks and transactions
    • Creates unique fingerprints for data integrity
    • Time: O(n) where n = data size
  2. Proof of Work:

    • Mining algorithm with nonce finding
    • Adjustable difficulty (leading zeros)
    • Time: O(n * d) where d = difficulty level
  3. Merkle Tree Construction:

    • Binary tree traversal for hash computation
    • Bottom-up tree building
    • Time: O(n) for n transactions
  4. Chain Validation:

    • Recursive validation of all blocks
    • Hash verification and chain integrity
    • Time: O(n * m) where n = blocks, m = transactions per block
  5. Balance Calculation:

    • Hash map lookup and updates
    • Transaction validation
    • Time: O(1) average case

Algorithm Complexity Analysis

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

πŸš€ Getting Started

Prerequisites

  • 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

Installation

Linux (Ubuntu/Debian)

# Install SFML for GUI support
sudo apt-get update
sudo apt-get install libsfml-dev

# Navigate to project directory
cd blockchain-simulation

Windows

Download SFML from sfml-dev.org and extract to project directory.

Compilation

CLI Application (No dependencies)

g++ cli/main.cpp blockchain.cpp block.cpp transaction.cpp merkle_tree.cpp logger.cpp -o cli/algochain_console -std=c++17

GUI Application (Requires SFML)

Linux:

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++17

Windows:

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

Running the Applications

# CLI Version
./cli/algochain_console

# GUI Version
./algochain_gui

πŸ“± Usage Guide

CLI Interface Features

Main Menu Options:

  1. Add Transaction: Create transactions between addresses
  2. Mine Block: Process pending transactions into new block
  3. Display Blockchain: View entire blockchain with details
  4. Validate Blockchain: Verify blockchain integrity
  5. Display Transaction Pool: View pending transactions
  6. Display Balance: Check account balance
  7. Undo Last Block: Revert most recent block
  8. Display Statistics: View blockchain metrics
  9. Exit: Close application

GUI Interface Features

  • Visual blockchain explorer with scrolling
  • Interactive transaction creation forms
  • Real-time mining animation
  • Balance checking interface
  • Transaction pool visualization
  • Professional visual feedback

Example Workflow

  1. Start Application: Launch CLI or GUI version
  2. Add Transactions: Create multiple transactions between addresses
  3. Mine Block: Process transactions and add to blockchain
  4. View Blockchain: Examine the created blocks and transactions
  5. Check Balances: Verify account balances after transactions
  6. Validate Chain: Ensure blockchain integrity
  7. Undo (Optional): Revert last block if needed

πŸ“ Project Structure

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

πŸ” Security & Validation

Hash-Based Security

  • Each block contains cryptographic hash of its contents
  • Any tampering changes the hash, breaking the chain
  • Previous hash links create immutable chain structure

Merkle Tree Verification

  • Efficient proof of transaction inclusion
  • Tamper-evident data structure
  • O(log n) verification complexity

Transaction Validation

  • Balance verification before transaction
  • Prevents double-spending
  • Maintains accurate account states
  • Input validation for all user data

πŸŽ“ Educational Value & Learning Outcomes

Data Structures Concepts Demonstrated

  • 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

Algorithm Concepts Demonstrated

  • 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

Software Engineering Principles

  • 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

πŸ”§ Configuration & Customization

Adjustable Parameters

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

Extension Ideas for Further Development

  • 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

πŸ› Troubleshooting

Common Issues

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

πŸ“Š Testing & Validation

Initial Test Data

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)

Recommended Test Scenarios

  1. Basic Transaction Flow: Add transactions β†’ Mine β†’ Verify balances
  2. Chain Validation: Mine multiple blocks β†’ Validate integrity
  3. Undo Functionality: Mine blocks β†’ Undo β†’ Verify state restoration
  4. Balance Validation: Attempt transaction with insufficient funds
  5. Mining Difficulty: Test with different difficulty levels

πŸ“„ License

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.


πŸ‘¨β€πŸ’» Author

Sanaullah Turab

  • Data Structures and Algorithms Semester Project
  • University Semester Project - 2025

πŸ™ Acknowledgments

  • Course: Data Structures and Algorithms
  • Technologies: C++17, SFML Graphics Library
  • Inspiration: Bitcoin and Ethereum blockchain implementations
  • Educational Resources: Various blockchain tutorials and documentation

πŸ“ž Support & Contact

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

About

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.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages