aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation')
-rw-r--r--src/federation/held.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs
index 508b2eb..cb130ba 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -1015,6 +1015,46 @@ impl Holding {
self.inner.lock_or_recover().store.as_deref().map(f)
}
+ /// Itself, its store carried by `set`: the store's own document, read from no
+ /// other (`store::set`).
+ pub(crate) fn carrying(self, set: Set<Federation>) -> Self {
+ {
+ let mut inner = self.inner.lock_or_recover();
+ inner.store = Some(Carried::Set(Box::new(set)));
+ self.settle(&mut inner);
+ }
+ self
+ }
+
+ /// The kind of the document that carries the store: a set of events, or the
+ /// document the node holds, which the store is read from.
+ pub(crate) fn kind(&self) -> &'static str {
+ match self.inner.lock_or_recover().store {
+ Some(Carried::Set(_)) => super::store::set::KIND,
+ _ => crate::document::KIND,
+ }
+ }
+
+ /// What `f` makes of the set of events carrying the store, where one does.
+ pub(crate) fn with_set<T>(&self, f: impl FnOnce(&Set<Federation>) -> T) -> Option<T> {
+ match &self.inner.lock_or_recover().store {
+ Some(Carried::Set(set)) => Some(f(set)),
+ _ => None,
+ }
+ }
+
+ /// Has `f` change the set of events carrying the store, where one does, and
+ /// settles what the node keeps of its store by it: its members, as it reads now.
+ pub(crate) fn change_set<T>(&self, f: impl FnOnce(&mut Set<Federation>) -> T) -> Option<T> {
+ let mut inner = self.inner.lock_or_recover();
+ let Some(Carried::Set(set)) = inner.store.as_mut() else {
+ return None;
+ };
+ let out = f(set);
+ self.settle(&mut inner);
+ Some(out)
+ }
+
/// What a remake's snapshot holds of the store: as this node reads it anew from
/// `base`, the document at the remake's base, by its own refusals and its operator's
/// drops, as it reads its document whenever it starts; and what it holds back there,
@@ -2730,4 +2770,54 @@ mod tests {
LATER,
);
}
+
+ #[test]
+ fn a_store_carried_by_a_set_is_the_nodes_store() {
+ use crate::federation::store::passing;
+
+ let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2));
+ let first = Spec::first(&[&tv, &lass], &[]);
+ let root = first.root();
+ let set = |root: &[u8]| {
+ Set::new(
+ Store::<Federation>::new(root.to_vec(), Vec::new()).expect("a root"),
+ Duration::from_secs(60),
+ )
+ };
+ let holding = Holding::default().carrying(set(&root));
+ assert_eq!(holding.kind(), crate::federation::store::set::KIND);
+ assert_eq!(Holding::default().kind(), crate::document::KIND);
+ assert!(holding.tells_members());
+ // Offered as to any store; and what another's set holds comes by passing on.
+ let write = tv.write(&[Id::of(&root)], "home/tv/a", harness::str("1"));
+ assert!(matches!(
+ holding.offer(write.clone()),
+ Some(Offered::Taken(_))
+ ));
+ assert_eq!(holding.with(|store| store.taken().len()), Some(1));
+ let other = Holding::default().carrying(set(&root));
+ let stamp = holding.with_set(passing::stamp).expect("a set");
+ let mut to_other = vec![passing::Message::Stamp(stamp)];
+ let mut to_holding = Vec::new();
+ for _ in 0..10 {
+ for message in std::mem::take(&mut to_other) {
+ let (replies, _) = other
+ .change_set(|set| passing::receive(set, message, Instant::now()))
+ .expect("a set");
+ to_holding.extend(replies);
+ }
+ for message in std::mem::take(&mut to_holding) {
+ let (replies, _) = holding
+ .change_set(|set| passing::receive(set, message, Instant::now()))
+ .expect("a set");
+ to_other.extend(replies);
+ }
+ }
+ assert_eq!(
+ other.with(|store| store.taken().to_vec()),
+ Some(vec![Id::of(&write)])
+ );
+ // A store read from a document has no set.
+ assert!(Holding::default().with_set(|_| ()).is_none());
+ }
}