fix(sync) address saving logic, fixes
@@ -250,25 +250,34 @@ impl<'a> AccountSynchronizer<'a> {
.map(|(id, message)| Message::from_iota_message(*id, &message).unwrap())
.collect(),
);
- let mut found_unspent = None;
- self.account.append_addresses(
- found_addresses
- .into_iter()
- .take_while(|(address, outputs)| {
- let address_is_unspent = !outputs.iter().any(|o| o.is_spent);
- let old_found_unspent_status = found_unspent;
- found_unspent = Some(address_is_unspent);
- match old_found_unspent_status {
- // keep collecting if the previous address was spent
- // (because we want to collect all spent addresses + one unspent)
- Some(unspent) => !unspent,
- // if this is the first found address, we keep collecting anyway
- None => true,
+
+ let mut addresses_to_save = vec![];
+ let mut ignored_addresses = vec![];
+ let mut previous_address_is_unused = false;
+ for (found_address, address_outputs) in found_addresses.into_iter() {
+ let address_is_unused = address_outputs.is_empty();
+
+ // if the previous address is unused, we'll keep checking to see if an used address was found on the gap limit
+ if previous_address_is_unused {
+ // subsequent unused address found; add it to the ignored addresses list
+ if address_is_unused {
+ ignored_addresses.push(found_address);
}
- })
- .map(|(a, _)| a)
- .collect(),
- );
+ // used address found after finding unused addresses; we'll save all the previous ignored address and this one aswell
+ else {
+ addresses_to_save.extend(ignored_addresses.into_iter());
+ ignored_addresses = vec![];
+ addresses_to_save.push(found_address);
+ }
+ }
+ // if the previous address is used or this is the first address,
+ // we'll save it because we want at least one unused address
+ else {
+ addresses_to_save.push(found_address);
+ }
+ previous_address_is_unused = address_is_unused;
+ }
+ self.account.append_addresses(addresses_to_save);
if !self.skip_persistance {
crate::storage::with_adapter(&self.storage_path, |storage| {
