| Crates.io | blockchain_rs |
| lib.rs | blockchain_rs |
| version | 0.1.0 |
| created_at | 2025-01-03 09:09:35.743821+00 |
| updated_at | 2025-01-03 09:09:35.743821+00 |
| description | A lightweight and efficient blockchain implementation in Rust. |
| homepage | |
| repository | https://github.com/yangsnapify/blockchain-rs |
| max_upload_size | |
| id | 1502245 |
| size | 35,183 |
A lightweight and efficient blockchain implementation in Rust that features transaction management, signature verification, and Merkle tree implementation for block validation.
Add this to your Cargo.toml:
[dependencies]
blockchain_rs = "0.1.0"
use blockchain_rs::{Transaction, Config, TransactionPool, NonceManager, Blockchain};
use std::sync::{Arc, Mutex};
fn main() {
// Initialize blockchain components
let mut blockchain = Blockchain::new();
let config = Config::load_config();
let key_pair = Transaction::construct_key_pair();
let nonce_manager = Arc::new(Mutex::new(NonceManager::new()));
let mut tx_pool = TransactionPool::new(config.max_transactions_per_pool);
// Create and sign a transaction
let mut tx = Transaction::new("sender", "receiver", "1 ETH", nonce_manager.clone());
tx.signature(&key_pair);
// Add to pool
tx_pool.update_pending_pool(tx, &mut blockchain);
}
The main data structure that maintains the chain of blocks. Each block contains:
Represents a single transaction in the blockchain:
Manages pending transactions:
Handles transaction ordering:
let mut tx1 = Transaction::new("Alice", "Bob", "1 eth", nonce_manager.clone());
let mut tx2 = Transaction::new("Bob", "Charlie", "2 eth", nonce_manager.clone());
tx1.signature(&key_pair);
tx2.signature(&key_pair);
transact_pool.update_pending_pool(tx1, &mut chain);
transact_pool.update_pending_pool(tx2, &mut chain);
for block in &chain.chain {
println!("Block Index: {:?}", block.tree);
println!("Previous Hash: {}",
block.previous_hash.as_ref().unwrap_or(&"Genesis Block".to_string()));
}
The blockchain can be configured through the Config struct:
Always verify transaction signatures before adding to pool
Use proper error handling for failed transactions
Implement proper synchronization for multi-threaded access
Regularly backup blockchain state
Monitor transaction pool size
This project is licensed under the MIT License - see the LICENSE file for details.