aboutsummaryrefslogtreecommitdiffstats
path: root/src/state
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-30 14:33:09 +0000
committertv <tv@krebsco.de>2026-11-30 14:33:09 +0000
commit8e97bcfa65e92da21df912cc5e19cf18027b82b8 (patch)
treec0d5b42766e79b2215cb65a0214fc25607d73d5f /src/state
parent362633603e9fc1912fd35807da5653dc1273a5be (diff)
swarm: a node whose store a set of events carries
Such a node (Holding::carrying) holds an Automerge document with nothing in it, which nothing passes on, and its set is its document: * swarm::set passes the set on. Where it stands goes in the node's STATUSes as another kind's stamp, and a peer's is given to the set; what the set says besides goes as a SYNC's body; what the set took, of a peer or of the node itself, goes on to the node's other neighbours. What compares stamps for an Automerge document's sake is handed the node's own, and finds nothing to do; the Automerge sync isn't begun. * The node writes its own entry (AppState::mutate_set): what writes protocol data through a view writes to the members' entries as a tree, and what the node's own reads as after is put as its next entry, signed and passed on. Anything else is refused, and so is a write to an Automerge document as such. * An event the node writes (held::write) is offered to the set and passed on; a set lacks nothing of its store to put again. * The node's swarm is its store's, said at its start. A simulation: three such nodes of two parties register, and each holds the others' entries; events put on two of them reach all three, and they come to stand alike, the documents they hold beside empty.
Diffstat (limited to 'src/state')
-rw-r--r--src/state/mod.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/state/mod.rs b/src/state/mod.rs
index 5fefe11..d512b2b 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.
@@ -636,6 +640,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)
@@ -654,6 +662,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)
@@ -673,9 +685,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.