Compare commits

...

2 commits

Author SHA1 Message Date
hinto.janai
2d2d4931f8
find_nonce() + add_aux_pow async wrapper 2024-11-15 20:48:34 -05:00
hinto.janai
854b1752b1
checkup 2024-11-15 20:11:56 -05:00

View file

@ -1,4 +1,8 @@
//! RPC request handler functions (JSON-RPC). //! RPC request handler functions (JSON-RPC).
//!
//! TODO:
//! Many handlers have `todo!()`s for internals that must be completed, see:
//! <https://github.com/Cuprate/cuprate/pull/308>
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -436,9 +440,7 @@ async fn get_info(
let rpc_connections_count = if restricted { let rpc_connections_count = if restricted {
0 0
} else { } else {
todo!( todo!("implement a connection counter in axum/RPC")
"implement a connection counter in axum/RPC, maybe `AtomicU64` any handler activating"
)
}; };
let start_time = if restricted { 0 } else { *START_INSTANT_UNIX }; let start_time = if restricted { 0 } else { *START_INSTANT_UNIX };
let synchronized = blockchain_manager::synced(&mut state.blockchain_manager).await?; let synchronized = blockchain_manager::synced(&mut state.blockchain_manager).await?;
@ -1007,8 +1009,16 @@ async fn flush_cache(
}) })
} }
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2072-L2207> /// An async-friendly wrapper for [`add_aux_pow_inner`].
async fn add_aux_pow( async fn add_aux_pow(
state: CupratedRpcHandler,
request: AddAuxPowRequest,
) -> Result<AddAuxPowResponse, Error> {
tokio::task::spawn_blocking(|| add_aux_pow_inner(state, request)).await?
}
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2072-L2207>
fn add_aux_pow_inner(
mut state: CupratedRpcHandler, mut state: CupratedRpcHandler,
request: AddAuxPowRequest, request: AddAuxPowRequest,
) -> Result<AddAuxPowResponse, Error> { ) -> Result<AddAuxPowResponse, Error> {
@ -1037,53 +1047,43 @@ async fn add_aux_pow(
let len = aux_pow.len(); let len = aux_pow.len();
// TODO: why is this here? it does nothing:
// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2110-L2112>
let mut path_domain = 1_usize; let mut path_domain = 1_usize;
while 1 << path_domain < len { while 1 << path_domain < len {
path_domain += 1; path_domain += 1;
} }
let mut nonce = 0_u32; fn find_nonce(aux_pow_len: usize) -> Result<(u32, Box<[u32]>), Error> {
let mut collision = true; // INVARIANT: this must be the same `.len()` as `aux_pow`
let mut slots: Box<[u32]> = vec![0; len].into_boxed_slice(); // 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;
let slot_seen: Vec<bool> = vec![false; len];
collision = false;
slots.iter_mut().for_each(|i| *i = u32::MAX);
for nonce in 0..u32::MAX {
for i in &mut slots { 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: 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 = u32_to_usize(slot_u32);
if slot >= len { if slot >= aux_pow_len {
return Err(anyhow!("Computed slot is out of range")); return Err(anyhow!("Computed slot is out of range"));
} }
if slot_seen[slot] { if slot_seen[slot] {
collision = true; return Ok((nonce, slots));
break;
} }
slot_seen[slot] = true; slot_seen[slot] = true;
*i = slot_u32; *i = slot_u32;
} }
if !collision { slots.fill(u32::MAX);
break;
}
} }
let nonce = nonce; Err(anyhow!("Failed to find a suitable 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. // FIXME: use iterator version.
let (aux_pow_id_raw, aux_pow_raw) = { let (aux_pow_id_raw, aux_pow_raw) = {
let mut aux_pow_id_raw = Vec::<[u8; 32]>::with_capacity(len); let mut aux_pow_id_raw = Vec::<[u8; 32]>::with_capacity(len);