From f3bfea8ebda0fa7efe1fd9ed6fe670ddb544d38e Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 15 Nov 2026 18:43:13 +0000 Subject: rules: the time before strict is the change that turned it, written again or not A change written under a policy that isn't strict was refused, in a strict document, unless it was in the history of the change that wrote the policy value the document read now. So the policy written again by a node that held a change from before, taken beside the strict policy, put that change in the new writer's history: that node kept it, every other node refused it, and the new policy with it, and the node was cut off. Now the reference is the change that turned the swarm strict: one that wrote a strict policy where the policy at its history wasn't, found by following back the writes of what the policy holds (`store::writers`). A change written under a policy that isn't strict is refused where such a change is beside it, neither in its history nor built on it. A policy written again, strict over strict, names the same change. One turning the swarm strict beside it is refused like any other. Of two at once neither is preferred: any rule choosing one, the lower hash say, would let a change made later to look as old take the place of the one every node took, and every node would drop it with all that was built on it. So two members turning a swarm strict at once split the nodes that took one from those that took the other; access.md says to turn it from one. --- src/access/rules.rs | 92 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 19 deletions(-) (limited to 'src/access') diff --git a/src/access/rules.rs b/src/access/rules.rs index 0690696..43404cf 100644 --- a/src/access/rules.rs +++ b/src/access/rules.rs @@ -29,13 +29,14 @@ //! any member may. A change written while the document had no policy (at its //! dependencies) is judged by none: the rules hold from the policy on. -use std::collections::{BTreeSet, HashMap}; +use std::collections::{BTreeSet, HashMap, HashSet}; use crate::access::policy::Policy; use crate::access::registry; use crate::access::sealed::Declared; use crate::access::{Caps, Right}; use crate::document::at::At; +use crate::document::changes::in_history; use crate::document::inspect::{Action, Op, Path}; use crate::document::meta; use crate::document::replica::Epoch; @@ -351,28 +352,53 @@ pub(crate) fn judge_reading( } /// Why `change`, written under a policy that isn't strict, is refused where `doc`'s is: -/// it isn't in the history of the change that wrote the policy `doc` reads. Nothing is -/// added to the time before a swarm turned strict, so nothing planted there can stand -/// where every value is its user's to sign; what a node held of it beside that change -/// it drops, once told (`swarm::refusals`). A swarm strict from its genesis change -/// (`--bootstrap-policy`) has no such time. None where `doc`'s policy isn't strict: -/// there, nothing changes. +/// a change that turned the swarm strict is beside it, neither in its history nor +/// built on it. Nothing is added to the time before a swarm turned strict, so nothing +/// planted there can stand where every value is its user's to sign; what a node held of +/// it beside that change it drops, once told (`swarm::refusals`). A policy written again +/// later, strict over strict, names the same change; one written beside it, strict or +/// not, is refused like any other, so a change made later to look as old can't take its +/// place. A swarm strict from its genesis change (`--bootstrap-policy`) has no such +/// time. None where `doc`'s policy isn't strict: there, nothing changes. fn before_strict(doc: &Document, change: &Change<'_>) -> Option { if !super::policy::of(doc).strict() { return None; } - let policy = store::written_by(doc, &super::policy::path())?; - let before = change.hash == policy - || (crate::document::changes::holds(doc, &change.hash) - && !doc - .get_changes(&[policy]) - .iter() - .any(|later| later.hash() == change.hash)); - (!before).then(|| { - format!( - "written before the swarm turned strict, beside the change that turned it ({policy})" - ) - }) + let held = crate::document::changes::holds(doc, &change.hash); + let beside = turned_strict(doc).into_iter().find(|turned| { + *turned != change.hash + && !in_history(doc, turned, change.deps) + && !(held && in_history(doc, &change.hash, std::slice::from_ref(turned))) + })?; + Some(format!( + "written before the swarm turned strict, beside the change that turned it ({beside})" + )) +} + +/// The changes that turned `doc` strict: each wrote a strict policy where the one at its +/// history wasn't. Found by following back the writes of what the policy holds, each +/// write's history in turn, so every one is found however often the policy was written +/// since. +fn turned_strict(doc: &Document) -> Vec { + let path = super::policy::path(); + let strict_at = |heads: &[ChangeHash]| { + At::new(doc, heads).is_some_and(|at| super::policy::of(&at).strict()) + }; + let mut turned = Vec::new(); + let mut looked: HashSet = HashSet::new(); + let mut writes = store::writers(doc, doc, &path); + while let Some((hash, deps)) = writes.pop() { + if !looked.insert(hash) { + continue; + } + if strict_at(&[hash]) && !strict_at(&deps) { + turned.push(hash); + } + if let Some(before) = At::new(doc, &deps) { + writes.extend(store::writers(doc, &before, &path)); + } + } + turned } /// `hash`, which `doc` (of `epoch`) holds, judged by its rules now, as received: why @@ -1192,6 +1218,34 @@ mod tests { Some(Err(_)) )); assert_eq!(judge_held(&doc, Epoch::GENESIS, &kept), Some(Ok(()))); + // The policy written again, by a change that saw the one beside: the change that + // turned the swarm strict is still the one, and the one beside still refused. + let mut again = doc.clone(); + write( + &mut again, + "_continuum/access/policy", + json!({"version": 1, "values": "signed"}), + ); + assert!(matches!( + judge_held(&again, Epoch::GENESIS, &planted), + Some(Err(_)) + )); + assert_eq!(judge_held(&again, Epoch::GENESIS, &kept), Some(Ok(()))); + // Turned strict beside it too: refused all the same, so a change made later to + // look as old can't take its place. + let mut rival = beside.fork_at(&before).unwrap(); + write( + &mut rival, + "_continuum/access/policy", + json!({"version": 1, "values": "signed"}), + ); + let rival_hash = rival.get_heads()[0]; + let mut both = doc.clone(); + both.merge(&mut rival).unwrap(); + assert!(matches!( + judge_held(&both, Epoch::GENESIS, &rival_hash), + Some(Err(_)) + )); // Received, from the time before: refused too. let touched = paths(&["notes/late"]); assert!(refused(judge( -- cgit v1.3.1