fixed unnecessary output requests.
@@ -300,8 +300,6 @@ impl<'a> ClientMessageBuilder<'a> {
total_to_spend: u64,
dust_and_allowance_recorders: &mut Vec<(u64, Address, bool)>,
) -> Result<(Vec<Input>, Vec<Output>, Vec<AddressIndexRecorder>)> {
- let mut outputs = Vec::new();
- let mut dust_outputs = Vec::new();
let mut inputs_for_essence = Vec::new();
let mut outputs_for_essence = Vec::new();
let mut address_index_recorders = Vec::new();
@@ -310,6 +308,8 @@ impl<'a> ClientMessageBuilder<'a> {
let mut gap_index = self.initial_address_index.unwrap_or(0);
let mut empty_address_count: u64 = 0;
'input_selection: loop {
+ let mut outputs = Vec::new();
+ let mut dust_outputs = Vec::new();
// Get the addresses in the BIP path/index ~ path/index+20
let addresses = self
.client
@@ -347,25 +347,9 @@ impl<'a> ClientMessageBuilder<'a> {
OutputDto::SignatureLockedDustAllowance(_) => dust_outputs.push(output_wrapper),
OutputDto::Treasury(_) => {}
};
- }
- }
-
- // If there are more than 20 (ADDRESS_GAP_RANGE) consecutive empty addresses, then we stop
- // looking up the addresses belonging to the seed. Note that we don't
- // really count the exact 20 consecutive empty addresses, which is
- // unnecessary. We just need to check the address range,
- // (index * ADDRESS_GAP_RANGE, index * ADDRESS_GAP_RANGE + ADDRESS_GAP_RANGE), where index is
- // natural number, and to see if the outputs are all empty.
- if outputs.is_empty() {
- // Accumulate the empty_address_count for each run of output address searching
- empty_address_count += 1;
- } else {
- // Reset counter if there is an output
- empty_address_count = 0;
- }
outputs.sort_by(|l, r| l.amount.cmp(&r.amount));
- for (_offset, output_wrapper) in [outputs.clone(), dust_outputs.clone()].concat().iter().enumerate() {
+ for (_offset, output_wrapper) in outputs.iter().chain(dust_outputs.iter()).enumerate() {
if total_already_spent < total_to_spend {
total_already_spent += output_wrapper.amount;
let address_index_record = ClientMessageBuilder::create_address_index_recorder(
@@ -397,9 +381,28 @@ impl<'a> ClientMessageBuilder<'a> {
}
}
// Break if we have enough funds and don't create dust for the remainder
- if total_already_spent == total_to_spend || total_already_spent >= total_to_spend + DUST_THRESHOLD {
+ if total_already_spent == total_to_spend
+ || total_already_spent >= total_to_spend + DUST_THRESHOLD
+ {
break 'input_selection;
}
+ }
+ }
+
+ // If there are more than 20 (ADDRESS_GAP_RANGE) consecutive empty addresses, then we stop
+ // looking up the addresses belonging to the seed. Note that we don't
+ // really count the exact 20 consecutive empty addresses, which is
+ // unnecessary. We just need to check the address range,
+ // (index * ADDRESS_GAP_RANGE, index * ADDRESS_GAP_RANGE + ADDRESS_GAP_RANGE), where index is
+ // natural number, and to see if the outputs are all empty.
+ if outputs.is_empty() {
+ // Accumulate the empty_address_count for each run of output address searching
+ empty_address_count += 1;
+ } else {
+ // Reset counter if there is an output
+ empty_address_count = 0;
+ }
+
// if we just processed an even index, increase the address index
// (because the list has public and internal addresses)
if index % 2 == 1 {
