https://github.com/omniflare/block-chain-rust
https://github.com/omniflare/block-chain-rust
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/omniflare/block-chain-rust
- Owner: omniflare
- Created: 2025-01-20T17:48:00.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-01-20T19:04:54.000Z (4 months ago)
- Last Synced: 2025-01-20T20:20:13.429Z (4 months ago)
- Language: Rust
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ๐ Rust Blockchain Implementation
A modern, lightweight blockchain implementation built with Rust and Axum web framework. This project demonstrates the core concepts of blockchain technology through a simple HTTP API.
## ๐ Tech Stack
- **Rust** `1.70+` - Systems programming language ๐ฆ
- **Axum** `0.7` - Lightning-fast web framework for Rust
- **Tokio** - Asynchronous runtime for Rust
- **Serde** - Serialization framework
- **Chrono** - Date and time library
- **SHA-256** - Cryptographic hash function## ๐ก Motivation
This project was created to understand and demonstrate core blockchain concepts in a simple, educational way. Instead of dealing with the complexity of a full cryptocurrency implementation, we focus on the fundamental blockchain data structure and its key properties:
- Immutable chain of blocks
- Cryptographic linking between blocks
- Basic proof-of-work mining
- Transaction handling
- Chain validation## ๐๏ธ Architecture
The project consists of three main components:
1. **Blockchain Core** - Handles the blockchain data structure and mining
2. **HTTP API** - Provides endpoints to interact with the blockchain
3. **Persistence Layer** - Saves and loads the blockchain from disk### Core Data Structures
```rust
// Transaction represents a transfer between parties
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Transaction {
sender: String,
receiver: String,
amount: u64,
}// Block represents a set of transactions in the chain
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Block {
hash: String,
transactions: Vec,
time_stamp: i64,
prev_hash: String,
nonce: u64,
}
```## ๐ ๏ธ Installation
1. Clone the repository:
```bash
git clone https://github.com/omniflare/block-chain-rust.git
cd rust-blockchain
```2. Build the project:
```bash
cargo build --release
```3. Run the server:
```bash
cargo run
```The server will start on `http://localhost:8000`
## ๐ก API Endpoints
### Get Blockchain
Retrieve the entire blockchain.```bash
curl http://localhost:8000/chain
```### Add Block
Add a new block with transactions.```bash
curl -X POST http://localhost:8000/add_block \
-H "Content-Type: application/json" \
-d '[
{
"sender": "Alice",
"receiver": "Bob",
"amount": 100
}
]'
```## ๐ Core Features
### Proof of Work Mining
Each block is mined using a simple proof-of-work algorithm:```rust
fn mine_block(&mut self, difficult: usize) {
while &self.hash[0..difficult] != "0".repeat(difficult) {
self.nonce += 1;
self.hash = self.calculate_hash();
}
}
```### Chain Validation
The blockchain maintains integrity through continuous validation:```rust
fn is_chain_valid(&self) -> bool {
for (i, block) in self.chain.iter().enumerate() {
if i == 0 { continue; }
let prev_block = &self.chain[i - 1];
if block.hash != block.calculate_hash() { return false; }
if prev_block.hash != block.prev_hash { return false; }
}
return true;
}
```## ๐พ Persistence
The blockchain is automatically saved to and loaded from a file:
```rust
// Save blockchain to file
blockchain.save_to_file("blockchain.txt")?;// Load blockchain from file
let blockchain = Blockchain::load_from_file("blockchain.txt")?;
```## ๐งช Testing
Run the test suite:
```bash
cargo test
```## ๐ง Configuration
The blockchain has configurable parameters:
- **Mining Difficulty**: Set in the `Blockchain::new()` method (default: 2)
- **Server Port**: Configurable in `main.rs` (default: 8000)
- **Storage File**: Configurable in `main.rs` (default: "blockchain.txt")## ๐ง Limitations
- No distributed consensus mechanism
- Simple proof-of-work implementation
- No transaction validation
- No peer-to-peer networking
- No cryptocurrency functionality## ๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## ๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
## โญ๏ธ Future Improvements
- [ ] Add peer-to-peer networking
- [ ] Implement transaction validation
- [ ] Add merkle tree for transactions
- [ ] Create web interface
- [ ] Add configurable mining difficulty
- [ ] Implement wallet functionality## ๐ Resources
- [Rust Book](https://doc.rust-lang.org/book/)
- [Axum Documentation](https://docs.rs/axum/latest/axum/)
- [Blockchain Basics](https://developer.mozilla.org/en-US/docs/Web/API/Blockchain)