From a347f7fd9db2da47abc11d30b22fbbbb47183447 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Fri, 26 Apr 2024 20:56:32 -0400 Subject: [PATCH] read: move `Output` mapping to `crate::free` it's needed in tests too --- database/src/free.rs | 42 ++++++++++++++++++++++++++++++++++-- database/src/lib.rs | 2 +- database/src/service/read.rs | 29 +++---------------------- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/database/src/free.rs b/database/src/free.rs index a9b93855..ca44052c 100644 --- a/database/src/free.rs +++ b/database/src/free.rs @@ -1,10 +1,48 @@ //! General free functions (related to the database). -//! -//! TODO. //---------------------------------------------------------------------------------------------------- Import +use cuprate_types::OutputOnChain; +use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, edwards::CompressedEdwardsY, Scalar}; +use monero_serai::{transaction::Timelock, H}; + +use crate::{ + tables::{Tables, TxUnlockTime}, + types::{Amount, Output, OutputFlags}, + DatabaseRo, RuntimeError, +}; + //---------------------------------------------------------------------------------------------------- Free functions +/// Map a [`crate::types::Output`] to a [`cuprate_types::OutputOnChain`]. +pub(crate) fn output_to_output_on_chain( + output: &Output, + amount: Amount, + table_tx_unlock_time: &impl DatabaseRo, +) -> Result { + // FIXME: implement lookup table for common values: + // + let commitment = ED25519_BASEPOINT_POINT + H() * Scalar::from(amount); + + let time_lock = if output + .output_flags + .contains(OutputFlags::NON_ZERO_UNLOCK_TIME) + { + Timelock::Time(table_tx_unlock_time.get(&output.tx_idx)?) + } else { + Timelock::None + }; + + let key = CompressedEdwardsY::from_slice(&output.key) + .map(|y| y.decompress()) + .unwrap_or(None); + + Ok(OutputOnChain { + height: u64::from(output.height), + time_lock, + key, + commitment, + }) +} //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] diff --git a/database/src/lib.rs b/database/src/lib.rs index 90f56c57..5ae7ace6 100644 --- a/database/src/lib.rs +++ b/database/src/lib.rs @@ -242,7 +242,7 @@ pub use env::{Env, EnvInner}; mod error; pub use error::{InitError, RuntimeError}; -mod free; +pub(crate) mod free; pub mod resize; diff --git a/database/src/service/read.rs b/database/src/service/read.rs index 24123f5a..d7bc5b75 100644 --- a/database/src/service/read.rs +++ b/database/src/service/read.rs @@ -29,6 +29,7 @@ use crate::{ config::ReaderThreads, constants::DATABASE_CORRUPT_MSG, error::RuntimeError, + free::output_to_output_on_chain, ops::{ block::{get_block_extended_header_from_height, get_block_info}, blockchain::{cumulative_generated_coins, top_block_height}, @@ -434,33 +435,9 @@ fn outputs(env: &ConcreteEnv, map: HashMap>) -> Res amount, amount_index, }; + let output = get_output(&pre_rct_output_id, tables.outputs())?; - - //--- Map `Output` -> `OutputOnChain` - - // FIXME: implement lookup table for common values: - // - let commitment = ED25519_BASEPOINT_POINT + H() * Scalar::from(amount); - - let time_lock = if output - .output_flags - .contains(OutputFlags::NON_ZERO_UNLOCK_TIME) - { - Timelock::Time(tables.tx_unlock_time().get(&output.tx_idx)?) - } else { - Timelock::None - }; - - let key = CompressedEdwardsY::from_slice(&output.key) - .map(|y| y.decompress()) - .unwrap_or(None); - - let output_on_chain = OutputOnChain { - height: u64::from(output.height), - time_lock, - key, - commitment, - }; + let output_on_chain = output_to_output_on_chain(&output, amount, tables.tx_unlock_time())?; Ok((amount_index, output_on_chain)) };