Skip to content

askwhyharsh/blockchain-with-libp2p

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

P2P Blockchain with libp2p

A decentralized blockchain implementation using Go and libp2p for peer-to-peer networking.

✨ Features

  • P2P Networking: Built with libp2p for decentralized communication
  • Proof of Work: Mining blocks with adjustable difficulty
  • Cryptographic Security: SHA-256 hashing and digital signatures
  • Real-time Sync: Automatic blockchain synchronization across peers
  • Blockchain Explorer: Web interface for monitoring the network
  • Docker Support: Easy deployment with containers
  • Comprehensive Testing: Unit tests and network tests
  • REST API: Full API for blockchain operations
  • WebSocket: Real-time updates via WebSocket

What This Project Has (Very Basic Blockchain for learning purpose)

  • ✅ Core Blockchain Features
  • ✅ Block structure with hash chaining
  • ✅ Proof of Work (basic mining)
  • ✅ P2P networking with libp2p
  • ✅ Block validation
  • ✅ Chain synchronization
  • ✅ Modern Features
  • ✅ Web interface
  • ✅ REST API
  • ✅ Real-time updates
  • ✅ Docker deployment
image

What's Missing - alot haha

  1. Transactions
  • ❌ No transaction system
  • ❌ No digital signatures
  • ❌ No transaction validation
  • ❌ No UTXO or account-based model
  1. Consensus Mechanisms
  • ❌ No real consensus (just basic PoW)
  • ❌ No Byzantine fault tolerance
  • ❌ No finality guarantees
  • ❌ No fork resolution
  1. Security
  • ❌ No cryptographic signatures
  • ❌ No private/public key pairs
  • ❌ No wallet system
  • ❌ No transaction fees
  1. Network Layer
  • ❌ No peer discovery
  • ❌ No NAT traversal
  • ❌ No message routing
  • ❌ No network resilience
  1. Storage & Persistence
  • ❌ No persistent storage
  • ❌ No database integration
  • ❌ No state management
  • ❌ No pruning

🏗️ Architecture

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   Node A        │◄──►│   Node B        │◄──►│   Node C        │
│  ┌─────────────┐ │    │  ┌─────────────┐ │    │  ┌─────────────┐ │
│  │ Blockchain │ │    │  │ Blockchain │ │    │  │ Blockchain │ │
│  └─────────────┘ │    │  └─────────────┘ │    │  └─────────────┘ │
└─────────────────┘    └─────────────────┘    └─────────────────┘

🚀 Quick Start

Prerequisites

  • Go 1.25
  • Git
  • Docker (optional)

Installation

# Clone the repository
git clone https://github.com/yourusername/pow-blockchain-libp2p
cd pow-blockchain-libp2p

# Install dependencies
make deps

# Build the application
make build

Running a Node

# Start first node
make run

# Start second node (in another terminal)
make run

# Connect nodes
connect /ip4/127.0.0.1/tcp/8080/p2p/QmPeerID

Docker Deployment

# Build and run with Docker Compose
make docker-run

# Or build Docker image
make docker

🎮 Demo Scenarios

Scenario 1: Basic Network

# Terminal 1
make run
# Add BPM: 75

# Terminal 2  
make run
# Connect to first node
# Add BPM: 80

Scenario 2: Multi-Node Network

# Start 3 nodes and watch them sync!
make test-network

Scenario 3: API Server

# Start API server with web interface
make api
# Visit http://localhost:8080

📊 Monitoring

Access the blockchain explorer:

# Start with explorer
make api
# Visit http://localhost:8080

🔧 Configuration

Create .env file:

NODE_PORT=8080
DIFFICULTY=4
MINING_REWARD=10
BLOCK_TIME=10

🧪 Testing

# Run tests
make test

# Run benchmarks
make bench

# Test network
make test-network

# Coverage analysis
make coverage

🏗️ Project Structure

PoWblockchain-libp2p/
├── cmd/                    # Application entry points
│   ├── node/              # Blockchain node
│   └── api/               # API server
├── internal/              # Private application code
│   ├── blockchain/        # Blockchain logic
│   ├── consensus/         # Consensus algorithms
│   └── network/          # P2P networking
├── pkg/                   # Public library code
│   ├── crypto/           # Cryptographic functions
│   └── utils/            # Utility functions
├── web/                   # Web interface
├── scripts/              # Utility scripts
├── docker/               # Docker configuration

🔌 API Endpoints

Blockchain Operations

  • GET /api/v1/blocks - Get all blocks
  • GET /api/v1/blocks/:index - Get block by index
  • POST /api/v1/blocks - Add new block
  • GET /api/v1/status - Get blockchain status
  • GET /api/v1/peers - Get connected peers
  • POST /api/v1/connect - Connect to peer
  • GET /api/v1/ws - WebSocket connection

WebSocket Events

  • blockchain - Full blockchain update
  • new_block - New block added
  • status - Network status update

🛠️ Development

Setup Development Environment

# Install dependencies
make deps

# Format code
make fmt

# Run linter
make lint

# Run tests
make test

# Generate documentation
make docs

Adding New Features

  1. Create feature branch
  2. Implement feature
  3. Add tests
  4. Update documentation
  5. Submit pull request

Build Errors

# Clean and rebuild
make clean
make build

Docker Issues

# Rebuild Docker image
docker-compose down
docker-compose up --build

About

Simple blockchain with libp2p peers, with a basic PoW consensus

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors