From 2e26aa5802e755c2057110e59655a70515a3a44f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Fri, 29 Mar 2024 16:27:29 +0800 Subject: [PATCH] fix: rm `Drain::none` method We already have const `Drain::NONE` which should be used instead. --- src/coin_selector.rs | 6 +++--- src/drain.rs | 5 ----- src/metrics.rs | 2 +- src/metrics/lowest_fee.rs | 6 +++--- tests/bnb.rs | 4 ++-- tests/lowest_fee.rs | 4 ++-- tests/weight.rs | 2 +- 7 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/coin_selector.rs b/src/coin_selector.rs index a9c169e..f343657 100644 --- a/src/coin_selector.rs +++ b/src/coin_selector.rs @@ -391,7 +391,7 @@ impl<'a> CoinSelector<'a> { /// Whether the constraints of `Target` have been met. pub fn is_target_met(&self, target: Target) -> bool { - self.is_target_met_with_drain(target, Drain::none()) + self.is_target_met_with_drain(target, Drain::NONE) } /// Select all unselected candidates @@ -434,7 +434,7 @@ impl<'a> CoinSelector<'a> { } /// Figures out whether the current selection should have a change output given the - /// `change_policy`. If it shouldn't then it will return a [`Drain::none`]. The value of the + /// `change_policy`. If it should not, then it will return [`Drain::NONE`]. The value of the /// `Drain` will be the same as [`drain_value`]. /// /// If [`is_target_met`] returns true for this selection then [`is_target_met_with_drain`] will @@ -475,7 +475,7 @@ impl<'a> CoinSelector<'a> { pub fn select_until_target_met(&mut self, target: Target) -> Result<(), InsufficientFunds> { self.select_until(|cs| cs.is_target_met(target)) .ok_or_else(|| InsufficientFunds { - missing: self.excess(target, Drain::none()).unsigned_abs(), + missing: self.excess(target, Drain::NONE).unsigned_abs(), }) } diff --git a/src/drain.rs b/src/drain.rs index e416cc5..8d116bc 100644 --- a/src/drain.rs +++ b/src/drain.rs @@ -80,11 +80,6 @@ impl Drain { value: 0, }; - /// A drian representing no drain at all. - pub fn none() -> Self { - Self::default() - } - /// is the "none" drain pub fn is_none(&self) -> bool { self == &Drain::NONE diff --git a/src/metrics.rs b/src/metrics.rs index 4b52572..4e7d90d 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -28,7 +28,7 @@ fn change_lower_bound(cs: &CoinSelector, target: Target, change_policy: ChangePo least_excess.drain(target, change_policy) } else { - Drain::none() + Drain::NONE } } diff --git a/src/metrics/lowest_fee.rs b/src/metrics/lowest_fee.rs index 873cf3c..4b348f2 100644 --- a/src/metrics/lowest_fee.rs +++ b/src/metrics/lowest_fee.rs @@ -98,7 +98,7 @@ impl BnbMetric for LowestFee { self.long_term_feerate, self.target.outputs.n_outputs, ); - let cost_of_no_change = cs.excess(self.target, Drain::none()); + let cost_of_no_change = cs.excess(self.target, Drain::NONE); let best_score_with_change = Ordf32(current_score.0 - cost_of_no_change as f32 + cost_of_adding_change); @@ -131,7 +131,7 @@ impl BnbMetric for LowestFee { // scale = remaining_value_to_reach_feerate / effective_value_of_resized_input // // This should be intutive since we're finding out how to scale the input we're resizing to get the effective value we need. - let rate_excess = cs.rate_excess(self.target, Drain::none()) as f32; + let rate_excess = cs.rate_excess(self.target, Drain::NONE) as f32; let mut scale = Ordf32(0.0); if rate_excess < 0.0 { @@ -150,7 +150,7 @@ impl BnbMetric for LowestFee { // We can use the same approach for replacement we just have to use the // incremental_relay_feerate. if let Some(replace) = self.target.fee.replace { - let replace_excess = cs.replacement_excess(self.target, Drain::none()) as f32; + let replace_excess = cs.replacement_excess(self.target, Drain::NONE) as f32; if replace_excess < 0.0 { let remaining_value_to_reach_feerate = replace_excess.abs(); let effective_value_of_resized_input = diff --git a/tests/bnb.rs b/tests/bnb.rs index 434050c..cab483b 100644 --- a/tests/bnb.rs +++ b/tests/bnb.rs @@ -35,7 +35,7 @@ const EXCESS_RATIO: f32 = 1_000_000_f32; impl BnbMetric for MinExcessThenWeight { fn score(&mut self, cs: &CoinSelector<'_>) -> Option { - let excess = cs.excess(self.target, Drain::none()); + let excess = cs.excess(self.target, Drain::NONE); if excess < 0 { None } else { @@ -135,7 +135,7 @@ fn bnb_finds_solution_if_possible_in_n_iter() { .expect("found a solution"); assert_eq!(rounds, 193); - let excess = sol.excess(target, Drain::none()); + let excess = sol.excess(target, Drain::NONE); assert_eq!(excess, 1); } diff --git a/tests/lowest_fee.rs b/tests/lowest_fee.rs index 4e3ba99..3a6bf69 100644 --- a/tests/lowest_fee.rs +++ b/tests/lowest_fee.rs @@ -222,7 +222,7 @@ fn adding_another_input_to_remove_change() { let mut cs = cs.clone(); cs.select(0); cs.select(2); - cs.excess(target, Drain::none()); + cs.excess(target, Drain::NONE); assert!(cs.is_target_met(target)); cs }; @@ -262,7 +262,7 @@ fn adding_another_input_to_remove_change() { let (score, _) = common::bnb_search(&mut cs, metric, 10).expect("finds solution"); let best_solution_score = metric.score(&best_solution).expect("must be a solution"); - assert_eq!(best_solution.drain(target, change_policy), Drain::none()); + assert_eq!(best_solution.drain(target, change_policy), Drain::NONE); assert!(score <= best_solution_score); assert_eq!(cs.selected_indices(), best_solution.selected_indices()); diff --git a/tests/weight.rs b/tests/weight.rs index cbb5900..b7b3eb2 100644 --- a/tests/weight.rs +++ b/tests/weight.rs @@ -101,7 +101,7 @@ fn segwit_two_inputs_one_output() { ); assert_eq!( (coin_selector - .implied_feerate(target_ouputs, Drain::none()) + .implied_feerate(target_ouputs, Drain::NONE) .unwrap() .as_sat_vb() * 10.0)