Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blockchain Simulator

A simple blockchain implementation written in Rust that simulates cryptocurrency transactions and mining operations.

Features

  • 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

Architecture

Core Components

  • 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

Dependencies

  • sha2: For SHA-256 cryptographic hashing
  • chrono: For timestamp handling and date formatting

Installation

  1. Make sure you have Rust installed on your system
  2. Clone this repository
  3. Navigate to the project directory
  4. Build the project:
cargo build

Usage

Run the blockchain simulator:

cargo run

The program will:

  1. Prompt you to enter your miner name
  2. Start mining blocks with simulated transactions
  3. Display mining progress and transaction details
  4. Show final statistics including total blocks and coins traded

Example Output

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
...

Configuration

Mining Difficulty

The mining difficulty can be adjusted by changing the DIFFICULTY constant in src/main.rs:

const DIFFICULTY: usize = 2; // Number of leading zeros required

Coin Rewards

The coin reward per block can be modified in the main function:

let bekcoin_per_block: usize = 137; // Coins per block

Code Structure

src/
├── main.rs          # Main application logic

Key Functions

  • Block::new(): Creates a new block with current timestamp
  • Block::calculate_hash(): Generates SHA-256 hash of block data
  • Block::mine_block_with_visual_effects(): Mines block with proof-of-work
  • Blockchain::add_block(): Adds validated block to chain
  • Blockchain::get_total_block(): Returns total number of blocks

Technical Details

Hash Calculation

Blocks are hashed using SHA-256 with the following data:

  • Block index
  • Previous block hash
  • Timestamp
  • Transaction data
  • Nonce value

Mining Process

  1. Calculate hash of block data
  2. Check if hash starts with required number of zeros
  3. If not, increment nonce and try again
  4. Continue until valid hash is found
  5. Add block to blockchain

Chain Validation

  • Each block contains hash of previous block
  • Genesis block has empty previous hash
  • Chain integrity maintained through hash linking

Future Enhancements

  • 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

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is open source and available under the MIT License.

Educational Purpose

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.

About

A simple blockchain implementation written in Rust that simulates cryptocurrency transactions and mining operations.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages