From 3f7bdaa64b1eab6936fe14977aa6add62dff4ea5 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Mon, 13 Nov 2023 00:17:58 -0500 Subject: [PATCH] Make the output distribution cache only available under a feature Enables a mode with reduced memory usage *and* increased safety given current unsafety of the cache. Relevant to https://github.com/serai-dex/serai/issues/415. --- coins/monero/Cargo.toml | 3 ++- coins/monero/src/wallet/decoys.rs | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/coins/monero/Cargo.toml b/coins/monero/Cargo.toml index cf0549fe..2960fb46 100644 --- a/coins/monero/Cargo.toml +++ b/coins/monero/Cargo.toml @@ -91,7 +91,7 @@ std = [ "monero-generators/std", - "futures/std", + "futures?/std", "hex/std", "serde/std", @@ -100,6 +100,7 @@ std = [ "base58-monero/std", ] +cache-distribution = ["futures"] http-rpc = ["digest_auth", "simple-request", "tokio"] multisig = ["transcript", "frost", "dleq", "std"] binaries = ["tokio/rt-multi-thread", "tokio/macros", "http-rpc"] diff --git a/coins/monero/src/wallet/decoys.rs b/coins/monero/src/wallet/decoys.rs index fd1b8bcd..7f1e64af 100644 --- a/coins/monero/src/wallet/decoys.rs +++ b/coins/monero/src/wallet/decoys.rs @@ -1,8 +1,11 @@ -use std_shims::{sync::OnceLock, vec::Vec, collections::HashSet}; +use std_shims::{vec::Vec, collections::HashSet}; -#[cfg(not(feature = "std"))] +#[cfg(feature = "cache-distribution")] +use std_shims::sync::OnceLock; + +#[cfg(all(feature = "cache-distribution", not(feature = "std")))] use std_shims::sync::Mutex; -#[cfg(feature = "std")] +#[cfg(all(feature = "cache-distribution", feature = "std"))] use futures::lock::Mutex; use zeroize::{Zeroize, ZeroizeOnDrop}; @@ -27,9 +30,11 @@ const BLOCK_TIME: usize = 120; const BLOCKS_PER_YEAR: usize = 365 * 24 * 60 * 60 / BLOCK_TIME; const TIP_APPLICATION: f64 = (LOCK_WINDOW * BLOCK_TIME) as f64; -// TODO: Expose an API to reset this in case a reorg occurs/the RPC fails/returns garbage +// TODO: Resolve safety of this in case a reorg occurs/the network changes // TODO: Update this when scanning a block, as possible +#[cfg(feature = "cache-distribution")] static DISTRIBUTION_CELL: OnceLock>> = OnceLock::new(); +#[cfg(feature = "cache-distribution")] #[allow(non_snake_case)] fn DISTRIBUTION() -> &'static Mutex> { DISTRIBUTION_CELL.get_or_init(|| Mutex::new(Vec::with_capacity(3000000))) @@ -159,11 +164,16 @@ impl Decoys { height: usize, inputs: &[SpendableOutput], ) -> Result, RpcError> { + #[cfg(feature = "cache-distribution")] #[cfg(not(feature = "std"))] let mut distribution = DISTRIBUTION().lock(); + #[cfg(feature = "cache-distribution")] #[cfg(feature = "std")] let mut distribution = DISTRIBUTION().lock().await; + #[cfg(not(feature = "cache-distribution"))] + let mut distribution = vec![]; + let decoy_count = ring_len - 1; // Convert the inputs in question to the raw output data