Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/metrics/lowest_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,11 @@ impl BnbMetric for LowestFee {
}

// `scale` could be 0 even if `is_funded` is `false` due to the latter being based on
// rounded-up vbytes.
let ideal_fee = scale.0 * to_resize.value as f32 + cs.selected_value() as f32
- cs.target().value() as f32;
assert!(ideal_fee >= 0.0);
// rounded-up vbytes. f32 rounding can also push the sum slightly below zero; 0 is
// always an admissible lower bound for a fee, so clamp.
let ideal_fee = (scale.0 * to_resize.value as f32 + cs.selected_value() as f32
- cs.target().value() as f32)
.max(0.0);
Comment on lines 263 to +268

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The comment above still only explains why scale can be 0. Now that the assert! is gone, the reason for the .max(0.0) isn't written anywhere. Suggest a line on it:

Suggested change
// `scale` could be 0 even if `is_funded` is `false` due to the latter being based on
// rounded-up vbytes.
let ideal_fee = scale.0 * to_resize.value as f32 + cs.selected_value() as f32
- target.value() as f32;
assert!(ideal_fee >= 0.0);
let ideal_fee = (scale.0 * to_resize.value as f32 + cs.selected_value() as f32
- target.value() as f32)
.max(0.0);
// `scale` could be 0 even if `is_funded` is `false` due to the latter being based on
// rounded-up vbytes. f32 rounding can also push the sum slightly below zero; 0 is
// always an admissible lower bound for a fee, so clamp.
let ideal_fee = (scale.0 * to_resize.value as f32 + cs.selected_value() as f32
- target.value() as f32)
.max(0.0);


Some(Ordf32(ideal_fee))
}
Expand Down
25 changes: 25 additions & 0 deletions tests/lowest_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,28 @@ fn identical_candidates_are_excluded_together_when_interleaved() {
.count();
assert!(rounds <= 100, "took {} rounds", rounds);
}

#[test]
fn bound_does_not_panic_on_f32_rounding() {
// At a zero feerate, the bound's ideal fee for an empty selection is
// `(target / value) * value - target`. In f32, `(1 / 41) * 41` rounds to `1 - 2^-24`, so this
// came out slightly negative and tripped the old `assert!` (#57).
let candidates = [Candidate::new_tr_keyspend(41)];
let target = Target {
fee: TargetFee::ZERO,
outputs: TargetOutputs {
value_sum: 1,
weight_sum: 0,
n_outputs: 1,
},
max_weight: None,
};
let mut metric = LowestFee {
long_term_feerate: FeeRate::ZERO,
dust_relay_feerate: FeeRate::ZERO,
drain_weights: DrainWeights::TR_KEYSPEND,
};
let cs = CoinSelector::new(&candidates, target);
let bound = metric.bound(&cs).expect("target is reachable");
assert_eq!(bound.0, 0.0);
}
Loading