A simple yet functional blockchain implementation in Rust that demonstrates core blockchain concepts including block mining, proof-of-work, and transaction chains.
This project implements a basic blockchain simulator that allows you to:
- Create a genesis block
- Mine new blocks with proof-of-work
- Simulate transactions between traders
- Visualize the mining process
- Display the complete blockchain
- Block Structure: Each block contains an index, previous hash, timestamp, data, nonce, and hash
- Proof-of-Work Mining: Implements a configurable difficulty level for mining blocks
- SHA-256 Hashing: Uses cryptographic hashing to secure the blockchain
- Chain Validation: Ensures each block is properly linked to the previous block
- Genesis Block: Automatically creates the first block in the chain
- Custom Miner Name: Enter your name as the blockchain miner
- Automated Transactions: Simulates 10 transactions between predefined traders
- Mining Visualization: Shows real-time mining progress and calculated hashes
- Blockchain Display: Prints the complete blockchain with timestamps
- NovaCoin Trading Statistics: Calculates total NovaCoin traded (10 per block)
- Simulation Metrics: Displays simulation end time and completion status
- Rust: Version 1.70 or higher
- Cargo: Rust's package manager (comes with Rust)
-
Clone or navigate to the project directory:
cd /Users/subh/Desktop/code-playground/blockchain -
Build the project:
cargo build
-
Run the blockchain simulator:
cargo run
The project uses the following Rust crates:
- sha2 (v0.10.9): For SHA-256 cryptographic hashing
- chrono (v0.4.42): For timestamp handling and formatting
These dependencies are automatically downloaded and installed by Cargo.
When you run the program, you'll be prompted to enter your miner name:
Welcome to the Blockchain stimulator!
Enter your miner name:
After entering your name, the simulator will:
- Create a genesis block
- Mine 10 new blocks, each containing a transaction
- Display mining progress for each block
- Show the total number of blocks
- Print the complete blockchain with timestamps
Welcome to the Blockchain stimulator!
Enter your miner name:
Alice
Let's start mining and stimulating transactions!
Mining Block 1 ...
Block mined : 1
Transaction added : Send Alice to Neha
Mining Block 2 ...
Block mined : 2
Transaction added : Send Neha to Subh
...
Total number of blocks : 11
Blockchain contents:
Block 0 : Genesis Block at 2025-12-12 12:47:50
Block 1 : Send Alice to Neha at 2025-12-12 12:47:53
Block 2 : Send Neha to Subh at 2025-12-12 12:47:56
...
๐ฐ Total NovaCoin traded: 110 NovaCoin
โฐ Simulation ended at: 2025-12-12 12:48:15 UTC
๐ Congrats! Mining operation completed successfully!
Here's what the actual program output looks like:
Each block in the blockchain contains:
struct Block {
index: u32, // Position in the blockchain
previous_hash: String, // Hash of the previous block
timestamp: u64, // Unix timestamp
data: String, // Transaction data
nonce: u64, // Proof-of-work nonce
hash: String, // Block's hash
}The mining process uses a proof-of-work algorithm:
- Calculate the hash of the block (index + previous_hash + timestamp + data + nonce)
- Check if the hash starts with the required number of zeros (difficulty)
- If not, increment the nonce and try again
- Repeat until a valid hash is found
Difficulty Level: Currently set to 2, meaning the hash must start with "00"
The hash is calculated using SHA-256:
fn calculate_hash(&self) -> String {
let data = format!(
"{}{}{}{}{}",
self.index, self.previous_hash, self.timestamp, self.data, self.nonce
);
let mut hasher = Sha256::new();
hasher.update(data.as_bytes());
let result = hasher.finalize();
format!("{:x}", result)
}The blockchain is a vector of blocks:
struct Blockchain {
chain: Vec<Block>,
}This project demonstrates several important blockchain concepts:
-
Immutability: Each block contains the hash of the previous block, making it impossible to alter past blocks without invalidating the entire chain
-
Proof-of-Work: Mining requires computational effort to find a valid hash, making it expensive to create fraudulent blocks
-
Cryptographic Hashing: SHA-256 ensures that any change to block data results in a completely different hash
-
Chain Integrity: The blockchain maintains integrity through the linked hash structure
-
Timestamps: Each block is timestamped, creating a chronological record of transactions
blockchain/
โโโ Cargo.toml # Project configuration and dependencies
โโโ Cargo.lock # Locked dependency versions
โโโ errors.md # Documentation of fixed syntax errors
โโโ README.md # This file
โโโ src/
โโโ main.rs # Main blockchain implementation
If you encounter build errors, ensure:
- You're using Rust edition 2021 or later
- All dependencies are properly installed (
cargo update) - You have the latest stable version of Rust (
rustup update)
If the mining process seems stuck:
- The difficulty is set to 2, which should mine blocks quickly
- If mining takes too long, you can reduce the
DIFFICULTYconstant inmain.rs
Change the DIFFICULTY constant at the top of main.rs:
const DIFFICULTY: usize = 2; // Increase for harder mining, decrease for easierModify the trader_names vector in the main() function:
let trader_names = vec!["Neha", "Subh", "Your Name Here", ...];Modify the transaction format in the loop:
let transaction = format!("Your custom format: {} -> {}", sender, recipient);Change the novacoin_per_block value:
let novacoin_per_block: usize = 10; // Change to your desired reward amountThis is an educational project created for learning blockchain concepts.
Feel free to fork this project and experiment with:
- Adding transaction validation
- Implementing a merkle tree
- Adding wallet functionality
- Creating a network layer for distributed nodes
- Implementing smart contracts
To learn more about blockchain technology:
This project uses:
- The Rust programming language
- SHA-256 cryptographic hashing
- Chrono for time handling
Happy Mining! โ๏ธ
