Skip to content

A simple blockchain implementation in Rust with proof-of-work consensus, SHA-256 hashing, and CLI interface

License

Notifications You must be signed in to change notification settings

yashhzd/rust-blockchain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-blockchain

A simple blockchain implementation in Rust, built from scratch to demonstrate core blockchain concepts.

Features

  • Block Creation - Create blocks with SHA-256 hashing
  • Proof of Work - Configurable mining difficulty
  • Chain Validation - Verify integrity of the entire blockchain
  • Transaction Support - Simple transaction model with sender, receiver, and amount
  • CLI Interface - Interactive command-line interface for blockchain operations
  • Persistence - Save and load blockchain from JSON files

Architecture

src/
├── main.rs          # CLI entry point
├── lib.rs           # Library exports
├── block.rs         # Block struct and hashing
├── blockchain.rs    # Chain management and validation
├── transaction.rs   # Transaction model
└── pow.rs           # Proof of work consensus

Quick Start

# Clone the repository
git clone https://github.com/yashhzd/rust-blockchain.git
cd rust-blockchain

# Build
cargo build --release

# Run the CLI
cargo run

# Run tests
cargo test

CLI Commands

mine <data>       - Mine a new block with the given data
chain             - Display the full blockchain
validate          - Validate the blockchain integrity
balance <address> - Check balance of an address
send <from> <to> <amount> - Create a transaction
save              - Save blockchain to file
load              - Load blockchain from file
help              - Show available commands
exit              - Exit the program

How It Works

Block Structure

Each block contains:

  • Index, timestamp, and data payload
  • SHA-256 hash of the block contents
  • Hash of the previous block (linking the chain)
  • Nonce (proof of work solution)

Proof of Work

The mining process finds a nonce value that produces a hash with a required number of leading zeros (configurable difficulty).

Chain Validation

Validates that:

  1. Each block's hash is correctly computed
  2. Each block references the previous block's hash
  3. The genesis block is valid
  4. All proof-of-work solutions are valid

Development

# Run with logging
RUST_LOG=debug cargo run

# Run specific tests
cargo test test_block_creation
cargo test test_chain_validation

# Format code
cargo fmt

# Lint
cargo clippy

License

MIT

About

A simple blockchain implementation in Rust with proof-of-work consensus, SHA-256 hashing, and CLI interface

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages