Skip to content

Commit 0a78539

Browse files
authored
fix: add iteration limit to SPV fee calculation loop (#620)
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 557acb2). Co-authored-by: PastaClaw <thepastaclaw@users.noreply.github.com>
1 parent cbc9bc5 commit 0a78539

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/backend_task/core/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,13 @@ impl AppContext {
528528
.map(|h| h.current_height())
529529
})
530530
.ok_or("Cannot build transaction: SPV sync height is not yet known")?;
531+
const MAX_FEE_ITERATIONS: usize = 50;
532+
531533
let total_amount: u64 = recipients.iter().map(|(_, amt)| *amt).sum();
532534
let mut scale_factor = 1.0f64;
533535
let mut attempted_fallback = false;
534536

535-
loop {
537+
for _ in 0..MAX_FEE_ITERATIONS {
536538
let scaled_recipients: Vec<(Address, u64)> = recipients
537539
.iter()
538540
.map(|(addr, amt)| (addr.clone(), (*amt as f64 * scale_factor) as u64))
@@ -574,6 +576,8 @@ impl AppContext {
574576
}
575577
}
576578
}
579+
580+
Err("Could not build transaction after maximum fee adjustment attempts".to_string())
577581
}
578582

579583
fn estimate_fallback_amount(

0 commit comments

Comments
 (0)