From 27e5436b5cc5b94831aec272cd7b350be808e3f1 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 00:19:51 +0000 Subject: access: the changes founding a fork's swarm taken, unsigned, as built byte for byte A change of a swarm holding a store naming no node is taken where it is one of those founding the swarm (`events::is_founding`): built from the root its document holds and the events the root keeps, those at its history and those it puts, by the fold (`access::holding`). A change of the genesis change's writer waits for no signature, as every node builds it and nobody signs it (`screen::built_by_every_node`); judged, it is refused unless it is one of them. --- src/access/holding.rs | 46 +++++++++++++++++++++++++---- src/access/screen.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 6 deletions(-) (limited to 'src/access') diff --git a/src/access/holding.rs b/src/access/holding.rs index 3c6fd13..4fc41f5 100644 --- a/src/access/holding.rs +++ b/src/access/holding.rs @@ -9,10 +9,11 @@ //! //! Everything else is closed: whatever isn't under `_continuum`, the policy, the swarm's //! ID, users, bans, the swarm's topology, remakes and their acks. Nothing deletes or -//! replaces an event. A change names its node, as signed (signatures are required): the -//! genesis change's writer (`document::events`) names none, so no change under it is -//! taken but the genesis change, which the epoch's root is. Which events the store takes -//! is the store's (`federation::held`); this judges none. +//! replaces an event. A change names its node, as signed (signatures are required), but +//! for the changes founding the swarm, which every node builds and nobody signs: the +//! genesis change, which the epoch's root is, and those of its writer putting the events +//! a fork's root keeps (`document::events`), taken only as built byte for byte. Which +//! events the store takes is the store's (`federation::held`); this judges none. //! //! In a swarm not holding a store, no change writes a policy naming one (`turns_store`): //! a swarm holds a store from its genesis change, or not at all. @@ -22,7 +23,8 @@ use sha2::{Digest, Sha256}; use super::rules::{Change, Refusal}; use crate::document::at::At; use crate::document::inspect::{Action, Op, Path}; -use crate::document::{meta, store}; +use crate::document::{events, meta, store}; +use crate::federation::store::Id; /// The most bytes an event of the fold `fold` may have. fn largest(fold: &str) -> usize { @@ -33,10 +35,44 @@ fn largest(fold: &str) -> usize { } } +/// The events the store's root `root` keeps, by the fold `fold`: those of a fork's. +fn keeps(fold: &str, root: &[u8]) -> Option> { + use crate::federation::store::Fold; + match fold { + crate::federation::fold::Federation::NAME => { + crate::federation::fold::Federation::parts(root).map(|(_, keeps)| keeps) + } + _ => None, + } +} + +/// Whether `change`, naming no node, is one of those founding the swarm, which every node +/// builds from the store's root and the events it keeps (`events::is_founding`). +fn founding(at: &At<'_>, change: &Change<'_>, fold: &str) -> bool { + let Some(root) = events::swarm(at).and_then(|swarm| events::bytes(at, &swarm)) else { + return false; + }; + let Some(keeps) = keeps(fold, &root) else { + return false; + }; + let written: Vec> = change + .ops + .iter() + .filter_map(|op| match &op.action { + Action::PutBytes(bytes) => Some(bytes.clone()), + _ => None, + }) + .collect(); + events::is_founding(at, fold, (&root, &keeps), &change.hash, &written) +} + /// Judges `change`, of a swarm holding a store of the fold `fold`, at its history `at`. pub(crate) fn judge(at: &At<'_>, change: &Change<'_>, fold: &str) -> Result<(), Refusal> { let refuse = |why: String| Err(Refusal::Rule(why)); let Some(node) = change.committer else { + if founding(at, change, fold) { + return Ok(()); + } return refuse("it names no node".to_owned()); }; let events = meta::path(&[meta::EVENTS]); diff --git a/src/access/screen.rs b/src/access/screen.rs index 885badc..f9c67bf 100644 --- a/src/access/screen.rs +++ b/src/access/screen.rs @@ -536,12 +536,14 @@ pub(crate) fn apply( let root = change.deps().is_empty(); // Written under a policy requiring signatures, and none vouches for it yet: it // waits. As that policy has it, not this document's, which may have none yet - // (a joiner's) or another: so every node waits for the same. + // (a joiner's) or another: so every node waits for the same. But for a change + // every node builds itself, which nobody signs. if judging && !root && signatures.is_some() && !vouched.contains(&hash) && reads.required(doc, &seen_at) + && !built_by_every_node(doc, &change) { unsigned.insert(hash); waiting.0.push(change); @@ -721,6 +723,23 @@ pub(crate) struct Outcome { pub(crate) fresh_actor: Option, } +/// Whether `change` is one every node builds itself, and nobody signs: in a swarm holding a +/// store, one of its genesis change's writer, which only the changes founding the swarm +/// are, as judging has it (`access::holding`). +fn built_by_every_node(doc: &Document, change: &Change) -> bool { + #[cfg(feature = "federation")] + { + crate::document::events::swarm(doc).is_some_and(|swarm| { + *change.actor_id() == crate::document::events::genesis_writer(&swarm) + }) + } + #[cfg(not(feature = "federation"))] + { + let _ = (doc, change); + false + } +} + /// [`apply`] of the changes in `bytes`, as sync messages and downloads carry them, with /// what `screening` keeps for document `epoch_seq` (of `epoch`). Bytes that don't load /// as changes, or with a misplaced author (`changes::misplaced_author`), are refused @@ -1330,6 +1349,67 @@ mod tests { assert_eq!(doc.get_heads(), vec![genesis.hash()]); } + /// The changes founding a fork's swarm, unsigned: taken by an empty document, with + /// signatures required, where built byte for byte; another under their writer refused. + #[cfg(feature = "federation")] + #[test] + fn a_store_takes_the_changes_founding_a_fork() { + use crate::document::events; + use crate::federation::fold::Federation; + use crate::federation::harness::{self, Party, Spec}; + use crate::federation::store::{Fold, Id}; + let tv = Party::new("tv", 1); + let first = Spec::first(&[&tv], &[]); + let mut store = + crate::federation::store::memory::Store::::new(first.root(), Vec::new()) + .unwrap(); + let root = store.id(); + let write = tv.write(&[root], "home/tv/x", harness::str("1")); + let head = Id::of(&write); + store.offer(write); + let mut fork = first.after(first.hash()); + fork.parties[0].nodes_since = 1; + let (fork_root, kept) = store.fork(&[head], &fork.json().bytes()).unwrap(); + assert_eq!(kept.len(), 1); + let id = Id::of(&fork_root); + let screening = std::sync::Mutex::new(Screening::default().holding(Some(id))); + let mut signatures = Signatures::default(); + let mut offer = |doc: &mut Document, changes: &[Change]| { + let bytes: Vec = changes + .iter() + .flat_map(|change| change.raw_bytes().to_vec()) + .collect(); + apply_bytes( + doc, + Epoch::GENESIS, + 0, + &bytes, + &screening, + Some(&mut signatures), + &mut crate::document::PatchLog::inactive(), + ) + .unwrap() + }; + let founding = events::founding(Federation::NAME, &fork_root, &kept); + assert_eq!(founding.len(), 2); + // Another change of the writer, putting what the root keeps: refused. + let mut crafted = Document::new().with_actor(events::genesis_writer(&id)); + crafted.apply_changes([founding[0].clone()]).unwrap(); + crate::document::testing::change(&mut crafted, |doc| { + store::write_path(doc, &path("_continuum/members/m"), &json!({})).unwrap(); + }); + let crafted = crafted.get_last_local_change().unwrap().clone(); + let mut doc = Document::new(); + offer(&mut doc, &[founding[0].clone(), crafted]); + assert_eq!(doc.get_heads(), vec![founding[0].hash()]); + offer(&mut doc, &founding[1..]); + assert_eq!(doc.get_heads(), vec![founding[1].hash()]); + assert_eq!( + events::bytes(&doc, &Id::of(&kept[0])), + Some(kept[0].clone()) + ); + } + /// Judged as its writer saw the registry: a grant earlier in the same batch counts. #[test] fn a_grant_in_the_same_batch_counts() { -- cgit v1.3.1