Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Prevent multiple events per synced peer
cloudhead committed 2 years ago
commit f38d2b2ef81b254edb6f0a58429ddb1a63c0e418
parent 159944f7b573753c3a552d1fc2200f195512be63
1 file changed +15 -6
modified radicle/src/node.rs
@@ -1,4 +1,5 @@
#![allow(clippy::type_complexity)]
+
#![allow(clippy::collapsible_if)]
mod features;

pub mod address;
@@ -897,13 +898,13 @@ impl Node {
        rid: RepoId,
        seeds: impl IntoIterator<Item = NodeId>,
        timeout: time::Duration,
-
        mut callback: impl FnMut(AnnounceEvent, &[PublicKey]) -> ControlFlow<()>,
+
        mut callback: impl FnMut(AnnounceEvent, &HashSet<PublicKey>) -> ControlFlow<()>,
    ) -> Result<AnnounceResult, Error> {
        let events = self.subscribe(timeout)?;
        let refs = self.announce_refs(rid)?;

        let mut unsynced = seeds.into_iter().collect::<BTreeSet<_>>();
-
        let mut synced = Vec::new();
+
        let mut synced = HashSet::new();
        let mut timeout: Vec<NodeId> = Vec::new();

        callback(AnnounceEvent::Announced, &synced);
@@ -915,10 +916,15 @@ impl Node {
                    rid: rid_,
                    at,
                }) if rid == rid_ && refs.at == at => {
+
                    log::debug!(target: "radicle", "Received {e:?}");
+

                    unsynced.remove(&remote);
-
                    synced.push(remote);
-
                    if callback(AnnounceEvent::RefsSynced { remote }, &synced).is_break() {
-
                        break;
+
                    // We can receive synced events from nodes we didn't directly announce to,
+
                    // and it's possible to receive duplicates as well.
+
                    if synced.insert(remote) {
+
                        if callback(AnnounceEvent::RefsSynced { remote }, &synced).is_break() {
+
                            break;
+
                        }
                    }
                }
                Ok(_) => {}
@@ -933,7 +939,10 @@ impl Node {
                break;
            }
        }
-
        Ok(AnnounceResult { timeout, synced })
+
        Ok(AnnounceResult {
+
            timeout,
+
            synced: synced.into_iter().collect(),
+
        })
    }
}