From 2d2d4931f82029304c4fa378b66abb0727fb3495 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Fri, 15 Nov 2024 20:48:34 -0500 Subject: [PATCH] `find_nonce()` + `add_aux_pow` async wrapper --- binaries/cuprated/src/rpc/json.rs | 60 +++++++++++++++---------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/binaries/cuprated/src/rpc/json.rs b/binaries/cuprated/src/rpc/json.rs index 0b273fa..5cf7c14 100644 --- a/binaries/cuprated/src/rpc/json.rs +++ b/binaries/cuprated/src/rpc/json.rs @@ -1009,8 +1009,16 @@ async fn flush_cache( }) } -/// +/// An async-friendly wrapper for [`add_aux_pow_inner`]. async fn add_aux_pow( + state: CupratedRpcHandler, + request: AddAuxPowRequest, +) -> Result { + tokio::task::spawn_blocking(|| add_aux_pow_inner(state, request)).await? +} + +/// +fn add_aux_pow_inner( mut state: CupratedRpcHandler, request: AddAuxPowRequest, ) -> Result { @@ -1046,47 +1054,35 @@ async fn add_aux_pow( path_domain += 1; } - let mut nonce = 0_u32; - let mut collision = true; - let mut slots: Box<[u32]> = vec![0; len].into_boxed_slice(); // INVARIANT: this must be the same `.len()` as `aux_pow` + fn find_nonce(aux_pow_len: usize) -> Result<(u32, Box<[u32]>), Error> { + // INVARIANT: this must be the same `.len()` as `aux_pow` + let mut slots: Box<[u32]> = vec![u32::MAX; aux_pow_len].into_boxed_slice(); + let mut slot_seen: Box<[bool]> = vec![false; aux_pow_len].into_boxed_slice(); - for n in 0..u32::MAX { - nonce = n; + for nonce in 0..u32::MAX { + for i in &mut slots { + let slot_u32: u32 = todo!("const uint32_t slot = cryptonote::get_aux_slot(aux_pow[idx].first, nonce, aux_pow.size());"); + let slot = u32_to_usize(slot_u32); - let slot_seen: Vec = vec![false; len]; + if slot >= aux_pow_len { + return Err(anyhow!("Computed slot is out of range")); + } - collision = false; + if slot_seen[slot] { + return Ok((nonce, slots)); + } - slots.fill(u32::MAX); - - for i in &mut slots { - let slot_u32: u32 = todo!("const uint32_t slot = cryptonote::get_aux_slot(aux_pow[idx].first, nonce, aux_pow.size());"); - let slot = u32_to_usize(slot_u32); - - if slot >= len { - return Err(anyhow!("Computed slot is out of range")); + slot_seen[slot] = true; + *i = slot_u32; } - if slot_seen[slot] { - collision = true; - break; - } - - slot_seen[slot] = true; - *i = slot_u32; + slots.fill(u32::MAX); } - if !collision { - break; - } + Err(anyhow!("Failed to find a suitable nonce")) } - let nonce = nonce; - let slots = slots; - - if collision { - return Err(anyhow!("Failed to find a suitable nonce")); - } + let (nonce, slots) = find_nonce(len)?; // FIXME: use iterator version. let (aux_pow_id_raw, aux_pow_raw) = {