Skip to content

Commit 96f25a9

Browse files
committed
Take a reference to WeightedUtxo in OutputGroup
1 parent 082f1b8 commit 96f25a9

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/wallet/coin_selection.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub fn calculate_waste(
200200
) -> Result<Waste, Error> {
201201
// Always consider the cost of spending an input now vs in the future.
202202
let utxo_groups: Vec<OutputGroup> = selected
203-
.into_iter()
203+
.iter()
204204
.map(|u| OutputGroup::new(u, fee_rate))
205205
.collect();
206206

@@ -342,16 +342,16 @@ impl<D: Database> CoinSelectionAlgorithm<D> for LargestFirstCoinSelection {
342342

343343
#[derive(Debug, Clone)]
344344
// Adds fee information to an UTXO.
345-
struct OutputGroup {
346-
weighted_utxo: WeightedUtxo,
345+
struct OutputGroup<'u> {
346+
weighted_utxo: &'u WeightedUtxo,
347347
// Amount of fees for spending a certain utxo, calculated using a certain FeeRate
348348
fee: u64,
349349
// The effective value of the UTXO, i.e., the utxo value minus the fee for spending it
350350
effective_value: i64,
351351
}
352352

353-
impl OutputGroup {
354-
fn new(weighted_utxo: WeightedUtxo, fee_rate: FeeRate) -> Self {
353+
impl<'u> OutputGroup<'u> {
354+
fn new(weighted_utxo: &'u WeightedUtxo, fee_rate: FeeRate) -> Self {
355355
let fee = fee_rate.fee_wu(TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight);
356356
let effective_value = weighted_utxo.utxo.txout().value as i64 - fee as i64;
357357
OutputGroup {
@@ -399,14 +399,14 @@ impl<D: Database> CoinSelectionAlgorithm<D> for BranchAndBoundCoinSelection {
399399
fee_amount: u64,
400400
) -> Result<CoinSelectionResult, Error> {
401401
// Mapping every (UTXO, usize) to an output group
402-
let required_utxos: Vec<OutputGroup> = required_utxos
403-
.into_iter()
402+
let required_utxos: Vec<OutputGroup<'_>> = required_utxos
403+
.iter()
404404
.map(|u| OutputGroup::new(u, fee_rate))
405405
.collect();
406406

407407
// Mapping every (UTXO, usize) to an output group.
408-
let optional_utxos: Vec<OutputGroup> = optional_utxos
409-
.into_iter()
408+
let optional_utxos: Vec<OutputGroup<'_>> = optional_utxos
409+
.iter()
410410
.map(|u| OutputGroup::new(u, fee_rate))
411411
.collect();
412412

@@ -474,8 +474,8 @@ impl BranchAndBoundCoinSelection {
474474
#[allow(clippy::too_many_arguments)]
475475
fn bnb(
476476
&self,
477-
required_utxos: Vec<OutputGroup>,
478-
mut optional_utxos: Vec<OutputGroup>,
477+
required_utxos: Vec<OutputGroup<'_>>,
478+
mut optional_utxos: Vec<OutputGroup<'_>>,
479479
mut curr_value: i64,
480480
mut curr_available_value: i64,
481481
actual_target: i64,
@@ -583,8 +583,8 @@ impl BranchAndBoundCoinSelection {
583583

584584
fn single_random_draw(
585585
&self,
586-
required_utxos: Vec<OutputGroup>,
587-
mut optional_utxos: Vec<OutputGroup>,
586+
required_utxos: Vec<OutputGroup<'_>>,
587+
mut optional_utxos: Vec<OutputGroup<'_>>,
588588
curr_value: i64,
589589
actual_target: i64,
590590
fee_amount: u64,
@@ -613,16 +613,16 @@ impl BranchAndBoundCoinSelection {
613613
BranchAndBoundCoinSelection::calculate_cs_result(selected_utxos, required_utxos, fee_amount)
614614
}
615615

616-
fn calculate_cs_result(
617-
mut selected_utxos: Vec<OutputGroup>,
618-
mut required_utxos: Vec<OutputGroup>,
616+
fn calculate_cs_result<'u>(
617+
mut selected_utxos: Vec<OutputGroup<'u>>,
618+
mut required_utxos: Vec<OutputGroup<'u>>,
619619
mut fee_amount: u64,
620620
) -> CoinSelectionResult {
621621
selected_utxos.append(&mut required_utxos);
622622
fee_amount += selected_utxos.iter().map(|u| u.fee).sum::<u64>();
623623
let selected = selected_utxos
624624
.into_iter()
625-
.map(|u| u.weighted_utxo.utxo)
625+
.map(|u| u.weighted_utxo.utxo.clone())
626626
.collect::<Vec<_>>();
627627

628628
CoinSelectionResult::new(selected, fee_amount, None)
@@ -1030,8 +1030,8 @@ mod test {
10301030
#[should_panic(expected = "BnBNoExactMatch")]
10311031
fn test_bnb_function_no_exact_match() {
10321032
let fee_rate = FeeRate::from_sat_per_vb(10.0);
1033-
let utxos: Vec<OutputGroup> = get_test_utxos()
1034-
.into_iter()
1033+
let utxos: Vec<OutputGroup<'_>> = get_test_utxos()
1034+
.iter()
10351035
.map(|u| OutputGroup::new(u, fee_rate))
10361036
.collect();
10371037

@@ -1057,7 +1057,7 @@ mod test {
10571057
fn test_bnb_function_tries_exceeded() {
10581058
let fee_rate = FeeRate::from_sat_per_vb(10.0);
10591059
let utxos: Vec<OutputGroup> = generate_same_value_utxos(100_000, 100_000)
1060-
.into_iter()
1060+
.iter()
10611061
.map(|u| OutputGroup::new(u, fee_rate))
10621062
.collect();
10631063

@@ -1087,7 +1087,7 @@ mod test {
10871087
let cost_of_change = size_of_change as f32 * fee_rate.as_sat_vb();
10881088

10891089
let utxos: Vec<_> = generate_same_value_utxos(50_000, 10)
1090-
.into_iter()
1090+
.iter()
10911091
.map(|u| OutputGroup::new(u, fee_rate))
10921092
.collect();
10931093

@@ -1123,7 +1123,7 @@ mod test {
11231123

11241124
for _ in 0..200 {
11251125
let optional_utxos: Vec<_> = generate_random_utxos(&mut rng, 40)
1126-
.into_iter()
1126+
.iter()
11271127
.map(|u| OutputGroup::new(u, fee_rate))
11281128
.collect();
11291129

0 commit comments

Comments
 (0)