diff options
Diffstat (limited to 'src/federation/store')
| -rw-r--r-- | src/federation/store/memory.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs index f721b47..8493735 100644 --- a/src/federation/store/memory.rs +++ b/src/federation/store/memory.rs @@ -222,6 +222,47 @@ impl<F: Fold> Store<F> { .map(|(id, why)| (id, why.as_str(), self.held_back.len())) } + /// What the store holds back, stopped (`Fold::stops`): the events offered since, and + /// those waiting for events it doesn't hold, as it can't tell which will be taken; each + /// after those of its dependencies among them, by ID otherwise. Nothing while it isn't + /// stopped. + pub(crate) fn held_back(&self) -> Vec<(Id, &[u8])> { + if self.stopped.is_none() { + return Vec::new(); + } + let all: BTreeMap<Id, &Event> = self + .held_back + .iter() + .map(|(id, event)| (*id, event)) + .chain(self.waiting.iter().map(|(id, event)| (*id, event))) + .collect(); + let mut placed: HashSet<Id> = HashSet::new(); + let mut ordered = Vec::new(); + for first in all.keys() { + let mut todo = vec![(*first, false)]; + while let Some((id, ready)) = todo.pop() { + if placed.contains(&id) { + continue; + } + let event = all[&id]; + if ready { + placed.insert(id); + ordered.push((id, event.bytes.as_slice())); + continue; + } + todo.push((id, true)); + todo.extend( + event + .dependencies + .iter() + .filter(|dependency| all.contains_key(dependency)) + .map(|dependency| (*dependency, false)), + ); + } + } + ordered + } + /// What a new event builds on: the events nothing else held builds on, or the /// checkpoint's heads where nothing came after it. pub(crate) fn heads(&self) -> Vec<Id> { @@ -971,10 +1012,17 @@ mod tests { assert_eq!(store.taken(), &[mark]); assert_eq!(store.refused().count(), 0); assert_eq!(store.stopped().map(|(_, _, held)| held), Some(3)); + // What it holds back, what waits among it, each after what it builds on. + let held: Vec<Id> = store.held_back().iter().map(|(id, _)| *id).collect(); + let place = |id: Id| held.iter().position(|held| *held == id); + assert_eq!(held.len(), 4); + assert!(place(at) < place(Id::of(&on))); + assert!(place(Id::of(&later)).is_some() && place(Id::of(&junk)).is_some()); assert!(store.drop_taken(&[Id::of(&later)]).is_err()); // Dropped: what it held back is taken, or refused, and what waited for it refused. assert_eq!(store.drop_taken(&[at]), Ok(vec![at])); assert!(store.stopped().is_none()); + assert!(store.held_back().is_empty()); assert_eq!(store.taken(), &[mark, Id::of(&later)]); assert!(matches!( store.refusal(&Id::of(&junk)), |
