diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/access/holding.rs | 46 | ||||
| -rw-r--r-- | src/access/screen.rs | 82 | ||||
| -rw-r--r-- | src/document/events.rs | 73 |
3 files changed, 195 insertions, 6 deletions
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<Vec<Id>> { + 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<Vec<u8>> = 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<crate::document::ActorId>, } +/// 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::<Federation>::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<u8> = 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() { 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<u8>]) -> 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<Id> { + 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<u8>], +) -> bool { + let kept: Vec<Vec<u8>> = 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<Id> = 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 + )); } } |
