A simple blockchain implementation in Rust, built from scratch to demonstrate core blockchain concepts.
- 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
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
# 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 testmine <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
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)
The mining process finds a nonce value that produces a hash with a required number of leading zeros (configurable difficulty).
Validates that:
- Each block's hash is correctly computed
- Each block references the previous block's hash
- The genesis block is valid
- All proof-of-work solutions are valid
# 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 clippyMIT