- README1.md: This file - project overview and quick start
- ARCHITECTURE.md: Detailed system design, components, and patterns
- docs/COMPLEXITY_ANALYSIS.md: Performance analysis and optimization strategies
- docs/VALIDATION_GUIDE.md: Comprehensive validation testing guide
ARCHITECTURE.md covers:
- Component responsibilities and interfaces
- Data flow diagrams
- Security model and guarantees
- Design patterns used
- Future enhancement roadmap
COMPLEXITY_ANALYSIS.md covers:
- Time complexity: O(nĆm) for duplicate detection (current)
- Optimization path: Hash set for O(1) lookups
- Space complexity analysis
- Scalability limits and benchmarking
- Trade-off analysis for different approaches
VALIDATION_GUIDE.md covers:
- Multi-layer validation explanation
- Test case examples
- Error message reference
- Customizing validation rules
- Debugging tips## š Example Output
When you run the program, you'll see comprehensive test results:
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Blockchain Voting System - Vote Validation Tests ā
ā Testing duplicate detection & field validation ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
============================================================
TEST 1: Valid Votes
============================================================
Adding valid votes to Block #1:
ā Vote accepted: VOTER-A1B2C3 -> Alice Johnson
ā Vote accepted: VOTER-D4E5F6 -> Bob Smith
ā Vote accepted: VOTER-G7H8I9 -> Charlie Davis
ā Block #1 added successfully to the blockchain
ā All valid votes accepted successfully
============================================================
TEST 2: Invalid Vote Fields
============================================================
Testing invalid vote scenarios:
1. Empty voter ID:
ā Vote rejected: Invalid vote data (empty fields)
2. Empty candidate:
ā Vote rejected: Invalid vote data (empty fields)
3. Whitespace-only voter ID:
ā Vote rejected: Invalid vote data (empty fields)
ā All invalid votes correctly rejected
============================================================
TEST 3: Duplicate Vote Detection
============================================================
Block #1 - Adding initial votes:
ā Vote accepted: VOTER-001 -> Alice Johnson
ā Vote accepted: VOTER-002 -> Bob Smith
ā Block #1 added successfully to the blockchain
Block #2 - Attempting duplicate vote:
Attempting to vote again with VOTER-001:
ā Vote rejected: Voter VOTER-001 has already voted in the blockchain
Adding new voter VOTER-003:
ā Vote accepted: VOTER-003 -> Alice Johnson
ā Block #2 added successfully to the blockchain
ā Duplicate detection working correctly
============================================================
ALL TESTS COMPLETED
============================================================
ā
Vote field validation: WORKING
ā
Duplicate detection (cross-block): WORKING
ā
Duplicate detection (same-block): WORKING
ā
VoteValidator rules: WORKING
ā
Complex scenarios: WORKING
š Blockchain voting system is production-ready!
```# š³ļø Blockchain Voting System
A production-ready, enterprise-grade C++ implementation of a blockchain-based voting system with SHA-256 cryptographic security, comprehensive vote validation, duplicate detection, and Proof of Authority (PoA) consensus mechanism to ensure election integrity.
## š Features
### Core Blockchain Features
- **SHA-256 Cryptographic Hashing**: Industry-standard security using OpenSSL
- **Immutable Vote Records**: Each vote is timestamped and stored permanently
- **Blockchain Manager**: Complete chain validation and integrity checking
- **Tampering Detection**: Automatically detects any unauthorized modifications
- **Block Chaining**: Blocks are cryptographically linked via hash references
- **Genesis Block**: Proper blockchain initialization
### Proof of Authority (PoA) Consensus ā NEW
- **4 Authorized Validators**: Trusted entities who approve blocks
- **Multi-Signature Approval**: Requires 3 out of 4 validator signatures
- **Digital Signatures**: Simulated cryptographic signing and verification
- **Block Finalization**: Only blocks with sufficient consensus are added
- **Consensus Rejection**: Blocks without enough approvals are rejected
- **Validator Management**: Register, activate/deactivate validators
- **Audit Trail**: Complete record of all validator approvals
### Vote Validation & Security
- **Multi-Layer Validation**: Comprehensive vote verification system
- Field validation (non-empty voter ID and candidate)
- Format validation (length constraints, character restrictions)
- Duplicate detection (prevents double-voting)
- Hash integrity verification
- **Cross-Chain Duplicate Detection**: Scans entire blockchain to prevent voter from voting twice
- **Same-Block Duplicate Prevention**: Validates votes within pending blocks
- **Detailed Error Reporting**: Clear feedback for rejected votes with specific reasons
- **Customizable Validation Rules**: Configurable constraints via VoteValidator class
### Rejected Vote Tracking (DeadBlock)
- **Invalid Vote Logging**: Tracks all votes rejected due to format errors
- **Duplicate Vote Logging**: Records all duplicate voting attempts
- **Detailed Metadata**: Stores voter ID, candidate, timestamp, rejection reason
- **Categorized Rejections**: Separates invalid votes from duplicates
- **Export Capabilities**: CSV and JSON export for analysis
- **Audit Transparency**: Complete record for election officials
### REST API (Day 9) ā NEW
- **HTTP Endpoints**: RESTful API for vote submission and blockchain access
- **POST /addVote**: Submit votes via HTTP with JSON
- **GET /stats**: Real-time voting statistics
- **GET /block/:index**: Retrieve specific blocks
- **GET /deadblock**: Access rejected votes
- **GET /validate**: Validate blockchain integrity
- **Multi-threaded**: 8 concurrent worker threads
- **Thread-safe**: Mutex-protected operations
- **JSON Responses**: Structured response format
- **Processing Metrics**: Response time tracking
### Developer Features
- **Clean OOP Design**: Modular, testable, and maintainable architecture
- **Comprehensive Testing**: 7+ test suites covering all scenarios
- **Cross-Platform**: Standard C++11/14, works on Linux, macOS, and Windows
- **Well-Documented**: Extensive inline comments and documentation
- **Build Automation**: Makefile and CMake support
## š Project Structure
blockchain-voting/ āāā include/ # Header files ā āāā Vote.h # Vote class with validation ā āāā Block.h # Block class with vote management ā āāā Blockchain.h # Blockchain manager with duplicate detection ā āāā SHA256Helper.h # SHA-256 hashing utility ā āāā VoteValidator.h # Centralized validation rules ā āāā DeadBlock.h # Rejected vote tracking system ā āāā Validator.h # ā PoA validator entity ā āāā ConsensusPoA.h # ā Proof of Authority consensus engine āāā src/ # Implementation files ā āāā Vote.cpp # Vote validation logic ā āāā Block.cpp # Block-level duplicate checks ā āāā Blockchain.cpp # Cross-chain duplicate detection ā āāā SHA256Helper.cpp # SHA-256 hashing implementation ā āāā VoteValidator.cpp # Validation rule enforcement ā āāā DeadBlock.cpp # Rejected vote logging ā āāā Validator.cpp # ā Validator management ā āāā ConsensusPoA.cpp # ā PoA consensus implementation āāā docs/ # Documentation ā āāā COMPLEXITY_ANALYSIS.md # Performance & optimization guide ā āāā VALIDATION_GUIDE.md # Testing & validation reference āāā main.cpp # Validation test suite (7 scenarios) āāā test_poa.cpp # ā PoA consensus test suite (6 scenarios) āāā Makefile # Build automation āāā CMakeLists.txt # CMake configuration āāā ARCHITECTURE.md # System design documentation āāā README.md # This file āāā LICENSE # MIT License
## š§ Building the Project
### Prerequisites
- **C++17 or later compiler** (GCC 7+, Clang 5+, MSVC 2017+)
- **CMake 3.10+**
- **OpenSSL library** installed
**Install OpenSSL:**
- **Ubuntu/Debian**: `sudo apt-get install libssl-dev cmake build-essential`
- **macOS**: `brew install openssl cmake`
- **Windows**: Download from [slproweb.com/products/Win32OpenSSL.html](https://slproweb.com/products/Win32OpenSSL.html)
---
### š Quick Build (All Platforms)
**Windows:**
```batch
build.bat
cd build\bin
api_server.exe
macOS/Linux:
chmod +x build.sh
./build.sh
cd build/bin
./api_serverWindows (PowerShell/CMD):
build.bat
macOS/Linux (Bash):
chmod +x build.sh ./build.sh
What the scripts do:
- Create
build/directory - Configure CMake
- Build all targets
- Create executables in
build/bin/ - Display build summary
All Platforms:
# Create and enter build directory mkdir build cd build # Configure (choose one based on platform) cmake .. # Linux/macOS with Make cmake .. -G "MinGW Makefiles" # Windows with MinGW cmake .. -G "Visual Studio 17 2022" # Windows with VS 2022 # Build cmake --build . --config Release # Run executables cd bin ./voting_system # or voting_system.exe on Windows ./test_poa # or test_poa.exe on Windows ./api_server # or api_server.exe on Windows
CMake creates 4 executables:
| Executable | Purpose | Command |
|---|---|---|
voting_system |
Run validation tests (7 tests) | ./voting_system |
test_poa |
Run PoA consensus tests (6 tests) | ./test_poa |
api_server |
Start REST API server | ./api_server |
blockchain_server |
Start REST API server (alt) | ./blockchain_server |
All executables are in build/bin/ directory.
Validation Tests:
g++ -std=c++17 -I./include \
src/Vote.cpp \
src/SHA256Helper.cpp \
src/Block.cpp \
src/Blockchain.cpp \
src/VoteValidator.cpp \
src/DeadBlock.cpp \
main.cpp \
-o voting_system \
-lssl -lcrypto
./voting_systemPoA Consensus Tests:
g++ -std=c++17 -I./include \
src/Vote.cpp \
src/SHA256Helper.cpp \
src/Block.cpp \
src/Validator.cpp \
src/ConsensusPoA.cpp \
test_poa.cpp \
-o test_poa \
-lssl -lcrypto
./test_poaREST API Server:
Windows (PowerShell):
g++ -std=c++17 -I./include -I./include/crow ` src/Vote.cpp ` src/SHA256Helper.cpp ` src/Block.cpp ` src/Blockchain.cpp ` src/VoteValidator.cpp ` src/DeadBlock.cpp ` src/VotingAPI.cpp ` api_server.cpp ` -o api_server.exe ` -lssl -lcrypto -lpthread -lws2_32 .\api_server.exe
Linux/macOS:
g++ -std=c++17 -I./include -I./include/crow \
src/Vote.cpp \
src/SHA256Helper.cpp \
src/Block.cpp \
src/Blockchain.cpp \
src/VoteValidator.cpp \
src/DeadBlock.cpp \
src/VotingAPI.cpp \
api_server.cpp \
-o api_server \
-lssl -lcrypto -lpthread
./api_serverTest API:
# In another terminal while server is running curl -X POST http://localhost:8080/addVote \ -H "Content-Type: application/json" \ -d '{"voterTempID":"VOTER-001","candidate":"Alice Johnson"}'
#include "Blockchain.h" #include "Block.h" #include "Vote.h" // Initialize blockchain with genesis block Blockchain votingChain; // Create a new block Block block1(1, votingChain.getChain().back().getHash()); // Create and add votes (with automatic validation) Vote vote1("VOTER-001", "Alice Johnson"); Vote vote2("VOTER-002", "Bob Smith"); // Add votes to pending block (validates and checks for duplicates) if (votingChain.addVoteToPendingBlock(block1, vote1)) { std::cout << "Vote 1 accepted!" << std::endl; } if (votingChain.addVoteToPendingBlock(block1, vote2)) { std::cout << "Vote 2 accepted!" << std::endl; } // Try to vote again (will be rejected - duplicate detection) Vote duplicateVote("VOTER-001", "Charlie Davis"); if (!votingChain.addVoteToPendingBlock(block1, duplicateVote)) { std::cout << "Duplicate vote rejected!" << std::endl; } // Add block to chain votingChain.addBlock(block1); // Validate blockchain integrity if (votingChain.isChainValid()) { std::cout << "ā Blockchain is valid!" << std::endl; } // Display entire blockchain votingChain.display();
The project includes comprehensive test suites:
# Run all 6 test scenarios ./voting_system # Or with make make run
Test Coverage:
- ā Valid vote acceptance
- ā Invalid field rejection (empty voter ID, empty candidate)
- ā Cross-block duplicate detection
- ā Same-block duplicate detection
- ā VoteValidator rule enforcement
- ā Complex multi-block scenarios
- Stores voter ID, candidate, and timestamp
- Immutable after creation
- Field validation: Checks for empty/whitespace-only fields
- Serializable for hashing
- Contains multiple votes
- SHA-256 cryptographic hash
- Links to previous block via hash
- Vote validation: Ensures valid votes before adding
- Duplicate prevention: Checks for duplicate voters within block
- Self-validation capability
- Manages the entire chain of blocks
- Cross-chain duplicate detection: Scans all blocks for duplicate voters
- Multi-layer validation:
- Field validation
- Format validation
- Duplicate detection across chain
- Duplicate detection within block
- Hash integrity verification
- Provides statistics and analytics
- Chain integrity validation
- DeadBlock integration: Logs all rejected votes
- Centralized validation rules
- Voter ID validation:
- Length: 3-50 characters
- Allowed: alphanumeric, hyphen, underscore, space
- Candidate validation:
- Length: 2-100 characters
- Same character restrictions
- Detailed error messages
- Easily customizable rules
- Tracks all rejected votes with metadata
- Invalid votes: Format/field validation failures
- Duplicate votes: Double-voting attempts
- Rejection reasons: Categorized with enums
- Export: CSV and JSON formats
- Audit trail for transparency
- Represents authorized validator in PoA network
- Digital signatures: Simulated cryptographic signing
- Public key: For signature verification
- Active status: Can be activated/deactivated
- Statistics: Tracks blocks validated per validator
- Proof of Authority consensus engine
- Manages 4 authorized validators
- Multi-signature approval: Requires 3/4 signatures
- Block submission: Pending blocks await consensus
- Signature collection: Validators sign blocks
- Finalization: Only approved blocks enter chain
- Rejection mechanism: Insufficient consensus = rejected
- Complete audit trail of all approvals
- REST API handler for HTTP endpoints
- Thread-safe vote processing with mutex
- POST /addVote: Submit votes via JSON
- GET /stats: Real-time statistics
- GET /block/:index: Retrieve blocks
- GET /deadblock: Rejected votes access
- GET /validate: Chain integrity check
- Automatic block finalization (100 votes/block)
- Processing time tracking
- Wrapper for OpenSSL SHA-256 functions
- Converts binary hash to hexadecimal string
- Used by Block class for secure hashing
- SHA-256 Hashing: Industry-standard cryptographic hash function
- Immutable Chain: Once added, blocks cannot be modified without detection
- Hash Chain: Each block references previous block's hash
- Tampering Detection: Any modification breaks the hash chain
Example Hash:
Input: Block data with votes...
SHA-256: 3f786850e387550fdab836ed7e6dc881de23001b2b9ad9f29cde4b424445c988
^ 64 hexadecimal characters (256 bits)
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Layer 1: Field Validation ā Empty/whitespace checks
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Layer 2: Format Validation ā Length & character restrictions
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Layer 3: Cross-Chain Duplicates ā Scan entire blockchain
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Layer 4: Same-Block Duplicates ā Check pending block
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Layer 5: Hash Integrity ā Cryptographic verification
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Problem: Prevent voters from voting multiple times
Solution: Comprehensive duplicate detection system
// Example: Duplicate vote prevention Vote vote1("VOTER-001", "Alice Johnson"); blockchain.addVoteToPendingBlock(block1, vote1); // ā Accepted // Later, in a different block... Vote vote2("VOTER-001", "Bob Smith"); blockchain.addVoteToPendingBlock(block2, vote2); // ā REJECTED // Output: "ā Vote rejected: Voter VOTER-001 has already voted in the blockchain"
-
Current Implementation: O(n Ć m) where n = blocks, m = votes per block
- Scans entire blockchain for each vote
- Suitable for small-medium elections (<10,000 votes)
-
Optimized Version (see COMPLEXITY_ANALYSIS.md): O(1) with hash set
- Uses
std::unordered_setfor instant lookups - Scales to millions of votes
- Trade-off: O(nĆm) extra memory for O(1) time
- Uses
Voter ID:
- ā Minimum 3 characters
- ā Maximum 50 characters
- ā Alphanumeric + hyphen/underscore/space only
- ā No empty or whitespace-only values
Candidate Name:
- ā Minimum 2 characters
- ā Maximum 100 characters
- ā Same character restrictions
- ā No empty or whitespace-only values
Examples:
ā Vote("VOTER-123", "Alice Johnson") // Valid ā Vote("temp_voter_001", "Bob Smith") // Valid ā Vote("", "Alice Johnson") // Empty voter ID ā Vote("AB", "Alice") // Voter ID too short ā Vote("VOTER@123", "Alice") // Invalid character ā Vote("VOTER-001", "VOTER-001") // Duplicate (if voted before)
| Attack Type | Prevention Method |
|---|---|
| Double-voting | Cross-chain duplicate detection |
| Empty votes | Field validation |
| Malformed data | Format validation with VoteValidator |
| Data tampering | SHA-256 hash verification |
| Chain corruption | Integrity validation via isChainValid() |
| Replay attacks | Timestamp + chain position verification |
- Implement SHA-256 hashing
- Add Blockchain manager class
- Implement chain validation
- Vote field validation
- Duplicate voter detection (cross-chain)
- Duplicate voter detection (same-block)
- VoteValidator with customizable rules
- Comprehensive validation test suite
- DeadBlock rejected vote tracking
- Proof of Authority (PoA) consensus
- Multi-signature block approval (3/4 validators)
- Validator management system
- REST API with HTTP endpoints ā
- JSON request/response format ā
- Multi-threaded API server ā
- Complete documentation
- Hash set optimization for O(1) duplicate detection
- Integrate PoA consensus with REST API
- JWT authentication for API
- Rate limiting for API endpoints
- HTTPS/TLS support
- WebSocket support for real-time updates
- Frontend web interface
- Proof of Work (PoW) consensus option
- Merkle tree for efficient verification
- Blockchain persistence (save/load to disk)
- Digital signatures with real cryptography (ECDSA)
- P2P network layer for distributed voting
- Zero-knowledge proofs for privacy
- Smart contracts for automated vote counting
- Mobile app integration
Next Recommended:
- Implement hash set for duplicate detection (O(nĆm) ā O(1))
- Integrate PoA consensus with main voting workflow
- Add real ECDSA cryptographic signatures
See:
docs/COMPLEXITY_ANALYSIS.mdfor implementation guide
Contributions are welcome! Please feel free to submit a Pull Request.
- Follow existing code style and conventions
- Add comments for complex logic
- Update tests for new features
- Run all tests before submitting PR
- Update documentation as needed
# Compile make # Run all tests make run # Verify no memory leaks (optional) valgrind --leak-check=full ./voting_system
- Performance optimizations (hash set implementation)
- Additional validation rules
- More comprehensive test cases
- Documentation improvements
- Bug fixes and security enhancements
MIT License - feel free to use this project for learning and development.
This project demonstrates:
- Blockchain fundamentals: Hashing, chaining, immutability
- Cryptography: SHA-256 implementation and security
- Consensus mechanisms: Proof of Authority (PoA) with multi-signature approval
- Data structures: Linked structures, hash-based lookups, maps
- Algorithm design: Duplicate detection, validation layers, consensus voting
- Software engineering: OOP, design patterns, testing, documentation
- Performance analysis: Time/space complexity trade-offs
- Enterprise patterns: Validator networks, audit trails, governance
Perfect for:
- Learning blockchain technology from scratch
- Understanding consensus mechanisms (PoA, and extensible to PoW/PoS)
- Practicing advanced C++ and OOP design
- Studying cryptographic security implementations
- Building production-ready voting systems
- Understanding enterprise blockchain architecture
- Creating portfolio projects for blockchain development
Proof of Authority (PoA):
- 4 registered validators (trusted authorities)
- Minimum 3 out of 4 signatures required (75% consensus)
- Digital signature simulation (ready for ECDSA upgrade)
- Block finalization only with sufficient approvals
- Automatic rejection of insufficient consensus
- Complete audit trail of validator decisions
- Multi-layer vote validation
- Cryptographic hash verification (SHA-256)
- Duplicate vote prevention across entire chain
- Rejected vote tracking (DeadBlock system)
- Validator-based consensus (PoA)
- Tamper-proof block linking
- O(nĆm) duplicate detection (optimizable to O(1))
- Efficient hash-based block validation
- Scalable validator signature collection
- Ready for hash set optimization
Built with ā¤ļø as a comprehensive learning project for blockchain technology and secure C++ development.
- OpenSSL for cryptographic functions
- C++ Standard Library for data structures
- Blockchain community for design patterns and best practices
- Issues: Use GitHub Issues for bug reports
- Questions: Check documentation in
docs/folder - Performance: See
COMPLEXITY_ANALYSIS.mdfor optimization tips
ā If you find this project helpful, please consider giving it a star on GitHub!