Skip to content

Interactive CLI-based blockchain with adaptive CAP/BFT consistency, trust scoring, hybrid PoW-BFT consensus, UTXO model, and Merkle forest architecture.

Notifications You must be signed in to change notification settings

saadamir1/hybrid-consensus-chain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hybrid Consensus Blockchain

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.

Features

Core Blockchain Features

  • 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

Advanced Consensus Features

  • 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

Architecture

Components

├── 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

Key Technologies

  • Language: Go
  • Database: BadgerDB
  • Cryptography: ECDSA for wallet addresses
  • Consensus: Proof-of-Work with adaptive consistency
  • Architecture: UTXO model with state management

Installation

Prerequisites

  • Go 1.19 or higher
  • Git

Setup

# 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

Usage

Interactive Mode (Recommended)

Simply run the executable without arguments to enter interactive mode:

./blockchain

The interactive menu provides:

  1. Create Wallet - Generate new cryptographic wallet
  2. List Addresses - View all wallet addresses
  3. Initialize Blockchain - Create genesis block
  4. Get Balance - Check address balance
  5. Send Coins - Transfer funds between addresses
  6. Print Chain - Display all blocks
  7. Add Funds - Add coins to an address (for testing)
  8. Validate Chain - Verify blockchain integrity
  9. Node Management - Advanced consensus settings

Command Line Mode

# 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

Advanced Consensus Management

# 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

Consensus Mechanisms

Consistency Levels

Strong Consistency

  • Use Case: Financial transactions, critical data
  • Properties: All nodes see the same data simultaneously
  • Trade-off: Higher latency, lower availability during partitions

Causal Consistency

  • Use Case: General-purpose applications
  • Properties: Maintains causal relationships between operations
  • Trade-off: Balanced performance and consistency

Eventual Consistency

  • Use Case: High-availability systems, social media
  • Properties: All nodes will converge eventually
  • Trade-off: Highest availability and partition tolerance

Trust Score System

  • 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

Network Adaptability

The system automatically adapts to network conditions:

  1. Partition Detection: Monitors network connectivity
  2. Consistency Adjustment: Downgrades consistency during partitions
  3. Trust Recalibration: Adjusts node trust based on behavior
  4. Performance Optimization: Balances speed vs. security

Development

Project Structure

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

Key Algorithms

  • 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)

Example Workflow

# 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

Configuration

Default Settings

  • Initial Coins: 100 (genesis block)
  • Default Consistency: Strong
  • Default Trust Threshold: 0.5
  • Mining Difficulty: Adjustable based on network

Customization

Modify constants in the blockchain package for:

  • Mining difficulty
  • Block rewards
  • Trust thresholds
  • Consistency timeouts

Testing

Scenarios to Test

  1. Basic Operations: Wallet creation, transactions, balance checks
  2. Consensus Behavior: Different consistency levels under various network conditions
  3. Byzantine Resilience: Behavior with malicious nodes (low trust scores)
  4. Partition Tolerance: System behavior during network splits
  5. Performance: Transaction throughput under different consistency modes

Test Commands

# 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

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Future Enhancements

  • 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

License

This project is open source and available under the MIT License.

Technical Details

CAP Theorem Implementation

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

BFT Properties

  • Tolerates up to 1/3 Byzantine nodes
  • Trust scoring mechanism for node reputation
  • Adaptive consensus based on network trust levels

Contact

Project Maintainer: saadamir1 Repository: hybrid-consensus-chain

For questions, issues, or contributions, please use the GitHub issue tracker.

About

Interactive CLI-based blockchain with adaptive CAP/BFT consistency, trust scoring, hybrid PoW-BFT consensus, UTXO model, and Merkle forest architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages