diff options
| author | tv <tv@krebsco.de> | 2026-11-30 13:58:03 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-30 13:58:03 +0000 |
| commit | af9de78d7de36848f6ff115c9710fb90d2468af2 (patch) | |
| tree | 57013cd112c7928d72e2a6e2c0c7fb4a55edc92b | |
| parent | 27b0fb294c47fea75359c3abadab56cd74c93b33 (diff) | |
swarm: a node says the kind of the document it holds
In its handshakes, and checks a peer's against it (AppState::kind): a
set of events where one carries its store (Holding::of_a_set), else
Automerge's, as it was for every node.
| -rw-r--r-- | src/federation/held.rs | 18 | ||||
| -rw-r--r-- | src/state/mod.rs | 10 | ||||
| -rw-r--r-- | src/swarm/peer/handshake.rs | 4 | ||||
| -rw-r--r-- | src/swarm/peer/inbound.rs | 4 |
4 files changed, 28 insertions, 8 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs index dc0db6f..fec21ab 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -111,6 +111,8 @@ pub(crate) struct Holding { members: RwLock<Option<BTreeSet<(String, String)>>>, /// What it votes for its party with, if it votes by itself. voter: Option<Voter>, + /// Whether a set of events carries the store (`carrying`), from the node's start on. + of_a_set: bool, // A std mutex: never held while waiting for the document. inner: Mutex<Inner>, } @@ -261,6 +263,7 @@ impl Holding { near: Vec::new(), members: RwLock::default(), voter: None, + of_a_set: false, inner: Mutex::new(Inner { kept, ..Inner::default() @@ -1017,7 +1020,8 @@ impl Holding { /// 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 { + pub(crate) fn carrying(mut self, set: Set<Federation>) -> Self { + self.of_a_set = true; { let mut inner = self.inner.lock_or_recover(); inner.store = Some(Carried::Set(Box::new(set))); @@ -1029,12 +1033,18 @@ impl Holding { /// 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, + if self.of_a_set { + super::store::set::KIND + } else { + crate::document::KIND } } + /// Whether a set of events carries the store. + pub(crate) fn of_a_set(&self) -> bool { + self.of_a_set + } + /// 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 { diff --git a/src/state/mod.rs b/src/state/mod.rs index 604f6ae..36dab9d 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -377,6 +377,16 @@ impl AppState { *self.closing.borrow() } + /// The kind of this node's document (`document::KIND`, or a set of events where + /// one carries its store). + pub(crate) fn kind(&self) -> &'static str { + #[cfg(feature = "federation")] + if self.holding.of_a_set() { + return self.holding.kind(); + } + crate::document::KIND + } + pub(crate) fn local_id(&self) -> &str { self.transport.local_id() } diff --git a/src/swarm/peer/handshake.rs b/src/swarm/peer/handshake.rs index 2e5a315..fc1bc86 100644 --- a/src/swarm/peer/handshake.rs +++ b/src/swarm/peer/handshake.rs @@ -144,7 +144,7 @@ async fn transmit_hello(state: &AppState, peer: &str) -> Result<()> { .await .map(|root| root.0.to_vec()), party: party(state), - kind: crate::document::KIND.to_owned(), + kind: state.kind().to_owned(), }), ) .await; @@ -255,7 +255,7 @@ pub(super) async fn send_hello_reply( .await .map(|root| root.0.to_vec()), party: party(state), - kind: crate::document::KIND.to_owned(), + kind: state.kind().to_owned(), }; debug!(to = %peer, ready, "Sending HELLO_REPLY"); diff --git a/src/swarm/peer/inbound.rs b/src/swarm/peer/inbound.rs index a558ded..dbf1a2e 100644 --- a/src/swarm/peer/inbound.rs +++ b/src/swarm/peer/inbound.rs @@ -114,11 +114,11 @@ pub(crate) async fn handle_message( ParsedMessage::HelloReply(reply) => Some(&reply.kind), _ => None, }; - if let Some(theirs) = kind.filter(|kind| *kind != crate::document::KIND) { + if let Some(theirs) = kind.filter(|kind| *kind != state.kind()) { anyhow::bail!( "{peer} holds a document of another kind ({theirs}; this node's is {}): they sync \ nothing; a release that knows the kind does", - crate::document::KIND + state.kind() ); } |
