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/document/events.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'src/document') diff --git a/src/document/events.rs b/src/document/events.rs index 81651f1..95e99c9 100644 --- a/src/document/events.rs +++ b/src/document/events.rs @@ -163,6 +163,36 @@ pub(crate) fn started(fold: &str, root: &[u8], kept: &[Vec]) -> anyhow::Resu Ok(doc.with_actor(ActorId::random())) } +/// The ID of the store `doc` holds, its swarm's, where it holds one. +pub(crate) fn swarm(doc: &impl ReadDoc) -> Option { + match store::read_plain(doc, &meta::path(&[meta::SWARM])) { + Ok(Some(JsonValue::String(swarm))) => Id::from_hex(&swarm), + _ => None, + } +} + +/// Whether the change of hash `hash`, putting the events `written`, is one founding the +/// swarm whose root `doc` holds, `root`, keeping `keeps`: built byte for byte from the root +/// and the kept events `doc` holds or it puts, as every node builds them (`founding`). +pub(crate) fn is_founding( + doc: &impl ReadDoc, + fold: &str, + (root, keeps): (&[u8], &[Id]), + hash: &super::ChangeHash, + written: &[Vec], +) -> bool { + let kept: Vec> = keeps + .iter() + .filter_map(|keep| { + bytes(doc, keep) + .or_else(|| written.iter().find(|event| Id::of(event) == *keep).cloned()) + }) + .collect(); + founding(fold, root, &kept) + .iter() + .any(|change| change.hash() == *hash) +} + /// Whether `change` is the genesis change of the swarm holding a store of the fold `fold` /// whose root's ID is `id`: built again from the root it holds, byte for byte the same. pub(crate) fn is_genesis(change: &Change, fold: &str, id: &Id) -> bool { @@ -283,5 +313,48 @@ mod tests { let doc = started("federation", ROOT, &kept).unwrap(); let keeps: Vec = kept.iter().map(|event| Id::of(event)).collect(); assert!(keeps.iter().all(|id| bytes(&doc, id).is_some())); + assert_eq!(swarm(&doc), Some(Id::of(ROOT))); + // Each known at its history, by what it puts. + let mut before = Document::new(); + before.apply_changes(changes[..2].to_vec()).unwrap(); + let last = &changes[2]; + assert!(is_founding( + &before, + "federation", + (ROOT, &keeps), + &last.hash(), + &carried(last) + )); + assert!(!is_founding( + &before, + "federation", + (ROOT, &keeps[..2]), + &last.hash(), + &carried(last) + )); + // All three in one change of the writer: not one of them. + let mut one = Document::new(); + one.apply_changes([changes[0].clone()]).unwrap(); + let mut write = one.transaction(); + for event in &kept { + store::write_val_path( + &mut write, + &self::event(&Id::of(event)), + &Val::Scalar(ScalarValue::Bytes(event.clone())), + store::Writing::sticky(TimestampFormat::default()), + ) + .unwrap(); + } + write.commit_with(CommitOptions::default().with_time(0)); + let crafted = one.get_changes(&[]).pop().unwrap(); + let mut root_only = Document::new(); + root_only.apply_changes([changes[0].clone()]).unwrap(); + assert!(!is_founding( + &root_only, + "federation", + (ROOT, &keeps), + &crafted.hash(), + &kept + )); } } -- cgit v1.3.1