From a3af68fafee80b3eef493e8b041e21352e547da2 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 29 Nov 2026 14:27:24 +0000 Subject: federation: a node's store carried by a set of events Holding::carrying: the node's store is a set's, read from no document. What the API and the node's own voting do with the store, they do with it as before; Holding::kind says which kind of document carries it, and with_set and change_set give the set to what passes it on, the node's members read anew after. No node starts so yet. --- src/federation/held.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) (limited to 'src/federation') 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) -> 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(&self, f: impl FnOnce(&Set) -> T) -> Option { + 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(&self, f: impl FnOnce(&mut Set) -> T) -> Option { + 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::::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()); + } } -- cgit v1.3.1