diff --git a/Cargo.lock b/Cargo.lock index 2a96fa4..8c1a04e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -577,6 +577,7 @@ dependencies = [ "bytemuck", "criterion", "engine-cpu", + "env_logger", "futures", "hex", "log", diff --git a/crates/engine-gpu/Cargo.toml b/crates/engine-gpu/Cargo.toml index 0ff2020..a362c9e 100644 --- a/crates/engine-gpu/Cargo.toml +++ b/crates/engine-gpu/Cargo.toml @@ -41,6 +41,7 @@ rand_chacha = "0.3" # For deterministic random test generation hex = { workspace = true } criterion = "0.5" rand = { workspace = true, features = ["std", "std_rng"] } +env_logger.workspace = true [[bench]] name = "gpu_engine_bench" diff --git a/crates/engine-gpu/examples/verify_nonce.rs b/crates/engine-gpu/examples/verify_nonce.rs new file mode 100644 index 0000000..8745898 --- /dev/null +++ b/crates/engine-gpu/examples/verify_nonce.rs @@ -0,0 +1,91 @@ +use engine_cpu::{BaselineCpuEngine, EngineStatus, MinerEngine, Range}; +use engine_gpu::GpuEngine; +use pow_core::JobContext; +use primitive_types::U512; +use std::sync::atomic::AtomicBool; + +fn main() { + // Initialize logging + env_logger::builder() + .filter_level(log::LevelFilter::Info) + .parse_default_env() + .init(); + + log::info!("Starting verify_nonce example"); + + // 1. Setup Context + // Use a fixed header and easy difficulty (1) so any nonce is valid + let header = [1u8; 32]; + let difficulty = U512::from(1u64); + let cpu_engine = BaselineCpuEngine::new(); + let ctx = cpu_engine.prepare_context(header, difficulty); + + log::info!("Context prepared. Difficulty: {}", difficulty); + + // 2. Find a valid nonce using CPU engine + // Since difficulty is 1, the first nonce (0) should be valid. + // But let's search a small range to be sure. + let range = Range { + start: U512::from(0u64), + end: U512::from(100u64), + }; + let cancel = AtomicBool::new(false); + + log::info!("Searching for nonce with CPU engine..."); + let cpu_result = cpu_engine.search_range(&ctx, range.clone(), &cancel); + + let valid_nonce = match cpu_result { + EngineStatus::Found { candidate, .. } => { + log::info!("CPU found nonce: {}", candidate.nonce); + log::info!("CPU hash: {:x}", candidate.hash); + candidate.nonce + } + _ => { + log::error!("CPU failed to find a nonce in range!"); + return; + } + }; + + // 3. Verify with GPU engine + log::info!("Initializing GPU engine..."); + let gpu_engine = GpuEngine::new(); + + // Search a small range around the valid nonce + let gpu_range = Range { + start: valid_nonce, + end: valid_nonce + U512::from(10u64), // Search 10 nonces starting from the valid one + }; + + log::info!( + "Searching for nonce with GPU engine in range {} - {}", + gpu_range.start, + gpu_range.end + ); + let gpu_result = gpu_engine.search_range(&ctx, gpu_range, &cancel); + + match gpu_result { + EngineStatus::Found { candidate, .. } => { + log::info!("GPU found nonce: {}", candidate.nonce); + log::info!("GPU hash: {:x}", candidate.hash); + + if candidate.nonce == valid_nonce { + log::info!("SUCCESS: GPU found the same nonce as CPU!"); + } else { + log::warn!( + "GPU found a DIFFERENT nonce: {} (Expected: {})", + candidate.nonce, + valid_nonce + ); + } + } + EngineStatus::Exhausted { .. } => { + log::error!("FAILURE: GPU exhausted range without finding the nonce!"); + } + EngineStatus::Cancelled { .. } => { + log::error!("FAILURE: GPU search cancelled!"); + } + EngineStatus::Running { .. } => { + log::error!("FAILURE: GPU returned Running status!"); + } + } +} diff --git a/crates/engine-gpu/src/lib.rs b/crates/engine-gpu/src/lib.rs index 889baf9..a27ab9d 100644 --- a/crates/engine-gpu/src/lib.rs +++ b/crates/engine-gpu/src/lib.rs @@ -153,7 +153,7 @@ impl GpuEngine { .write_buffer(&ctx.header_buffer, 0, bytemuck::cast_slice(&header_u32s)); // Update Target Buffer - let target_bytes = job_ctx.difficulty.to_little_endian(); + let target_bytes = job_ctx.target.to_little_endian(); let mut target_u32s = [0u32; 16]; for i in 0..16 { let chunk = &target_bytes[i * 4..(i + 1) * 4];