diff options
Diffstat (limited to 'src/swarm/roots.rs')
| -rw-r--r-- | src/swarm/roots.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/swarm/roots.rs b/src/swarm/roots.rs index 2fb6d6e..c0aa4ea 100644 --- a/src/swarm/roots.rs +++ b/src/swarm/roots.rs @@ -14,7 +14,9 @@ //! the seed that answers first doesn't decide alone. Seeds are the nodes proven at //! the seeds' addresses (`seed_found`): a peer answering under identities of its own //! neither ends the wait nor starts its clock, only disputes. One seed hands a joiner -//! another root only if it is all the joiner hears of its seeds within the wait. +//! another root only if it is all the joiner hears of its seeds within the wait. A +//! seed naming none, joining itself, names it once ready, in a handshake again with +//! each peer it named none to (`named_to`, `unnamed`). use std::collections::{HashMap, HashSet}; use std::time::Duration; @@ -45,6 +47,8 @@ pub(crate) struct Roots { awaiting: Option<(usize, Option<tokio::time::Instant>)>, /// Its own, for the document (`Replica::epoch_seq`), once known. own: Option<(u32, ChangeHash)>, + /// Peers our handshakes named no root to, while we had none (`named_to`). + unnamed: HashSet<String>, /// Peers whose root differs, logged once each. differing: HashSet<String>, } @@ -94,6 +98,27 @@ pub(crate) async fn own(state: &AppState) -> Option<ChangeHash> { Some(root) } +/// This node's epoch's root, for a handshake with `peer` (`HelloRequest::root`, +/// `HelloReply::root`). Where it has none yet, joining itself, `peer` is noted: a peer +/// joining through it takes no root before its seeds named one, so once this node is +/// ready it shakes hands with those again (`unnamed`), and names its root then. +pub(crate) async fn named_to(state: &AppState, peer: &str) -> Option<ChangeHash> { + let root = own(state).await; + if root.is_none() { + state + .roots + .lock_or_recover() + .unnamed + .insert(peer.to_owned()); + } + root +} + +/// The peers this node's handshakes named no root to (`named_to`), forgotten here. +pub(crate) fn unnamed(state: &AppState) -> Vec<String> { + state.roots.lock_or_recover().unnamed.drain().collect() +} + /// `peer`, in a handshake of `epoch`, names `root` as its root. Refused (an error) if /// this node's document holds another of the same epoch; of the first epoch, what an /// empty document takes as its root follows (`pin`). |
