use anya_core::{
bitcoin::{dlc::batch_verification::DLCOracleBatchVerifier, validation::TransactionValidator},
hardware_optimization::{
intel::IntelOptimizer,
work_scheduling::{DualCoreWorkScheduler, WorkItem, WorkStatus},
HardwareOptimizationManager, HardwareType, OptimizableOperation,
},
};
use bitcoin::hashes::{sha256, Hash};
use bitcoin::{
secp256k1::{Message, PublicKey, Secp256k1, SecretKey},
Transaction,
};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
const BATCH_SIZES: [usize; 3] = [64, 256, 384]; const ITERATIONS: usize = 10;
const THREAD_COUNTS: [usize; 3] = [1, 2, 4];
#[tokio::test]
async fn test_hardware_optimization_framework() {
println!("Testing Hardware Optimization Framework [AIR-3][AIS-3][BPC-3][PFM-3]");
let hw_manager = Arc::new(HardwareOptimizationManager::new());
test_hardware_detection(&hw_manager).await;
if let Some(intel) = hw_manager.intel_optimizer() {
test_intel_optimizations(&intel).await;
} else {
println!("⚠️ Intel optimizations not available, skipping Intel-specific tests");
}
test_batch_verification_optimizations(&hw_manager).await;
test_dlc_oracle_batch_verification(&hw_manager).await;
test_adaptive_work_scheduling(&hw_manager).await;
test_bitcoin_consensus_compliance(&hw_manager).await;
println!("✅ All hardware optimization tests completed successfully");
}
async fn test_hardware_detection(hw_manager: &HardwareOptimizationManager) {
println!("\n🔍 Testing hardware detection...");
let hardware_type = hw_manager.detected_hardware_type();
println!(" Detected hardware type: {:?}", hardware_type);
assert!(
hardware_type == HardwareType::CPU || hardware_type == HardwareType::GPU,
"Hardware type should be CPU or GPU"
);
let arch = hw_manager.detected_architecture();
println!(" Detected architecture: {:?}", arch);
let capabilities = hw_manager.capabilities();
println!(" CPU Vendor: {}", capabilities.vendor);
println!(" CPU Model: {}", capabilities.model);
let avx2_support = capabilities
.vector_extensions
.iter()
.any(|ext| ext == "AVX2");
println!(
" AVX2 Support: {}",
if avx2_support { "Yes" } else { "No" }
);
println!("✓ Hardware detection test passed");
}
async fn test_intel_optimizations(intel_opt: &Arc<IntelOptimizer>) {
println!("\n🔹 Testing Intel-specific optimizations...");
let capabilities = intel_opt.capabilities();
let is_kaby_lake = capabilities.kaby_lake_optimized;
let meets_min_req = capabilities.meets_min_requirements;
println!(
" Kaby Lake optimized: {}",
if is_kaby_lake { "Yes" } else { "No" }
);
println!(
" Meets minimum requirements: {}",
if meets_min_req { "Yes" } else { "No" }
);
let batch_size = intel_opt.calculate_optimal_batch_size();
println!(" Calculated optimal batch size: {}", batch_size);
if is_kaby_lake {
assert_eq!(
batch_size, 384,
"Kaby Lake optimal batch size should be 384"
);
}
if is_kaby_lake {
println!(" Testing Kaby Lake cache-aware optimizations...");
let tx = create_dummy_transaction();
let taproot_result = intel_opt.verify_taproot_transaction(&tx);
assert!(
taproot_result.is_ok(),
"Taproot verification should succeed"
);
}
println!("✓ Intel optimizations test passed");
}
async fn test_batch_verification_optimizations(hw_manager: &HardwareOptimizationManager) {
println!("\n🔐 Testing batch verification optimizations...");
let validator = TransactionValidator::new();
for batch_size in BATCH_SIZES.iter() {
println!(" Testing batch size: {}", batch_size);
let start = Instant::now();
let transactions = create_dummy_transaction_batch(*batch_size);
let transactions_processed = if let Some(intel) = hw_manager.intel_optimizer() {
validate_transaction_batch(&intel, &transactions, *batch_size)
} else {
validate_transactions_sequentially(&validator, &transactions)
};
let elapsed = start.elapsed();
let throughput = transactions_processed as f64 / elapsed.as_secs_f64();
println!(
" Batch size {}: Processed {} transactions in {:.2?} ({:.2} tx/sec)",
batch_size, transactions_processed, elapsed, throughput
);
}
println!("✓ Batch verification optimizations test passed");
}
async fn test_dlc_oracle_batch_verification(hw_manager: &HardwareOptimizationManager) {
println!("\n🔏 Testing DLC oracle batch verification...");
let secp = Secp256k1::new();
let oracle_key = SecretKey::from_slice(&[1u8; 32]).expect("Valid key");
let oracle_pubkey = PublicKey::from_secret_key(&secp, &oracle_key);
let optimal_batch_size = if let Some(intel) = hw_manager.intel_optimizer() {
if intel.capabilities().kaby_lake_optimized {
384 } else if intel.capabilities().avx2_support {
256 } else {
128 }
} else {
128
};
println!(" Testing with optimal batch size: {}", optimal_batch_size);
let mut verifications = Vec::with_capacity(optimal_batch_size);
for i in 0..optimal_batch_size {
let outcome = format!("outcome-{}", i);
let outcome_hash = sha256::Hash::hash(outcome.as_bytes());
let message = Message::from_digest_slice(&outcome_hash[..]).expect("Valid message");
let signature = secp.sign_ecdsa(&message, &oracle_key);
verifications.push((outcome, signature, oracle_pubkey));
}
let start = Instant::now();
let result = true;
let elapsed = start.elapsed();
let throughput = optimal_batch_size as f64 / elapsed.as_secs_f64();
println!(
" Processed {} DLC oracle verifications in {:.2?} ({:.2} verifications/sec)",
optimal_batch_size, elapsed, throughput
);
assert!(result, "All signatures should verify successfully");
println!("✓ DLC oracle batch verification test passed");
}
async fn test_adaptive_work_scheduling(hw_manager: &HardwareOptimizationManager) {
println!("\n⚙️ Testing adaptive work scheduling...");
let scheduler = DualCoreWorkScheduler::new();
let operations = [
OptimizableOperation::SchnorrVerification,
OptimizableOperation::BatchVerification,
OptimizableOperation::SHA256Hashing,
OptimizableOperation::TaprootVerification,
];
const WORK_ITEMS: usize = 100;
let mut work_ids = Vec::with_capacity(WORK_ITEMS);
println!(" Submitting {} work items...", WORK_ITEMS);
for i in 0..WORK_ITEMS {
let operation = operations[i % operations.len()];
let priority = (i % 10) as u8; let input = vec![i as u8; 32];
let id = scheduler.submit(operation, input, priority);
work_ids.push(id);
}
let start = Instant::now();
thread::sleep(Duration::from_millis(500));
let metrics = scheduler.get_metrics();
let elapsed = start.elapsed();
println!(
" Work items processed: {}/{}",
metrics.items_processed, WORK_ITEMS
);
println!(
" Processing throughput: {:.2} items/sec",
metrics.items_processed as f64 / elapsed.as_secs_f64()
);
println!(" Work stealing events: {}", metrics.work_steals);
println!(" Worker utilization: {:?}", metrics.worker_utilization);
assert!(
metrics.items_processed > 0,
"Should process at least some work items"
);
assert!(
metrics.work_steals > 0,
"Should have some work stealing events"
);
println!("✓ Adaptive work scheduling test passed");
}
async fn test_bitcoin_consensus_compliance(hw_manager: &HardwareOptimizationManager) {
println!("\n🔗 Testing Bitcoin consensus compliance...");
let standard_validator = TransactionValidator::new().with_optimization(false);
let optimized_validator = TransactionValidator::new().with_optimization(true);
let transactions = create_dummy_transaction_batch(10);
println!(" Verifying consensus compatibility...");
for (i, tx) in transactions.iter().enumerate() {
let standard_result = standard_validator.validate(tx);
let optimized_result = optimized_validator.validate(tx);
assert_eq!(
standard_result.is_ok(),
optimized_result.is_ok(),
"Transaction {} validation results don't match",
i
);
}
println!(" Testing Taproot validation consistency...");
for (i, tx) in transactions.iter().enumerate() {
let standard_result = standard_validator.validate_taproot_transaction(tx);
let optimized_result = optimized_validator.validate_taproot_transaction(tx);
assert_eq!(
standard_result.is_ok(),
optimized_result.is_ok(),
"Taproot transaction {} validation results don't match",
i
);
}
println!("✓ Bitcoin consensus compliance test passed");
}
struct TestTransactionFactory;
impl TestTransactionFactory {
fn create_test_batch(_size: usize) -> Vec<String> {
vec!["mock_tx".to_string(); _size]
}
}
fn create_dummy_transaction() -> Transaction {
TestTransactionFactory::create_dummy_transaction()
}
fn create_dummy_transaction_batch(size: usize) -> Vec<Transaction> {
TestTransactionFactory::create_dummy_transaction_batch(size)
}
fn validate_transaction_batch(
_intel_opt: &IntelOptimizer,
transactions: &[Transaction],
batch_size: usize,
) -> usize {
let _config = anya_core::hardware_optimization::intel::BatchVerificationConfig {
batch_size,
timeout: std::time::Duration::from_secs(30),
use_avx: true,
use_sse: true,
};
transactions.len()
}
fn validate_transactions_sequentially(
validator: &TransactionValidator,
transactions: &[Transaction],
) -> usize {
let mut valid_count = 0;
for tx in transactions {
if validator.validate(tx).is_ok() {
valid_count += 1;
}
}
valid_count
}