A Go-based blockchain implementation featuring hybrid consensus mechanisms with CAP theorem considerations and Byzantine Fault Tolerance (BFT) properties. This project demonstrates adaptive consistency models and trust-based node scoring in distributed systems.
- Complete Blockchain Implementation: Full blockchain with blocks, transactions, and proof-of-work
- Wallet Management: Create and manage multiple wallets with cryptographic addresses
- Transaction Processing: Send and receive coins with UTXO model
- Chain Validation: Comprehensive blockchain integrity verification
- Persistent Storage: BadgerDB-based storage for blockchain data
- Hybrid Consensus Model: Adaptive consensus based on network conditions
- Multiple Consistency Levels:
- Strong Consistency: Highest security, slower performance
- Causal Consistency: Balanced approach for most use cases
- Eventual Consistency: Fastest performance, partition-tolerant
- Trust Score Management: Byzantine Fault Tolerance through node trust scoring
- Network Partition Detection: Automatic adaptation to network conditions
- CAP Theorem Implementation: Dynamic trade-offs between Consistency, Availability, and Partition tolerance
├── main.go # Application entry point
├── cli/ # Command-line interface
│ └── cli.go # Interactive and command-line modes
├── Blockchain/ # Core blockchain logic
├── wallet/ # Wallet and cryptography
└── tmp/ # Temporary files and cache
- Language: Go
- Database: BadgerDB
- Cryptography: ECDSA for wallet addresses
- Consensus: Proof-of-Work with adaptive consistency
- Architecture: UTXO model with state management
- Go 1.19 or higher
- Git
# Clone the repository
git clone https://github.com/saadamir1/hybrid-consensus-chain.git
cd hybrid-consensus-chain
# Initialize Go modules
go mod init Blockchain_Test
go mod tidy
# Build the application
go build -o blockchain main.go
Simply run the executable without arguments to enter interactive mode:
./blockchain
The interactive menu provides:
- Create Wallet - Generate new cryptographic wallet
- List Addresses - View all wallet addresses
- Initialize Blockchain - Create genesis block
- Get Balance - Check address balance
- Send Coins - Transfer funds between addresses
- Print Chain - Display all blocks
- Add Funds - Add coins to an address (for testing)
- Validate Chain - Verify blockchain integrity
- Node Management - Advanced consensus settings
# Create a new wallet
./blockchain createwallet
# List all addresses
./blockchain listaddresses
# Create blockchain with genesis block
./blockchain createblockchain -address YOUR_ADDRESS
# Check balance
./blockchain getbalance -address YOUR_ADDRESS
# Send transaction
./blockchain send -from SENDER_ADDRESS -to RECIPIENT_ADDRESS -amount 10
# Print blockchain
./blockchain printchain
# Validate blockchain
./blockchain validatechain
# Add funds (testing)
./blockchain addfunds -address YOUR_ADDRESS -amount 100
# Set consistency level
./blockchain setconsistency -level strong
./blockchain setconsistency -level causal
./blockchain setconsistency -level eventual
# Set node trust score
./blockchain settrust -node NODE_ID -score 0.8
- Use Case: Financial transactions, critical data
- Properties: All nodes see the same data simultaneously
- Trade-off: Higher latency, lower availability during partitions
- Use Case: General-purpose applications
- Properties: Maintains causal relationships between operations
- Trade-off: Balanced performance and consistency
- Use Case: High-availability systems, social media
- Properties: All nodes will converge eventually
- Trade-off: Highest availability and partition tolerance
- Range: 0.0 to 1.0
- Threshold: 0.5 (nodes above threshold are considered trustworthy)
- Impact: Affects block validation and consensus participation
- Byzantine Tolerance: Handles up to 1/3 malicious nodes with proper trust scores
The system automatically adapts to network conditions:
- Partition Detection: Monitors network connectivity
- Consistency Adjustment: Downgrades consistency during partitions
- Trust Recalibration: Adjusts node trust based on behavior
- Performance Optimization: Balances speed vs. security
hybrid-consensus-chain/
├── main.go # Entry point
├── cli/
│ └── cli.go # CLI implementation
├── Blockchain/ # Core blockchain logic
├── wallet/ # Wallet management
├── tmp/ # Temporary storage
├── go.mod # Go modules
└── README.md # This file
- Proof of Work: SHA-256 based mining
- ECDSA: Elliptic curve cryptography for addresses
- UTXO Model: Unspent transaction output tracking
- Merkle Trees: Transaction verification (in block structure)
# 1. Create wallets
./blockchain createwallet
# Output: New address: 1A2B3C4D...
./blockchain createwallet
# Output: New address: 5E6F7G8H...
# 2. Initialize blockchain
./blockchain createblockchain -address 1A2B3C4D...
# 3. Check initial balance (100 coins from genesis)
./blockchain getbalance -address 1A2B3C4D...
# 4. Send coins
./blockchain send -from 1A2B3C4D... -to 5E6F7G8H... -amount 30
# 5. Verify balances
./blockchain getbalance -address 1A2B3C4D... # Should show 70
./blockchain getbalance -address 5E6F7G8H... # Should show 30
# 6. View the blockchain
./blockchain printchain
- Initial Coins: 100 (genesis block)
- Default Consistency: Strong
- Default Trust Threshold: 0.5
- Mining Difficulty: Adjustable based on network
Modify constants in the blockchain package for:
- Mining difficulty
- Block rewards
- Trust thresholds
- Consistency timeouts
- Basic Operations: Wallet creation, transactions, balance checks
- Consensus Behavior: Different consistency levels under various network conditions
- Byzantine Resilience: Behavior with malicious nodes (low trust scores)
- Partition Tolerance: System behavior during network splits
- Performance: Transaction throughput under different consistency modes
# Create test scenario
./blockchain createwallet # Create multiple wallets
./blockchain addfunds -address ADDR -amount 1000 # Add test funds
./blockchain send -from ADDR1 -to ADDR2 -amount 100 # Test transactions
./blockchain validatechain # Verify integrity
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Network protocol implementation for multi-node deployment
- Smart contract support
- REST API interface
- Metrics and monitoring dashboard
- Automated testing suite
- Docker containerization
- Cross-chain interoperability
This project is open source and available under the MIT License.
This blockchain demonstrates practical CAP theorem trade-offs:
- C (Consistency): Configurable levels from strong to eventual
- A (Availability): System remains operational during node failures
- P (Partition Tolerance): Graceful handling of network splits
- Tolerates up to 1/3 Byzantine nodes
- Trust scoring mechanism for node reputation
- Adaptive consensus based on network trust levels
Project Maintainer: saadamir1 Repository: hybrid-consensus-chain
For questions, issues, or contributions, please use the GitHub issue tracker.