A lightweight, educational blockchain implementation in pure Java demonstrating core concepts like Proof of Work, cryptographic hashing, and chain validation.
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.
- Java 11 or higher
- Maven (optional, for dependency management)
Option 1: Direct Compilation
# Compile all classes
javac -d out src/com/blockchain/**/*.java
# Run the demo
java -cp out com.blockchain.MainOption 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
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)
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...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
🚀 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
─────────────────────────
# 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.BlockchainTestThe mining difficulty can be configured when creating the blockchain:
// Difficulty 4 = 4 leading zeros
BlockchainService blockchain = new BlockchainService(4);- Difficulty 2 ≈ Instant (recommended for testing)
- Difficulty 4 ≈ 0.5–2 seconds
- Difficulty 5 ≈ 5–15 seconds
| 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 |
Replace plain string data with Transaction objects, implement digital signatures (ECDSA), and introduce the UTXO model.
Save and load the blockchain from disk using JSON, binary serialization, or an embedded database such as H2 or SQLite.
Implement peer-to-peer communication, add a gossip protocol, and handle competing chains using the longest valid chain rule.
Generate public/private key pairs, sign transactions, and verify signatures.
Experiment with alternative consensus mechanisms such as:
- Dynamic difficulty adjustment
- Proof of Stake (PoS)
- Byzantine Fault Tolerance (BFT)
This project is licensed under the MIT License.
Feel free to use, modify, and distribute it.
Contributions are welcome!
If you find a bug or have an idea for an improvement:
- Fork the repository.
- Create a feature branch.
git checkout -b feature/AmazingFeature- Commit your changes.
git commit -m "Add some AmazingFeature"- Push your branch.
git push origin feature/AmazingFeature- Open a Pull Request.
- Bitcoin Whitepaper
- Blockchain Demo
- Mastering Bitcoin
- Java Cryptography Architecture
- Built to better understand blockchain fundamentals.
- Inspired by various blockchain tutorials and educational resources.
- Special thanks to the open-source community.