Skip to content

About

A mini blockchain built from scratch in Rust with SHA-256 hashing and Proof of Work

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

⛓️ mini-blockchain

A proof-of-concept blockchain engine built from scratch in Rust — featuring SHA-256 hashing, Proof of Work mining, and cryptographic tamper detection.

CI Rust License Status


📸 Demo

🚀 Starting Mini Blockchain

⛏️  Mining block 0...
✅ Block mined! Nonce: 203 | Hash: 0066ab8fabd9
⛏️  Mining block 1...
✅ Block mined! Nonce: 107 | Hash: 00d52fb99b10
⛏️  Mining block 2...
✅ Block mined! Nonce: 420 | Hash: 002c8391b69c
⛏️  Mining block 3...
✅ Block mined! Nonce: 89 | Hash: 00444e377e11

========== 🔗 BLOCKCHAIN ==========

📦 Block #1
   Data     : Alice sends 10 BTC to Bob
   Nonce    : 107
   Prev Hash: 0066ab8fabd9...
   Hash     : 00d52fb99b10...
   Time     : 2026-08-10T03:11:19.456504700+00:00

   ... blocks #0, #2 and #3 omitted here for brevity ...

====================================

✅ Blockchain is valid!

🔧 Tampering with Block 1...

❌ Tampering detected: Block 1 has an invalid hash — data may have been tampered with

🧠 How It Works

A blockchain is a linked list of blocks where each block contains a cryptographic hash of the one before it. Changing any block breaks all subsequent links — making fraud instantly detectable.

Block Structure

struct Block {
    index:         u64,     // Position in the chain
    timestamp:     String,  // RFC3339 creation time
    data:          String,  // Transaction payload
    previous_hash: String,  // Hash of the previous block
    hash:          String,  // SHA-256 fingerprint of this block
    nonce:         u64,     // Value found during mining
}

Proof of Work

To add a block, the miner must find a nonce such that:

SHA256(index + timestamp + data + previous_hash + nonce)
    must start with "00..."  (configurable difficulty)

Tamper Detection

is_valid() checks three things for every block, the genesis block included:

  1. Hash integrity — recalculates and compares the stored hash
  2. Proof of work — confirms the hash really meets the difficulty target, so a hand-crafted block that was never mined cannot pass as genuine
  3. Chain linkage — verifies previous_hash matches the actual previous block

🛠️ Tech Stack

Crate Version Purpose
sha2 0.10 SHA-256 hashing
hex 0.4 Encode hash bytes as readable strings
serde + serde_json 1.0 Block serialization
chrono 0.4 RFC3339 timestamps

▶️ Getting Started

Prerequisites: Rust 1.75+ (https://rustup.rs)

git clone https://github.com/vagif693/mini-blockchain.git
cd mini-blockchain/mini-blockchain
cargo run

Run the test suite:

cargo test

🗺️ Roadmap

  • SHA-256 block hashing
  • Proof of Work mining with configurable difficulty
  • Cryptographic chain validation
  • Tamper detection demo
  • Unit test suite and CI
  • CLI interface with clap
  • Persist chain to JSON file
  • Wallet addresses and digital signatures

💡 Key Rust Concepts Demonstrated

  • Ownership & borrowing — Rust's core memory model used throughout
  • Structs and impl blocks — clean OOP-style design in Rust
  • Cryptographic hashing — real SHA-256 via the sha2 crate
  • Iterators — chain validation using Rust iterators

📄 License

MIT

About

A mini blockchain built from scratch in Rust with SHA-256 hashing and Proof of Work

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages