aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 21:26:47 +0000
committertv <tv@krebsco.de>2026-11-16 23:04:42 +0100
commitd46368d5ba8d7a12e657b5f293830b6c0d8dce00 (patch)
tree85cce42b4b21839a0a606ae64e55043d34e7bb87 /src
parentd22ca4351b86c0066f13e735960838bb5d85e044 (diff)
federation: events offered to a node's store, and its operator's drops
A node's store takes events from elsewhere than its document too: one put at its API is offered to it first (`Holding::offer`), to say what became of it. Its operator has it drop events it took after its checkpoint, with everything built on them (`Holding::drop_taken`), and takes them back (`Holding::allow`), the document's events then offered again (`docs/store.md`, A node's own). After each, what the node keeps of its own is written to its file where it changed, and the members are as the store reads now.
Diffstat (limited to 'src')
-rw-r--r--src/federation/fold.rs2
-rw-r--r--src/federation/held.rs84
2 files changed, 84 insertions, 2 deletions
diff --git a/src/federation/fold.rs b/src/federation/fold.rs
index ad9ff59..8ce6e60 100644
--- a/src/federation/fold.rs
+++ b/src/federation/fold.rs
@@ -210,7 +210,7 @@ impl State {
}
/// The node list of `party` by `current`.
- fn nodes_of(&self, party: &str, current: &Version) -> Vec<String> {
+ pub(crate) fn nodes_of(&self, party: &str, current: &Version) -> Vec<String> {
let Some(given) = current.parties.get(party) else {
return Vec::new();
};
diff --git a/src/federation/held.rs b/src/federation/held.rs
index 542aeed..07660e0 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -32,7 +32,7 @@ use tracing::{error, info, warn};
use super::event::{Does, Event, Signed};
use super::fold::Federation;
use super::store::memory::{Built, Offered, Store};
-use super::store::{Fold, Id};
+use super::store::{Fold, Id, Refused};
use super::version::Version;
use crate::document::{Change, ChangeHash, ReadDoc, ScalarValue, events, meta, store};
use crate::files::{FileKind, Files};
@@ -253,6 +253,15 @@ impl Holding {
if taken > 0 || refused > 0 {
info!(taken, refused, "Read events from the document");
}
+ self.settle(inner);
+ }
+
+ /// After the store changed: what the node keeps of its own, written to its file where
+ /// it changed, and the members, as the store reads now.
+ fn settle(&self, inner: &mut Inner) {
+ let Some(store) = &inner.store else {
+ return;
+ };
let (own, dropped) = store.own();
let kept = Kept::of(&own, &dropped);
if kept != inner.kept {
@@ -273,6 +282,47 @@ impl Holding {
}
}
+ /// Offers the store `bytes`, an event put at the node's API: what became of it. None
+ /// while the node holds no store.
+ pub(crate) fn offer(&self, bytes: Vec<u8>) -> Option<Offered> {
+ let mut inner = self.inner.lock_or_recover();
+ let offered = inner.store.as_mut()?.offer(bytes);
+ self.settle(&mut inner);
+ Some(offered)
+ }
+
+ /// Drops `ids`, events the store took after its checkpoint, with everything built on
+ /// them, as its operator has the node do: it refuses them from then on, and keeps so
+ /// (`docs/store.md`, A node's own). What it dropped, in the order taken.
+ pub(crate) fn drop_taken(&self, ids: &[Id]) -> Option<Result<Vec<Id>, Refused>> {
+ let mut inner = self.inner.lock_or_recover();
+ let dropped = inner.store.as_mut()?.drop_taken(ids);
+ if let Ok(dropped) = &dropped {
+ warn!(
+ dropped = dropped.len(),
+ "Dropped events, as this node's operator says"
+ );
+ }
+ self.settle(&mut inner);
+ Some(dropped)
+ }
+
+ /// Takes back what the node refused of its own, or dropped, among `ids`, and what was
+ /// refused for building on them, as its operator says otherwise: the document's events
+ /// are offered again, and taken where the store takes them then. Whether it holds a
+ /// store.
+ pub(crate) fn allow(&self, ids: &[Id]) -> bool {
+ let mut inner = self.inner.lock_or_recover();
+ let Some(store) = inner.store.as_mut() else {
+ return false;
+ };
+ store.allow(ids);
+ inner.read.clear();
+ inner.looked.clear();
+ self.settle(&mut inner);
+ true
+ }
+
/// Writes what the node keeps of its own to its file, if it has one.
fn keep(&self, kept: &Kept) {
let Some((path, files)) = &self.file else {
@@ -625,6 +675,38 @@ mod tests {
assert!(elsewhere.with(|_| ()).is_none());
}
+ /// An event put at the API is offered to the store; what the operator drops is
+ /// refused, and kept so, until it allows it again.
+ #[test]
+ fn an_operator_drops_what_its_node_took_and_allows_it_again() {
+ let tv = Party::new("tv", 1);
+ let first = Spec::first(&[&tv], &[]);
+ let root = root_of(&first);
+ let write = tv.write(&[Id::of(&root)], "home/tv/x", harness::str("1"));
+ let on = tv.write(&[Id::of(&write)], "home/tv/x", harness::str("2"));
+ let holding = Holding::default();
+ assert_eq!(holding.offer(write.clone()), None);
+ let mut doc = events::started(Federation::NAME, &root).unwrap();
+ put(&mut doc, &write);
+ read(&holding, &doc, 0);
+ assert!(matches!(holding.offer(on.clone()), Some(Offered::Taken(_))));
+ assert_eq!(
+ holding.drop_taken(&[Id::of(&write)]),
+ Some(Ok(vec![Id::of(&write), Id::of(&on)]))
+ );
+ assert!(matches!(holding.drop_taken(&[Id::of(&root)]), Some(Err(_))));
+ assert_eq!(holding.with(|store| store.taken().len()), Some(0));
+ assert_eq!(holding.inner.lock_or_recover().kept.dropped.len(), 1);
+ // Allowed again: the document's events are taken again as it reads them.
+ assert!(holding.allow(&[Id::of(&write)]));
+ read(&holding, &doc, 0);
+ assert_eq!(
+ holding.with(|store| store.taken().to_vec()),
+ Some(vec![Id::of(&write)])
+ );
+ assert!(holding.inner.lock_or_recover().kept.dropped.is_empty());
+ }
+
#[test]
fn a_node_puts_again_what_its_document_lacks() {
let tv = Party::new("tv", 1);