A simple blockchain implementation built with Rust and Actix-web, demonstrating core blockchain concepts including block creation, proof-of-work mining, and a RESTful API interface.
- Overview
- Features
- Technology Stack
- Project Structure
- Prerequisites
- Installation
- Configuration
- Usage
- API Endpoints
- How It Works
- Development
- Contributing
- License
DemoBlockChain is an educational blockchain implementation that showcases the fundamental principles of blockchain technology. Built with Rust for performance and safety, it provides a REST API for interacting with the blockchain, allowing users to add transactions, mine blocks, and view the chain state.
- Block Creation: Create blocks with transaction data
- Proof of Work: Implements mining algorithm with adjustable difficulty
- SHA-256 Hashing: Cryptographic hashing for block integrity
- RESTful API: Easy-to-use HTTP endpoints for blockchain interaction
- CORS Support: Cross-origin resource sharing enabled
- Logging: Comprehensive logging for debugging and monitoring
- Transaction Management: Add and manage transactions before mining
- Chain Validation: Ensures blockchain integrity
- Language: Rust (2024 edition)
- Web Framework: Actix-web 4.11.0
- Hashing: SHA-2 (sha2 crate)
- Serialization: Serde & Serde JSON
- CORS: Actix-cors 0.7.1
- Logging: env_logger 0.11.8 & log 0.4.28
- Time Management: Chrono 0.4.42
- Environment Variables: dotenv 0.15.0
DemoBlockChain/
βββ src/
β βββ main.rs # Main application code
βββ Cargo.toml # Rust dependencies and project metadata
βββ Cargo.lock # Locked dependency versions
βββ .env.example # Example environment configuration
βββ .gitignore # Git ignore rules
βββ README.md # Project documentation
Before running this project, ensure you have the following installed:
- Rust: Version 1.70 or higher
- Install from rustup.rs
- Cargo: Comes with Rust installation
- Git: For cloning the repository
-
Clone the repository
git clone https://github.com/raunit-dev/DemoBlockChain.git cd DemoBlockChain -
Install dependencies
cargo build
-
Set up environment variables
cp .env.example .env # Edit .env with your configuration
Create a .env file based on .env.example:
# Server configuration
RUST_LOG=info
PORT=8080You can adjust the following settings:
RUST_LOG: Logging level (trace, debug, info, warn, error)PORT: Server port (default: 8080)
-
Development mode
cargo run
-
Production build
cargo build --release ./target/release/rust-back
-
With logging
RUST_LOG=debug cargo run
The server will start on http://localhost:8080 (or your configured port).
GET /blockchainReturns the entire blockchain with all blocks.
Response:
{
"chain": [...],
"length": 3
}POST /transaction
Content-Type: application/jsonRequest Body:
{
"sender": "Alice",
"recipient": "Bob",
"amount": 50
}Response:
{
"message": "Transaction added successfully"
}POST /mineMines a new block with pending transactions.
Response:
{
"message": "Block mined successfully",
"block": {...}
}GET /latestReturns the most recent block in the chain.
GET /validateChecks if the blockchain is valid.
Response:
{
"valid": true
}Each block contains:
- Index: Position in the blockchain
- Timestamp: When the block was created
- Transactions: List of transactions in the block
- Proof: Nonce value from proof-of-work
- Previous Hash: Hash of the previous block
- Hash: Current block's hash
The mining algorithm:
- Starts with a nonce of 0
- Calculates hash of block data + nonce
- Checks if hash meets difficulty requirement (leading zeros)
- Increments nonce and repeats until valid hash found
Validates the blockchain by:
- Checking each block's hash is correct
- Verifying each block's previous_hash matches the previous block
- Ensuring proof-of-work is valid for each block
cargo testcargo fmtcargo clippycargo install cargo-watch
cargo watch -x runContributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is open source and available for educational purposes.
raunit-dev
- GitHub: @raunit-dev
- Built as a learning project to understand blockchain fundamentals
- Inspired by the original Bitcoin whitepaper
- Uses Rust for memory safety and performance
β Star this repository if you found it helpful!