aboutsummaryrefslogtreecommitdiffstats
path: root/src/document
diff options
context:
space:
mode:
Diffstat (limited to 'src/document')
-rw-r--r--src/document/events.rs73
1 files changed, 73 insertions, 0 deletions
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
+ ));
}
}