read: flatten indentation, add thread_local()

This commit is contained in:
hinto.janai 2024-04-23 21:25:54 -04:00
parent 26dc7cffaf
commit bf7c11ce84
No known key found for this signature in database
GPG key ID: D47CE05FA175A499

View file

@ -227,6 +227,13 @@ fn map_request(
/* TODO: post-request handling, run some code for each request? */ /* TODO: post-request handling, run some code for each request? */
} }
//---------------------------------------------------------------------------------------------------- Thread Local
/// TODO: explain this.
#[inline]
fn thread_local<T: Send>(env: &impl Env) -> ThreadLocal<T> {
ThreadLocal::with_capacity(env.config().reader_threads.as_threads().get())
}
//---------------------------------------------------------------------------------------------------- Handler functions //---------------------------------------------------------------------------------------------------- Handler functions
// These are the actual functions that do stuff according to the incoming [`Request`]. // These are the actual functions that do stuff according to the incoming [`Request`].
// //
@ -279,8 +286,8 @@ fn block_extended_header_in_range(
) -> ResponseResult { ) -> ResponseResult {
let env_inner = env.env_inner(); let env_inner = env.env_inner();
let tx_ro = ThreadLocal::with_capacity(env.config().reader_threads.as_threads().get()); let tx_ro = thread_local(env);
let tables = ThreadLocal::with_capacity(env.config().reader_threads.as_threads().get()); let tables = thread_local(env);
// This iterator will early return as `Err` if there's even 1 error. // This iterator will early return as `Err` if there's even 1 error.
let vec = range let vec = range
@ -330,8 +337,8 @@ fn generated_coins(env: &ConcreteEnv) -> ResponseResult {
fn outputs(env: &ConcreteEnv, map: HashMap<Amount, HashSet<AmountIndex>>) -> ResponseResult { fn outputs(env: &ConcreteEnv, map: HashMap<Amount, HashSet<AmountIndex>>) -> ResponseResult {
let env_inner = env.env_inner(); let env_inner = env.env_inner();
let tx_ro = ThreadLocal::with_capacity(env.config().reader_threads.as_threads().get()); let tx_ro = thread_local(env);
let table_outputs = ThreadLocal::with_capacity(env.config().reader_threads.as_threads().get()); let table_outputs = thread_local(env);
// -> Result<(AmountIndex, OutputOnChain), RuntimeError> // -> Result<(AmountIndex, OutputOnChain), RuntimeError>
let inner_map = |amount, amount_index| { let inner_map = |amount, amount_index| {
@ -344,30 +351,35 @@ fn outputs(env: &ConcreteEnv, map: HashMap<Amount, HashSet<AmountIndex>>) -> Res
}; };
let output = crate::ops::output::get_output(&pre_rct_output_id, table_outputs)?; let output = crate::ops::output::get_output(&pre_rct_output_id, table_outputs)?;
// Map `Output` -> `OutputOnChain`
// FIXME: This should be in a function somewhere.
// FIXME: implement lookup table for common values: // FIXME: implement lookup table for common values:
// <https://github.com/monero-project/monero/blob/c8214782fb2a769c57382a999eaf099691c836e7/src/ringct/rctOps.cpp#L322> // <https://github.com/monero-project/monero/blob/c8214782fb2a769c57382a999eaf099691c836e7/src/ringct/rctOps.cpp#L322>
let commitment = ED25519_BASEPOINT_POINT + monero_serai::H() * Scalar::from(amount); let commitment = ED25519_BASEPOINT_POINT + monero_serai::H() * Scalar::from(amount);
Ok(( let time_lock = if output
amount_index, .output_flags
OutputOnChain { .contains(OutputFlags::NON_ZERO_UNLOCK_TIME)
#[allow(clippy::cast_lossless)] {
height: output.height as u64, // TODO: how to recover the timelock height/time?
time_lock: if output todo!()
.output_flags } else {
.contains(OutputFlags::NON_ZERO_UNLOCK_TIME) Timelock::None
{ };
// TODO: how to recover the timelock height/time?
todo!() let key = CompressedEdwardsY::from_slice(&output.key)
} else { .map(|y| y.decompress())
Timelock::None .unwrap_or(None);
},
key: CompressedEdwardsY::from_slice(&output.key) let output_on_chain = OutputOnChain {
.map(|y| y.decompress()) height: u64::from(output.height),
.unwrap_or(None), time_lock,
commitment, key,
}, commitment,
)) };
Ok((amount_index, output_on_chain))
}; };
let map = map let map = map
@ -409,9 +421,8 @@ fn number_outputs_with_amount(env: &ConcreteEnv, amounts: Vec<Amount>) -> Respon
fn check_k_is_not_spent(env: &ConcreteEnv, key_images: HashSet<KeyImage>) -> ResponseResult { fn check_k_is_not_spent(env: &ConcreteEnv, key_images: HashSet<KeyImage>) -> ResponseResult {
let env_inner = env.env_inner(); let env_inner = env.env_inner();
let tx_ro = ThreadLocal::with_capacity(env.config().reader_threads.as_threads().get()); let tx_ro = thread_local(env);
let table_key_images = let table_key_images = thread_local(env);
ThreadLocal::with_capacity(env.config().reader_threads.as_threads().get());
let key_image_exists = |key_image| { let key_image_exists = |key_image| {
let tx_ro = tx_ro.get_or_try(|| env_inner.tx_ro())?; let tx_ro = tx_ro.get_or_try(|| env_inner.tx_ro())?;