135 lines
4.0 KiB
Rust
135 lines
4.0 KiB
Rust
use std::collections::HashMap;
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
// Simple standalone test to verify core HyperBFT concepts work
|
|
#[derive(Debug, Clone)]
|
|
pub struct SimpleTransaction {
|
|
pub from: String,
|
|
pub to: String,
|
|
pub amount: u64,
|
|
pub fee: u64,
|
|
pub id: String,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct SimpleBlock {
|
|
pub height: u64,
|
|
pub transactions: Vec<SimpleTransaction>,
|
|
pub timestamp: u64,
|
|
pub hash: String,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct SimpleBlockchain {
|
|
pub blocks: Vec<SimpleBlock>,
|
|
pub accounts: HashMap<String, u64>,
|
|
}
|
|
|
|
impl SimpleBlockchain {
|
|
pub fn new() -> Self {
|
|
let mut accounts = HashMap::new();
|
|
accounts.insert("alice".to_string(), 10000);
|
|
accounts.insert("bob".to_string(), 5000);
|
|
accounts.insert("charlie".to_string(), 3000);
|
|
|
|
Self {
|
|
blocks: vec![],
|
|
accounts,
|
|
}
|
|
}
|
|
|
|
pub fn add_block(&mut self, transactions: Vec<SimpleTransaction>) -> Result<(), String> {
|
|
// Validate transactions
|
|
for tx in &transactions {
|
|
let from_balance = self.accounts.get(&tx.from).unwrap_or(&0);
|
|
if *from_balance < tx.amount + tx.fee {
|
|
return Err(format!("Insufficient balance for {}", tx.from));
|
|
}
|
|
}
|
|
|
|
// Execute transactions
|
|
for tx in &transactions {
|
|
let from_balance = self.accounts.get_mut(&tx.from).unwrap();
|
|
*from_balance -= tx.amount + tx.fee;
|
|
|
|
let to_balance = self.accounts.entry(tx.to.clone()).or_insert(0);
|
|
*to_balance += tx.amount;
|
|
}
|
|
|
|
// Create block
|
|
let block = SimpleBlock {
|
|
height: self.blocks.len() as u64,
|
|
transactions,
|
|
timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
|
|
hash: format!("block_{}", self.blocks.len()),
|
|
};
|
|
|
|
self.blocks.push(block);
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
println!("🧪 HyperSolana Core Functionality Test");
|
|
println!("=====================================");
|
|
|
|
let mut blockchain = SimpleBlockchain::new();
|
|
|
|
println!("✅ Blockchain initialized");
|
|
println!("Initial balances: {:?}", blockchain.accounts);
|
|
|
|
// Test transaction 1
|
|
let tx1 = SimpleTransaction {
|
|
from: "alice".to_string(),
|
|
to: "bob".to_string(),
|
|
amount: 1000,
|
|
fee: 10,
|
|
id: "tx1".to_string(),
|
|
};
|
|
|
|
match blockchain.add_block(vec![tx1]) {
|
|
Ok(_) => println!("✅ Transaction 1 processed successfully"),
|
|
Err(e) => println!("❌ Transaction 1 failed: {}", e),
|
|
}
|
|
|
|
// Test transaction 2
|
|
let tx2 = SimpleTransaction {
|
|
from: "bob".to_string(),
|
|
to: "charlie".to_string(),
|
|
amount: 500,
|
|
fee: 5,
|
|
id: "tx2".to_string(),
|
|
};
|
|
|
|
match blockchain.add_block(vec![tx2]) {
|
|
Ok(_) => println!("✅ Transaction 2 processed successfully"),
|
|
Err(e) => println!("❌ Transaction 2 failed: {}", e),
|
|
}
|
|
|
|
// Test invalid transaction (insufficient balance)
|
|
let tx3 = SimpleTransaction {
|
|
from: "charlie".to_string(),
|
|
to: "alice".to_string(),
|
|
amount: 10000,
|
|
fee: 10,
|
|
id: "tx3".to_string(),
|
|
};
|
|
|
|
match blockchain.add_block(vec![tx3]) {
|
|
Ok(_) => println!("❌ Invalid transaction should have failed!"),
|
|
Err(e) => println!("✅ Invalid transaction correctly rejected: {}", e),
|
|
}
|
|
|
|
println!("\nFinal Results:");
|
|
println!("Blocks created: {}", blockchain.blocks.len());
|
|
println!("Final balances: {:?}", blockchain.accounts);
|
|
|
|
println!("\nBlock Details:");
|
|
for (i, block) in blockchain.blocks.iter().enumerate() {
|
|
println!("Block {}: {} transactions at height {}",
|
|
i, block.transactions.len(), block.height);
|
|
}
|
|
|
|
println!("\n🎉 HyperSolana Core Test PASSED!");
|
|
println!("Core blockchain functionality working correctly!");
|
|
} |