aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/config/mod.rs2
-rw-r--r--src/swarm/bootstrap.rs67
2 files changed, 57 insertions, 12 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index ce82ba9..142dc01 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -206,7 +206,7 @@ pub(crate) const OPTIONS: &[OptionSpec] = &[
Kind::Text,
"PATH",
None,
- "Start a swarm holding a store (docs/store.md), or go on with one, from the store's root, as bytes in this file: the node builds the swarm's genesis change from it, as every node given the root does, and is ready at once. The swarm's ID is the root's: its SHA-256, in hex. Can't be combined with store-id, genesis, or what makes a new swarm's first change (bootstrap-users, bootstrap-keys, bootstrap-policy, remake, genesis-root).",
+ "Start a swarm holding a store (docs/store.md), or go on with one, from the store's root, as bytes on this file's first line, and, for a fork, the events the root keeps, each on a line of its own: the node builds the changes founding the swarm from them, as every node given them does, and is ready at once. The swarm's ID is the root's: its SHA-256, in hex. Can't be combined with store-id, genesis, or what makes a new swarm's first change (bootstrap-users, bootstrap-keys, bootstrap-policy, remake, genesis-root).",
),
#[cfg(feature = "federation")]
option(
diff --git a/src/swarm/bootstrap.rs b/src/swarm/bootstrap.rs
index 1deb390..594db8f 100644
--- a/src/swarm/bootstrap.rs
+++ b/src/swarm/bootstrap.rs
@@ -271,14 +271,16 @@ impl Origin {
pub(crate) struct StoreStart {
/// The root's ID, the swarm's.
pub(crate) id: crate::federation::store::Id,
- /// The root, given.
- root: Option<Vec<u8>>,
+ /// The root, given, and the events it keeps: a fork's.
+ root: Option<(Vec<u8>, Vec<Vec<u8>>)>,
}
#[cfg(feature = "federation")]
impl StoreStart {
- /// The store `root` (a file holding it) or `id` names, if either does: a root the
- /// federation takes as one, which a store's root must be.
+ /// The store `root` or `id` names, if either does: a root the federation takes as one,
+ /// with the events it keeps, which a store's root must be. The file `root` holds the
+ /// root on its first line, and, for a fork, the events it keeps, each on a line of its
+ /// own: JCS, which writes no line break.
pub(crate) fn of(root: Option<&Path>, id: Option<&str>, files: &Files) -> Result<Option<Self>> {
use crate::federation::fold::Federation;
use crate::federation::store::{Id, memory::Store};
@@ -287,16 +289,22 @@ impl StoreStart {
.and_then(Id::from_hex)
.map(|id| StoreStart { id, root: None }));
};
- let root = files
+ let file = files
.read(FileKind::Store, path)
.with_context(|| format!("Reading the store's root {}", path.display()))?
.with_context(|| format!("The store's root {} doesn't exist", path.display()))?;
- if let Err(refused) = Store::<Federation>::new(root.clone(), Vec::new()) {
+ let mut lines = file
+ .split(|byte| *byte == b'\n')
+ .filter(|line| !line.is_empty())
+ .map(<[u8]>::to_vec);
+ let root = lines.next().unwrap_or_default();
+ let kept: Vec<Vec<u8>> = lines.collect();
+ if let Err(refused) = Store::<Federation>::new(root.clone(), kept.clone()) {
bail!("The store's root {} is none: {}", path.display(), refused.0);
}
Ok(Some(StoreStart {
id: Id::of(&root),
- root: Some(root),
+ root: Some((root, kept)),
}))
}
@@ -329,11 +337,11 @@ impl StoreStart {
}
return Ok(Some(starting));
}
- let Some(root) = &self.root else {
+ let Some((root, kept)) = &self.root else {
return Ok(None);
};
use crate::federation::store::Fold;
- let doc = document::events::started(crate::federation::fold::Federation::NAME, root, &[])?;
+ let doc = document::events::started(crate::federation::fold::Federation::NAME, root, kept)?;
info!(swarm_id = %swarm_id, "Starting the swarm holding the store from its root (--store-root)");
Ok(Some(StartingDocument {
doc,
@@ -568,7 +576,7 @@ mod tests {
let root = b"a root".to_vec();
let store = StoreStart {
id: Id::of(&root),
- root: Some(root),
+ root: Some((root, Vec::new())),
};
let started = store.starting(None, None).unwrap().unwrap();
assert_eq!(started.swarm_id, store.swarm_id());
@@ -578,7 +586,7 @@ mod tests {
assert!(store.starting(None, Some("another")).is_err());
let other = StoreStart {
id: Id::of(b"another"),
- root: Some(b"another".to_vec()),
+ root: Some((b"another".to_vec(), Vec::new())),
};
assert!(
store
@@ -591,4 +599,41 @@ mod tests {
};
assert!(joining.starting(None, None).unwrap().is_none());
}
+
+ /// A fork's root, and the event it keeps on a line of its own: the swarm starts from
+ /// the changes founding it. Without that event: no store's root.
+ #[test]
+ fn a_fork_starts_from_its_root_and_what_it_keeps() {
+ use crate::federation::fold::Federation;
+ use crate::federation::harness::{self, Party, Spec};
+ use crate::federation::store::memory::Store;
+ let tv = Party::new("tv", 1);
+ let first = Spec::first(&[&tv], &[]);
+ let mut store = Store::<Federation>::new(first.root(), Vec::new()).unwrap();
+ let write = tv.write(&[store.id()], "home/tv/x", harness::str("1"));
+ let head = Id::of(&write);
+ store.offer(write);
+ let mut spec = first.after(first.hash());
+ spec.parties[0].nodes_since = 1;
+ let (root, kept) = store.fork(&[head], &spec.json().bytes()).unwrap();
+ let dir = crate::files::scratch_dir("fork-root");
+ let (path, alone) = (dir.join("fork"), dir.join("alone"));
+ let files = Files::default();
+ let mut file = root.clone();
+ for event in &kept {
+ file.push(b'\n');
+ file.extend_from_slice(event);
+ }
+ file.push(b'\n');
+ std::fs::write(&path, file).unwrap();
+ std::fs::write(&alone, &root).unwrap();
+ let fork = StoreStart::of(Some(&path), None, &files).unwrap().unwrap();
+ assert_eq!(fork.id, Id::of(&root));
+ let started = fork.starting(None, None).unwrap().unwrap();
+ assert_eq!(
+ document::events::bytes(&started.doc, &head),
+ Some(kept[0].clone())
+ );
+ assert!(StoreStart::of(Some(&alone), None, &files).is_err());
+ }
}