From e36e1a38c36adf18d15d15a360874d6af8f1a798 Mon Sep 17 00:00:00 2001 From: chris-belcher Date: Thu, 3 Feb 2022 16:20:02 +0000 Subject: [PATCH] Remove infinite loop --- src/wallet_sync.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/wallet_sync.rs b/src/wallet_sync.rs index 69663f85..8ff9e3a8 100644 --- a/src/wallet_sync.rs +++ b/src/wallet_sync.rs @@ -1278,8 +1278,12 @@ impl Wallet { } } - fn generate_amount_fractions(count: usize, total_amount: u64, lower_limit: u64) -> Vec { - loop { + fn generate_amount_fractions( + count: usize, + total_amount: u64, + lower_limit: u64, + ) -> Result, Error> { + for _ in 0..100000 { let mut knives = (1..count) .map(|_| OsRng.next_u32() as f32 / u32::MAX as f32) .collect::>(); @@ -1297,9 +1301,12 @@ impl Wallet { .iter() .all(|f| *f * (total_amount as f32) > lower_limit as f32) { - return fractions; + return Ok(fractions); } } + Err(Error::Protocol( + "unable to generate amount fractions, probably amount too small", + )) } fn create_spending_txes( @@ -1334,7 +1341,7 @@ impl Wallet { destinations.len(), coinswap_amount, 5000, //use 5000 satoshi as the lower limit for now - ) + )? .iter() .map(|f| (*f * coinswap_amount as f32) as u64) .collect::>();