From b0f0e1a9edb08b324df2ec5e97d15e7089349fdb Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 18 Nov 2026 12:32:13 +0000 Subject: continuum-party: proposes only what a party may back One check (`client::backable`) decides what a party's client proposes and what it votes `true` on: a version, changing the current one as a version must, whose checkpoint, where it names one, names none of the heads of the current version's checkpoint, as a version naming those again is never written. A proposal nobody may back is refused before it is signed, saying why. --- src/federation/client.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) (limited to 'src/federation/client.rs') diff --git a/src/federation/client.rs b/src/federation/client.rs index 0f06ca3..88e370f 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -303,10 +303,7 @@ impl Record { if !yes { return Ok(Vec::new()); } - let version = Version::read(proposed).map_err(|why| format!("no version: {why}"))?; - version - .changes_validly(Some(current)) - .map_err(|why| format!("no version changing the current one: {why}"))?; + backable(current, proposed)?; let mut kept = Vec::new(); for earlier in cast.iter().filter(|vote| vote.yes) { if !dead_by(current, &earlier.proposal, nays) { @@ -364,6 +361,30 @@ impl Record { } } +/// The version `proposed` is, where a party may back it on `current`, and so propose it: a +/// version, changing `current` as a version must (`Version::changes_validly`), and naming +/// none of the heads of the checkpoint `current` names: a version's checkpoint is after the +/// current one's, and one naming those heads again is never written. +pub(crate) fn backable(current: &Version, proposed: &Json) -> Result { + let version = Version::read(proposed).map_err(|why| format!("no version: {why}"))?; + version + .changes_validly(Some(current)) + .map_err(|why| format!("no version changing the current one: {why}"))?; + let named = version.checkpoint.as_deref().unwrap_or_default(); + if let Some(head) = current + .checkpoint + .iter() + .flatten() + .find(|head| named.contains(*head)) + { + return Err(format!( + "its checkpoint names {head}, a head of the current version's checkpoint: a \ + version naming those again is never written" + )); + } + Ok(version) +} + /// What the version `proposed` changes of `current`, a line each: each field whose value /// differs, and of `keys` and `nodes` each party's, as `field: before -> after`, its JCS /// on either side, `-` where a version lacks it. @@ -698,6 +719,30 @@ mod tests { assert!(again.contains(&Id::of(&later)) && again.contains(&Id::of(&keys))); } + /// A party backs, and proposes, only a version changing the current one whose + /// checkpoint, where it names one, names none of the current checkpoint's heads. + #[test] + fn a_version_backed_changes_the_current_one_and_moves_on() { + let (_, _, _, first, current, one, _) = three(); + assert!(backable(¤t, &one.json()).is_ok()); + let mut far = one.clone(); + far.number += 1; + assert!(backable(¤t, &far.json()).is_err()); + assert!(backable(¤t, &first.after(Id::of(b"another")).json()).is_err()); + assert!(backable(¤t, &harness::str("no version")).is_err()); + let (head, later) = (Id::of(b"a head"), Id::of(b"a later head")); + let mut moved = one.clone(); + moved.checkpoint = Some(vec![head]); + let moved_to = Version::read(&moved.json()).unwrap(); + let mut again = moved.after(moved_to.hash); + again.checkpoint = Some(vec![head]); + assert!(backable(&moved_to, &again.json()).is_err()); + again.checkpoint = Some(vec![later]); + assert!(backable(&moved_to, &again.json()).is_ok()); + again.checkpoint = None; + assert!(backable(&moved_to, &again.json()).is_ok()); + } + /// What a proposed version changes: each field apart, each party's keys and nodes. #[test] fn changes_name_each_field_and_party_a_version_changes() { -- cgit v1.3.1