From 9f4f7a4b980e3b6b33d3449eb0536e7b7649d600 Mon Sep 17 00:00:00 2001 From: PastaClaw Date: Sun, 22 Feb 2026 11:14:58 -0600 Subject: [PATCH] fix: add iteration limit to SPV fee calculation loop Replace unbounded loop with a capped for loop (max 50 iterations) in the SPV fee calculation to prevent potential infinite loops when the scale factor converges but never meets the exit condition. Cherry-picked from ralph/improvements (commit 557acb23). --- src/backend_task/core/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/backend_task/core/mod.rs b/src/backend_task/core/mod.rs index 797af5348..c0f8803ff 100644 --- a/src/backend_task/core/mod.rs +++ b/src/backend_task/core/mod.rs @@ -499,11 +499,13 @@ impl AppContext { .map(|h| h.current_height()) }) .ok_or("Cannot build transaction: SPV sync height is not yet known")?; + const MAX_FEE_ITERATIONS: usize = 50; + let total_amount: u64 = recipients.iter().map(|(_, amt)| *amt).sum(); let mut scale_factor = 1.0f64; let mut attempted_fallback = false; - loop { + for _ in 0..MAX_FEE_ITERATIONS { let scaled_recipients: Vec<(Address, u64)> = recipients .iter() .map(|(addr, amt)| (addr.clone(), (*amt as f64 * scale_factor) as u64)) @@ -545,6 +547,8 @@ impl AppContext { } } } + + Err("Could not build transaction after maximum fee adjustment attempts".to_string()) } fn estimate_fallback_amount(