Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

⛓️ Simple Java Blockchain

A lightweight, educational blockchain implementation in pure Java demonstrating core concepts like Proof of Work, cryptographic hashing, and chain validation.

🎯 Purpose

This project was built to understand the fundamental mechanics behind blockchain technology. It implements a minimal but functional blockchain with:

  • SHA-256 cryptographic hashing
  • Proof of Work consensus (mining with adjustable difficulty)
  • Chain integrity validation
  • Tampering detection

Note: This is an educational implementation, not production-ready. It's designed to be simple, readable, and demonstrate core concepts without external dependencies.

🚀 Getting Started

Prerequisites

  • Java 11 or higher
  • Maven (optional, for dependency management)

Running the Project

Option 1: Direct Compilation

# Compile all classes
javac -d out src/com/blockchain/**/*.java

# Run the demo
java -cp out com.blockchain.Main

Option 2: Using Maven

# Build the project
mvn clean compile

# Run the application
mvn exec:java -Dexec.mainClass="com.blockchain.Main"

Option 3: Create JAR (with Maven)

mvn clean package

java -jar target/simple-blockchain-1.0.0.jar

💡 How It Works

1. Block Structure

Each block contains:

  • Index — Position in the chain
  • Timestamp — Creation time
  • Data — Transaction information (string format)
  • Previous Hash — Link to the previous block
  • Hash — SHA-256 hash of the current block content + nonce
  • Nonce — Number used for mining (Proof of Work)

2. Mining (Proof of Work)

The system requires every block hash to start with a certain number of leading zeros (difficulty). During mining, the nonce is incremented until a valid hash is found.

// Difficulty 4 means hash must start with "0000"

Target: 0000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Valid:  0000a1b2c3d4e5f6...
Invalid: 1234a1b2c3d4e5f6...

3. Chain Validation

Whenever a new block is added, the blockchain verifies:

  • Each block's hash matches its content
  • Every block correctly references the previous block
  • The blockchain has not been tampered with

📊 Sample Output

🚀 Starting Simple Blockchain in Java
═══════════════════════════════════════════

🌍 Genesis Block created and mined!

⛏️  Mining Block 0 with data: "Genesis Block"
   Target: 0000... (difficulty: 4 zeros)
   ✅ Block mined! Hash: 0000a1b2c3d4...
   ⏱️  Time: 125ms, Attempts: 15432

📤 Adding Block 1...

⛏️  Mining Block 1 with data: "Alice pays Bob 10 BTC"
   ✅ Block mined! Hash: 0000def456...
   ⏱️  Time: 234ms, Attempts: 28901

📦 BLOCKCHAIN (3 blocks)
═══════════════════════════════════════════

Block #0
  📝 Data: Genesis Block
  🔗 Hash: 0000a1b2c3d4...
  🔙 Previous: 0
  🔢 Nonce: 15432
  ⏱️  Mining Time: 125ms
  ─────────────────────────

Block #1
  📝 Data: Alice pays Bob 10 BTC
  🔗 Hash: 0000def456...
  🔙 Previous: 0000a1b2c3d4...
  🔢 Nonce: 28901
  ⏱️  Mining Time: 234ms
  ─────────────────────────

Block #2
  📝 Data: Bob pays Charlie 5 BTC
  🔗 Hash: 0000ghi789...
  🔙 Previous: 0000def456...
  🔢 Nonce: 21345
  ⏱️  Mining Time: 187ms
  ─────────────────────────

🧪 Running Tests

# With Maven
mvn test

# Without Maven (requires JUnit in classpath)
javac -cp ".:junit-4.13.2.jar" test/com/blockchain/BlockchainTest.java

java -cp ".:junit-4.13.2.jar:hamcrest-core-1.3.jar" \
org.junit.runner.JUnitCore com.blockchain.BlockchainTest

⚙️ Configuration

The mining difficulty can be configured when creating the blockchain:

// Difficulty 4 = 4 leading zeros
BlockchainService blockchain = new BlockchainService(4);

Difficulty Impact

  • Difficulty 2 ≈ Instant (recommended for testing)
  • Difficulty 4 ≈ 0.5–2 seconds
  • Difficulty 5 ≈ 5–15 seconds

🔬 Key Concepts Demonstrated

Concept Implementation
Cryptographic Hashing SHA-256 via Java's MessageDigest
Proof of Work Nonce increment until hash satisfies difficulty
Immutable Chain Each block stores the previous block's hash
Tamper Detection Blockchain validation detects modifications
Consensus Simplified longest valid chain rule

🔧 Extending the Project

1. Transaction System

Replace plain string data with Transaction objects, implement digital signatures (ECDSA), and introduce the UTXO model.

2. Persistence

Save and load the blockchain from disk using JSON, binary serialization, or an embedded database such as H2 or SQLite.

3. Networking

Implement peer-to-peer communication, add a gossip protocol, and handle competing chains using the longest valid chain rule.

4. Wallet

Generate public/private key pairs, sign transactions, and verify signatures.

5. Consensus Variations

Experiment with alternative consensus mechanisms such as:

  • Dynamic difficulty adjustment
  • Proof of Stake (PoS)
  • Byzantine Fault Tolerance (BFT)

📝 License

This project is licensed under the MIT License.

Feel free to use, modify, and distribute it.


🤝 Contributing

Contributions are welcome!

If you find a bug or have an idea for an improvement:

  1. Fork the repository.
  2. Create a feature branch.
git checkout -b feature/AmazingFeature
  1. Commit your changes.
git commit -m "Add some AmazingFeature"
  1. Push your branch.
git push origin feature/AmazingFeature
  1. Open a Pull Request.

📚 Learning Resources

  • Bitcoin Whitepaper
  • Blockchain Demo
  • Mastering Bitcoin
  • Java Cryptography Architecture

🙏 Acknowledgments

  • Built to better understand blockchain fundamentals.
  • Inspired by various blockchain tutorials and educational resources.
  • Special thanks to the open-source community.

About

simple Blockchain Network with Java

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages