diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/federation/client.rs | 33 | ||||
| -rw-r--r-- | src/federation/harness.rs | 6 | ||||
| -rw-r--r-- | src/federation/properties.rs | 17 | ||||
| -rw-r--r-- | src/federation/revision.rs | 18 | ||||
| -rw-r--r-- | src/federation/version.rs | 57 |
5 files changed, 121 insertions, 10 deletions
diff --git a/src/federation/client.rs b/src/federation/client.rs index ca2db86..74be7c8 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -433,8 +433,8 @@ pub(crate) fn backable(current: &Version, proposed: &Json) -> Result<Version, St /// What the version `proposed` does to `current`, in words, a line each: the parties it /// lets in or takes out, the keys and node lists it names anew, the rules it adds or drops, -/// the checkpoint it moves the store to, and the revision it moves to; what a party votes -/// on, without its JSON. Its number and the hash it changes go without saying. A node goes +/// the checkpoint it moves the store to, the revision it moves to, and the federation's +/// name it gives; what a party votes on, without its JSON. Its number and the hash it changes go without saying. A node goes /// by its name where `names` has one (`node_names`). pub(crate) fn described( current: &Version, @@ -492,6 +492,14 @@ pub(crate) fn described( if proposed.revision != current.revision { lines.push(format!("moves to {}", proposed.revision)); } + match (¤t.name, &proposed.name) { + (None, Some(name)) => lines.push(format!("names the federation {name}")), + (Some(was), Some(name)) if was != name => { + lines.push(format!("renames the federation {was} -> {name}")); + } + (Some(was), None) => lines.push(format!("drops the federation's name, {was}")), + _ => {} + } lines } @@ -1411,6 +1419,27 @@ mod tests { "moves to revision 1 (merge-events)".to_owned(), ] ); + let mut named = Spec::first(&[&tv, &lass], &rules); + named.revision = Revision::newest(); + named.name = Some("krebs".to_owned()); + assert_eq!( + described(¤t, &read(&named)), + [ + format!("moves to {}", Revision::newest()), + "names the federation krebs".to_owned(), + ] + ); + let mut renamed = named.clone(); + renamed.name = Some("krebs-test".to_owned()); + assert_eq!( + described(&read(&named), &read(&renamed)), + ["renames the federation krebs -> krebs-test"] + ); + renamed.name = None; + assert_eq!( + described(&read(&named), &read(&renamed)), + ["drops the federation's name, krebs"] + ); } /// Each hash is named by its shortest prefix no other has, of four digits at least, and diff --git a/src/federation/harness.rs b/src/federation/harness.rs index f14736d..04308da 100644 --- a/src/federation/harness.rs +++ b/src/federation/harness.rs @@ -138,6 +138,8 @@ pub(crate) struct Spec { pub(crate) parties: Vec<SpecParty>, /// The revision it follows: named where it isn't the base revision. pub(crate) revision: Revision, + /// The federation's name, where it gives one. + pub(crate) name: Option<String>, /// Each rule: its path and its writer. pub(crate) rules: Vec<(String, Json)>, pub(crate) checkpoint: Option<Vec<Id>>, @@ -151,6 +153,7 @@ impl Spec { number: 0, changes: None, revision: Revision::default(), + name: None, parties: parties .iter() .enumerate() @@ -256,6 +259,9 @@ impl Spec { if self.revision != Revision::default() { members.push(("revision", str(&self.revision.id().to_string()))); } + if let Some(name) = &self.name { + members.push(("name", str(name))); + } object(members) } diff --git a/src/federation/properties.rs b/src/federation/properties.rs index 2b824f0..dce0984 100644 --- a/src/federation/properties.rs +++ b/src/federation/properties.rs @@ -108,7 +108,7 @@ fn generate(rng: &mut Rng, kind: Kind) -> History { } /// A history of `kind`, its first version following `revision`, or, where none is given, -/// the base revision or one with merge events alike. +/// the base revision or the newest alike. pub(super) fn generate_at(rng: &mut Rng, kind: Kind, revision: Option<Revision>) -> History { let count = rng.between(3, 5); let parties: Vec<Party> = (0..count) @@ -117,13 +117,18 @@ pub(super) fn generate_at(rng: &mut Rng, kind: Kind, revision: Option<Revision>) let adversary = count - 1; let refs: Vec<&Party> = parties.iter().collect(); let mut first = Spec::first(&refs, &[("names/{party}", harness::str("{party}"))]); - // Half the federations begin at a revision with merge events, which nodes then sign. - let merging = rng.chance(0.5); - first.revision = revision.unwrap_or(if merging { - Feature::MergeEvents.revision() + // Half the federations begin at the newest revision: with merge events, which nodes + // then sign, and a name, which versions change. + let newest = rng.chance(0.5); + first.revision = revision.unwrap_or(if newest { + Revision::newest() } else { Revision::default() }); + let naming = first.revision.has(Feature::FederationName); + if naming { + first.name = Some("f".to_owned()); + } let root = first.root(); let root_id = Id::of(&root); let mut world: Store<Federation> = Store::new(root.clone(), Vec::new()).expect("a root"); @@ -204,6 +209,8 @@ pub(super) fn generate_at(rng: &mut Rng, kind: Kind, revision: Option<Revision>) let head = *deps.iter().find(|dep| **dep != root_id).expect("one"); next.checkpoint = Some(vec![head]); deps = vec![head]; + } else if naming && rng.chance(0.3) { + next.name = Some(format!("f{step}")); } else { next.rules .push((format!("names/x{step}"), harness::str(&party.name))); diff --git a/src/federation/revision.rs b/src/federation/revision.rs index 7be6254..2e5464d 100644 --- a/src/federation/revision.rs +++ b/src/federation/revision.rs @@ -16,6 +16,9 @@ pub(crate) enum Feature { /// Merge events: a node its party lists, with its own key, builds on events it holds, /// writing nothing (`Does::Merge`). MergeEvents, + /// The federation's name, in its versions (`Version::name`): the first version's, in + /// the root, parts it from a federation of a first version alike otherwise. + FederationName, } impl Feature { @@ -24,6 +27,7 @@ impl Feature { pub(crate) fn name(self) -> &'static str { match self { Feature::MergeEvents => "merge-events", + Feature::FederationName => "federation-name", } } @@ -34,6 +38,10 @@ impl Feature { "merge events, which a node its party lists signs with its own key, building \ on events it holds and writing nothing" } + Feature::FederationName => { + "the federation's name, which a version gives it and a later one changes: the \ + first version's, in the root, parts it from a federation alike otherwise" + } } } @@ -49,7 +57,7 @@ impl Feature { /// The features, in the order revisions add them: revision n has the first n, the base /// revision none. Only ever appended to. -pub(crate) const FEATURES: &[Feature] = &[Feature::MergeEvents]; +pub(crate) const FEATURES: &[Feature] = &[Feature::MergeEvents, Feature::FederationName]; /// A revision: how many of `FEATURES` it has. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] @@ -158,6 +166,14 @@ mod tests { Id::of(br#"["merge-events"]"#), "a released revision's ID changes never" ); + assert_eq!( + Revision(2).id(), + Id::of(br#"["merge-events","federation-name"]"#) + ); + assert_eq!( + Feature::FederationName.revision().to_string(), + "revision 2 (merge-events, federation-name)" + ); let names: Vec<&str> = FEATURES.iter().map(|feature| feature.name()).collect(); let distinct: std::collections::BTreeSet<&str> = names.iter().copied().collect(); assert_eq!(distinct.len(), names.len(), "a feature named twice"); diff --git a/src/federation/version.rs b/src/federation/version.rs index fa534b3..cebb83a 100644 --- a/src/federation/version.rs +++ b/src/federation/version.rs @@ -5,7 +5,7 @@ use std::collections::{BTreeMap, BTreeSet}; use super::jcs::Json; use super::keys; -use super::revision::Revision; +use super::revision::{Feature, Revision}; use super::store::Id; /// The federation this is: a version naming another is none here. @@ -34,6 +34,10 @@ pub(crate) struct Version { pub(crate) json: Json, /// The revision of the federation it follows: the base revision where it names none. pub(crate) revision: Revision, + /// The federation's name, where it gives one (`Feature::FederationName`): spelled as a + /// party's name. The first version's, in the root, parts the federation from one of a + /// first version alike otherwise; a later version's changes only what it is called. + pub(crate) name: Option<String>, pub(crate) number: u64, /// The version it changes; none for the first. pub(crate) changes: Option<Id>, @@ -157,7 +161,7 @@ impl Version { json, "a version", &["federation", "number", "changes", "keys", "nodes", "rules"], - &["checkpoint", "revision"], + &["checkpoint", "revision", "name"], )?; match json.get("federation").and_then(Json::as_int) { Some(FEDERATION) => {} @@ -192,6 +196,22 @@ impl Version { } } }; + let name = match json.get("name") { + None => None, + Some(_) if !revision.has(Feature::FederationName) => { + return Err(format!( + "a `name`, which {revision} doesn't give a version: {} does", + Feature::FederationName.revision() + )); + } + Some(Json::Str(name)) if is_party(name) => Some(name.clone()), + Some(_) => { + return Err( + "a `name` that is no name: 1 to 64 of A-Z a-z 0-9 . _ @ -, as a party's" + .to_owned(), + ); + } + }; let number = json .get("number") .and_then(Json::as_int) @@ -226,6 +246,7 @@ impl Version { hash: Id::of(&json.bytes()), json: json.clone(), revision, + name, number, changes, parties, @@ -624,6 +645,38 @@ mod tests { } } + /// A version gives its federation a name only under a revision that has names, spelled + /// as a party's name; one without gives none. + #[test] + fn a_version_names_its_federation_under_a_revision_with_names() { + let named = Feature::FederationName.revision(); + let with = |revision: Revision, name: &str| { + let text = String::from_utf8(first().bytes()).expect("UTF-8"); + let text = text.replacen(r#""nodes":"#, &format!(r#""name":{name},"nodes":"#), 1); + let text = text.replacen( + r#""rules":"#, + &format!(r#""revision":"{}","rules":"#, revision.id()), + 1, + ); + Version::read(&jcs::read(text.as_bytes()).expect("JCS")) + }; + assert_eq!( + with(named, r#""krebs""#) + .expect("a version") + .name + .as_deref(), + Some("krebs") + ); + let why = with(Revision(named.0 - 1), r#""krebs""#).unwrap_err(); + assert!(why.starts_with("a `name`, which revision 1"), "{why}"); + let long = format!(r#""{}""#, "x".repeat(65)); + for none in [r#""""#, r#""a/b""#, "1", long.as_str()] { + let why = with(named, none).unwrap_err(); + assert!(why.contains("no name"), "{none}: {why}"); + } + assert_eq!(Version::read(&first()).expect("a version").name, None); + } + #[test] fn a_version_changes_another_by_its_number_hash_and_sinces() { let first = Version::read(&first()).expect("a version"); |
