A simple blockchain implementation written in Rust that simulates cryptocurrency transactions and mining operations.
- Block Mining: Implements a proof-of-work mining algorithm with configurable difficulty
- Transaction Simulation: Simulates cryptocurrency transactions between users
- SHA-256 Hashing: Uses cryptographic hashing for block integrity
- Interactive Mining: Visual feedback during the mining process
- Timestamp Tracking: Records creation time for each block
- Blockchain Validation: Maintains chain integrity through hash linking
-
Block: Individual units of the blockchain containing:
- Index (position in chain)
- Previous block hash
- Timestamp
- Transaction data
- Nonce (proof-of-work)
- Current block hash
-
Blockchain: The main chain structure that:
- Maintains a vector of blocks
- Validates new blocks before adding
- Tracks total blocks and transactions
-
Mining Algorithm: Proof-of-work system that:
- Requires hash to start with configurable number of zeros
- Increments nonce until valid hash is found
- Provides visual feedback during mining
sha2: For SHA-256 cryptographic hashingchrono: For timestamp handling and date formatting
- Make sure you have Rust installed on your system
- Clone this repository
- Navigate to the project directory
- Build the project:
cargo buildRun the blockchain simulator:
cargo runThe program will:
- Prompt you to enter your miner name
- Start mining blocks with simulated transactions
- Display mining progress and transaction details
- Show final statistics including total blocks and coins traded
Welcome to the Blockchain Simulator!
Enter your miner name:
> Alice
Lets start mining and simulating transactions
Mining Block 1
Mining in Progress...
Calculated hash: a1b2c3d4e5f6...
Block Mined: 1
Transaction added to block Transaction from Alice to Bob
Mining Block 2
...
The mining difficulty can be adjusted by changing the DIFFICULTY constant in src/main.rs:
const DIFFICULTY: usize = 2; // Number of leading zeros requiredThe coin reward per block can be modified in the main function:
let bekcoin_per_block: usize = 137; // Coins per blocksrc/
├── main.rs # Main application logic
Block::new(): Creates a new block with current timestampBlock::calculate_hash(): Generates SHA-256 hash of block dataBlock::mine_block_with_visual_effects(): Mines block with proof-of-workBlockchain::add_block(): Adds validated block to chainBlockchain::get_total_block(): Returns total number of blocks
Blocks are hashed using SHA-256 with the following data:
- Block index
- Previous block hash
- Timestamp
- Transaction data
- Nonce value
- Calculate hash of block data
- Check if hash starts with required number of zeros
- If not, increment nonce and try again
- Continue until valid hash is found
- Add block to blockchain
- Each block contains hash of previous block
- Genesis block has empty previous hash
- Chain integrity maintained through hash linking
- Add digital signatures for transactions
- Implement merkle trees for transaction verification
- Add network simulation with multiple nodes
- Create wallet functionality
- Add transaction fees
- Implement different consensus algorithms
- Add block size limits
- Create REST API for blockchain interaction
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source and available under the MIT License.
This blockchain simulator is designed for educational purposes to demonstrate:
- How blockchain technology works
- Proof-of-work consensus mechanism
- Cryptographic hashing in blockchains
- Transaction processing and validation
- Block mining and chain maintenance
Note: This is a simplified implementation and should not be used for production cryptocurrency systems.