Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ac30bda

Browse files
committedMar 8, 2022
Wrap waste field with Cell to compute value once
1 parent fbfe23f commit ac30bda

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed
 

‎src/wallet/coin_selection.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ use rand::seq::SliceRandom;
109109
use rand::thread_rng;
110110
#[cfg(test)]
111111
use rand::{rngs::StdRng, SeedableRng};
112+
use std::cell::Cell;
112113
use std::convert::TryInto;
113114

114115
/// Default coin selection algorithm used by [`TxBuilder`](super::tx_builder::TxBuilder) if not
@@ -131,7 +132,7 @@ pub struct CoinSelectionResult {
131132
/// Total fee amount in satoshi
132133
pub fee_amount: u64,
133134
/// Waste value of current coin selection
134-
waste: Option<Waste>,
135+
waste: Cell<Option<Waste>>,
135136
}
136137

137138
impl CoinSelectionResult {
@@ -140,7 +141,7 @@ impl CoinSelectionResult {
140141
CoinSelectionResult {
141142
selected: selected_utxos,
142143
fee_amount,
143-
waste: selection_waste,
144+
waste: Cell::new(selection_waste),
144145
}
145146
}
146147

@@ -168,16 +169,20 @@ impl CoinSelectionResult {
168169
fee_rate: FeeRate,
169170
long_term_fee_rate: FeeRate,
170171
) -> Waste {
171-
match self.waste {
172+
match self.waste.get() {
172173
Some(waste) => waste,
173-
None => calculate_waste(
174-
selected,
175-
cost_of_change,
176-
target,
177-
fee_rate,
178-
long_term_fee_rate,
179-
)
180-
.unwrap(),
174+
None => {
175+
let calculated_waste = calculate_waste(
176+
selected,
177+
cost_of_change,
178+
target,
179+
fee_rate,
180+
long_term_fee_rate,
181+
)
182+
.unwrap();
183+
self.waste.set(Some(calculated_waste));
184+
calculated_waste
185+
}
181186
}
182187
}
183188
}

0 commit comments

Comments
 (0)
Please sign in to comment.