Grab sync lock when sending to prevent anchor changes ported from 5a885adc15

This commit is contained in:
DenioD
2020-07-30 11:30:19 +02:00
parent e6ad8d9617
commit 21e7e20d6c
2 changed files with 18 additions and 7 deletions

View File

@@ -1802,12 +1802,19 @@ impl LightWallet {
// Select notes to cover the target value
println!("{}: Selecting notes", now() - start_time);
let target_value = Amount::from_u64(total_value).unwrap() + DEFAULT_FEE ;
let notes: Vec<_> = self.txs.read().unwrap().iter()
// Select the candidate notes that are eligible to be spent
let mut candidate_notes: Vec<_> = self.txs.read().unwrap().iter()
.map(|(txid, tx)| tx.notes.iter().map(move |note| (*txid, note)))
.flatten()
.filter_map(|(txid, note)|
SpendableNote::from(txid, note, anchor_offset, &self.extsks.read().unwrap()[note.account])
)
).collect();
// Sort by highest value-notes first.
candidate_notes.sort_by(|a, b| b.note.value.cmp(&a.note.value));
// Select the minimum number of notes required to satisfy the target value
let notes: Vec<_> = candidate_notes.iter()
.scan(0, |running_total, spendable| {
let value = spendable.note.value;
let ret = if *running_total < u64::from(target_value) {