diff options
| author | tv <tv@krebsco.de> | 2026-12-01 02:45:46 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-01 02:45:46 +0100 |
| commit | 34bfcb661ed74bf40809576bbb388362cb7ffaa1 (patch) | |
| tree | 8422db7db396dd4ae83403837cd954a94f882a7a /src/state | |
| parent | 34434f7fd5a6bab1884a63c96679f48045f4bffd (diff) | |
| parent | 6a06bc4a955cdf2b5a183c9ff21e25c79a4e5d9f (diff) | |
Diffstat (limited to 'src/state')
| -rw-r--r-- | src/state/mod.rs | 105 | ||||
| -rw-r--r-- | src/state/peer.rs | 4 |
2 files changed, 109 insertions, 0 deletions
diff --git a/src/state/mod.rs b/src/state/mod.rs index 604f6ae..c3bb4e8 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -50,6 +50,10 @@ use crate::swarm::throughput::ThroughputTable; use crate::transport; use crate::transport::firewall::Firewall; +/// Why a write is refused where a set of events carries the node's store. +#[cfg(feature = "federation")] +const NOT_A_SETS: &str = "this node's document is a set of events: what is written to it is events, and of its members' entries the node's own"; + #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum NodeLifecycle { // TODO maybe rename discovered_swarm_id to swarm_id if that doesn't impair readability. @@ -377,6 +381,44 @@ impl AppState { *self.closing.borrow() } + /// The kinds of document this node holds, which it says in handshakes: more than + /// one where it holds its store in both (`Holding::kinds`). + pub(crate) fn kinds(&self) -> &'static [&'static str] { + #[cfg(feature = "federation")] + { + self.holding.kinds() + } + #[cfg(not(feature = "federation"))] + { + &[crate::document::KIND] + } + } + + /// Whether this node's document is a set of events (`federation::store::set`): + /// the Automerge document it holds is empty then, and nothing passes it on. + pub(crate) fn of_a_set(&self) -> bool { + #[cfg(feature = "federation")] + { + self.holding.of_a_set() + } + #[cfg(not(feature = "federation"))] + { + false + } + } + + /// What the node reads of a document at paths (its members, the policy, the swarm's + /// ID), where this node's document keeps + /// them apart (a set of events, `Holding::entries`): none, where they are in the + /// document it holds. + pub(crate) fn entries(&self) -> Option<crate::document::view::Tree> { + #[cfg(feature = "federation")] + if self.holding.has_a_set() { + return self.holding.entries(); + } + None + } + pub(crate) fn local_id(&self) -> &str { self.transport.local_id() } @@ -602,6 +644,10 @@ impl AppState { T: Send + 'static, E: From<StoreError> + Send + 'static, { + #[cfg(feature = "federation")] + if self.of_a_set() { + return self.mutate_set(f).await; + } self.mutate_doc_as(None, Sign::Protocol, false, move |write| f(write)) .await .map(|(out, _)| out) @@ -620,6 +666,10 @@ impl AppState { T: Send + 'static, E: From<StoreError> + Send + 'static, { + #[cfg(feature = "federation")] + if self.of_a_set() { + return self.mutate_set(f).await; + } self.mutate_doc_as(user, sign, true, move |write| f(write)) .await .map(|(out, _)| out) @@ -639,9 +689,64 @@ impl AppState { T: Send + 'static, E: From<StoreError> + Send + 'static, { + #[cfg(feature = "federation")] + if self.of_a_set() { + return Err(E::from(StoreError::Refused(NOT_A_SETS.to_owned()))); + } self.mutate_doc_as(user, sign, true, f).await } + /// A write where a set of events carries this node's store (`federation::store:: + /// set`): to the members' entries as a document has them (`Holding::entries`), of + /// which the node writes its own alone. What its own reads as after is put as its + /// next entry, signed, and passed on (`swarm::set`). + #[cfg(feature = "federation")] + async fn mutate_set<T, E>(&self, f: impl FnOnce(&mut dyn Write) -> Result<T, E>) -> Result<T, E> + where + E: From<StoreError>, + { + use crate::document::view::Read; + use crate::federation::store::entry::Entry; + use crate::federation::store::set::Put; + + let refused = || E::from(StoreError::Refused(NOT_A_SETS.to_owned())); + let own = crate::swarm::members::member_path(self.local_id()); + let before = self.holding.entries().unwrap_or_default(); + let mut after = before.clone(); + let out = f(&mut after)?; + let was = before.read_path(&own).map_err(E::from)?; + let is = after.read_path(&own).map_err(E::from)?; + // Nothing but its own entry, which it doesn't take away. + let (mut rest_before, mut rest_after) = (before, after); + let _ = rest_before.delete_path(&own, true); + let _ = rest_after.delete_path(&own, true); + if rest_before != rest_after { + return Err(refused()); + } + let Some(is) = is else { + return if was.is_some() { + Err(refused()) + } else { + Ok(out) + }; + }; + if Some(&is) == was.as_ref() { + return Ok(out); + } + let Some(key) = self.node_key.clone() else { + return Err(refused()); + }; + let node = self.local_id().to_owned(); + let put = self.holding.change_set(|set| { + let count = set.entry(&node).map_or(1, |entry| entry.count() + 1); + set.put_entry(Entry::new(&set.store().id(), &key, count, &is)) + }); + if put == Some(Put::Taken) { + crate::swarm::set::registered(self).await; + } + Ok(out) + } + /// [`mutate_doc_for`](Self::mutate_doc_for); with `registering_first`, after this /// node's registration if the policy needs it first. While writes are held /// (`writes_held`), after they no longer are. diff --git a/src/state/peer.rs b/src/state/peer.rs index 721a754..356736a 100644 --- a/src/state/peer.rs +++ b/src/state/peer.rs @@ -92,6 +92,9 @@ pub(crate) struct PeerState { /// The party its handshakes name, in a swarm holding a store (`swarm::peer::members`). #[cfg(feature = "federation")] pub(crate) party: Option<String>, + /// The kind of document its handshakes say it holds, of those this node holds: what + /// this node says to it in turn, and passes on to it (`swarm::peer::inbound`). + pub(crate) kind: Option<&'static str>, pub(crate) hello_state: HelloState, /// Wait between unanswered HELLOs; reset whenever a new handshake starts. pub(crate) hello_backoff: Backoff, @@ -339,6 +342,7 @@ impl PeerState { tree_writer: None, left: false, hello_attempts: 0, + kind: None, last_hello_at: None, unreachable_since: None, incidents: Incidents::default(), |
